Fixed opanai early termination from tool calls
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/ai-utils",
|
"name": "@ztimson/ai-utils",
|
||||||
"version": "1.6.9",
|
"version": "1.6.10",
|
||||||
"description": "AI Utility library",
|
"description": "AI Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
+10
-3
@@ -110,7 +110,9 @@ export class OpenAi extends LLMProvider {
|
|||||||
}
|
}
|
||||||
if(chunk.choices[0]?.delta?.tool_calls) {
|
if(chunk.choices[0]?.delta?.tool_calls) {
|
||||||
for(const deltaTC of 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(existing) {
|
||||||
if(deltaTC.id) existing.id = deltaTC.id;
|
if(deltaTC.id) existing.id = deltaTC.id;
|
||||||
if(deltaTC.function?.name) existing.function.name = deltaTC.function.name;
|
if(deltaTC.function?.name) existing.function.name = deltaTC.function.name;
|
||||||
@@ -133,8 +135,13 @@ export class OpenAi extends LLMProvider {
|
|||||||
const duration = Date.now() - callStart;
|
const duration = Date.now() - callStart;
|
||||||
const tps = usage?.completion_tokens && duration > 0 ? usage.completion_tokens / (duration / 1000) : 0;
|
const tps = usage?.completion_tokens && duration > 0 ? usage.completion_tokens / (duration / 1000) : 0;
|
||||||
|
|
||||||
if(finishReason === 'length') {
|
if(finishReason === 'length' && !controller.signal.aborted) {
|
||||||
console.warn('[OpenAi] Response truncated: max_completion_tokens reached before finish_reason=stop');
|
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 || [];
|
const toolCalls = msg.tool_calls || [];
|
||||||
|
|||||||
Reference in New Issue
Block a user