Skip to content

Commit ed736d2

Browse files
committed
fix error state
1 parent b33e386 commit ed736d2

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bearbobo/ling",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "The framework for LLMs",
55
"main": "lib/cjs/index.js",
66
"module": "lib/esm/index.js",

src/bot/index.ts

+23-13
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,25 @@ export class Bot extends EventEmitter {
6161
}
6262

6363
async chat(message: string) {
64-
this.chatState = ChatState.CHATTING;
65-
const messages = [...this.prompts, ...this.history, { role: "user", content: message }];
66-
if(this.config.model_name.startsWith('coze:')) {
67-
return getCozeChatCompletions(this.tube, messages, this.config, {...this.options, custom_variables: this.customParams},
64+
try {
65+
this.chatState = ChatState.CHATTING;
66+
const prompts = this.prompts.length > 0 ? [...this.prompts] : [{
67+
role: 'system',
68+
content: `[Output]\nOutput with json format, starts with '{'\n[Example]\n{"answer": "My answer"}`,
69+
}];
70+
const messages = [...prompts, ...this.history, { role: "user", content: message }];
71+
if(this.config.model_name.startsWith('coze:')) {
72+
return await getCozeChatCompletions(this.tube, messages, this.config, {...this.options, custom_variables: this.customParams},
73+
(content) => { // on complete
74+
this.chatState = ChatState.FINISHED;
75+
this.emit('response', content);
76+
}, (content) => { // on string response
77+
this.emit('string-response', content);
78+
}).then((content) => {
79+
this.emit('inference-done', content);
80+
});
81+
}
82+
return await getChatCompletions(this.tube, messages, this.config, this.options,
6883
(content) => { // on complete
6984
this.chatState = ChatState.FINISHED;
7085
this.emit('response', content);
@@ -73,16 +88,11 @@ export class Bot extends EventEmitter {
7388
}).then((content) => {
7489
this.emit('inference-done', content);
7590
});
91+
} catch(ex: any) {
92+
// this.emit('error', ex.message);
93+
this.tube.enqueue({event: 'error', data: ex.message});
94+
this.tube.cancel();
7695
}
77-
return getChatCompletions(this.tube, messages, this.config, this.options,
78-
(content) => { // on complete
79-
this.chatState = ChatState.FINISHED;
80-
this.emit('response', content);
81-
}, (content) => { // on string response
82-
this.emit('string-response', content);
83-
}).then((content) => {
84-
this.emit('inference-done', content);
85-
});
8696
}
8797

8898
get state() {

test/server.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,37 @@ app.get('/', async (req, res) => {
8383
}
8484
});
8585

86+
app.get('/ai/chat', async (req, res) => {
87+
// setting below headers for Streaming the data
88+
res.writeHead(200, {
89+
'Content-Type': "text/event-stream",
90+
'Cache-Control': "no-cache",
91+
'Connection': "keep-alive"
92+
});
93+
94+
const question = req.query.question as string;
95+
const config: ChatConfig = {
96+
model_name,
97+
api_key: apiKey,
98+
endpoint: endpoint,
99+
};
100+
101+
const ling = new Ling(config);
102+
ling.setSSE(true);
103+
104+
const bot = ling.createBot();
105+
// bot.addPrompt(prompt);
106+
console.log(question);
107+
bot.chat(question);
108+
ling.close();
109+
110+
try {
111+
await pipeline((ling.stream as any), res);
112+
} catch(ex) {
113+
ling.cancel();
114+
}
115+
});
116+
86117
app.post('/api', async (req, res) => {
87118
// res.writeHead(200, {
88119
// 'Content-Type': "text/event-stream",
@@ -95,7 +126,6 @@ app.post('/api', async (req, res) => {
95126
try {
96127
await pipeline((ling.stream as any), res);
97128
} catch(ex) {
98-
console.log(1111);
99129
ling.cancel();
100130
}
101131
});

0 commit comments

Comments
 (0)