52 lines
1.9 KiB
TypeScript
52 lines
1.9 KiB
TypeScript
|
|
import * as plugins from './plugins.js';
|
||
|
|
import type { ISmartAiOptions, LanguageModelV3 } from './smartai.interfaces.js';
|
||
|
|
import { createOllamaModel } from './smartai.provider.ollama.js';
|
||
|
|
import { createAnthropicCachingMiddleware } from './smartai.middleware.anthropic.js';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Returns a LanguageModelV3 for the given provider and model.
|
||
|
|
* This is the primary API — consumers use the returned model with AI SDK's
|
||
|
|
* generateText(), streamText(), etc.
|
||
|
|
*/
|
||
|
|
export function getModel(options: ISmartAiOptions): LanguageModelV3 {
|
||
|
|
switch (options.provider) {
|
||
|
|
case 'anthropic': {
|
||
|
|
const p = plugins.createAnthropic({ apiKey: options.apiKey });
|
||
|
|
const base = p(options.model) as LanguageModelV3;
|
||
|
|
if (options.promptCaching === false) return base;
|
||
|
|
return plugins.wrapLanguageModel({
|
||
|
|
model: base,
|
||
|
|
middleware: createAnthropicCachingMiddleware(),
|
||
|
|
}) as unknown as LanguageModelV3;
|
||
|
|
}
|
||
|
|
case 'openai': {
|
||
|
|
const p = plugins.createOpenAI({ apiKey: options.apiKey });
|
||
|
|
return p(options.model) as LanguageModelV3;
|
||
|
|
}
|
||
|
|
case 'google': {
|
||
|
|
const p = plugins.createGoogleGenerativeAI({ apiKey: options.apiKey });
|
||
|
|
return p(options.model) as LanguageModelV3;
|
||
|
|
}
|
||
|
|
case 'groq': {
|
||
|
|
const p = plugins.createGroq({ apiKey: options.apiKey });
|
||
|
|
return p(options.model) as LanguageModelV3;
|
||
|
|
}
|
||
|
|
case 'mistral': {
|
||
|
|
const p = plugins.createMistral({ apiKey: options.apiKey });
|
||
|
|
return p(options.model) as LanguageModelV3;
|
||
|
|
}
|
||
|
|
case 'xai': {
|
||
|
|
const p = plugins.createXai({ apiKey: options.apiKey });
|
||
|
|
return p(options.model) as LanguageModelV3;
|
||
|
|
}
|
||
|
|
case 'perplexity': {
|
||
|
|
const p = plugins.createPerplexity({ apiKey: options.apiKey });
|
||
|
|
return p(options.model) as LanguageModelV3;
|
||
|
|
}
|
||
|
|
case 'ollama':
|
||
|
|
return createOllamaModel(options);
|
||
|
|
default:
|
||
|
|
throw new Error(`Unknown provider: ${(options as ISmartAiOptions).provider}`);
|
||
|
|
}
|
||
|
|
}
|