First commit
This commit is contained in:
10
emb.py
Normal file
10
emb.py
Normal file
@@ -0,0 +1,10 @@
|
||||
import os, requests
|
||||
|
||||
def embed_text(text: str, api_base: str, api_key: str|None, model: str) -> list[float]:
|
||||
url = f"{api_base.rstrip('/')}/v1/embeddings"
|
||||
headers = {"Content-Type":"application/json"}
|
||||
if api_key:
|
||||
headers["Authorization"] = f"Bearer {api_key}"
|
||||
r = requests.post(url, headers=headers, json={"model": model, "input": text}, timeout=60)
|
||||
r.raise_for_status()
|
||||
return r.json()["data"][0]["embedding"]
|
Reference in New Issue
Block a user