xref: /expo/tools/src/OpenAI.ts (revision 7a13b901)
1*7a13b901SGabriel Donadel Dall'Agnolimport { Configuration, OpenAIApi } from 'openai';
2*7a13b901SGabriel Donadel Dall'Agnol
3*7a13b901SGabriel Donadel Dall'Agnolconst configuration = new Configuration({
4*7a13b901SGabriel Donadel Dall'Agnol  organization: process.env.OPENAI_API_ORG,
5*7a13b901SGabriel Donadel Dall'Agnol  apiKey: process.env.OPENAI_API_KEY,
6*7a13b901SGabriel Donadel Dall'Agnol});
7*7a13b901SGabriel Donadel Dall'Agnol
8*7a13b901SGabriel Donadel Dall'Agnolconst openai = new OpenAIApi(configuration);
9*7a13b901SGabriel Donadel Dall'Agnol
10*7a13b901SGabriel Donadel Dall'Agnolexport async function askChatGPTAsync(question: string): Promise<string | undefined> {
11*7a13b901SGabriel Donadel Dall'Agnol  const response = await openai.createChatCompletion({
12*7a13b901SGabriel Donadel Dall'Agnol    model: 'gpt-4',
13*7a13b901SGabriel Donadel Dall'Agnol    messages: [{ role: 'user', content: question }],
14*7a13b901SGabriel Donadel Dall'Agnol  });
15*7a13b901SGabriel Donadel Dall'Agnol
16*7a13b901SGabriel Donadel Dall'Agnol  return response.data.choices[0]?.message?.content;
17*7a13b901SGabriel Donadel Dall'Agnol}
18