Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d29c0ca389 |
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/ai-utils",
|
||||
"version": "1.6.8",
|
||||
"version": "1.6.9",
|
||||
"description": "AI Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
||||
+8
-2
@@ -98,11 +98,12 @@ export class OpenAi extends LLMProvider {
|
||||
throw err;
|
||||
});
|
||||
|
||||
let usage: any, msg: any = {content: '', tool_calls: []};
|
||||
let usage: any, finishReason: string | undefined, msg: any = {content: '', tool_calls: []};
|
||||
if(options.stream) {
|
||||
for await (const chunk of resp) {
|
||||
if(controller.signal.aborted) break;
|
||||
if(chunk.usage) usage = chunk.usage;
|
||||
if(chunk.choices[0]?.finish_reason) finishReason = chunk.choices[0].finish_reason;
|
||||
if(chunk.choices[0]?.delta?.content) {
|
||||
msg.content += chunk.choices[0].delta.content;
|
||||
options.stream({text: chunk.choices[0].delta.content});
|
||||
@@ -126,11 +127,16 @@ export class OpenAi extends LLMProvider {
|
||||
}
|
||||
} else {
|
||||
usage = resp.usage;
|
||||
finishReason = resp.choices[0].finish_reason;
|
||||
msg = resp.choices[0].message;
|
||||
}
|
||||
const duration = Date.now() - callStart;
|
||||
const tps = usage?.completion_tokens && duration > 0 ? usage.completion_tokens / (duration / 1000) : 0;
|
||||
|
||||
if(finishReason === 'length') {
|
||||
console.warn('[OpenAi] Response truncated: max_completion_tokens reached before finish_reason=stop');
|
||||
}
|
||||
|
||||
const toolCalls = msg.tool_calls || [];
|
||||
if(toolCalls.length && !controller.signal.aborted) {
|
||||
if(msg.content?.trim()) history.push({role: 'assistant', content: msg.content.trim(), timestamp: Date.now(), duration, tps});
|
||||
@@ -147,7 +153,7 @@ export class OpenAi extends LLMProvider {
|
||||
if(!tool) { entry.error = 'Tool not found'; return; }
|
||||
try {
|
||||
const toolStream = options.stream && ((chunk: any) => {
|
||||
if(chunk.done) { terminal = true; return; }
|
||||
if(chunk.done) return;
|
||||
options.stream!(chunk);
|
||||
});
|
||||
const result = await tool.fn(entry.args, toolStream, this.ai, tc.id);
|
||||
|
||||
Reference in New Issue
Block a user