Completions library now supports statically typed responses
The completions library has been updated to support statically typed responses. This update allows developers to receive JSON responses with TypeScript types from ChatGPT. The library now generates a prompt that is sent to the API using the "expect" parameter. The prompt contains a schema that the response must satisfy and examples of what the response should look like.
Here is an example of how to use the updated library:
const response = await chat.sendMessage("Suggest a random startup name", {
expect: {
examples: [
{
name: "OpenAI",
domain: "openai.com",
},
],
schema: {
additionalProperties: false,
type: "object",
properties: {
name: { type: "string" },
domain: { type: "string" },
},
required: ["name", "domain"],
},
},
});
console.log(response.content); // { name: string; domain: string }
The "expect" parameter is used to generate a prompt that is sent to the API. The prompt contains a schema that the response must satisfy and examples of what the response should look like. The library then generates a statically typed response with TypeScript types.
This update to the completions library is a useful tool for developers who want to receive statically typed JSON responses from ChatGPT. It simplifies the process of generating responses and ensures that the responses are valid.