Skip to content

Commit 01dee4d

Browse files
Merge remote-tracking branch 'origin' into feat/assistant-streaming
2 parents 9252292 + 40d1910 commit 01dee4d

File tree

6 files changed

+48
-38
lines changed

6 files changed

+48
-38
lines changed

apps/api/src/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async function bootstrap() {
99
const globalPrefix = 'api';
1010
const config = new DocumentBuilder()
1111
.setTitle('@boldare/openai-assistant')
12-
.setVersion('0.1.0')
12+
.setVersion('1.0.0')
1313
.build();
1414
const document = SwaggerModule.createDocument(app, config);
1515

apps/spa/src/app/modules/+chat/shared/chat-gateway.service.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { ChatEvents } from './chat.model';
33
import io from 'socket.io-client';
44
import {
55
ChatCallDto,
6-
TextCreatedPayload, TextDeltaPayload, TextDonePayload
6+
TextCreatedPayload,
7+
TextDeltaPayload,
8+
TextDonePayload,
79
} from '@boldare/openai-assistant';
810
import { Observable } from 'rxjs';
911
import { environment } from '../../../../environments/environment';

apps/spa/src/app/modules/+chat/shared/chat.service.ts

+24-32
Original file line numberDiff line numberDiff line change
@@ -139,48 +139,40 @@ export class ChatService {
139139
}
140140

141141
watchTextCreated(): Subscription {
142-
return this.chatGatewayService
143-
.textCreated()
144-
.subscribe((data) => {
145-
this.isTyping$.next(false)
146-
this.addMessage({ content: data.text.value, role: ChatRole.Assistant })
147-
});
142+
return this.chatGatewayService.textCreated().subscribe(data => {
143+
this.isTyping$.next(false);
144+
this.addMessage({ content: data.text.value, role: ChatRole.Assistant });
145+
});
148146
}
149147

150148
watchTextDelta(): Subscription {
151-
return this.chatGatewayService
152-
.textDelta()
153-
.subscribe((data) => {
154-
const length = this.messages$.value.length;
155-
this.messages$.value[length - 1].content = data.text.value;
156-
});
149+
return this.chatGatewayService.textDelta().subscribe(data => {
150+
const length = this.messages$.value.length;
151+
this.messages$.value[length - 1].content = data.text.value;
152+
});
157153
}
158154

159155
watchTextDone(): Subscription {
160-
return this.chatGatewayService
161-
.textDone()
162-
.subscribe((data) => {
163-
this.isTyping$.next(false);
164-
this.messages$.next([
165-
...this.messages$.value.slice(0, -1),
166-
{
167-
content: data.text.value,
168-
role: ChatRole.Assistant,
169-
},
170-
]);
171-
});
156+
return this.chatGatewayService.textDone().subscribe(data => {
157+
this.isTyping$.next(false);
158+
this.messages$.next([
159+
...this.messages$.value.slice(0, -1),
160+
{
161+
content: data.text.value,
162+
role: ChatRole.Assistant,
163+
},
164+
]);
165+
});
172166
}
173167

174168
watchMessages(): Subscription {
175-
return this.chatGatewayService
176-
.callDone()
177-
.subscribe(data => {
178-
this.addMessage({
179-
content: data.content,
180-
role: ChatRole.Assistant,
181-
});
182-
this.isTyping$.next(false);
169+
return this.chatGatewayService.callDone().subscribe(data => {
170+
this.addMessage({
171+
content: data.content,
172+
role: ChatRole.Assistant,
183173
});
174+
this.isTyping$.next(false);
175+
});
184176
}
185177

186178
sendAudio(file: Blob): void {

libs/openai-assistant/src/lib/assistant/assistant.module.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { DynamicModule, Inject, Module, OnModuleInit } from '@nestjs/common';
1+
import {
2+
DynamicModule,
3+
Inject,
4+
Module,
5+
OnModuleInit,
6+
Optional,
7+
} from '@nestjs/common';
28
import { HttpModule } from '@nestjs/axios';
39
import {
410
AssistantService,
@@ -38,10 +44,14 @@ export class AssistantModule implements OnModuleInit {
3844
constructor(
3945
private readonly assistantService: AssistantService,
4046
private readonly configService: ConfigService,
41-
@Inject('config') private config: AssistantConfigParams,
47+
@Inject('config') @Optional() private config: AssistantConfigParams,
4248
) {}
4349

4450
async onModuleInit(): Promise<void> {
51+
if (!this.config) {
52+
return;
53+
}
54+
4555
this.configService.set(this.config);
4656
await this.assistantService.init();
4757
}

libs/openai-assistant/src/lib/chat/chat.model.ts

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ export class ChatCallDto {
5959
@ApiProperty()
6060
content!: string;
6161

62+
@ApiProperty({ required: false })
63+
assistantId?: string;
64+
6265
@ApiProperty({ required: false })
6366
file_ids?: string[];
6467

libs/openai-assistant/src/lib/chat/chat.service.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ export class ChatService {
3636

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

39-
const run = this.assistantStream(threadId, callbacks);
39+
const assistantId =
40+
payload?.assistantId || process.env['ASSISTANT_ID'] || '';
41+
const run = this.assistantStream(assistantId, threadId, callbacks);
4042
const finalRun = await run.finalRun();
4143

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

5052
assistantStream(
53+
assistantId: string,
5154
threadId: string,
5255
callbacks?: ChatCallCallbacks,
5356
): AssistantStream {
5457
const runner = this.threads.runs.createAndStream(threadId, {
55-
assistant_id: process.env['ASSISTANT_ID'] || '',
58+
assistant_id: assistantId,
5659
});
5760

5861
return assistantStreamEventHandler<AssistantStream>(runner, callbacks);

0 commit comments

Comments
 (0)