mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 13:58:19 +00:00
refactor(ai): remove generic providers and hardcode gemini-embedding-2 in embedding activity
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
import { AiProviderSettings } from '@/dtos/ai'
|
||||
import { Provider, Usage } from './provider'
|
||||
|
||||
export class ElevenLabsProvider implements Provider {
|
||||
private config: AiProviderSettings
|
||||
|
||||
constructor(config: AiProviderSettings) {
|
||||
this.config = config
|
||||
}
|
||||
|
||||
async chat(
|
||||
model: string,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
_prompt: string,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
_images: string[],
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
|
||||
_schema?: any,
|
||||
): Promise<{ text: string; usage: Usage }> {
|
||||
// TODO: Implement ElevenLabs chat
|
||||
return {
|
||||
text: 'elevenlabs response',
|
||||
usage: { model, inputTokens: 0, outputTokens: 0 },
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async generateImageEmbedding(_model: string, _imageBuffer: Buffer): Promise<number[]> {
|
||||
throw new Error('Not implemented')
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async generateVideoEmbedding(_model: string, _videoBuffer: Buffer): Promise<number[]> {
|
||||
throw new Error('Not implemented')
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async generateTextEmbedding(_model: string, _text: string): Promise<number[]> {
|
||||
throw new Error('Not implemented')
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
import { GoogleGenAI } from '@google/genai'
|
||||
import { AiProviderSettings } from '@/dtos/ai'
|
||||
import { Provider, Usage } from './provider'
|
||||
|
||||
export class GeminiProvider implements Provider {
|
||||
private config: AiProviderSettings
|
||||
private client: GoogleGenAI
|
||||
|
||||
constructor(config: AiProviderSettings) {
|
||||
this.config = config
|
||||
this.client = new GoogleGenAI({
|
||||
apiKey: config.apiKey,
|
||||
})
|
||||
}
|
||||
|
||||
private async downloadImage(url: string): Promise<{ data: Buffer; contentType: string }> {
|
||||
const resp = await fetch(url)
|
||||
if (!resp.ok) {
|
||||
throw new Error(`Failed to download image, status: ${resp.status}`)
|
||||
}
|
||||
const arrayBuffer = await resp.arrayBuffer()
|
||||
const contentType = resp.headers.get('content-type') || 'image/jpeg'
|
||||
return { data: Buffer.from(arrayBuffer), contentType }
|
||||
}
|
||||
|
||||
async chat(
|
||||
model: string,
|
||||
prompt: string,
|
||||
images: string[],
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
schema?: any,
|
||||
): Promise<{ text: string; usage: Usage }> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const parts: any[] = []
|
||||
|
||||
for (const imgUrl of images) {
|
||||
const { data, contentType } = await this.downloadImage(imgUrl)
|
||||
parts.push({
|
||||
inlineData: {
|
||||
data: data.toString('base64'),
|
||||
mimeType: contentType,
|
||||
},
|
||||
})
|
||||
}
|
||||
parts.push({ text: prompt })
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const config: any = {}
|
||||
|
||||
if (schema) {
|
||||
config.responseMimeType = 'application/json'
|
||||
config.responseSchema = schema
|
||||
}
|
||||
|
||||
const result = await this.client.models.generateContent({
|
||||
model,
|
||||
contents: [{ role: 'user', parts }],
|
||||
config,
|
||||
})
|
||||
|
||||
const usage: Usage = { model, inputTokens: 0, outputTokens: 0 }
|
||||
if (result.usageMetadata) {
|
||||
usage.inputTokens = result.usageMetadata.promptTokenCount || 0
|
||||
usage.outputTokens = result.usageMetadata.candidatesTokenCount || 0
|
||||
}
|
||||
|
||||
return { text: result.text || '', usage }
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async generateImageEmbedding(_model: string, _imageBuffer: Buffer): Promise<number[]> {
|
||||
throw new Error('Not implemented')
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async generateVideoEmbedding(_model: string, _videoBuffer: Buffer): Promise<number[]> {
|
||||
throw new Error('Not implemented')
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async generateTextEmbedding(_model: string, _text: string): Promise<number[]> {
|
||||
throw new Error('Not implemented')
|
||||
}
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
import OpenAI from 'openai'
|
||||
|
||||
import { AiProviderSettings } from '@/dtos/ai'
|
||||
import { Provider, Usage } from './provider'
|
||||
|
||||
export class OpenAiProvider implements Provider {
|
||||
private config: AiProviderSettings
|
||||
private client: OpenAI
|
||||
|
||||
constructor(config: AiProviderSettings) {
|
||||
this.config = config
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const opts: any = {
|
||||
apiKey: config.apiKey,
|
||||
}
|
||||
if (config.baseUrl) {
|
||||
opts.baseUrl = config.baseUrl
|
||||
}
|
||||
this.client = new OpenAI(opts)
|
||||
}
|
||||
|
||||
async chat(
|
||||
model: string,
|
||||
prompt: string,
|
||||
images: string[],
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
schema?: any,
|
||||
): Promise<{ text: string; usage: Usage }> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const messages: any[] = []
|
||||
|
||||
if (images.length > 0) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const contentParts: any[] = [{ type: 'text', text: prompt }]
|
||||
for (const imgUrl of images) {
|
||||
contentParts.push({
|
||||
type: 'imageUrl',
|
||||
imageUrl: { url: imgUrl },
|
||||
})
|
||||
}
|
||||
messages.push({ role: 'user', content: contentParts })
|
||||
} else {
|
||||
messages.push({ role: 'user', content: prompt })
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const params: any = {
|
||||
model,
|
||||
messages,
|
||||
}
|
||||
|
||||
if (schema) {
|
||||
params.response_format = {
|
||||
type: 'jsonSchema',
|
||||
jsonSchema: {
|
||||
name: 'response',
|
||||
description: 'Structured response',
|
||||
schema: schema,
|
||||
strict: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const chat = await this.client.chat.completions.create(params)
|
||||
|
||||
if (chat.choices.length === 0) {
|
||||
throw new Error('No choices returned from openai')
|
||||
}
|
||||
|
||||
const usage: Usage = {
|
||||
model,
|
||||
inputTokens: chat.usage?.prompt_tokens || 0,
|
||||
outputTokens: chat.usage?.completion_tokens || 0,
|
||||
}
|
||||
|
||||
return { text: chat.choices[0].message.content || '', usage }
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async generateImageEmbedding(_model: string, _imageBuffer: Buffer): Promise<number[]> {
|
||||
throw new Error('Not implemented')
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async generateVideoEmbedding(_model: string, _videoBuffer: Buffer): Promise<number[]> {
|
||||
throw new Error('Not implemented')
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async generateTextEmbedding(_model: string, _text: string): Promise<number[]> {
|
||||
throw new Error('Not implemented')
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
export interface Usage {
|
||||
inputTokens: number
|
||||
outputTokens: number
|
||||
model: string
|
||||
}
|
||||
|
||||
export interface Provider {
|
||||
chat(
|
||||
model: string,
|
||||
prompt: string,
|
||||
images: string[],
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
schema?: any,
|
||||
): Promise<{ text: string; usage: Usage }>
|
||||
|
||||
generateImageEmbedding(model: string, imageBuffer: Buffer): Promise<number[]>
|
||||
|
||||
generateVideoEmbedding(model: string, videoBuffer: Buffer): Promise<number[]>
|
||||
|
||||
generateTextEmbedding(model: string, text: string): Promise<number[]>
|
||||
}
|
||||
Reference in New Issue
Block a user