fixed openai system calls in history breaking anthropic calls
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user