mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 15:14:10 +00:00
feat: configure embedding servers and transcript agents
This commit is contained in:
@@ -4,19 +4,26 @@ import { LruTtlCache } from '@shumai/core/src/cache/lru-ttl-cache'
|
||||
import { logger } from '@shumai/core/src/logger'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import sharp from 'sharp'
|
||||
import { getGemmaEmbeddingEndpoint } from './gemma-embedding-endpoint'
|
||||
import { embeddingServerService, LOCAL_EMBEDDING_PROVIDER } from './embedding-server'
|
||||
|
||||
export function getImageEmbeddingDimensions(): 768 | 1536 {
|
||||
return process.env.EMBEDDING_PROVIDER === 'gemma-embedding-2-openai' ? 768 : 1536
|
||||
export function getImageEmbeddingDimensions(provider: string): 768 | 1536 {
|
||||
return provider === LOCAL_EMBEDDING_PROVIDER ? 768 : 1536
|
||||
}
|
||||
|
||||
export class ImageEmbeddingService {
|
||||
private readonly cache = new LruTtlCache<string, number[]>(100)
|
||||
|
||||
async generateEmbedding(image: string, teamId: string): Promise<number[]> {
|
||||
const dimensions = getImageEmbeddingDimensions()
|
||||
const { provider, embeddingEndpoint } =
|
||||
await embeddingServerService.resolveRuntimeConfig(teamId)
|
||||
const dimensions = getImageEmbeddingDimensions(provider)
|
||||
const digest = createHash('sha256').update(image).digest('hex')
|
||||
const cacheKey = `${teamId}:${dimensions}:${digest}`
|
||||
const cacheKey = JSON.stringify([
|
||||
teamId,
|
||||
dimensions,
|
||||
dimensions === 768 ? embeddingEndpoint : null,
|
||||
digest,
|
||||
])
|
||||
const cached = this.cache.get(cacheKey)
|
||||
if (cached) return cached
|
||||
|
||||
@@ -37,7 +44,7 @@ export class ImageEmbeddingService {
|
||||
const signal = AbortSignal.timeout(15000)
|
||||
embedding =
|
||||
dimensions === 768
|
||||
? await this.embedWithGemma(image, signal)
|
||||
? await this.embedWithGemma(embeddingEndpoint, image, signal)
|
||||
: await this.embedWithGemini(image, signal)
|
||||
if (
|
||||
!embedding ||
|
||||
@@ -73,8 +80,8 @@ export class ImageEmbeddingService {
|
||||
return response.embeddings?.[0]?.values
|
||||
}
|
||||
|
||||
private async embedWithGemma(image: string, signal: AbortSignal) {
|
||||
const response = await fetch(getGemmaEmbeddingEndpoint(), {
|
||||
private async embedWithGemma(endpoint: string, image: string, signal: AbortSignal) {
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
|
||||
Reference in New Issue
Block a user