Simplify provider logic and migrate getContextWindow (#142)
This commit is contained in:
@@ -5,7 +5,7 @@ import { createOpenRouter } from "@openrouter/ai-sdk-provider";
|
||||
import { createOllama } from "ollama-ai-provider";
|
||||
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
||||
import type { LargeLanguageModel, UserSettings } from "../../lib/schemas";
|
||||
import { AUTO_MODELS, MODEL_OPTIONS } from "../../constants/models";
|
||||
import { AUTO_MODELS } from "../../constants/models";
|
||||
import { getEnvVar } from "./read_env";
|
||||
import log from "electron-log";
|
||||
import { getLanguageModelProviders } from "../shared/language_model_helpers";
|
||||
@@ -140,19 +140,3 @@ export async function getModelClient(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Most models support at least 8000 output tokens so we use it as a default value.
|
||||
const DEFAULT_MAX_TOKENS = 8_000;
|
||||
|
||||
export function getMaxTokens(model: LargeLanguageModel) {
|
||||
if (!MODEL_OPTIONS[model.provider as keyof typeof MODEL_OPTIONS]) {
|
||||
logger.warn(
|
||||
`Model provider ${model.provider} not found in MODEL_OPTIONS. Using default max tokens.`,
|
||||
);
|
||||
return DEFAULT_MAX_TOKENS;
|
||||
}
|
||||
const modelOption = MODEL_OPTIONS[
|
||||
model.provider as keyof typeof MODEL_OPTIONS
|
||||
].find((m) => m.name === model.name);
|
||||
return modelOption?.maxOutputTokens || DEFAULT_MAX_TOKENS;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { LargeLanguageModel } from "@/lib/schemas";
|
||||
import { readSettings } from "../../main/settings";
|
||||
import { Message } from "../ipc_types";
|
||||
import { MODEL_OPTIONS } from "../../constants/models";
|
||||
import log from "electron-log";
|
||||
|
||||
const logger = log.scope("token_utils");
|
||||
import { getLanguageModels } from "../shared/language_model_helpers";
|
||||
|
||||
// Estimate tokens (4 characters per token)
|
||||
export const estimateTokens = (text: string): number => {
|
||||
@@ -19,17 +18,26 @@ export const estimateMessagesTokens = (messages: Message[]): number => {
|
||||
|
||||
const DEFAULT_CONTEXT_WINDOW = 128_000;
|
||||
|
||||
export function getContextWindow() {
|
||||
export async function getContextWindow() {
|
||||
const settings = readSettings();
|
||||
const model = settings.selectedModel;
|
||||
if (!MODEL_OPTIONS[model.provider as keyof typeof MODEL_OPTIONS]) {
|
||||
logger.warn(
|
||||
`Model provider ${model.provider} not found in MODEL_OPTIONS. Using default max tokens.`,
|
||||
);
|
||||
return DEFAULT_CONTEXT_WINDOW;
|
||||
}
|
||||
const modelOption = MODEL_OPTIONS[
|
||||
model.provider as keyof typeof MODEL_OPTIONS
|
||||
].find((m) => m.name === model.name);
|
||||
|
||||
const models = await getLanguageModels({
|
||||
providerId: model.provider,
|
||||
});
|
||||
|
||||
const modelOption = models.find((m) => m.apiName === model.name);
|
||||
return modelOption?.contextWindow || DEFAULT_CONTEXT_WINDOW;
|
||||
}
|
||||
|
||||
// Most models support at least 8000 output tokens so we use it as a default value.
|
||||
const DEFAULT_MAX_TOKENS = 8_000;
|
||||
|
||||
export async function getMaxTokens(model: LargeLanguageModel) {
|
||||
const models = await getLanguageModels({
|
||||
providerId: model.provider,
|
||||
});
|
||||
|
||||
const modelOption = models.find((m) => m.apiName === model.name);
|
||||
return modelOption?.maxOutputTokens || DEFAULT_MAX_TOKENS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user