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

chatapi: send files to chat apis (#7529) #7539

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions chatapi/src/config/nano.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import dotenv from 'dotenv';

dotenv.config();

const db = nano(process.env.COUCHDB_HOST || 'http://couchdb:5984').use('chat_history');
const db = nano(process.env.COUCHDB_HOST || 'http://couchdb:5984');
const chatDB = db.use('chat_history');
const resourceDB = db.use('resources');

export default db;
export { chatDB, resourceDB };
6 changes: 3 additions & 3 deletions chatapi/src/services/chat.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DocumentInsertResponse } from 'nano';

import db from '../config/nano.config';
import { chatDB } from '../config/nano.config';
import { aiChat } from '../utils/chat.utils';
import { retrieveChatHistory } from '../utils/db.utils';
import { handleChatError } from '../utils/chat-error.utils';
Expand Down Expand Up @@ -40,7 +40,7 @@ export async function chat(data: any, stream?: boolean, callback?: (response: st
}

dbData.conversations.push({ 'query': content, 'response': '' });
const res = await db.insert(dbData);
const res = await chatDB.insert(dbData);

messages.push({ 'role': 'user', content });

Expand All @@ -52,7 +52,7 @@ export async function chat(data: any, stream?: boolean, callback?: (response: st
dbData.updatedDate = Date.now();
dbData._id = res?.id;
dbData._rev = res?.rev;
const couchSaveResponse = await db.insert(dbData);
const couchSaveResponse = await chatDB.insert(dbData);

return {
completionText,
Expand Down
24 changes: 17 additions & 7 deletions chatapi/src/utils/chat-assistant.utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs';
import openai from '../config/openai.config';
import dotenv from 'dotenv';

Expand All @@ -9,14 +10,13 @@ dotenv.config();
* @returns Assistant object
*/
export async function createAssistant(model: string) {
// export async function createAssistant(model: string, additionalContext?: any) {

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

return await openai.beta.assistants.create({
'name': process.env.ASSISTANT_NAME,
'instructions': process.env.ASSISTANT_INSTRUCTIONS,
'tools': [{ 'type': 'code_interpreter' }],
'tools': [
{ 'type': 'code_interpreter' },
{ 'type': 'file_search' }
],
model,
});
}
Expand All @@ -25,12 +25,22 @@ export async function createThread() {
return await openai.beta.threads.create();
}

export async function addToThread(threadId: any, message: string) {
export async function addToThread(threadId: any, message: string, attachment?: any) {
let attachmentBlob;

if (attachment.valid) {
attachmentBlob = await openai.files.create({
'file': attachment.doc,
'purpose': 'assistants',
});
}

return await openai.beta.threads.messages.create(
threadId,
{
'role': 'user',
'content': message
'content': message,
'attachments': attachment.valid ? [{ 'file_id': attachmentBlob?.id, 'tools': [{ 'type': 'file_search' }] }] : []
}
);
}
Expand Down
12 changes: 9 additions & 3 deletions chatapi/src/utils/chat-helpers.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
retrieveResponse,
createAndHandleRunWithStreaming,
} from './chat-assistant.utils';
import { fetchFileFromCouchDB } from './db.utils';

const modelsConfig = JSON.parse(process.env.MODELS_CONFIG || '{}');

Expand Down Expand Up @@ -68,7 +69,6 @@ export async function aiChatStream(
messages: ChatMessage[],
aiProvider: AIProvider,
assistant: boolean,
context: any,
callback?: (response: string) => void
): Promise<string> {
const provider = providers[aiProvider.name];
Expand Down Expand Up @@ -135,20 +135,26 @@ export async function aiChatNonStream(
throw new Error('Unsupported AI provider');
}
const model = aiProvider.model ?? provider.defaultModel;
let attachment: any = { 'valid': false };

if (context?.attachment) {
const attachmentFile = await fetchFileFromCouchDB(context.attachment?.id, context.attachment?.filename);
attachment = { 'valid': true, 'doc': attachmentFile };
}

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

return await retrieveResponse(thread.id);
} catch (error) {
return 'Error processing request';
return `Error processing request ${error}`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion chatapi/src/utils/chat.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function aiChat(
callback?: (response: string) => void
): Promise<string> {
if (stream) {
return await aiChatStream(messages, aiProvider, assistant, context, callback);
return await aiChatStream(messages, aiProvider, assistant, callback);
} else {
return await aiChatNonStream(messages, aiProvider, assistant, context);
}
Expand Down
14 changes: 12 additions & 2 deletions chatapi/src/utils/db.utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import db from '../config/nano.config';
import { chatDB, resourceDB } from '../config/nano.config';
import { DbDoc } from '../models/db-doc.model';
import { ChatMessage } from '../models/chat-message.model';

Expand All @@ -9,7 +9,7 @@ import { ChatMessage } from '../models/chat-message.model';
*/
async function getChatDocument(id: string) {
try {
const res = await db.get(id) as DbDoc;
const res = await chatDB.get(id) as DbDoc;
return {
'conversations': res.conversations,
'title': res.title,
Expand Down Expand Up @@ -37,3 +37,13 @@ export async function retrieveChatHistory(dbData: any, messages: ChatMessage[])
messages.push({ 'role': 'assistant', 'content': response });
}
}

export async function fetchFileFromCouchDB(docId: string, attachmentName: string) {
try {
return await resourceDB.attachment.get(docId, attachmentName, { 'binary': true });
} catch (error) {
return {
'error': `Unable to retrieve file from CouchDB ${error}`
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ <h3 class="margin-lr-3 ellipsis-title"><ng-container i18n>Step</ng-container> {{
</div>
</mat-toolbar>
<div class="view-container view-full-height" [ngClass]="{'grid-view': showChat, 'flex-view': !isGridView && !showChat}" *ngIf="stepDetail?.description || resource?._attachments; else emptyRecord">
<planet-chat-window *ngIf="stepDetail?.description && showChat" [context]="{type: 'coursestep', data: localizedStepInfo}"></planet-chat-window>
<planet-chat-window *ngIf="stepDetail?.description && showChat" [context]="{type: 'coursestep', data: localizedStepInfo, 'attachment': { id: resource._id, filename: resource.filename}}"></planet-chat-window>
<ng-container *ngIf="showChat; else stepViewContent">
<div class="flex-view">
<ng-container *ngTemplateOutlet="stepViewContent"></ng-container>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core';
import { CoursesService } from '../courses.service';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { MatDialog } from '@angular/material/dialog';
import { MatMenuTrigger } from '@angular/material/menu';
import { Subject, combineLatest } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { CoursesService } from '../courses.service';
import { UserService } from '../../shared/user.service';
import { SubmissionsService } from '../../submissions/submissions.service';
import { ResourcesService } from '../../resources/resources.service';
Expand Down
Loading