worsarise commited on
Commit
0179583
·
verified ·
1 Parent(s): da2d175

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +11 -12
main.py CHANGED
@@ -2,13 +2,14 @@ import os
2
  from fastapi import FastAPI, Request, HTTPException
3
  from sentence_transformers import SentenceTransformer
4
 
5
- # Optimize CPU threads for Hugging Face free tier
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():
@@ -16,26 +17,24 @@ def health_check():
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",
@@ -48,6 +47,6 @@ async def create_embeddings(request: Request):
48
  return {
49
  "object": "list",
50
  "data": response_data,
51
- "model": "snowflake-arctic-embed-l",
52
  "usage": {"prompt_tokens": 0, "total_tokens": 0}
53
  }
 
2
  from fastapi import FastAPI, Request, HTTPException
3
  from sentence_transformers import SentenceTransformer
4
 
 
5
  os.environ["OMP_NUM_THREADS"] = "2"
6
  os.environ["MKL_NUM_THREADS"] = "2"
7
 
8
  app = FastAPI(title="OpenAI Compatible API")
9
+
10
+ # Keep the model you specifically chose to host
11
+ MODEL_ID = "Snowflake/snowflake-arctic-embed-l"
12
+ model = SentenceTransformer(MODEL_ID, device="cpu")
13
 
14
  @app.get("/health")
15
  def health_check():
 
17
 
18
  @app.post("/v1/embeddings")
19
  async def create_embeddings(request: Request):
 
 
 
20
  try:
21
  data = await request.json()
22
  except Exception:
23
  raise HTTPException(status_code=400, detail="Invalid JSON format")
24
 
 
25
  inputs = data.get("input")
26
+
27
+ # 🛡️ THE SAFEGUARD: If n8n sends an empty string, force a valid text
28
+ # so the model generates real numbers instead of an array of zeros.
29
+ if not inputs or inputs == [""] or inputs == "":
30
+ inputs = ["dummy text to prevent zero vector database crash"]
31
 
32
  if isinstance(inputs, str):
33
  inputs = [inputs]
34
 
35
+ # Generate vectors
36
  embeddings = model.encode(inputs, normalize_embeddings=True).tolist()
37
 
 
38
  response_data = [
39
  {
40
  "object": "embedding",
 
47
  return {
48
  "object": "list",
49
  "data": response_data,
50
+ "model": MODEL_ID,
51
  "usage": {"prompt_tokens": 0, "total_tokens": 0}
52
  }