Last active
May 24, 2023 03:34
-
-
Save hill/89c8b89922fb4492810bb3ffd47d05bd to your computer and use it in GitHub Desktop.
jsonPrompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const jsonPrompt = <T extends z.ZodTypeAny>({ | |
task, | |
outputSchema, | |
params = { | |
maxTokens: 100, | |
model: 'gpt-3.5-turbo', | |
temperature: 0, | |
}, | |
}: { | |
task: string; | |
outputSchema: T; | |
params?: { maxTokens: number; model: 'gpt-3.5-turbo' | 'gpt-3.5'; temperature: number }; | |
}) => { | |
const execute = async (userMessage: string) => { | |
const { openaiKey } = await definitelyGetCreds(); | |
const configuration = new Configuration({ | |
apiKey: openaiKey, | |
}); | |
const openai = new OpenAIApi(configuration); | |
const completion = await openai.createChatCompletion({ | |
model: params.model, | |
temperature: params.temperature, | |
max_tokens: params.maxTokens, | |
messages: [ | |
{ | |
role: 'system', | |
content: task, | |
}, | |
{ | |
role: 'user', | |
content: userMessage, | |
}, | |
{ | |
role: 'system', | |
content: | |
'You must respond with a valid JSON object surrounded by a codeblock and nothing else.' + | |
'The object should have this shape: ```json' + | |
zodToJsonSchema(outputSchema) + | |
'``` (include the codeblock)', | |
}, | |
], | |
}); | |
const response = completion.data.choices[0].message?.content; | |
if (!response) { | |
throw new Error('Invalid response from OpenAI'); | |
} | |
const json = response.includes('```') ? response.trim().split(/```(?:json)?/)[1] : response.trim(); | |
const parsedResponse = outputSchema.safeParse(JSON.parse(json)); | |
if (!parsedResponse.success) { | |
throw new Error('Invalid response from OpenAI'); | |
} | |
return parsedResponse.data; | |
}; | |
return { | |
execute, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment