|
| 1 | +import { compileSchema, SchemaNode } from 'json-schema-library'; |
| 2 | +import { ReadableStream } from 'web-streams-polyfill'; |
| 3 | +// @ts-expect-error |
| 4 | +globalThis.ReadableStream = ReadableStream; |
| 5 | +// if (typeof global.ReadableStream === undefined) { |
| 6 | +// global.ReadableStream = ReadableStream; |
| 7 | +// } |
| 8 | + |
| 9 | +import { ChatAnthropic } from '@langchain/anthropic'; |
| 10 | +// import { ChatWebLLM } from '@langchain/community/chat_models/webllm'; |
| 11 | +import { ChromeAI } from '@langchain/community/experimental/llms/chrome_ai'; |
| 12 | +import { ChatGoogleGenerativeAI } from '@langchain/google-genai'; |
| 13 | +import { ChatMistralAI } from '@langchain/mistralai'; |
| 14 | +import { ChatOllama } from '@langchain/ollama'; |
| 15 | +import { ChatOpenAI } from '@langchain/openai'; |
| 16 | + |
| 17 | +// Import Settings |
| 18 | +import AnthropicSettings from '../default-providers/Anthropic/settings-schema.json'; |
| 19 | +import ChromeAISettings from '../default-providers/ChromeAI/settings-schema.json'; |
| 20 | +import GeminiSettings from '../default-providers/Gemini/settings-schema.json'; |
| 21 | +import MistralAISettings from '../default-providers/MistralAI/settings-schema.json'; |
| 22 | +import OllamaAISettings from '../default-providers/Ollama/settings-schema.json'; |
| 23 | +import OpenAISettings from '../default-providers/OpenAI/settings-schema.json'; |
| 24 | +// import WebLLMSettings from '../default-providers/WebLLM/settings-schema.json'; |
| 25 | +import { IAIProvider, IType } from '../tokens'; |
| 26 | +import { BaseChatModel, BaseChatModelCallOptions } from '@langchain/core/language_models/chat_models'; |
| 27 | +import { AIMessageChunk } from '@langchain/core/messages'; |
| 28 | + |
| 29 | +interface IAIProviderWithChat extends IAIProvider { |
| 30 | + chat: IType<BaseChatModel<BaseChatModelCallOptions, AIMessageChunk>>; |
| 31 | +} |
| 32 | +const AIProviders: IAIProviderWithChat[] = [ |
| 33 | + { |
| 34 | + name: 'Anthropic', |
| 35 | + chat: ChatAnthropic, |
| 36 | + settingsSchema: AnthropicSettings, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: 'ChromeAI', |
| 40 | + // TODO: fix |
| 41 | + // @ts-expect-error: missing properties |
| 42 | + chat: ChromeAI, |
| 43 | + settingsSchema: ChromeAISettings, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: 'MistralAI', |
| 47 | + chat: ChatMistralAI, |
| 48 | + settingsSchema: MistralAISettings |
| 49 | + }, |
| 50 | + { |
| 51 | + name: 'Ollama', |
| 52 | + chat: ChatOllama, |
| 53 | + settingsSchema: OllamaAISettings |
| 54 | + }, |
| 55 | + { |
| 56 | + name: 'Gemini', |
| 57 | + chat: ChatGoogleGenerativeAI, |
| 58 | + settingsSchema: GeminiSettings |
| 59 | + }, |
| 60 | + { |
| 61 | + name: 'OpenAI', |
| 62 | + chat: ChatOpenAI, |
| 63 | + settingsSchema: OpenAISettings |
| 64 | + }, |
| 65 | + // { |
| 66 | + // name: 'WebLLM', |
| 67 | + // chat: ChatWebLLM, |
| 68 | + // settingsSchema: WebLLMSettings |
| 69 | + // } |
| 70 | +]; |
| 71 | + |
| 72 | +it('test provider settings', () => { |
| 73 | + AIProviders.forEach(provider => { |
| 74 | + console.log(`PROVIDER: ${provider.name}`); |
| 75 | + const schema: SchemaNode = compileSchema(provider.settingsSchema); |
| 76 | + const defaultSettings = schema.getData(undefined, { addOptionalProps: true }); |
| 77 | + |
| 78 | + // Set a value for apiKey to avoid errors at instantiation. |
| 79 | + if (defaultSettings.apiKey !== undefined) { |
| 80 | + defaultSettings.apiKey = 'abc'; |
| 81 | + } |
| 82 | + defaultSettings.bnla = 'ble'; |
| 83 | + const model = new provider.chat(defaultSettings); |
| 84 | + |
| 85 | + Object.entries(defaultSettings).forEach(([key, value]) => { |
| 86 | + try { |
| 87 | + // @ts-expect-error |
| 88 | + expect(JSON.stringify(model[key])).toEqual(JSON.stringify(value)); |
| 89 | + } catch (err) { |
| 90 | + // @ts-expect-error |
| 91 | + err.message = `${err.message}\nproperty: ${key}\n`; |
| 92 | + throw err; // throw the error so test fails as expected |
| 93 | + } |
| 94 | + |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments