File size: 1,874 Bytes
e201fa0
cd320c7
4d3f40f
 
cd320c7
 
4d3f40f
e201fa0
cd320c7
 
e201fa0
cd320c7
 
 
 
 
 
 
 
 
 
4d3f40f
 
 
e201fa0
cd320c7
 
e201fa0
cd320c7
e201fa0
cd320c7
 
 
e201fa0
cd320c7
 
 
 
 
4d3f40f
e201fa0
 
 
 
 
 
 
 
 
 
 
 
 
cd320c7
 
 
 
 
e201fa0
cd320c7
4d3f40f
 
e201fa0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModel
import torch
from typing import List, Dict
import uvicorn

# 定义响应模型
class EmbeddingResponse(BaseModel):
    status: str
    embeddings: List[Listfloat]]

# 创建FastAPI应用
app = FastAPI(
    title="Jina Embeddings API",
    description="Text embedding generation service using jina-embeddings-v3",
    version="1.0.0"
)

# 加载模型和分词器
model_name = "jinaai/jina-embeddings-v3"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModel.from_pretrained(model_name, trust_remote_code=True)

async def generate_embeddings(text: str):
    try:
        # 使用分词器处理输入文本
        inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
        
 #生成嵌入
        with torch.no_grad():
            embeddings = model(**inputs).last_hidden_state.mean(dim=1)
        
 return EmbeddingResponse(
            status="success",
            embeddings=embeddings.numpy().tolist()
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/api/v1/embeddings")
@app.post("/hf/v1/embeddings")
async def embedding(request: Request):
    try:
        data = await request.json()
        text = data.get('input', '')
        if not text:
            raise HTTPException(status_code=400, detail="Input text is missing")
        
 return await generate_embeddings(text)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/")
async def root():
    return {
        "status": "active",
        "model": model_name,
        "usage": "Send POST request to /api/v1/embeddings"
    }

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)