Skip to content

Commit

Permalink
fix: some error
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangsx committed Jun 13, 2023
1 parent b4c8b52 commit aa72929
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ router.get('/ask/stream', async (ctx) => {
"Connection": "keep-alive",
});
const es = new EventStream();
ctx.body = es.stream();
if (!prompt) {
es.write(Event.error, {error: 'need prompt in query'})
return;
Expand Down
33 changes: 18 additions & 15 deletions model/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,26 @@ export interface ChatRequest {
model: ModelType;
}

export function PromptToString(prompt: string | Message[], limit: number): string {
if (typeof prompt === "string") {
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;
export function PromptToString(prompt: string, limit: number): 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);
}
result.push(item);
return `${result.reverse().map(item => `${item.role}
: ${item.content}
`).join('\n')}\nassistant: `;
} catch (e) {
return prompt;
}
return result.reverse().map(item => `${item.role}: ${item.content}`).join('\n');
}

export abstract class Chat {
Expand Down
1 change: 1 addition & 0 deletions model/you/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export class You extends Chat {
break;
case 'done':
stream.write(Event.done, {content: 'done'})
stream.stream().end();
return;
default:
return;
Expand Down

0 comments on commit aa72929

Please sign in to comment.