Skip to content

Commit 2423906

Browse files
authored
Fix UI sending chat history to backend (opea-project#2225)
Signed-off-by: WenjiaoYue <wenjiao.yue@intel.com>
1 parent 827affc commit 2423906

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

ChatQnA/ui/svelte/src/lib/network/chat/Network.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { SSE } from "sse.js";
1818
const CHAT_BASE_URL = env.CHAT_BASE_URL;
1919
const MODEL_ID = env.MODEL_ID;
2020

21-
export async function fetchTextStream(query: string) {
21+
export async function fetchTextStream(query: object) {
2222
let payload = {};
2323
let url = "";
2424
let modelId = "meta-llama/Meta-Llama-3-8B-Instruct";

ChatQnA/ui/svelte/src/routes/+page.svelte

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
return decoded;
103103
}
104104
105-
const callTextStream = async (query: string, startSendTime: number) => {
105+
const callTextStream = async (query: object, startSendTime: number) => {
106106
try {
107107
const eventSource = await fetchTextStream(query);
108108
eventSource.addEventListener("error", (e: any) => {
@@ -179,6 +179,22 @@
179179
}
180180
};
181181
182+
function mapRole(r: number): "user" | "assistant" | "system" {
183+
if (r === 1) return "user";
184+
if (r === 0) return "assistant";
185+
return "system";
186+
}
187+
188+
function multiMessages(
189+
history: any[]
190+
): { role: "user" | "assistant" | "system"; content: string }[] {
191+
return history.map((m) => ({
192+
role: mapRole(m.role),
193+
content:
194+
typeof m.content === "string" ? m.content : String(m.content ?? ""),
195+
}));
196+
}
197+
182198
const handleTextSubmit = async () => {
183199
loading = true;
184200
const newMessage = {
@@ -192,7 +208,7 @@
192208
storeMessages();
193209
query = "";
194210
195-
await callTextStream(newMessage.content, getCurrentTimeStamp());
211+
await callTextStream(multiMessages(chatMessages), getCurrentTimeStamp());
196212
197213
scrollToBottom(scrollToDiv);
198214
storeMessages();

0 commit comments

Comments
 (0)