fixed openai system calls in history breaking anthropic calls
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/ai-utils",
|
||||
"version": "1.3.1",
|
||||
"version": "1.3.2",
|
||||
"description": "AI Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
||||
@@ -52,7 +52,10 @@ export class Anthropic extends LLMProvider {
|
||||
ask(message: string, options: LLMRequest = {}): AbortablePromise<string | any> {
|
||||
const controller = new AbortController();
|
||||
return Object.assign(new Promise<any>(async (res) => {
|
||||
let history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]);
|
||||
let history = this.fromStandard([
|
||||
...(options.history || []).filter(h => h.role !== 'system'),
|
||||
{role: 'user', content: message, timestamp: Date.now()}
|
||||
]);
|
||||
const tools = options.tools || this.ai.options.llm?.tools || [];
|
||||
const requestParams: any = {
|
||||
model: options.model || this.model,
|
||||
@@ -83,7 +86,7 @@ export class Anthropic extends LLMProvider {
|
||||
};
|
||||
}
|
||||
|
||||
let resp: any, isFirstMessage = true, terminal = false;
|
||||
let resp: any, hasStreamedText = false, terminal = false;
|
||||
do {
|
||||
requestParams.messages = history.map(({timestamp, ...m}) => m);
|
||||
resp = await this.client.messages.create(requestParams).catch(err => {
|
||||
@@ -93,8 +96,7 @@ export class Anthropic extends LLMProvider {
|
||||
|
||||
// Streaming mode
|
||||
if(options.stream) {
|
||||
if(!isFirstMessage) options.stream({text: '\n\n'});
|
||||
else isFirstMessage = false;
|
||||
if(hasStreamedText) options.stream({text: '\n\n'});
|
||||
resp.content = [];
|
||||
for await (const chunk of resp) {
|
||||
if(controller.signal.aborted) break;
|
||||
@@ -108,7 +110,7 @@ export class Anthropic extends LLMProvider {
|
||||
if(chunk.delta.type === 'text_delta') {
|
||||
const text = chunk.delta.text;
|
||||
resp.content.at(-1).text += text;
|
||||
options.stream({text});
|
||||
if(text) { hasStreamedText = true; options.stream({text}); }
|
||||
} else if(chunk.delta.type === 'input_json_delta') {
|
||||
resp.content.at(-1).input += chunk.delta.partial_json;
|
||||
}
|
||||
|
||||
@@ -69,11 +69,12 @@ export class OpenAi extends LLMProvider {
|
||||
ask(message: string, options: LLMRequest = {}): AbortablePromise<string | any> {
|
||||
const controller = new AbortController();
|
||||
return Object.assign(new Promise<any>(async (res, rej) => {
|
||||
if(options.system) {
|
||||
if(options.history?.[0]?.role != 'system') options.history?.splice(0, 0, {role: 'system', content: options.system, timestamp: Date.now()});
|
||||
else options.history[0].content = options.system;
|
||||
}
|
||||
let history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]);
|
||||
const base = (options.history || []).filter(h => h.role !== 'system');
|
||||
let history = this.fromStandard([
|
||||
...(options.system ? [{role: <any>'system', content: options.system, timestamp: Date.now()}] : []),
|
||||
...base,
|
||||
{role: 'user', content: message, timestamp: Date.now()}
|
||||
]);
|
||||
const tools = options.tools || this.ai.options.llm?.tools || [];
|
||||
const requestParams: any = {
|
||||
model: options.model || this.model,
|
||||
@@ -107,7 +108,7 @@ export class OpenAi extends LLMProvider {
|
||||
};
|
||||
}
|
||||
|
||||
let resp: any, isFirstMessage = true, terminal = false;
|
||||
let resp: any, hasStreamedText = false, terminal = false;
|
||||
do {
|
||||
requestParams.messages = history.map(({timestamp, ...m}) => m);
|
||||
resp = await this.client.chat.completions.create(requestParams).catch(err => {
|
||||
@@ -116,16 +117,15 @@ export class OpenAi extends LLMProvider {
|
||||
});
|
||||
|
||||
if(options.stream) {
|
||||
if(!isFirstMessage) options.stream({text: '\n\n'});
|
||||
else isFirstMessage = false;
|
||||
if(hasStreamedText) options.stream({text: '\n\n'});
|
||||
resp.choices = [{message: {role: 'assistant', content: '', tool_calls: [], timestamp: Date.now()}}];
|
||||
for await (const chunk of resp) {
|
||||
if(controller.signal.aborted) break;
|
||||
if(chunk.choices[0].delta.content) {
|
||||
resp.choices[0].message.content += chunk.choices[0].delta.content;
|
||||
options.stream({text: chunk.choices[0].delta.content});
|
||||
const text = chunk.choices[0].delta.content;
|
||||
resp.choices[0].message.content += text;
|
||||
if(text) { hasStreamedText = true; options.stream({text}); }
|
||||
}
|
||||
|
||||
if(chunk.choices[0].delta.tool_calls) {
|
||||
for(const deltaTC of chunk.choices[0].delta.tool_calls) {
|
||||
const existing = resp.choices[0].message.tool_calls.find(tc => tc.index === deltaTC.index);
|
||||
@@ -184,8 +184,8 @@ export class OpenAi extends LLMProvider {
|
||||
history.push({role: 'assistant', content: textContent, timestamp: Date.now()});
|
||||
}
|
||||
history = this.toStandard(history);
|
||||
if(options.history) options.history.splice(0, options.history.length, ...history.filter(h => h.role !== 'system'));
|
||||
if(options.stream) options.stream({done: true});
|
||||
if(options.history) options.history.splice(0, options.history.length, ...history);
|
||||
const finalContent = history.at(-1)?.content;
|
||||
res(options.schema ? JSONAttemptParse(finalContent, finalContent) : finalContent);
|
||||
}), {abort: () => controller.abort()});
|
||||
|
||||
Reference in New Issue
Block a user