| |
| |
| |
|
|
| import os |
| from typing import Any, Dict, List, Optional |
|
|
| from openai import OpenAI |
|
|
| from haystack import component, default_from_dict, default_to_dict |
| from haystack.utils import Secret, deserialize_secrets_inplace |
|
|
| OPENAI_TIMEOUT = float(os.environ.get("OPENAI_TIMEOUT", 30)) |
| OPENAI_MAX_RETRIES = int(os.environ.get("OPENAI_MAX_RETRIES", 5)) |
|
|
|
|
| @component |
| class OpenAITextEmbedder: |
| """ |
| Embeds strings using OpenAI models. |
| |
| You can use it to embed user query and send it to an embedding Retriever. |
| |
| ### Usage example |
| |
| ```python |
| from haystack.components.embedders import OpenAITextEmbedder |
| |
| text_to_embed = "I love pizza!" |
| |
| text_embedder = OpenAITextEmbedder() |
| |
| print(text_embedder.run(text_to_embed)) |
| |
| # {'embedding': [0.017020374536514282, -0.023255806416273117, ...], |
| # 'meta': {'model': 'text-embedding-ada-002-v2', |
| # 'usage': {'prompt_tokens': 4, 'total_tokens': 4}}} |
| ``` |
| """ |
|
|
| def __init__( |
| self, |
| api_key: Secret = Secret.from_env_var("OPENAI_API_KEY"), |
| model: str = "text-embedding-ada-002", |
| dimensions: Optional[int] = None, |
| api_base_url: Optional[str] = None, |
| organization: Optional[str] = None, |
| prefix: str = "", |
| suffix: str = "", |
| timeout: Optional[float] = None, |
| max_retries: Optional[int] = None, |
| ): |
| """ |
| Creates an OpenAITextEmbedder component. |
| |
| Before initializing the component, you can set the 'OPENAI_TIMEOUT' and 'OPENAI_MAX_RETRIES' |
| environment variables to override the `timeout` and `max_retries` parameters respectively |
| in the OpenAI client. |
| |
| :param api_key: |
| The OpenAI API key. |
| You can set it with an environment variable `OPENAI_API_KEY`, or pass with this parameter |
| during initialization. |
| :param model: |
| The name of the model to use for calculating embeddings. |
| The default model is `text-embedding-ada-002`. |
| :param dimensions: |
| The number of dimensions of the resulting embeddings. Only `text-embedding-3` and |
| later models support this parameter. |
| :param api_base_url: |
| Overrides default base URL for all HTTP requests. |
| :param organization: |
| Your organization ID. See OpenAI's |
| [production best practices](https://platform.openai.com/docs/guides/production-best-practices/setting-up-your-organization) |
| for more information. |
| :param prefix: |
| A string to add at the beginning of each text to embed. |
| :param suffix: |
| A string to add at the end of each text to embed. |
| :param timeout: |
| Timeout for OpenAI client calls. If not set, it defaults to either the |
| `OPENAI_TIMEOUT` environment variable, or 30 seconds. |
| :param max_retries: |
| Maximum number of retries to contact OpenAI after an internal error. |
| If not set, it defaults to either the `OPENAI_MAX_RETRIES` environment variable, or set to 5. |
| """ |
| self.model = model |
| self.dimensions = dimensions |
| self.api_base_url = api_base_url |
| self.organization = organization |
| self.prefix = prefix |
| self.suffix = suffix |
| self.api_key = api_key |
|
|
| if timeout is None: |
| timeout = float(os.environ.get("OPENAI_TIMEOUT", 30.0)) |
| if max_retries is None: |
| max_retries = int(os.environ.get("OPENAI_MAX_RETRIES", 5)) |
|
|
| self.client = OpenAI( |
| api_key=api_key.resolve_value(), |
| organization=organization, |
| base_url=api_base_url, |
| timeout=timeout, |
| max_retries=max_retries, |
| ) |
|
|
| def _get_telemetry_data(self) -> Dict[str, Any]: |
| """ |
| Data that is sent to Posthog for usage analytics. |
| """ |
| return {"model": self.model} |
|
|
| def to_dict(self) -> Dict[str, Any]: |
| """ |
| Serializes the component to a dictionary. |
| |
| :returns: |
| Dictionary with serialized data. |
| """ |
| return default_to_dict( |
| self, |
| model=self.model, |
| api_base_url=self.api_base_url, |
| organization=self.organization, |
| prefix=self.prefix, |
| suffix=self.suffix, |
| dimensions=self.dimensions, |
| api_key=self.api_key.to_dict(), |
| ) |
|
|
| @classmethod |
| def from_dict(cls, data: Dict[str, Any]) -> "OpenAITextEmbedder": |
| """ |
| Deserializes the component from a dictionary. |
| |
| :param data: |
| Dictionary to deserialize from. |
| :returns: |
| Deserialized component. |
| """ |
| deserialize_secrets_inplace(data["init_parameters"], keys=["api_key"]) |
| return default_from_dict(cls, data) |
|
|
| @component.output_types(embedding=List[float], meta=Dict[str, Any]) |
| def run(self, text: str): |
| """ |
| Embeds a single string. |
| |
| :param text: |
| Text to embed. |
| |
| :returns: |
| A dictionary with the following keys: |
| - `embedding`: The embedding of the input text. |
| - `meta`: Information about the usage of the model. |
| """ |
| if not isinstance(text, str): |
| raise TypeError( |
| "OpenAITextEmbedder expects a string as an input." |
| "In case you want to embed a list of Documents, please use the OpenAIDocumentEmbedder." |
| ) |
|
|
| text_to_embed = self.prefix + text + self.suffix |
|
|
| |
| |
| text_to_embed = text_to_embed.replace("\n", " ") |
|
|
| if self.dimensions is not None: |
| response = self.client.embeddings.create(model=self.model, dimensions=self.dimensions, input=text_to_embed) |
| else: |
| response = self.client.embeddings.create(model=self.model, input=text_to_embed) |
|
|
| meta = {"model": response.model, "usage": dict(response.usage)} |
|
|
| return {"embedding": response.data[0].embedding, "meta": meta} |
|
|