feat(): support base url and model custom

This commit is contained in:
shouldnotappearcalm 2024-10-13 13:30:50 +08:00
parent ffa9f11360
commit ecfa261028
3 changed files with 26 additions and 6 deletions

View File

@ -1,9 +1,28 @@
import { createAnthropic } from '@ai-sdk/anthropic';
import { getAPIKey } from '~/lib/.server/llm/api-key';
import { env } from 'node:process';
export function getAnthropicModel(apiKey: string) {
export function getAnthropicModel(env: Env) {
const anthropic = createAnthropic({
apiKey,
apiKey: getAPIKey(env),
baseURL: getBaseUrl(env)
});
return anthropic('claude-3-5-sonnet-20240620');
return anthropic(getModel(env));
}
export function getBaseUrl(cloudflareEnv: Env) {
/**
* The `cloudflareEnv` is only used when deployed or when previewing locally.
* In development the environment variables are available through `env`.
*/
return env.ANTHROPIC_BASE_URL || cloudflareEnv.ANTHROPIC_BASE_URL;
}
export function getModel(cloudflareEnv: Env) {
/**
* The `cloudflareEnv` is only used when deployed or when previewing locally.
* In development the environment variables are available through `env`.
*/
return env.ANTHROPIC_MODEL || cloudflareEnv.ANTHROPIC_MODEL || 'claude-3-5-sonnet-20240620';
}

View File

@ -1,5 +1,4 @@
import { streamText as _streamText, convertToCoreMessages } from 'ai';
import { getAPIKey } from '~/lib/.server/llm/api-key';
import { getAnthropicModel } from '~/lib/.server/llm/model';
import { MAX_TOKENS } from './constants';
import { getSystemPrompt } from './prompts';
@ -23,7 +22,7 @@ export type StreamingOptions = Omit<Parameters<typeof _streamText>[0], 'model'>;
export function streamText(messages: Messages, env: Env, options?: StreamingOptions) {
return _streamText({
model: getAnthropicModel(getAPIKey(env)),
model: getAnthropicModel(env),
system: getSystemPrompt(),
maxTokens: MAX_TOKENS,
headers: {

View File

@ -1,3 +1,5 @@
interface Env {
ANTHROPIC_API_KEY: string;
ANTHROPIC_BASE_URL: string;
ANTHROPIC_MODEL: string;
}