33 lines
780 B
JavaScript
33 lines
780 B
JavaScript
const dotenv = require('dotenv');
|
|
dotenv.config();
|
|
|
|
const { Configuration, OpenAIApi } = require('openai');
|
|
|
|
const config = new Configuration({
|
|
apiKey: process.env.OPENAI_API_KEY,
|
|
});
|
|
|
|
const openaiClient = new OpenAIApi(config);
|
|
|
|
async function getEmbedding(text) {
|
|
const response = await openaiClient.createEmbedding({
|
|
model: 'text-embedding-ada-002',
|
|
input: text,
|
|
});
|
|
return response.data.data[0].embedding;
|
|
}
|
|
|
|
async function getChatCompletion(prompt) {
|
|
const response = await openaiClient.createChatCompletion({
|
|
model: 'gpt-3.5-turbo',
|
|
messages: [{ role: 'user', content: prompt }],
|
|
temperature: 0.7,
|
|
max_tokens: 500,
|
|
});
|
|
return response.data.choices[0].message.content.trim();
|
|
}
|
|
|
|
module.exports = {
|
|
getEmbedding,
|
|
getChatCompletion,
|
|
}; |