|
@@ -16,7 +16,6 @@ import createError from 'http-errors';
|
|
|
import mongoose, { type HydratedDocument, type Types } from 'mongoose';
|
|
import mongoose, { type HydratedDocument, type Types } from 'mongoose';
|
|
|
import { type OpenAI, toFile } from 'openai';
|
|
import { type OpenAI, toFile } from 'openai';
|
|
|
import { type ChatCompletionChunk } from 'openai/resources/chat/completions';
|
|
import { type ChatCompletionChunk } from 'openai/resources/chat/completions';
|
|
|
-import { type Stream } from 'openai/streaming';
|
|
|
|
|
|
|
|
|
|
import ExternalUserGroupRelation from '~/features/external-user-group/server/models/external-user-group-relation';
|
|
import ExternalUserGroupRelation from '~/features/external-user-group/server/models/external-user-group-relation';
|
|
|
import ThreadRelationModel, { type ThreadRelationDocument } from '~/features/openai/server/models/thread-relation';
|
|
import ThreadRelationModel, { type ThreadRelationDocument } from '~/features/openai/server/models/thread-relation';
|
|
@@ -74,10 +73,7 @@ const convertPathPatternsToRegExp = (pagePathPatterns: string[]): Array<string |
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
export interface IOpenaiService {
|
|
export interface IOpenaiService {
|
|
|
- generateAndProcessPreMessage(
|
|
|
|
|
- message: string,
|
|
|
|
|
- deltaProcessor: (delta: ChatCompletionChunk.Choice.Delta) => void,
|
|
|
|
|
- ): Promise<Nullable<Stream<OpenAI.Chat.Completions.ChatCompletionChunk>>>
|
|
|
|
|
|
|
+ generateAndProcessPreMessage(message: string, chunkProcessor: (chunk: ChatCompletionChunk) => void): Promise<void>
|
|
|
createThread(userId: string, type: ThreadType, aiAssistantId?: string, initialUserMessage?: string): Promise<ThreadRelationDocument>;
|
|
createThread(userId: string, type: ThreadType, aiAssistantId?: string, initialUserMessage?: string): Promise<ThreadRelationDocument>;
|
|
|
getThreadsByAiAssistantId(aiAssistantId: string): Promise<ThreadRelationDocument[]>
|
|
getThreadsByAiAssistantId(aiAssistantId: string): Promise<ThreadRelationDocument[]>
|
|
|
deleteThread(threadRelationId: string): Promise<ThreadRelationDocument>;
|
|
deleteThread(threadRelationId: string): Promise<ThreadRelationDocument>;
|
|
@@ -114,10 +110,7 @@ class OpenaiService implements IOpenaiService {
|
|
|
return getClient({ openaiServiceType });
|
|
return getClient({ openaiServiceType });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async generateAndProcessPreMessage(
|
|
|
|
|
- message: string,
|
|
|
|
|
- deltaProcessor: (delta: ChatCompletionChunk.Choice.Delta) => void,
|
|
|
|
|
- ): Promise<Nullable<Stream<OpenAI.Chat.Completions.ChatCompletionChunk>>> {
|
|
|
|
|
|
|
+ async generateAndProcessPreMessage(message: string, chunkProcessor: (delta: ChatCompletionChunk) => void): Promise<void> {
|
|
|
const systemMessage = [
|
|
const systemMessage = [
|
|
|
"Generate a message briefly confirming the user's question.",
|
|
"Generate a message briefly confirming the user's question.",
|
|
|
'Please generate up to 20 characters',
|
|
'Please generate up to 20 characters',
|
|
@@ -143,8 +136,7 @@ class OpenaiService implements IOpenaiService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
for await (const chunk of preMessageCompletion) {
|
|
for await (const chunk of preMessageCompletion) {
|
|
|
- const delta = chunk.choices[0].delta;
|
|
|
|
|
- deltaProcessor(delta);
|
|
|
|
|
|
|
+ chunkProcessor(chunk);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|