Added Cloudflare llm provider

This commit is contained in:
Tim Millwood 2025-02-18 11:02:55 +00:00
parent 0e2e1834e7
commit 9467330389
3 changed files with 60 additions and 2 deletions

View File

@ -0,0 +1,55 @@
import { BaseProvider } from '~/lib/modules/llm/base-provider';
import type { ModelInfo } from '~/lib/modules/llm/types';
import type { IProviderSetting } from '~/types/model';
import type { LanguageModelV1 } from 'ai';
import { createOpenAI } from '@ai-sdk/openai';
export default class CloudflareProvider extends BaseProvider {
name = 'Cloudflare';
getApiKeyLink = 'https://dash.cloudflare.com/profile/api-tokens';
config = {
apiTokenKey: 'CLOUDFLARE_API_TOKEN',
accountIdKey: 'CLOUDFLARE_ACCOUNT_ID',
};
staticModels: ModelInfo[] = [
{ name: '@cf/meta/llama-2-7b-chat-int8', label: 'Llama-2-7b-chat-int8', provider: 'Cloudflare', maxTokenAllowed: 4096 },
{ name: '@cf/meta/llama-2-7b-chat-fp16', label: 'Llama-2-7b-chat-fp16', provider: 'Cloudflare', maxTokenAllowed: 4096 },
{ name: '@cf/mistral/mistral-7b-instruct-v0.1', label: 'Mistral-7b-instruct', provider: 'Cloudflare', maxTokenAllowed: 4096 },
];
getModelInstance(options: {
model: string;
serverEnv: Env;
apiKeys?: Record<string, string>;
providerSettings?: Record<string, IProviderSetting>;
}): LanguageModelV1 {
const { model, serverEnv, apiKeys, providerSettings } = options;
const { apiKey } = this.getProviderBaseUrlAndKey({
apiKeys,
providerSettings: providerSettings?.[this.name],
serverEnv: serverEnv as any,
defaultBaseUrlKey: '',
defaultApiTokenKey: 'CLOUDFLARE_API_TOKEN',
});
const accountId = serverEnv?.CLOUDFLARE_ACCOUNT_ID || process?.env?.CLOUDFLARE_ACCOUNT_ID;
if (!apiKey) {
throw new Error(`Missing API token for ${this.name} provider`);
}
if (!accountId) {
throw new Error(`Missing Account ID for ${this.name} provider`);
}
const openai = createOpenAI({
baseURL: `https://api.cloudflare.com/client/v4/accounts/${accountId}/ai/v1`,
apiKey,
});
return openai(model);
}
}

View File

@ -1,5 +1,6 @@
import AnthropicProvider from './providers/anthropic'; import AnthropicProvider from './providers/anthropic';
import CohereProvider from './providers/cohere'; import CohereProvider from './providers/cohere';
import CloudflareProvider from './providers/cloudflare';
import DeepseekProvider from './providers/deepseek'; import DeepseekProvider from './providers/deepseek';
import GoogleProvider from './providers/google'; import GoogleProvider from './providers/google';
import GroqProvider from './providers/groq'; import GroqProvider from './providers/groq';
@ -19,6 +20,7 @@ import GithubProvider from './providers/github';
export { export {
AnthropicProvider, AnthropicProvider,
CloudflareProvider,
CohereProvider, CohereProvider,
DeepseekProvider, DeepseekProvider,
GoogleProvider, GoogleProvider,

View File

@ -1,5 +1,4 @@
interface Env { interface Env {
RUNNING_IN_DOCKER: Settings;
DEFAULT_NUM_CTX:Settings; DEFAULT_NUM_CTX:Settings;
ANTHROPIC_API_KEY: string; ANTHROPIC_API_KEY: string;
OPENAI_API_KEY: string; OPENAI_API_KEY: string;
@ -18,4 +17,6 @@ interface Env {
XAI_API_KEY: string; XAI_API_KEY: string;
PERPLEXITY_API_KEY: string; PERPLEXITY_API_KEY: string;
AWS_BEDROCK_CONFIG: string; AWS_BEDROCK_CONFIG: string;
CLOUDFLARE_API_KEY: string;
CLOUDFLARE_ACCOUNT_ID: string;
} }