From 8c330cb23ff1044fb836def561c6fa7489ca46f6 Mon Sep 17 00:00:00 2001 From: ferenci84 Date: Mon, 3 Mar 2025 09:03:00 +0100 Subject: [PATCH] correct type errors --- core/index.d.ts | 2 +- core/llm/countTokens.ts | 12 +++++++----- core/llm/index.ts | 18 +++++++++--------- core/llm/llms/Ollama.ts | 30 +++++++++++++++--------------- core/util/messageContent.ts | 2 ++ 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/core/index.d.ts b/core/index.d.ts index e69fa8ee2..6dd5a3c94 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -308,7 +308,7 @@ export interface CompletionOptions extends BaseCompletionOptions { model: string; } -export type ChatMessageRole = "user" | "assistant" | "system" | "tool"; +export type ChatMessageRole = "user" | "assistant" | "thinking" | "system" | "tool"; export type TextMessagePart = { type: "text"; diff --git a/core/llm/countTokens.ts b/core/llm/countTokens.ts index dfbd2da5d..a05889c52 100644 --- a/core/llm/countTokens.ts +++ b/core/llm/countTokens.ts @@ -27,9 +27,9 @@ class LlamaEncoding implements Encoding { } class NonWorkerAsyncEncoder implements AsyncEncoder { - constructor(private readonly encoding: Encoding) {} + constructor(private readonly encoding: Encoding) { } - async close(): Promise {} + async close(): Promise { } async encode(text: string): Promise { return this.encoding.encode(text); @@ -366,6 +366,7 @@ function chatMessageIsEmpty(message: ChatMessage): boolean { message.content.trim() === "" && !message.toolCalls ); + case "thinking": case "tool": return false; } @@ -383,8 +384,8 @@ function compileChatMessages( ): ChatMessage[] { let msgsCopy = msgs ? msgs - .map((msg) => ({ ...msg })) - .filter((msg) => !chatMessageIsEmpty(msg) && msg.role !== "system") + .map((msg) => ({ ...msg })) + .filter((msg) => !chatMessageIsEmpty(msg) && msg.role !== "system") : []; msgsCopy = addSpaceToAnyEmptyMessages(msgsCopy); @@ -469,5 +470,6 @@ export { pruneLinesFromTop, pruneRawPromptFromTop, pruneStringFromBottom, - pruneStringFromTop, + pruneStringFromTop }; + diff --git a/core/llm/index.ts b/core/llm/index.ts index b08d8b214..a692d0bc0 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -181,11 +181,11 @@ export abstract class BaseLLM implements ILLM { options.completionOptions?.maxTokens ?? (llmInfo?.maxCompletionTokens ? Math.min( - llmInfo.maxCompletionTokens, - // Even if the model has a large maxTokens, we don't want to use that every time, - // because it takes away from the context length - this.contextLength / 4, - ) + llmInfo.maxCompletionTokens, + // Even if the model has a large maxTokens, we don't want to use that every time, + // because it takes away from the context length + this.contextLength / 4, + ) : DEFAULT_MAX_TOKENS), }; this.requestOptions = options.requestOptions; @@ -811,8 +811,8 @@ export abstract class BaseLLM implements ILLM { signal, completionOptions, )) { - completion += chunk.content; - yield chunk; + + if (chunk.role === "assistant") { completion += chunk.content; yield chunk; } @@ -912,7 +912,7 @@ export abstract class BaseLLM implements ILLM { ); } - protected async *_streamComplete( + protected async * _streamComplete( prompt: string, signal: AbortSignal, options: CompletionOptions, @@ -920,7 +920,7 @@ export abstract class BaseLLM implements ILLM { throw new Error("Not implemented"); } - protected async *_streamChat( + protected async * _streamChat( messages: ChatMessage[], signal: AbortSignal, options: CompletionOptions, diff --git a/core/llm/llms/Ollama.ts b/core/llm/llms/Ollama.ts index ee822eb27..7d581b750 100644 --- a/core/llm/llms/Ollama.ts +++ b/core/llm/llms/Ollama.ts @@ -1,12 +1,12 @@ import { JSONSchema7, JSONSchema7Object } from "json-schema"; -import { ChatMessage, CompletionOptions, LLMOptions } from "../../index.js"; +import { ChatMessage, ChatMessageRole, CompletionOptions, LLMOptions } from "../../index.js"; import { renderChatMessage } from "../../util/messageContent.js"; import { BaseLLM } from "../index.js"; import { streamResponse } from "../stream.js"; type OllamaChatMessage = { - role: "tool" | "user" | "assistant" | "system"; + role: ChatMessageRole; content: string; images?: string[] | null; tool_calls?: { @@ -82,10 +82,10 @@ type OllamaBaseResponse = { model: string; created_at: string; } & ( - | { + | { done: false; } - | { + | { done: true; done_reason: string; total_duration: number; // Time spent generating the response in nanoseconds @@ -96,7 +96,7 @@ type OllamaBaseResponse = { eval_duration: number; // Time spent generating the response in nanoseconds context: number[]; // An encoding of the conversation used in this response; can be sent in the next request to keep conversational memory } -); + ); type OllamaErrorResponse = { error: string; @@ -105,14 +105,14 @@ type OllamaErrorResponse = { type OllamaRawResponse = | OllamaErrorResponse | (OllamaBaseResponse & { - response: string; // the generated response - }); + response: string; // the generated response + }); type OllamaChatResponse = | OllamaErrorResponse | (OllamaBaseResponse & { - message: OllamaChatMessage; - }); + message: OllamaChatMessage; + }); interface OllamaTool { type: "function"; @@ -142,7 +142,7 @@ class Ollama extends BaseLLM { const headers: Record = { "Content-Type": "application/json", }; - + if (this.apiKey) { headers.Authorization = `Bearer ${this.apiKey}`; } @@ -331,7 +331,7 @@ class Ollama extends BaseLLM { const headers: Record = { "Content-Type": "application/json", }; - + if (this.apiKey) { headers.Authorization = `Bearer ${this.apiKey}`; } @@ -396,7 +396,7 @@ class Ollama extends BaseLLM { const headers: Record = { "Content-Type": "application/json", }; - + if (this.apiKey) { headers.Authorization = `Bearer ${this.apiKey}`; } @@ -482,7 +482,7 @@ class Ollama extends BaseLLM { const headers: Record = { "Content-Type": "application/json", }; - + if (this.apiKey) { headers.Authorization = `Bearer ${this.apiKey}`; } @@ -523,7 +523,7 @@ class Ollama extends BaseLLM { const headers: Record = { "Content-Type": "application/json", }; - + if (this.apiKey) { headers.Authorization = `Bearer ${this.apiKey}`; } @@ -549,7 +549,7 @@ class Ollama extends BaseLLM { const headers: Record = { "Content-Type": "application/json", }; - + if (this.apiKey) { headers.Authorization = `Bearer ${this.apiKey}`; } diff --git a/core/util/messageContent.ts b/core/util/messageContent.ts index 89462a1bf..57bc2896e 100644 --- a/core/util/messageContent.ts +++ b/core/util/messageContent.ts @@ -21,6 +21,7 @@ export function renderChatMessage(message: ChatMessage): string { switch (message.role) { case "user": case "assistant": + case "thinking": case "system": return stripImages(message.content); case "tool": @@ -36,6 +37,7 @@ export function normalizeToMessageParts(message: ChatMessage): MessagePart[] { switch (message.role) { case "user": case "assistant": + case "thinking": case "system": return Array.isArray(message.content) ? message.content