import {
Configuration,
OpenAIApi,
ChatCompletionRequestMessage
} from 'openai'
const MODELS = {
gpt35Turbo: `gpt-3.5-turbo`
};
export const chatWithOpenAI = async ({
content, question
}: {
content: string; question: string
}): Promise<string> => {
let result = ``
if (!question || !content) return result;
try {
const messages: Array<ChatCompletionRequestMessage> = [
{
role: 'system',
content: `
You are a helpful assistant. Answer the question as truthfully as possible using the provided text, and if the answer is not contained within the text below, say "I don't know."
Context:
${content}`,
},
{ role: 'user', content: question },
]
const response = await openai.createChatCompletion({
model: MODELS.gpt35Turbo,
messages: messages,
})
const { choices } = response?.data || {}
result = choices[0].message || ``
} catch (e) {
console.log(`chatWithOpenAI error Info`, e)
}
return result
}