refactor: streamline text and title generation model creation by consolidating provider logic
This commit is contained in:
@@ -1,8 +1,5 @@
|
|||||||
import { generateText } from 'ai';
|
import { generateText } from 'ai';
|
||||||
import { AIProvider, type AIProviderConfig } from '@cio/ai-assistant';
|
import { AIProvider, type AIProviderConfig, createModel } from '@cio/ai-assistant';
|
||||||
import { createOpenAI } from '@ai-sdk/openai';
|
|
||||||
import { createAnthropic } from '@ai-sdk/anthropic';
|
|
||||||
import { createGoogleGenerativeAI } from '@ai-sdk/google';
|
|
||||||
|
|
||||||
const TEXT_GEN_MODELS: Record<AIProvider, string> = {
|
const TEXT_GEN_MODELS: Record<AIProvider, string> = {
|
||||||
[AIProvider.OPENAI]: 'gpt-4o-mini',
|
[AIProvider.OPENAI]: 'gpt-4o-mini',
|
||||||
@@ -11,31 +8,6 @@ const TEXT_GEN_MODELS: Record<AIProvider, string> = {
|
|||||||
[AIProvider.MOONSHOT]: 'kimi-k2.6'
|
[AIProvider.MOONSHOT]: 'kimi-k2.6'
|
||||||
};
|
};
|
||||||
|
|
||||||
function createTextGenModel(config: AIProviderConfig) {
|
|
||||||
const modelName = TEXT_GEN_MODELS[config.provider];
|
|
||||||
|
|
||||||
switch (config.provider) {
|
|
||||||
case AIProvider.OPENAI: {
|
|
||||||
const openai = createOpenAI({ apiKey: config.apiKey });
|
|
||||||
return openai(modelName);
|
|
||||||
}
|
|
||||||
case AIProvider.ANTHROPIC: {
|
|
||||||
const anthropic = createAnthropic({ apiKey: config.apiKey });
|
|
||||||
return anthropic(modelName);
|
|
||||||
}
|
|
||||||
case AIProvider.GOOGLE: {
|
|
||||||
const google = createGoogleGenerativeAI({ apiKey: config.apiKey });
|
|
||||||
return google(modelName);
|
|
||||||
}
|
|
||||||
case AIProvider.MOONSHOT: {
|
|
||||||
const moonshot = createOpenAI({ apiKey: config.apiKey, baseURL: 'https://api.moonshot.cn/v1' });
|
|
||||||
return moonshot(modelName);
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
throw new Error(`Unsupported AI provider: ${config.provider}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface GenerateFieldTextResult {
|
export interface GenerateFieldTextResult {
|
||||||
text: string;
|
text: string;
|
||||||
usage: { promptTokens: number; completionTokens: number; totalTokens: number };
|
usage: { promptTokens: number; completionTokens: number; totalTokens: number };
|
||||||
@@ -49,8 +21,8 @@ export async function generateFieldText(
|
|||||||
context: string | undefined,
|
context: string | undefined,
|
||||||
providerConfig: AIProviderConfig
|
providerConfig: AIProviderConfig
|
||||||
): Promise<GenerateFieldTextResult> {
|
): Promise<GenerateFieldTextResult> {
|
||||||
const model = createTextGenModel(providerConfig);
|
|
||||||
const modelName = TEXT_GEN_MODELS[providerConfig.provider];
|
const modelName = TEXT_GEN_MODELS[providerConfig.provider];
|
||||||
|
const model = createModel({ ...providerConfig, model: modelName });
|
||||||
|
|
||||||
const contextLine = context ? `Context: ${context}.` : '';
|
const contextLine = context ? `Context: ${context}.` : '';
|
||||||
const formatLine =
|
const formatLine =
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
import { generateText, generateObject } from 'ai';
|
import { generateText, generateObject } from 'ai';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { AIProvider, type AIProviderConfig } from '@cio/ai-assistant';
|
import { AIProvider, type AIProviderConfig, createModel } from '@cio/ai-assistant';
|
||||||
import { createOpenAI } from '@ai-sdk/openai';
|
|
||||||
import { createAnthropic } from '@ai-sdk/anthropic';
|
|
||||||
import { createGoogleGenerativeAI } from '@ai-sdk/google';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cheap models used specifically for title generation to minimize token cost.
|
* Cheap models used specifically for title generation to minimize token cost.
|
||||||
@@ -16,28 +13,7 @@ const TITLE_MODELS: Record<AIProvider, string> = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function createTitleModel(config: AIProviderConfig) {
|
function createTitleModel(config: AIProviderConfig) {
|
||||||
const modelName = TITLE_MODELS[config.provider];
|
return createModel({ ...config, model: TITLE_MODELS[config.provider] });
|
||||||
|
|
||||||
switch (config.provider) {
|
|
||||||
case AIProvider.OPENAI: {
|
|
||||||
const openai = createOpenAI({ apiKey: config.apiKey });
|
|
||||||
return openai(modelName);
|
|
||||||
}
|
|
||||||
case AIProvider.ANTHROPIC: {
|
|
||||||
const anthropic = createAnthropic({ apiKey: config.apiKey });
|
|
||||||
return anthropic(modelName);
|
|
||||||
}
|
|
||||||
case AIProvider.GOOGLE: {
|
|
||||||
const google = createGoogleGenerativeAI({ apiKey: config.apiKey });
|
|
||||||
return google(modelName);
|
|
||||||
}
|
|
||||||
case AIProvider.MOONSHOT: {
|
|
||||||
const moonshot = createOpenAI({ apiKey: config.apiKey, baseURL: 'https://api.moonshot.cn/v1' });
|
|
||||||
return moonshot(modelName);
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
throw new Error(`Unsupported AI provider: ${config.provider}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user