|
| 1 | +import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai'; |
| 2 | +import { getAimlApiKey } from '../config'; |
| 3 | +import { ChatModel, EmbeddingModel } from '.'; |
| 4 | +import { BaseChatModel } from '@langchain/core/language_models/chat_models'; |
| 5 | +import { Embeddings } from '@langchain/core/embeddings'; |
| 6 | +import axios from 'axios'; |
| 7 | + |
| 8 | +export const PROVIDER_INFO = { |
| 9 | + key: 'aimlapi', |
| 10 | + displayName: 'AI/ML API', |
| 11 | +}; |
| 12 | + |
| 13 | +interface AimlApiModel { |
| 14 | + id: string; |
| 15 | + name?: string; |
| 16 | + type?: string; |
| 17 | +} |
| 18 | + |
| 19 | +const API_URL = 'https://api.aimlapi.com'; |
| 20 | + |
| 21 | +export const loadAimlApiChatModels = async () => { |
| 22 | + const apiKey = getAimlApiKey(); |
| 23 | + |
| 24 | + if (!apiKey) return {}; |
| 25 | + |
| 26 | + try { |
| 27 | + const response = await axios.get(`${API_URL}/models`, { |
| 28 | + headers: { |
| 29 | + 'Content-Type': 'application/json', |
| 30 | + Authorization: `Bearer ${apiKey}`, |
| 31 | + }, |
| 32 | + }); |
| 33 | + |
| 34 | + const chatModels: Record<string, ChatModel> = {}; |
| 35 | + |
| 36 | + response.data.data.forEach((model: AimlApiModel) => { |
| 37 | + if (model.type === 'chat-completion') { |
| 38 | + chatModels[model.id] = { |
| 39 | + displayName: model.name || model.id, |
| 40 | + model: new ChatOpenAI({ |
| 41 | + openAIApiKey: apiKey, |
| 42 | + modelName: model.id, |
| 43 | + temperature: 0.7, |
| 44 | + configuration: { |
| 45 | + baseURL: API_URL, |
| 46 | + }, |
| 47 | + }) as unknown as BaseChatModel, |
| 48 | + }; |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + return chatModels; |
| 53 | + } catch (err) { |
| 54 | + console.error(`Error loading AI/ML API models: ${err}`); |
| 55 | + return {}; |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +export const loadAimlApiEmbeddingModels = async () => { |
| 60 | + const apiKey = getAimlApiKey(); |
| 61 | + |
| 62 | + if (!apiKey) return {}; |
| 63 | + |
| 64 | + try { |
| 65 | + const response = await axios.get(`${API_URL}/models`, { |
| 66 | + headers: { |
| 67 | + 'Content-Type': 'application/json', |
| 68 | + Authorization: `Bearer ${apiKey}`, |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + const embeddingModels: Record<string, EmbeddingModel> = {}; |
| 73 | + |
| 74 | + response.data.data.forEach((model: AimlApiModel) => { |
| 75 | + if (model.type === 'embedding') { |
| 76 | + embeddingModels[model.id] = { |
| 77 | + displayName: model.name || model.id, |
| 78 | + model: new OpenAIEmbeddings({ |
| 79 | + openAIApiKey: apiKey, |
| 80 | + modelName: model.id, |
| 81 | + configuration: { |
| 82 | + baseURL: API_URL, |
| 83 | + }, |
| 84 | + }) as unknown as Embeddings, |
| 85 | + }; |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + return embeddingModels; |
| 90 | + } catch (err) { |
| 91 | + console.error(`Error loading AI/ML API embeddings models: ${err}`); |
| 92 | + return {}; |
| 93 | + } |
| 94 | +}; |
0 commit comments