-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapi-v3.py
53 lines (42 loc) · 1.51 KB
/
api-v3.py
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
from flask import Flask, request, jsonify
from vllm import VLLMModel
import torch
app = Flask(__name__)
# Initialize the model; make sure to load the correct configuration
model_name = "./model/bge-m3"
model = VLLMModel.from_pretrained(model_name)
tokenizer = model.tokenizer
@app.route('/v1/embeddings', methods=['POST'])
def create_embeddings():
data = request.json
input_text = data.get('input')
model_id = data.get('model', model_name)
encoding_format = data.get('encoding_format', 'float')
# Check input validity
if not input_text or (isinstance(input_text, str) and not input_text.strip()) or (isinstance(input_text, list) and not all(input_text)):
return jsonify({"error": "Input text cannot be empty."}), 400
inputs = tokenizer(input_text, return_tensors='pt', truncation=True)
# Convert text to embeddings
with torch.no_grad():
embeddings = model(
**inputs).last_hidden_state.mean(dim=1).cpu().tolist()
# Construct response
response = {
"object": "list",
"data": [
{
"object": "embedding",
"embedding": embedding,
"index": i
}
for i, embedding in enumerate(embeddings)
],
"model": model_id,
"usage": {
"prompt_tokens": len(inputs['input_ids'][0]),
"total_tokens": len(inputs['input_ids'][0])
}
}
return jsonify(response)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)