muxi feng commited on
Commit
23796a3
·
1 Parent(s): 152ad49

修复模型错误

Browse files
Files changed (1) hide show
  1. app/requests.ts +59 -68
app/requests.ts CHANGED
@@ -245,7 +245,7 @@ export async function requestChatStream(
245
  if (Bot == "Lemur"){
246
  const req = makeRevChatRequestParam(messages);
247
 
248
- chatMessage(req.messages,"文字","user")
249
  console.log("[Request] ", req);
250
 
251
  const controller = new AbortController();
@@ -319,7 +319,7 @@ export async function requestChatStream(
319
  overrideModel: options?.overrideModel,
320
  });
321
 
322
- chatMessage(JSON.stringify(req.messages),"文字","user")
323
  console.log("[Request] ", req);
324
 
325
  const controller = new AbortController();
@@ -516,76 +516,67 @@ export async function requestChatStream(
516
  options?.onMessage("请换一个问题试试吧", true);
517
  }
518
  }else{
519
- const req = makeRequestParam(messages, {
520
- stream: true,
521
- overrideModel: options?.overrideModel,
522
- });
523
-
524
- chatMessage(JSON.stringify(req.messages),"文字","user")
525
- console.log("[Request] ", req);
526
-
527
- const controller = new AbortController();
528
- const reqTimeoutId = setTimeout(() => controller.abort(), TIME_OUT_MS);
529
 
530
- try {
531
- const openaiUrl = useAccessStore.getState().openaiUrl;
532
- const res = await fetch(openaiUrl + "v1/chat/completions", {
533
- method: "POST",
534
- headers: {
535
- "Content-Type": "application/json",
536
- ...getHeaders(),
537
- },
538
- body: JSON.stringify(req),
539
- signal: controller.signal,
540
- });
541
-
542
- clearTimeout(reqTimeoutId);
543
-
544
- let responseText = "";
545
-
546
- const finish = () => {
547
- options?.onMessage(responseText, true);
548
- controller.abort();
549
- chatMessage(responseText,"文字","system")
550
- };
551
-
552
- if (res.ok) {
553
- const reader = res.body?.getReader();
554
- const decoder = new TextDecoder();
555
-
556
- options?.onController?.(controller);
557
-
558
- while (true) {
559
- const resTimeoutId = setTimeout(() => finish(), TIME_OUT_MS);
560
- const content = await reader?.read();
561
- clearTimeout(resTimeoutId);
562
-
563
- if (!content || !content.value) {
564
- break;
565
- }
566
-
567
- const text = decoder.decode(content.value, { stream: true });
568
- responseText += text;
569
-
570
- const done = content.done;
571
- options?.onMessage(responseText, false);
572
-
573
- if (done) {
574
- break;
 
575
  }
 
 
 
 
 
 
 
576
  }
577
- finish();
578
- } else if (res.status === 401) {
579
- console.error("Unauthorized");
580
- options?.onError(new Error("Unauthorized"), res.status);
581
- } else {
582
- console.error("Stream Error", res.body);
583
- options?.onError(new Error("Stream Error"), res.status);
584
  }
585
- } catch (err) {
586
- console.error("NetWork Error", err);
587
- options?.onError(err as Error);
588
- }
589
  }
590
  }
591
 
 
245
  if (Bot == "Lemur"){
246
  const req = makeRevChatRequestParam(messages);
247
 
248
+ chatMessage(messages[messages.length - 1].content,"文字","user")
249
  console.log("[Request] ", req);
250
 
251
  const controller = new AbortController();
 
319
  overrideModel: options?.overrideModel,
320
  });
321
 
322
+ chatMessage(messages[messages.length - 1].content,"文字","user")
323
  console.log("[Request] ", req);
324
 
325
  const controller = new AbortController();
 
516
  options?.onMessage("请换一个问题试试吧", true);
517
  }
518
  }else{
519
+ const req = makeRevChatRequestParam(messages);
 
 
 
 
 
 
 
 
 
520
 
521
+ chatMessage(messages[messages.length - 1].content,"文字","user")
522
+ console.log("[Request] ", req);
523
+
524
+ const controller = new AbortController();
525
+ const reqTimeoutId = setTimeout(() => controller.abort(), TIME_OUT_MS);
526
+
527
+ try {
528
+ const res = await fetch("/api/lemur", {
529
+ method: "POST",
530
+ headers: {
531
+ "Content-Type": "application/json",
532
+ ...getHeaders(),
533
+ },
534
+ body: JSON.stringify(req),
535
+ signal: controller.signal,
536
+ });
537
+ clearTimeout(reqTimeoutId);
538
+
539
+ let responseText = "";
540
+
541
+ const finish = () => {
542
+ options?.onMessage(responseText, true);
543
+ controller.abort();
544
+ chatMessage(responseText,"文字","system")
545
+ };
546
+
547
+ if (res.ok) {
548
+ const reader = res.body?.getReader();
549
+ const decoder = new TextDecoder();
550
+
551
+ options?.onController?.(controller);
552
+
553
+ while (true) {
554
+ // handle time out, will stop if no response in 10 secs
555
+ const resTimeoutId = setTimeout(() => finish(), TIME_OUT_MS);
556
+ const content = await reader?.read();
557
+ clearTimeout(resTimeoutId);
558
+ const text = decoder.decode(content?.value);
559
+ responseText += text;
560
+
561
+ const done = !content || content.done;
562
+ options?.onMessage(responseText, false);
563
+
564
+ if (done) {
565
+ break;
566
+ }
567
  }
568
+ finish();
569
+ } else if (res.status === 401) {
570
+ console.error("Unauthorized");
571
+ options?.onError(new Error("Unauthorized"), res.status);
572
+ } else {
573
+ console.error("Stream Error", res.body);
574
+ options?.onError(new Error("Stream Error"), res.status);
575
  }
576
+ } catch (err) {
577
+ console.error("NetWork Error", err);
578
+ options?.onError(err as Error);
 
 
 
 
579
  }
 
 
 
 
580
  }
581
  }
582