mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-06-26 18:26:38 +00:00
Merge a6018d67c8
into d0d9818964
This commit is contained in:
commit
bdaf05fa30
95
app/lib/modules/llm/providers/cloudflare.ts
Normal file
95
app/lib/modules/llm/providers/cloudflare.ts
Normal file
@ -0,0 +1,95 @@
|
||||
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[] = [];
|
||||
|
||||
async getDynamicModels(
|
||||
apiKeys?: Record<string, string>,
|
||||
serverEnv: Record<string, string> = {},
|
||||
): Promise<ModelInfo[]> {
|
||||
const { apiKey } = this.getProviderBaseUrlAndKey({
|
||||
apiKeys,
|
||||
serverEnv: serverEnv as any,
|
||||
defaultBaseUrlKey: '',
|
||||
defaultApiTokenKey: 'CLOUDFLARE_API_TOKEN',
|
||||
});
|
||||
|
||||
const accountId = serverEnv?.CLOUDFLARE_ACCOUNT_ID || process?.env?.CLOUDFLARE_ACCOUNT_ID;
|
||||
|
||||
if (!apiKey || !accountId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://api.cloudflare.com/client/v4/accounts/${accountId}/ai/models/search`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch models: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data = (await response.json()) as { result: Array<{ id: string }> };
|
||||
return data.result.map((model: any) => ({
|
||||
name: model.name,
|
||||
label: model.name,
|
||||
provider: this.name,
|
||||
maxTokenAllowed: 100000,
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error('Error fetching Cloudflare models:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import AnthropicProvider from './providers/anthropic';
|
||||
import CohereProvider from './providers/cohere';
|
||||
import CloudflareProvider from './providers/cloudflare';
|
||||
import DeepseekProvider from './providers/deepseek';
|
||||
import GoogleProvider from './providers/google';
|
||||
import GroqProvider from './providers/groq';
|
||||
@ -19,6 +20,7 @@ import GithubProvider from './providers/github';
|
||||
|
||||
export {
|
||||
AnthropicProvider,
|
||||
CloudflareProvider,
|
||||
CohereProvider,
|
||||
DeepseekProvider,
|
||||
GoogleProvider,
|
||||
|
2
worker-configuration.d.ts
vendored
2
worker-configuration.d.ts
vendored
@ -18,4 +18,6 @@ interface Env {
|
||||
XAI_API_KEY: string;
|
||||
PERPLEXITY_API_KEY: string;
|
||||
AWS_BEDROCK_CONFIG: string;
|
||||
CLOUDFLARE_API_KEY: string;
|
||||
CLOUDFLARE_ACCOUNT_ID: string;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user