-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cbb8dae
commit b1a353c
Showing
2 changed files
with
51 additions
and
28 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import OpenAI from 'openai' | ||
|
||
/** | ||
* @param {{ runId: string, openai: OpenAI, threadId: string, assistant_id: string, instructions?: string|null }} options | ||
*/ | ||
export async function getOrCreateRun({ runId, openai, threadId, assistant_id, instructions }) { | ||
const run = runId | ||
? await openai.beta.threads.runs.retrieve(threadId, runId) | ||
: await openai.beta.threads.runs.create(threadId, { | ||
assistant_id, | ||
instructions, | ||
}) | ||
return run | ||
} | ||
|
||
/** | ||
* @param {{ threadId: string, openai: OpenAI }} options | ||
*/ | ||
export async function getOrCreateThread({ threadId, openai }) { | ||
const thread = threadId === 'new' ? await openai.beta.threads.create() : await openai.beta.threads.retrieve(threadId) | ||
return thread | ||
} | ||
|
||
/** | ||
* @param {{ threadId: string, openai: OpenAI, content: string }} options | ||
*/ | ||
export async function createMessage({ openai, threadId, content }) { | ||
const message = await openai.beta.threads.messages.create(threadId, { | ||
role: 'user', | ||
content, | ||
}) | ||
return message | ||
} | ||
|
||
/** | ||
* @param {{ assistantId: string, openai: OpenAI, name?: string, instructions?: string, model: string }} options | ||
*/ | ||
export async function getAssistant({ assistantId, openai, name, instructions, model }) { | ||
const assistant = | ||
assistantId === 'new' | ||
? await openai.beta.assistants.create({ | ||
name, | ||
instructions, | ||
model: model || 'gpt-3.5-turbo', | ||
}) | ||
: await openai.beta.assistants.retrieve(assistantId) | ||
return assistant | ||
} |
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