mirror of
https://github.com/stackblitz/bolt.new
synced 2025-06-26 18:17:50 +00:00
feat(): support base url and model custom
This commit is contained in:
parent
ffa9f11360
commit
ecfa261028
@ -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';
|
||||
}
|
||||
|
||||
@ -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: {
|
||||
|
||||
2
worker-configuration.d.ts
vendored
2
worker-configuration.d.ts
vendored
@ -1,3 +1,5 @@
|
||||
interface Env {
|
||||
ANTHROPIC_API_KEY: string;
|
||||
ANTHROPIC_BASE_URL: string;
|
||||
ANTHROPIC_MODEL: string;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user