Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1fe1e0cafe | |||
| 3aa4684923 | |||
| 0730f5f3f9 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/ai-utils",
|
"name": "@ztimson/ai-utils",
|
||||||
"version": "0.1.18",
|
"version": "0.1.21",
|
||||||
"description": "AI Utility library",
|
"description": "AI Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import {Anthropic as anthropic} from '@anthropic-ai/sdk';
|
import {Anthropic as anthropic} from '@anthropic-ai/sdk';
|
||||||
import {findByProp, objectMap, JSONSanitize, JSONAttemptParse} from '@ztimson/utils';
|
import {findByProp, objectMap, JSONSanitize, JSONAttemptParse, deepCopy} from '@ztimson/utils';
|
||||||
import {Ai} from './ai.ts';
|
import {Ai} from './ai.ts';
|
||||||
import {LLMMessage, LLMRequest} from './llm.ts';
|
import {LLMMessage, LLMRequest} from './llm.ts';
|
||||||
import {AbortablePromise, LLMProvider} from './provider.ts';
|
import {AbortablePromise, LLMProvider} from './provider.ts';
|
||||||
@@ -39,19 +39,20 @@ export class Anthropic extends LLMProvider {
|
|||||||
if(history[i].role == 'tool') {
|
if(history[i].role == 'tool') {
|
||||||
const h: any = history[i];
|
const h: any = history[i];
|
||||||
history.splice(i, 1,
|
history.splice(i, 1,
|
||||||
{role: 'assistant', content: [{type: 'tool_use', id: h.id, name: h.name, input: h.args}], timestamp: h.timestamp},
|
{role: 'assistant', content: [{type: 'tool_use', id: h.id, name: h.name, input: h.args}]},
|
||||||
{role: 'user', content: [{type: 'tool_result', tool_use_id: h.id, is_error: !!h.error, content: h.error || h.content}], timestamp: Date.now()}
|
{role: 'user', content: [{type: 'tool_result', tool_use_id: h.id, is_error: !!h.error, content: h.error || h.content}]}
|
||||||
)
|
)
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return history;
|
return history.map(({timestamp, ...h}) => h);
|
||||||
}
|
}
|
||||||
|
|
||||||
ask(message: string, options: LLMRequest = {}): AbortablePromise<LLMMessage[]> {
|
ask(message: string, options: LLMRequest = {}): AbortablePromise<LLMMessage[]> {
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
const response = new Promise<any>(async (res, rej) => {
|
const response = new Promise<any>(async (res, rej) => {
|
||||||
let history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]);
|
let history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]);
|
||||||
|
const original = deepCopy(history);
|
||||||
if(options.compress) history = await this.ai.llm.compress(<any>history, options.compress.max, options.compress.min, options);
|
if(options.compress) history = await this.ai.llm.compress(<any>history, options.compress.max, options.compress.min, options);
|
||||||
const requestParams: any = {
|
const requestParams: any = {
|
||||||
model: options.model || this.model,
|
model: options.model || this.model,
|
||||||
@@ -73,11 +74,11 @@ export class Anthropic extends LLMProvider {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let resp: any;
|
let resp: any;
|
||||||
const loopMessages: any[] = [];
|
const assistantMessages: string[] = [];
|
||||||
do {
|
do {
|
||||||
resp = await this.client.messages.create(requestParams);
|
resp = await this.client.messages.create(requestParams);
|
||||||
if(options.stream) {
|
if(options.stream) {
|
||||||
if(loopMessages.length) options.stream({text: '\n\n'});
|
if(assistantMessages.length) options.stream({text: '\n\n'});
|
||||||
resp.content = [];
|
resp.content = [];
|
||||||
for await (const chunk of resp) {
|
for await (const chunk of resp) {
|
||||||
if(controller.signal.aborted) break;
|
if(controller.signal.aborted) break;
|
||||||
@@ -104,10 +105,12 @@ export class Anthropic extends LLMProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
loopMessages.push({role: 'assistant', content: resp.content, timestamp: Date.now()});
|
const textContent = resp.content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n');
|
||||||
|
if(textContent) assistantMessages.push(textContent);
|
||||||
const toolCalls = resp.content.filter((c: any) => c.type === 'tool_use');
|
const toolCalls = resp.content.filter((c: any) => c.type === 'tool_use');
|
||||||
if(toolCalls.length && !controller.signal.aborted) {
|
if(toolCalls.length && !controller.signal.aborted) {
|
||||||
history.push({role: 'assistant', content: resp.content, timestamp: Date.now()});
|
history.push({role: 'assistant', content: resp.content});
|
||||||
|
original.push({role: 'assistant', content: resp.content});
|
||||||
const results = await Promise.all(toolCalls.map(async (toolCall: any) => {
|
const results = await Promise.all(toolCalls.map(async (toolCall: any) => {
|
||||||
const tool = options.tools?.find(findByProp('name', toolCall.name));
|
const tool = options.tools?.find(findByProp('name', toolCall.name));
|
||||||
if(!tool) return {tool_use_id: toolCall.id, is_error: true, content: 'Tool not found'};
|
if(!tool) return {tool_use_id: toolCall.id, is_error: true, content: 'Tool not found'};
|
||||||
@@ -118,18 +121,13 @@ export class Anthropic extends LLMProvider {
|
|||||||
return {type: 'tool_result', tool_use_id: toolCall.id, is_error: true, content: err?.message || err?.toString() || 'Unknown'};
|
return {type: 'tool_result', tool_use_id: toolCall.id, is_error: true, content: err?.message || err?.toString() || 'Unknown'};
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
const userMsg = {role: 'user', content: results, timestamp: Date.now()};
|
history.push({role: 'user', content: results});
|
||||||
history.push(userMsg);
|
original.push({role: 'user', content: results});
|
||||||
loopMessages.push(userMsg);
|
|
||||||
requestParams.messages = history;
|
requestParams.messages = history;
|
||||||
}
|
}
|
||||||
} while (!controller.signal.aborted && resp.content.some((c: any) => c.type === 'tool_use'));
|
} while (!controller.signal.aborted && resp.content.some((c: any) => c.type === 'tool_use'));
|
||||||
|
|
||||||
const combinedContent = loopMessages.filter(m => m.role === 'assistant')
|
|
||||||
.map(m => m.content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n'))
|
|
||||||
.filter(c => c).join('\n\n');
|
|
||||||
if(options.stream) options.stream({done: true});
|
if(options.stream) options.stream({done: true});
|
||||||
res(this.toStandard([...history, {role: 'assistant', content: combinedContent, timestamp: Date.now()}]));
|
res(this.toStandard([...original, {role: 'assistant', content: assistantMessages.join('\n\n'), timestamp: Date.now()}]));
|
||||||
});
|
});
|
||||||
|
|
||||||
return Object.assign(response, {abort: () => controller.abort()});
|
return Object.assign(response, {abort: () => controller.abort()});
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ export type LLMMessage = {
|
|||||||
/** Message content */
|
/** Message content */
|
||||||
content: string | any;
|
content: string | any;
|
||||||
/** Timestamp */
|
/** Timestamp */
|
||||||
timestamp: number;
|
timestamp?: number;
|
||||||
} | {
|
} | {
|
||||||
/** Tool call */
|
/** Tool call */
|
||||||
role: 'tool';
|
role: 'tool';
|
||||||
@@ -27,7 +27,7 @@ export type LLMMessage = {
|
|||||||
/** Tool error */
|
/** Tool error */
|
||||||
error: undefined | string;
|
error: undefined | string;
|
||||||
/** Timestamp */
|
/** Timestamp */
|
||||||
timestamp: number;
|
timestamp?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type LLMOptions = {
|
export type LLMOptions = {
|
||||||
|
|||||||
@@ -31,8 +31,9 @@ export class Ollama extends LLMProvider {
|
|||||||
|
|
||||||
private fromStandard(history: LLMMessage[]): any[] {
|
private fromStandard(history: LLMMessage[]): any[] {
|
||||||
return history.map((h: any) => {
|
return history.map((h: any) => {
|
||||||
if(h.role != 'tool') return h;
|
const {timestamp, ...rest} = h;
|
||||||
return {role: 'tool', tool_name: h.name, content: h.error || h.content, timestamp: h.timestamp}
|
if(h.role != 'tool') return rest;
|
||||||
|
return {role: 'tool', tool_name: h.name, content: h.error || h.content}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,22 +91,21 @@ export class Ollama extends LLMProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loopMessages.push({role: 'assistant', content: resp.message?.content, timestamp: Date.now()});
|
loopMessages.push({role: 'assistant', content: resp.message?.content, timestamp: Date.now()});
|
||||||
|
|
||||||
if(resp.message?.tool_calls?.length && !controller.signal.aborted) {
|
if(resp.message?.tool_calls?.length && !controller.signal.aborted) {
|
||||||
history.push({...resp.message, timestamp: Date.now()});
|
history.push(resp.message);
|
||||||
const results = await Promise.all(resp.message.tool_calls.map(async (toolCall: any) => {
|
const results = await Promise.all(resp.message.tool_calls.map(async (toolCall: any) => {
|
||||||
const tool = (options.tools || this.ai.options.tools)?.find(findByProp('name', toolCall.function.name));
|
const tool = (options.tools || this.ai.options.tools)?.find(findByProp('name', toolCall.function.name));
|
||||||
if(!tool) return {role: 'tool', tool_name: toolCall.function.name, content: '{"error": "Tool not found"}', timestamp: Date.now()};
|
if(!tool) return {role: 'tool', tool_name: toolCall.function.name, content: '{"error": "Tool not found"}'};
|
||||||
const args = typeof toolCall.function.arguments === 'string' ? JSONAttemptParse(toolCall.function.arguments, {}) : toolCall.function.arguments;
|
const args = typeof toolCall.function.arguments === 'string' ? JSONAttemptParse(toolCall.function.arguments, {}) : toolCall.function.arguments;
|
||||||
try {
|
try {
|
||||||
const result = await tool.fn(args, this.ai);
|
const result = await tool.fn(args, this.ai);
|
||||||
return {role: 'tool', tool_name: toolCall.function.name, args, content: JSONSanitize(result), timestamp: Date.now()};
|
return {role: 'tool', tool_name: toolCall.function.name, args, content: JSONSanitize(result)};
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
return {role: 'tool', tool_name: toolCall.function.name, args, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'}), timestamp: Date.now()};
|
return {role: 'tool', tool_name: toolCall.function.name, args, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'})};
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
history.push(...results);
|
history.push(...results);
|
||||||
loopMessages.push(...results);
|
loopMessages.push(...results.map(r => ({...r, timestamp: Date.now()})));
|
||||||
requestParams.messages = history;
|
requestParams.messages = history;
|
||||||
}
|
}
|
||||||
} while (!controller.signal.aborted && resp.message?.tool_calls?.length);
|
} while (!controller.signal.aborted && resp.message?.tool_calls?.length);
|
||||||
|
|||||||
@@ -47,16 +47,15 @@ export class OpenAi extends LLMProvider {
|
|||||||
content: null,
|
content: null,
|
||||||
tool_calls: [{ id: h.id, type: 'function', function: { name: h.name, arguments: JSON.stringify(h.args) } }],
|
tool_calls: [{ id: h.id, type: 'function', function: { name: h.name, arguments: JSON.stringify(h.args) } }],
|
||||||
refusal: null,
|
refusal: null,
|
||||||
annotations: [],
|
annotations: []
|
||||||
timestamp: h.timestamp
|
|
||||||
}, {
|
}, {
|
||||||
role: 'tool',
|
role: 'tool',
|
||||||
tool_call_id: h.id,
|
tool_call_id: h.id,
|
||||||
content: h.error || h.content,
|
content: h.error || h.content
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
result.push(h);
|
const {timestamp, ...rest} = h;
|
||||||
|
result.push(rest);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}, [] as any[]);
|
}, [] as any[]);
|
||||||
@@ -111,20 +110,20 @@ export class OpenAi extends LLMProvider {
|
|||||||
|
|
||||||
const toolCalls = resp.choices[0].message.tool_calls || [];
|
const toolCalls = resp.choices[0].message.tool_calls || [];
|
||||||
if(toolCalls.length && !controller.signal.aborted) {
|
if(toolCalls.length && !controller.signal.aborted) {
|
||||||
history.push({...resp.choices[0].message, timestamp: Date.now()});
|
history.push(resp.choices[0].message);
|
||||||
const results = await Promise.all(toolCalls.map(async (toolCall: any) => {
|
const results = await Promise.all(toolCalls.map(async (toolCall: any) => {
|
||||||
const tool = options.tools?.find(findByProp('name', toolCall.function.name));
|
const tool = options.tools?.find(findByProp('name', toolCall.function.name));
|
||||||
if(!tool) return {role: 'tool', tool_call_id: toolCall.id, content: '{"error": "Tool not found"}', timestamp: Date.now()};
|
if(!tool) return {role: 'tool', tool_call_id: toolCall.id, content: '{"error": "Tool not found"}'};
|
||||||
try {
|
try {
|
||||||
const args = JSONAttemptParse(toolCall.function.arguments, {});
|
const args = JSONAttemptParse(toolCall.function.arguments, {});
|
||||||
const result = await tool.fn(args, this.ai);
|
const result = await tool.fn(args, this.ai);
|
||||||
return {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize(result), timestamp: Date.now()};
|
return {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize(result)};
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
return {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'}), timestamp: Date.now()};
|
return {role: 'tool', tool_call_id: toolCall.id, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'})};
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
history.push(...results);
|
history.push(...results);
|
||||||
loopMessages.push(...results);
|
loopMessages.push(...results.map(r => ({...r, timestamp: Date.now()})));
|
||||||
requestParams.messages = history;
|
requestParams.messages = history;
|
||||||
}
|
}
|
||||||
} while (!controller.signal.aborted && resp.choices?.[0]?.message?.tool_calls?.length);
|
} while (!controller.signal.aborted && resp.choices?.[0]?.message?.tool_calls?.length);
|
||||||
@@ -134,7 +133,6 @@ export class OpenAi extends LLMProvider {
|
|||||||
if(options.stream) options.stream({done: true});
|
if(options.stream) options.stream({done: true});
|
||||||
res(this.toStandard([...history, {role: 'assistant', content: combinedContent, timestamp: Date.now()}]));
|
res(this.toStandard([...history, {role: 'assistant', content: combinedContent, timestamp: Date.now()}]));
|
||||||
});
|
});
|
||||||
|
|
||||||
return Object.assign(response, {abort: () => controller.abort()});
|
return Object.assign(response, {abort: () => controller.abort()});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user