@re-ai/openai-like-api
Advanced tools
Comparing version 0.2.7 to 0.2.8
@@ -18,8 +18,13 @@ import { Transform } from "stream"; | ||
export declare const streamResponseToolCallsFormat: (stream: ChatCompletionStream<ChatCompletionChunk>) => Promise<ChatToolCallFuntion[]>; | ||
export declare const streamResponseFormat: (stream: ChatCompletionStream<ChatCompletionChunk>, cut: (data: string) => void, toolCall: (data: { | ||
name: string; | ||
arguments: { | ||
[key: string]: any; | ||
}; | ||
}) => void) => Promise<void>; | ||
export declare const streamResponseFormat: (stream: ChatCompletionStream<ChatCompletionChunk>, callbacks?: { | ||
onData?: (data: any) => void; | ||
onContent?: (data: string) => void; | ||
onCut?: (data: string) => void; | ||
onToolCall?: (data: { | ||
name: string; | ||
arguments: { | ||
[key: string]: any; | ||
}; | ||
}) => void; | ||
}) => Promise<void>; | ||
export declare const StreamUtils: { | ||
@@ -59,8 +64,13 @@ /** | ||
*/ | ||
format: (stream: ChatCompletionStream<ChatCompletionChunk>, cut: (data: string) => void, toolCall: (data: { | ||
name: string; | ||
arguments: { | ||
[key: string]: any; | ||
}; | ||
}) => void) => Promise<void>; | ||
format: (stream: ChatCompletionStream<ChatCompletionChunk>, callbacks?: { | ||
onData?: (data: any) => void; | ||
onContent?: (data: string) => void; | ||
onCut?: (data: string) => void; | ||
onToolCall?: (data: { | ||
name: string; | ||
arguments: { | ||
[key: string]: any; | ||
}; | ||
}) => void; | ||
}) => Promise<void>; | ||
}; |
@@ -130,3 +130,3 @@ "use strict"; | ||
exports.streamResponseToolCallsFormat = streamResponseToolCallsFormat; | ||
const streamResponseFormat = async (stream, cut, toolCall) => { | ||
const streamResponseFormat = async (stream, callbacks) => { | ||
let cutStrBuf = ""; | ||
@@ -137,3 +137,9 @@ const functionCalls = []; | ||
const delta = choices[0].delta; | ||
if (callbacks?.onData) { | ||
callbacks.onData(choices[0]); | ||
} | ||
const content = delta.content || ""; | ||
if (callbacks?.onContent && content) { | ||
callbacks.onContent(content); | ||
} | ||
for (let index = 0; index < content.length; index++) { | ||
@@ -143,3 +149,5 @@ const char = content[index]; | ||
if (char === '\n' || (0, _1.isPunctuation)(char)) { | ||
cut(cutStrBuf); | ||
if (callbacks?.onCut) { | ||
callbacks?.onCut(cutStrBuf); | ||
} | ||
cutStrBuf = ""; | ||
@@ -161,14 +169,11 @@ } | ||
try { | ||
toolCall({ | ||
name: functionCall.name, | ||
arguments: JSON.parse(functionCall.arguments) | ||
}); | ||
if (callbacks?.onToolCall) { | ||
callbacks.onToolCall({ | ||
name: functionCall.name, | ||
arguments: JSON.parse(functionCall.arguments) | ||
}); | ||
} | ||
} | ||
catch (err) { | ||
toolCall({ | ||
name: functionCall.name, | ||
arguments: { | ||
error: "parse arguments error" | ||
} | ||
}); | ||
Logger_1.Logger.debug("parse arguments error", err); | ||
} | ||
@@ -175,0 +180,0 @@ }); |
{ | ||
"name": "@re-ai/openai-like-api", | ||
"version": "0.2.7", | ||
"version": "0.2.8", | ||
"description": "reai openai like api", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -157,9 +157,14 @@ import { Transform } from "stream"; | ||
stream: ChatCompletionStream<ChatCompletionChunk>, | ||
cut: (data: string) => void, | ||
toolCall: (data: { | ||
name: string, | ||
arguments: { | ||
[key: string]: any | ||
} | ||
}) => void, | ||
callbacks?: { | ||
onData?: (data: any) => void, | ||
onContent?: (data: string) => void, | ||
onCut?: (data: string) => void, | ||
onToolCall?: (data: { | ||
name: string, | ||
arguments: { | ||
[key: string]: any | ||
} | ||
}) => void, | ||
} | ||
): Promise<void> => { | ||
@@ -171,4 +176,11 @@ let cutStrBuf = "" | ||
const delta = choices[0].delta | ||
if (callbacks?.onData) { | ||
callbacks.onData(choices[0]) | ||
} | ||
const content = delta.content || "" | ||
if (callbacks?.onContent && content) { | ||
callbacks.onContent(content) | ||
} | ||
for (let index = 0; index < content.length; index++) { | ||
@@ -178,3 +190,5 @@ const char = content[index]; | ||
if (char === '\n' || isPunctuation(char)) { | ||
cut(cutStrBuf) | ||
if (callbacks?.onCut) { | ||
callbacks?.onCut(cutStrBuf) | ||
} | ||
cutStrBuf = "" | ||
@@ -199,13 +213,10 @@ } | ||
try { | ||
toolCall({ | ||
name: functionCall.name, | ||
arguments: JSON.parse(functionCall.arguments) | ||
}) | ||
if (callbacks?.onToolCall) { | ||
callbacks.onToolCall({ | ||
name: functionCall.name, | ||
arguments: JSON.parse(functionCall.arguments) | ||
}) | ||
} | ||
} catch (err) { | ||
toolCall({ | ||
name: functionCall.name, | ||
arguments: { | ||
error: "parse arguments error" | ||
} | ||
}) | ||
Logger.debug("parse arguments error", err) | ||
} | ||
@@ -212,0 +223,0 @@ }) |
942808
6728