|
| 1 | +from typing import Self |
| 2 | + |
| 3 | +from pydantic import model_validator |
| 4 | +from uipath_langchain_client.base_client import UiPathBaseLLMClient |
| 5 | +from uipath_langchain_client.settings import UiPathAPIConfig |
| 6 | + |
| 7 | +try: |
| 8 | + from langchain_fireworks.embeddings import FireworksEmbeddings |
| 9 | + from openai import AsyncOpenAI, OpenAI |
| 10 | +except ImportError as e: |
| 11 | + raise ImportError( |
| 12 | + "The 'fireworks' extra is required to use UiPathFireworksEmbeddings. " |
| 13 | + 'Install it with: uv add "uipath-langchain-client[fireworks]"' |
| 14 | + ) from e |
| 15 | + |
| 16 | + |
| 17 | +class UiPathFireworksEmbeddings(UiPathBaseLLMClient, FireworksEmbeddings): |
| 18 | + api_config: UiPathAPIConfig = UiPathAPIConfig( |
| 19 | + api_type="embeddings", |
| 20 | + client_type="passthrough", |
| 21 | + vendor_type="openai", |
| 22 | + api_flavor="chat-completions", |
| 23 | + api_version="2025-03-01-preview", |
| 24 | + freeze_base_url=True, |
| 25 | + ) |
| 26 | + |
| 27 | + @model_validator(mode="after") |
| 28 | + def setup_uipath_client(self) -> Self: |
| 29 | + self.client = OpenAI( |
| 30 | + api_key="PLACEHOLDER", |
| 31 | + timeout=None, # handled by the UiPath client |
| 32 | + max_retries=0, # handled by the UiPath client |
| 33 | + http_client=self.uipath_sync_client, |
| 34 | + ) |
| 35 | + self.async_client = AsyncOpenAI( |
| 36 | + api_key="PLACEHOLDER", |
| 37 | + timeout=None, # handled by the UiPath client |
| 38 | + max_retries=0, # handled by the UiPath client |
| 39 | + http_client=self.uipath_async_client, |
| 40 | + ) |
| 41 | + return self |
| 42 | + |
| 43 | + def embed_documents(self, texts: list[str]) -> list[list[float]]: |
| 44 | + """Embed search docs.""" |
| 45 | + return [ |
| 46 | + i.embedding for i in self.client.embeddings.create(input=texts, model=self.model).data |
| 47 | + ] |
| 48 | + |
| 49 | + def embed_query(self, text: str) -> list[float]: |
| 50 | + """Embed query text.""" |
| 51 | + return self.embed_documents([text])[0] |
| 52 | + |
| 53 | + async def aembed_documents(self, texts: list[str]) -> list[list[float]]: |
| 54 | + """Embed search docs asynchronously.""" |
| 55 | + return [ |
| 56 | + i.embedding |
| 57 | + for i in (await self.async_client.embeddings.create(input=texts, model=self.model)).data |
| 58 | + ] |
| 59 | + |
| 60 | + async def aembed_query(self, text: str) -> list[float]: |
| 61 | + """Embed query text asynchronously.""" |
| 62 | + return (await self.aembed_documents([text]))[0] |
0 commit comments