Update main.py
Browse files
main.py
CHANGED
|
@@ -6,39 +6,48 @@ from sentence_transformers import SentenceTransformer
|
|
| 6 |
os.environ["OMP_NUM_THREADS"] = "2"
|
| 7 |
os.environ["MKL_NUM_THREADS"] = "2"
|
| 8 |
|
| 9 |
-
app = FastAPI(title="
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
MODEL_ID = "Snowflake/snowflake-arctic-embed-l"
|
| 13 |
-
model = SentenceTransformer(MODEL_ID, device="cpu")
|
| 14 |
|
| 15 |
@app.get("/health")
|
| 16 |
def health_check():
|
| 17 |
return {"status": "healthy"}
|
| 18 |
|
| 19 |
-
@app.post("/")
|
| 20 |
-
|
| 21 |
-
async def generate_embeddings(request: Request):
|
| 22 |
"""
|
| 23 |
-
Mimics the official
|
| 24 |
"""
|
| 25 |
try:
|
| 26 |
data = await request.json()
|
| 27 |
except Exception:
|
| 28 |
-
raise HTTPException(status_code=400, detail="Invalid JSON
|
| 29 |
-
|
| 30 |
-
# n8n's Hugging Face Embeddings node sends data under the 'inputs' key
|
| 31 |
-
inputs = data.get("inputs")
|
| 32 |
|
|
|
|
|
|
|
| 33 |
if not inputs:
|
| 34 |
-
raise HTTPException(status_code=400, detail="Missing '
|
| 35 |
-
|
| 36 |
-
# Ensure inputs is a list
|
| 37 |
if isinstance(inputs, str):
|
| 38 |
inputs = [inputs]
|
| 39 |
-
|
| 40 |
-
# Generate
|
| 41 |
-
embeddings = model.encode(inputs, normalize_embeddings=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
os.environ["OMP_NUM_THREADS"] = "2"
|
| 7 |
os.environ["MKL_NUM_THREADS"] = "2"
|
| 8 |
|
| 9 |
+
app = FastAPI(title="OpenAI Compatible API")
|
| 10 |
+
# Loads the production-grade text embedding model
|
| 11 |
+
model = SentenceTransformer("Snowflake/snowflake-arctic-embed-l", device="cpu")
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@app.get("/health")
|
| 14 |
def health_check():
|
| 15 |
return {"status": "healthy"}
|
| 16 |
|
| 17 |
+
@app.post("/v1/embeddings")
|
| 18 |
+
async def create_embeddings(request: Request):
|
|
|
|
| 19 |
"""
|
| 20 |
+
Mimics the official OpenAI Embeddings API structure.
|
| 21 |
"""
|
| 22 |
try:
|
| 23 |
data = await request.json()
|
| 24 |
except Exception:
|
| 25 |
+
raise HTTPException(status_code=400, detail="Invalid JSON format")
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# OpenAI sends the target text inside the "input" parameter
|
| 28 |
+
inputs = data.get("input")
|
| 29 |
if not inputs:
|
| 30 |
+
raise HTTPException(status_code=400, detail="Missing 'input' field")
|
| 31 |
+
|
|
|
|
| 32 |
if isinstance(inputs, str):
|
| 33 |
inputs = [inputs]
|
| 34 |
+
|
| 35 |
+
# Generate vectors via CPU
|
| 36 |
+
embeddings = model.encode(inputs, normalize_embeddings=True).tolist()
|
| 37 |
+
|
| 38 |
+
# Format the array exactly how n8n's OpenAI LangChain node expects it
|
| 39 |
+
response_data = [
|
| 40 |
+
{
|
| 41 |
+
"object": "embedding",
|
| 42 |
+
"embedding": emb,
|
| 43 |
+
"index": i
|
| 44 |
+
}
|
| 45 |
+
for i, emb in enumerate(embeddings)
|
| 46 |
+
]
|
| 47 |
|
| 48 |
+
return {
|
| 49 |
+
"object": "list",
|
| 50 |
+
"data": response_data,
|
| 51 |
+
"model": "snowflake-arctic-embed-l",
|
| 52 |
+
"usage": {"prompt_tokens": 0, "total_tokens": 0}
|
| 53 |
+
}
|