Skip to content

Commit

Permalink
Test using instruct field && some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Mutugiii committed Jul 31, 2024
1 parent 7b8b9a0 commit ef9a898
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
1 change: 0 additions & 1 deletion chatapi/src/services/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export async function chat(data: any, stream?: boolean, callback?: (response: st
const { content, ...dbData } = data;
const messages: ChatMessage[] = [];
const aiProvider = dbData.aiProvider as AIProvider || { 'name': 'openai' };
console.log(dbData);

if (!content || typeof content !== 'string') {
throw new Error('"data.content" is a required non-empty string field');
Expand Down
16 changes: 9 additions & 7 deletions chatapi/src/utils/chat-assistant.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ dotenv.config();
* @param model - Model to use for assistant
* @returns Assistant object
*/
export async function createAssistant(model: string, additionalContext?: any) {
console.log(additionalContext);
export async function createAssistant(model: string) {
// export async function createAssistant(model: string, additionalContext?: any) {

const instructions = process.env.ASSISTANT_INSTRUCTIONS + (additionalContext ? additionalContext?.data : '');
console.log(instructions);
// const instructions = process.env.ASSISTANT_INSTRUCTIONS + (additionalContext ? additionalContext?.data : '');

return await openai.beta.assistants.create({
'name': process.env.ASSISTANT_NAME,
instructions,
'instructions': process.env.ASSISTANT_INSTRUCTIONS,
'tools': [{ 'type': 'code_interpreter' }],
model,
});
Expand All @@ -36,10 +35,13 @@ export async function addToThread(threadId: any, message: string) {
);
}

export async function createRun(threadID: any, assistantID: any) {
export async function createRun(threadID: any, assistantID: any, instructions?: string) {
return await openai.beta.threads.runs.create(
threadID,
{ 'assistant_id': assistantID }
{
'assistant_id': assistantID,
instructions
}
);
}

Expand Down
6 changes: 3 additions & 3 deletions chatapi/src/utils/chat-helpers.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function aiChatStream(

if (assistant) {
try {
const asst = await createAssistant(model, context);
const asst = await createAssistant(model);
const thread = await createThread();
for (const message of messages) {
await addToThread(thread.id, message.content);
Expand Down Expand Up @@ -138,12 +138,12 @@ export async function aiChatNonStream(

if(assistant) {
try {
const asst = await createAssistant(model, context);
const asst = await createAssistant(model);
const thread = await createThread();
for (const message of messages) {
await addToThread(thread.id, message.content);
}
const run = await createRun(thread.id, asst.id);
const run = await createRun(thread.id, asst.id, context.data);
await waitForRunCompletion(thread.id, run.id);

return await retrieveResponse(thread.id);
Expand Down

0 comments on commit ef9a898

Please sign in to comment.