mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-12-24 20:48:14 +00:00
feat(providers): add anthropic
This commit is contained in:
5
src/lib/models/providers/anthropic/anthropicLLM.ts
Normal file
5
src/lib/models/providers/anthropic/anthropicLLM.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import OpenAILLM from "../openai/openaiLLM";
|
||||||
|
|
||||||
|
class AnthropicLLM extends OpenAILLM {}
|
||||||
|
|
||||||
|
export default AnthropicLLM;
|
||||||
115
src/lib/models/providers/anthropic/index.ts
Normal file
115
src/lib/models/providers/anthropic/index.ts
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
import { UIConfigField } from '@/lib/config/types';
|
||||||
|
import { getConfiguredModelProviderById } from '@/lib/config/serverRegistry';
|
||||||
|
import { Model, ModelList, ProviderMetadata } from '../../types';
|
||||||
|
import BaseEmbedding from '../../base/embedding';
|
||||||
|
import BaseModelProvider from '../../base/provider';
|
||||||
|
import BaseLLM from '../../base/llm';
|
||||||
|
import AnthropicLLM from './anthropicLLM';
|
||||||
|
|
||||||
|
interface AnthropicConfig {
|
||||||
|
apiKey: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const providerConfigFields: UIConfigField[] = [
|
||||||
|
{
|
||||||
|
type: 'password',
|
||||||
|
name: 'API Key',
|
||||||
|
key: 'apiKey',
|
||||||
|
description: 'Your Anthropic API key',
|
||||||
|
required: true,
|
||||||
|
placeholder: 'Anthropic API Key',
|
||||||
|
env: 'ANTHROPIC_API_KEY',
|
||||||
|
scope: 'server',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
class AnthropicProvider extends BaseModelProvider<AnthropicConfig> {
|
||||||
|
constructor(id: string, name: string, config: AnthropicConfig) {
|
||||||
|
super(id, name, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getDefaultModels(): Promise<ModelList> {
|
||||||
|
const res = await fetch('https://api.anthropic.com/v1/models?limit=999', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'x-api-key': this.config.apiKey,
|
||||||
|
'anthropic-version': '2023-06-01',
|
||||||
|
'Content-type': 'application/json',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new Error(`Failed to fetch Anthropic models: ${res.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = (await res.json()).data;
|
||||||
|
|
||||||
|
const models: Model[] = data.map((m: any) => {
|
||||||
|
return {
|
||||||
|
key: m.id,
|
||||||
|
name: m.display_name,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
embedding: [],
|
||||||
|
chat: models,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async getModelList(): Promise<ModelList> {
|
||||||
|
const defaultModels = await this.getDefaultModels();
|
||||||
|
const configProvider = getConfiguredModelProviderById(this.id)!;
|
||||||
|
|
||||||
|
return {
|
||||||
|
embedding: [],
|
||||||
|
chat: [...defaultModels.chat, ...configProvider.chatModels],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async loadChatModel(key: string): Promise<BaseLLM<any>> {
|
||||||
|
const modelList = await this.getModelList();
|
||||||
|
|
||||||
|
const exists = modelList.chat.find((m) => m.key === key);
|
||||||
|
|
||||||
|
if (!exists) {
|
||||||
|
throw new Error(
|
||||||
|
'Error Loading Anthropic Chat Model. Invalid Model Selected',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new AnthropicLLM({
|
||||||
|
apiKey: this.config.apiKey,
|
||||||
|
model: key,
|
||||||
|
baseURL: 'https://api.anthropic.com/v1'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async loadEmbeddingModel(key: string): Promise<BaseEmbedding<any>> {
|
||||||
|
throw new Error('Anthropic provider does not support embedding models.');
|
||||||
|
}
|
||||||
|
|
||||||
|
static parseAndValidate(raw: any): AnthropicConfig {
|
||||||
|
if (!raw || typeof raw !== 'object')
|
||||||
|
throw new Error('Invalid config provided. Expected object');
|
||||||
|
if (!raw.apiKey)
|
||||||
|
throw new Error('Invalid config provided. API key must be provided');
|
||||||
|
|
||||||
|
return {
|
||||||
|
apiKey: String(raw.apiKey),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static getProviderConfigFields(): UIConfigField[] {
|
||||||
|
return providerConfigFields;
|
||||||
|
}
|
||||||
|
|
||||||
|
static getProviderMetadata(): ProviderMetadata {
|
||||||
|
return {
|
||||||
|
key: 'anthropic',
|
||||||
|
name: 'Anthropic',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default AnthropicProvider;
|
||||||
@@ -6,6 +6,7 @@ import GeminiProvider from './gemini';
|
|||||||
import TransformersProvider from './transformers';
|
import TransformersProvider from './transformers';
|
||||||
import GroqProvider from './groq';
|
import GroqProvider from './groq';
|
||||||
import LemonadeProvider from './lemonade';
|
import LemonadeProvider from './lemonade';
|
||||||
|
import AnthropicProvider from './anthropic';
|
||||||
|
|
||||||
export const providers: Record<string, ProviderConstructor<any>> = {
|
export const providers: Record<string, ProviderConstructor<any>> = {
|
||||||
openai: OpenAIProvider,
|
openai: OpenAIProvider,
|
||||||
@@ -13,7 +14,8 @@ export const providers: Record<string, ProviderConstructor<any>> = {
|
|||||||
gemini: GeminiProvider,
|
gemini: GeminiProvider,
|
||||||
transformers: TransformersProvider,
|
transformers: TransformersProvider,
|
||||||
groq: GroqProvider,
|
groq: GroqProvider,
|
||||||
lemonade: LemonadeProvider
|
lemonade: LemonadeProvider,
|
||||||
|
anthropic: AnthropicProvider
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getModelProvidersUIConfigSection =
|
export const getModelProvidersUIConfigSection =
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
||||||
import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai';
|
|
||||||
import { Embeddings } from '@langchain/core/embeddings';
|
|
||||||
import { UIConfigField } from '@/lib/config/types';
|
import { UIConfigField } from '@/lib/config/types';
|
||||||
import { getConfiguredModelProviderById } from '@/lib/config/serverRegistry';
|
import { getConfiguredModelProviderById } from '@/lib/config/serverRegistry';
|
||||||
import BaseModelProvider from '../../base/provider';
|
import BaseModelProvider from '../../base/provider';
|
||||||
|
|||||||
Reference in New Issue
Block a user