1
1
import { FastifyPluginAsync } from "fastify" ;
2
- import { object , z } from "zod" ;
2
+ import { z } from "zod" ;
3
3
import { ZodTypeProvider } from "fastify-type-provider-zod" ;
4
- import { genAI } from "src/genAI" ;
4
+ import { gen_ai_hub } from "src/genAI" ;
5
5
import { EmbedContentRequest } from "@google/generative-ai" ;
6
6
7
7
// 定义请求体的 Zod Schema
@@ -15,6 +15,10 @@ const EMBEDDINGS_SCHEMA = z.object({
15
15
16
16
function build_embedding_payload ( body : z . infer < typeof EMBEDDINGS_SCHEMA > ) {
17
17
const { input, model, encoding_format, dimensions, user } = body ;
18
+ const genAI = gen_ai_hub . random ( ) ;
19
+ if ( ! genAI ) {
20
+ throw new Error ( "no genai available" ) ;
21
+ }
18
22
const gen_model = genAI . getGenerativeModel ( {
19
23
model : model ,
20
24
} ) ;
@@ -42,8 +46,8 @@ const embeddingsRoute: FastifyPluginAsync = async (app) => {
42
46
url : "/v1/embeddings" ,
43
47
schema : { body : EMBEDDINGS_SCHEMA } ,
44
48
handler : async ( req , res ) => {
45
- const { model } = req . body ;
46
- const payload = build_embedding_payload ( req . body ) ;
49
+ const { model } = req . body as any ;
50
+ const payload = build_embedding_payload ( req . body as any ) ;
47
51
48
52
const resp = await payload . gen_model . batchEmbedContents ( {
49
53
requests : payload . requests ,
0 commit comments