11 lines
443 B
Python
11 lines
443 B
Python
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"]
|