Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Error Handling for createChatCompletion() thread #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/openai-thread-completion.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const { Log } = require('debug-level')
const log = new Log('bot')

const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env["OPENAI_API_KEY"]
Expand All @@ -9,13 +12,21 @@ const max_tokens = Number(process.env["OPENAI_MAX_TOKENS"] ?? 2000)
const temperature = Number(process.env["OPENAI_TEMPERATURE"] ?? 1)

async function continueThread(messages) {
const response = await openai.createChatCompletion({
messages: messages,
model: model,
max_tokens: max_tokens,
temperature: temperature
});
return response.data?.choices?.[0]?.message?.content
try {
const response = await openai.createChatCompletion({
messages: messages,
model,
max_tokens
});
log.trace(response.data?.choices?.[0]?.text);
return response.data?.choices?.[0]?.message?.content;
} catch (e) {
if (e.response) {
log.error(e.response.status);
log.error(e.response.data);
}
throw e;
}
}

module.exports = { continueThread }