Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ee4147e24e | ||
|
|
d29c0ca389 |
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/ai-utils",
|
||||
"version": "1.6.8",
|
||||
"version": "1.6.10",
|
||||
"description": "AI Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
||||
+16
-3
@@ -98,18 +98,21 @@ 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});
|
||||
}
|
||||
if(chunk.choices[0]?.delta?.tool_calls) {
|
||||
for(const deltaTC of chunk.choices[0].delta.tool_calls) {
|
||||
const existing = msg.tool_calls.find((tc: any) => tc.index === deltaTC.index);
|
||||
const existing = deltaTC.index != null
|
||||
? msg.tool_calls.find((tc: any) => tc.index === deltaTC.index)
|
||||
: (deltaTC.id ? msg.tool_calls.find((tc: any) => tc.id === deltaTC.id) : undefined);
|
||||
if(existing) {
|
||||
if(deltaTC.id) existing.id = deltaTC.id;
|
||||
if(deltaTC.function?.name) existing.function.name = deltaTC.function.name;
|
||||
@@ -126,11 +129,21 @@ 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' && !controller.signal.aborted) {
|
||||
if(msg.content?.trim()) history.push({role: 'assistant', content: msg.content.trim(), timestamp: Date.now(), duration, tps});
|
||||
throw new Error(`[OpenAI] Response hit token limit before completing`);
|
||||
}
|
||||
|
||||
if(!finishReason && !controller.signal.aborted) {
|
||||
throw new Error('[OpenAI] Stream ended prematurely - connection likely dropped');
|
||||
}
|
||||
|
||||
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 +160,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