Skip to content

Commit

Permalink
feat(chat): support chat protocol (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinedied committed Oct 17, 2023
1 parent 06f1fdb commit 2c8d29f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
18 changes: 11 additions & 7 deletions packages/chat-component/src/core/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function parseStreamedMessages({
apiResponseBody: ReadableStream<Uint8Array> | null;
visit: () => void;
}) {
const chunks = readStream<Partial<BotResponse>>(apiResponseBody);
const chunks = readStream<BotResponseChunk>(apiResponseBody);

const streamedMessageRaw: string[] = [];
const stepsBuffer: string[] = [];
Expand All @@ -19,15 +19,19 @@ export async function parseStreamedMessages({
let isFollowupQuestion = false;
let stepIndex = 0;
let textBlockIndex = 0;
const thoughtProcess: string[] = [];
const result = {
data_points: [] as string[],
thoughts: '',
};

for await (const chunk of chunks) {
// eslint-disable-next-line no-prototype-builtins
if (chunk.hasOwnProperty('data_points')) {
thoughtProcess.push(chunk as string);
const { content, context } = chunk.choices[0].delta;
if (context?.data_points) {
result.data_points = context.data_points ?? [];
result.thoughts = context.thoughts ?? '';
continue;
}
let chunkValue = chunk.answer as string;
let chunkValue = content ?? '';

if (chunkValue === '') {
continue;
Expand Down Expand Up @@ -117,7 +121,7 @@ export async function parseStreamedMessages({

visit();
}
return thoughtProcess;
return result;
}

// update the citations entry and wrap the citations in a sup tag
Expand Down
17 changes: 7 additions & 10 deletions packages/chat-component/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ export class ChatComponent extends LitElement {
defaultPrompts: string[] = globalConfig.DEFAULT_PROMPTS;
defaultPromptsHeading: string = globalConfig.DEFAULT_PROMPTS_HEADING;
chatButtonLabelText: string = globalConfig.CHAT_BUTTON_LABEL_TEXT;
chatThoughtProcess: BotResponse[] = [];
chatThoughts: string | null = '';
chatDataPoints: string[] = [];

Expand Down Expand Up @@ -125,20 +124,18 @@ export class ChatComponent extends LitElement {
},
];

await parseStreamedMessages({
const result = await parseStreamedMessages({
chatThread: this.chatThread,
apiResponseBody: (this.apiResponse as Response).body,
visit: () => {
// NOTE: this function is called whenever we mutate sub-properties of the array
this.requestUpdate('chatThread');
},
// this will be processing thought process only with streaming enabled
}).then((thoughtProcess) => {
this.chatThoughtProcess = thoughtProcess as unknown as BotResponse[];
this.chatThoughts = this.chatThoughtProcess[0].thoughts;
this.chatDataPoints = this.chatThoughtProcess[0].data_points;
this.canShowThoughtProcess = true;
});
this.chatThoughts = result.thoughts;
this.chatDataPoints = result.data_points;
this.canShowThoughtProcess = true;
return true;
}

Expand Down Expand Up @@ -233,11 +230,11 @@ export class ChatComponent extends LitElement {
const response = this.apiResponse as BotResponse;
// adds thought process support when streaming is disabled
if (!this.useStream) {
this.chatThoughts = response.thoughts;
this.chatDataPoints = response.data_points;
this.chatThoughts = response.choices[0].message.context?.thoughts ?? '';
this.chatDataPoints = response.choices[0].message.context?.data_points ?? [];
this.canShowThoughtProcess = true;
}
const message: string = response.answer;
const message: string = response.choices[0].message.content;
await this.processApiResponse({ message, isUserMessage: false });
} catch (error) {
console.error(error);
Expand Down
32 changes: 32 additions & 0 deletions packages/chat-component/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,35 @@ declare interface BotResponse {
error?: string;
done?: boolean;
}

declare type MessageRole = 'system' | 'user' | 'assistant' | 'function';

declare interface Message {
role: MessageRole;
content: string;
}

declare interface BotResponse {
choices: Array<{
index: number;
message: BotResponseMessage;
}>;
object: 'chat.completion';
}

declare interface BotResponseChunk {
choices: Array<{
index: number;
delta: Partial<BotResponseMessage>;
finish_reason: string | null;
}>;
object: 'chat.completion.chunk';
}

declare type BotResponseMessage = Message & {
context?: Record<string, any> & {
data_points?: string[];
thoughts?: string;
};
session_state?: Record<string, any>;
};

0 comments on commit 2c8d29f

Please sign in to comment.