Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into feat/assistant-streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianmusial committed Mar 29, 2024
2 parents 9252292 + 40d1910 commit 059e283
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 39 deletions.
2 changes: 1 addition & 1 deletion apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async function bootstrap() {
const globalPrefix = 'api';
const config = new DocumentBuilder()
.setTitle('@boldare/openai-assistant')
.setVersion('0.1.0')
.setVersion('1.0.0')
.build();
const document = SwaggerModule.createDocument(app, config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { ChatEvents } from './chat.model';
import io from 'socket.io-client';
import {
ChatCallDto,
TextCreatedPayload, TextDeltaPayload, TextDonePayload
TextCreatedPayload,
TextDeltaPayload,
TextDonePayload,
} from '@boldare/openai-assistant';
import { Observable } from 'rxjs';
import { environment } from '../../../../environments/environment';
Expand Down
56 changes: 24 additions & 32 deletions apps/spa/src/app/modules/+chat/shared/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,48 +139,40 @@ export class ChatService {
}

watchTextCreated(): Subscription {
return this.chatGatewayService
.textCreated()
.subscribe((data) => {
this.isTyping$.next(false)
this.addMessage({ content: data.text.value, role: ChatRole.Assistant })
});
return this.chatGatewayService.textCreated().subscribe(data => {
this.isTyping$.next(false);
this.addMessage({ content: data.text.value, role: ChatRole.Assistant });
});
}

watchTextDelta(): Subscription {
return this.chatGatewayService
.textDelta()
.subscribe((data) => {
const length = this.messages$.value.length;
this.messages$.value[length - 1].content = data.text.value;
});
return this.chatGatewayService.textDelta().subscribe(data => {
const length = this.messages$.value.length;
this.messages$.value[length - 1].content = data.text.value;
});
}

watchTextDone(): Subscription {
return this.chatGatewayService
.textDone()
.subscribe((data) => {
this.isTyping$.next(false);
this.messages$.next([
...this.messages$.value.slice(0, -1),
{
content: data.text.value,
role: ChatRole.Assistant,
},
]);
});
return this.chatGatewayService.textDone().subscribe(data => {
this.isTyping$.next(false);
this.messages$.next([
...this.messages$.value.slice(0, -1),
{
content: data.text.value,
role: ChatRole.Assistant,
},
]);
});
}

watchMessages(): Subscription {
return this.chatGatewayService
.callDone()
.subscribe(data => {
this.addMessage({
content: data.content,
role: ChatRole.Assistant,
});
this.isTyping$.next(false);
return this.chatGatewayService.callDone().subscribe(data => {
this.addMessage({
content: data.content,
role: ChatRole.Assistant,
});
this.isTyping$.next(false);
});
}

sendAudio(file: Blob): void {
Expand Down
14 changes: 12 additions & 2 deletions libs/openai-assistant/src/lib/assistant/assistant.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { DynamicModule, Inject, Module, OnModuleInit } from '@nestjs/common';
import {
DynamicModule,
Inject,
Module,
OnModuleInit,
Optional,
} from '@nestjs/common';
import { HttpModule } from '@nestjs/axios';
import {
AssistantService,
Expand Down Expand Up @@ -38,10 +44,14 @@ export class AssistantModule implements OnModuleInit {
constructor(
private readonly assistantService: AssistantService,
private readonly configService: ConfigService,
@Inject('config') private config: AssistantConfigParams,
@Inject('config') @Optional() private config: AssistantConfigParams,
) {}

async onModuleInit(): Promise<void> {
if (!this.config) {
return;
}

this.configService.set(this.config);
await this.assistantService.init();
}
Expand Down
3 changes: 3 additions & 0 deletions libs/openai-assistant/src/lib/chat/chat.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export class ChatCallDto {
@ApiProperty()
content!: string;

@ApiProperty({ required: false })
assistantId?: string;

@ApiProperty({ required: false })
file_ids?: string[];

Expand Down
7 changes: 5 additions & 2 deletions libs/openai-assistant/src/lib/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export class ChatService {

await this.threads.messages.create(threadId, message);

const run = this.assistantStream(threadId, callbacks);
const assistantId =
payload?.assistantId || process.env['ASSISTANT_ID'] || '';
const run = this.assistantStream(assistantId, threadId, callbacks);
const finalRun = await run.finalRun();

await this.runService.resolve(finalRun, true, callbacks);
Expand All @@ -48,11 +50,12 @@ export class ChatService {
}

assistantStream(
assistantId: string,
threadId: string,
callbacks?: ChatCallCallbacks,
): AssistantStream {
const runner = this.threads.runs.createAndStream(threadId, {
assistant_id: process.env['ASSISTANT_ID'] || '',
assistant_id: assistantId,
});

return assistantStreamEventHandler<AssistantStream>(runner, callbacks);
Expand Down
10 changes: 9 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,13 @@
"e2eTestRunner": "none"
}
},
"useInferencePlugins": false
"useInferencePlugins": false,
"tasksRunnerOptions": {
"default": {
"runner": "nx/tasks-runners/default",
"options": {
"cacheableOperations": ["build", "test"]
}
}
}
}

0 comments on commit 059e283

Please sign in to comment.