This commit is contained in:
Rogelio
2025-10-13 18:16:25 +00:00
parent 739f087cef
commit 325f1ef439
415 changed files with 46870 additions and 0 deletions

View File

View File

@@ -0,0 +1,4 @@
from .blocking import Ada
from .nonblocking import AsyncAda
__all__ = ["Ada", "AsyncAda"]

View File

@@ -0,0 +1,51 @@
from typing import Protocol
class BaseAda:
def __init__(
self, model: str | None = None, *, endpoint: str, key: str, version: str
) -> None:
self.model = model
class Config(Protocol):
embedding_model: str
azure_endpoint: str
openai_api_key: str
openai_api_version: str
@classmethod
def from_config(cls, c: Config):
return cls(
model=c.embedding_model,
endpoint=c.azure_endpoint,
key=c.openai_api_key,
version=c.openai_api_version,
)
@classmethod
def from_vault(
cls,
vault: str,
*,
model: str | None = None,
url: str | None = None,
token: str | None = None,
mount_point: str = "secret",
):
from hvac import Client
client = Client(url=url or "https://vault.ia-innovacion.work", token=token)
if not client.is_authenticated():
raise Exception("Vault authentication failed")
secret_map = client.secrets.kv.v2.read_secret_version(
path=vault, mount_point=mount_point
)["data"]["data"]
return cls(
model=model,
endpoint=secret_map["azure_endpoint"],
key=secret_map["openai_api_key"],
version=secret_map["openai_api_version"],
)

View File

@@ -0,0 +1,47 @@
from langfuse.openai import AzureOpenAI
from openai.types.embedding import Embedding
from .base import BaseAda
class Ada(BaseAda):
def __init__(
self, model: str | None = None, *, endpoint: str, key: str, version: str
) -> None:
super().__init__(model, endpoint=endpoint, key=key, version=version)
self.client = AzureOpenAI(
azure_endpoint=endpoint, api_key=key, api_version=version
)
def embed(
self, input: str | list[str], *, model: str | None = None
) -> list[float] | list[list[float]]:
if isinstance(input, str):
return self.embed_query(input, model)
else:
return self.batch_embed(input, model)
def batch_embed(
self, texts: list[str], model: str | None = None
) -> list[list[float]]:
if model is None:
if self.model is None:
raise ValueError("No embedding model set")
model = self.model
batches = [texts[i : i + 2048] for i in range(0, len(texts), 2048)]
results = [
(self.client.embeddings.create(input=batch, model=model)).data
for batch in batches
]
flattened_results: list[Embedding] = sum(results, [])
return [result.embedding for result in flattened_results]
def embed_query(self, text: str, model: str | None = None) -> list[float]:
if model is None:
if self.model is None:
raise ValueError("No embedding model set")
model = self.model
response = self.client.embeddings.create(input=text, model=model)
return response.data[0].embedding

View File

@@ -0,0 +1,47 @@
from langfuse.openai import AsyncAzureOpenAI
from openai.types.embedding import Embedding
from .base import BaseAda
class AsyncAda(BaseAda):
def __init__(
self, model: str | None = None, *, endpoint: str, key: str, version: str
) -> None:
super().__init__(model, endpoint=endpoint, key=key, version=version)
self.client = AsyncAzureOpenAI(
azure_endpoint=endpoint, api_key=key, api_version=version
)
async def embed(
self, input: str | list[str], *, model: str | None = None
) -> list[float] | list[list[float]]:
if isinstance(input, str):
return await self.embed_query(input, model)
else:
return await self.batch_embed(input, model)
async def batch_embed(
self, texts: list[str], model: str | None = None
) -> list[list[float]]:
if model is None:
if self.model is None:
raise ValueError("No embedding model set")
model = self.model
batches = [texts[i : i + 2048] for i in range(0, len(texts), 2048)]
results = [
(await self.client.embeddings.create(input=batch, model=model)).data
for batch in batches
]
flattened_results: list[Embedding] = sum(results, [])
return [result.embedding for result in flattened_results]
async def embed_query(self, text: str, model: str | None = None) -> list[float]:
if model is None:
if self.model is None:
raise ValueError("No embedding model set")
model = self.model
response = await self.client.embeddings.create(input=text, model=model)
return response.data[0].embedding

View File

@@ -0,0 +1,15 @@
[project]
name = "azure-ada"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
authors = [{ name = "ajac-zero", email = "ajcardoza2000@gmail.com" }]
requires-python = ">=3.12"
dependencies = ["hvac","openai>=1.72.0"]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.hatch.build.targets.wheel]
packages = ["banortegpt"]