Initial project import
This commit is contained in:
@@ -41,12 +41,16 @@ def embed_texts_openai_compatible(
|
||||
*,
|
||||
model: str,
|
||||
base_url: str,
|
||||
dimensions: int = 0,
|
||||
api_key: str = "",
|
||||
timeout_seconds: int = 120,
|
||||
) -> list[list[float]]:
|
||||
if not model:
|
||||
raise ValueError("embedding_model is required")
|
||||
body = json.dumps({"model": model, "input": texts}, ensure_ascii=False).encode("utf-8")
|
||||
request_payload: dict[str, Any] = {"model": model, "input": texts}
|
||||
if int(dimensions or 0) > 0:
|
||||
request_payload["dimensions"] = int(dimensions)
|
||||
body = json.dumps(request_payload, ensure_ascii=False).encode("utf-8")
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if api_key:
|
||||
headers["Authorization"] = f"Bearer {api_key}"
|
||||
@@ -67,6 +71,13 @@ def embed_texts_openai_compatible(
|
||||
if not isinstance(embedding, list) or not embedding:
|
||||
raise ValueError("Embedding response item has no embedding[]")
|
||||
vector = [float(value) for value in embedding]
|
||||
if int(dimensions or 0) > 0:
|
||||
if len(vector) < int(dimensions):
|
||||
raise ValueError(
|
||||
f"Embedding response returned {len(vector)} dimensions, "
|
||||
f"fewer than requested {int(dimensions)}"
|
||||
)
|
||||
vector = vector[: int(dimensions)]
|
||||
by_index[int(index)] = l2_normalize(vector)
|
||||
vectors = [by_index[index] for index in range(len(texts)) if index in by_index]
|
||||
if len(vectors) != len(texts):
|
||||
@@ -96,6 +107,7 @@ def embed_texts(
|
||||
texts,
|
||||
model=model,
|
||||
base_url=base_url,
|
||||
dimensions=dimensions,
|
||||
api_key=api_key,
|
||||
timeout_seconds=timeout_seconds,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user