correct type errors

This commit is contained in:
ferenci84 2025-03-03 09:03:00 +01:00
parent 44d91ece03
commit 8c330cb23f
5 changed files with 34 additions and 30 deletions

2
core/index.d.ts vendored
View file

@ -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";

View file

@ -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<void> {}
async close(): Promise<void> { }
async encode(text: string): Promise<number[]> {
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
};

View file

@ -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,

View file

@ -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<string, string> = {
"Content-Type": "application/json",
};
if (this.apiKey) {
headers.Authorization = `Bearer ${this.apiKey}`;
}
@ -331,7 +331,7 @@ class Ollama extends BaseLLM {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (this.apiKey) {
headers.Authorization = `Bearer ${this.apiKey}`;
}
@ -396,7 +396,7 @@ class Ollama extends BaseLLM {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (this.apiKey) {
headers.Authorization = `Bearer ${this.apiKey}`;
}
@ -482,7 +482,7 @@ class Ollama extends BaseLLM {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (this.apiKey) {
headers.Authorization = `Bearer ${this.apiKey}`;
}
@ -523,7 +523,7 @@ class Ollama extends BaseLLM {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (this.apiKey) {
headers.Authorization = `Bearer ${this.apiKey}`;
}
@ -549,7 +549,7 @@ class Ollama extends BaseLLM {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (this.apiKey) {
headers.Authorization = `Bearer ${this.apiKey}`;
}

View file

@ -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