fix: some error

このコミットが含まれているのは:
xiang 2023-06-13 10:10:29 +08:00
コミット aa7292950b
3個のファイルの変更20行の追加15行の削除

ファイルの表示

@ -57,6 +57,7 @@ router.get('/ask/stream', async (ctx) => {
"Connection": "keep-alive", "Connection": "keep-alive",
}); });
const es = new EventStream(); const es = new EventStream();
ctx.body = es.stream();
if (!prompt) { if (!prompt) {
es.write(Event.error, {error: 'need prompt in query'}) es.write(Event.error, {error: 'need prompt in query'})
return; return;

ファイルの表示

@ -23,23 +23,26 @@ export interface ChatRequest {
model: ModelType; model: ModelType;
} }
export function PromptToString(prompt: string | Message[], limit: number): string { export function PromptToString(prompt: string, limit: number): string {
if (typeof prompt === "string") { try {
const messages: Message[] = JSON.parse(prompt);
let result: Message[] = [];
let tokenSize = 0;
for (let i = messages.length - 1; i >= 0; i--) {
const item = messages[i];
const {role, content} = item;
tokenSize += getTokenSize(content);
if (tokenSize > limit) {
break;
}
result.push(item);
}
return `${result.reverse().map(item => `${item.role}
: ${item.content}
`).join('\n')}\nassistant: `;
} catch (e) {
return prompt; return prompt;
} }
let result: Message[] = [];
let tokenSize = 0;
for (let i = prompt.length - 1; i >= 0; i--) {
const item = prompt[i];
const {role, content} = item;
tokenSize += getTokenSize(content);
if (tokenSize > limit) {
break;
}
result.push(item);
}
return result.reverse().map(item => `${item.role}: ${item.content}`).join('\n');
} }
export abstract class Chat { export abstract class Chat {

ファイルの表示

@ -124,6 +124,7 @@ export class You extends Chat {
break; break;
case 'done': case 'done':
stream.write(Event.done, {content: 'done'}) stream.write(Event.done, {content: 'done'})
stream.stream().end();
return; return;
default: default:
return; return;