|
13 | 13 |
|
14 | 14 |
|
15 | 15 | class AzureOpenAIEmbeddings(OpenAIEmbeddings): |
16 | | - """`Azure OpenAI` Embeddings API. |
| 16 | + """AzureOpenAI embedding model integration. |
17 | 17 |
|
18 | | - To use, you should have the |
19 | | - environment variable ``AZURE_OPENAI_API_KEY`` set with your API key or pass it |
20 | | - as a named parameter to the constructor. |
| 18 | + Setup: |
| 19 | + To access AzureOpenAI embedding models you'll need to create an Azure account, |
| 20 | + get an API key, and install the `langchain-openai` integration package. |
21 | 21 |
|
22 | | - Example: |
| 22 | + You’ll need to have an Azure OpenAI instance deployed. |
| 23 | + You can deploy a version on Azure Portal following this |
| 24 | + [guide](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/create-resource?pivots=web-portal). |
| 25 | +
|
| 26 | + Once you have your instance running, make sure you have the name of your |
| 27 | + instance and key. You can find the key in the Azure Portal, |
| 28 | + under the “Keys and Endpoint” section of your instance. |
| 29 | +
|
| 30 | + .. code-block:: bash |
| 31 | +
|
| 32 | + pip install -U langchain_openai |
| 33 | +
|
| 34 | + # Set up your environment variables (or pass them directly to the model) |
| 35 | + export AZURE_OPENAI_API_KEY="your-api-key" |
| 36 | + export AZURE_OPENAI_ENDPOINT="https://<your-endpoint>.openai.azure.com/" |
| 37 | + export AZURE_OPENAI_API_VERSION="2024-02-01" |
| 38 | +
|
| 39 | + Key init args — completion params: |
| 40 | + model: str |
| 41 | + Name of AzureOpenAI model to use. |
| 42 | + dimensions: Optional[int] |
| 43 | + Number of dimensions for the embeddings. Can be specified only |
| 44 | + if the underlying model supports it. |
| 45 | +
|
| 46 | + Key init args — client params: |
| 47 | + api_key: Optional[SecretStr] |
| 48 | +
|
| 49 | + See full list of supported init args and their descriptions in the params section. |
| 50 | +
|
| 51 | + Instantiate: |
23 | 52 | .. code-block:: python |
24 | 53 |
|
25 | 54 | from langchain_openai import AzureOpenAIEmbeddings |
26 | 55 |
|
27 | | - openai = AzureOpenAIEmbeddings(model="text-embedding-3-large") |
28 | | - """ |
| 56 | + embeddings = AzureOpenAIEmbeddings( |
| 57 | + model="text-embedding-3-large" |
| 58 | + # dimensions: Optional[int] = None, # Can specify dimensions with new text-embedding-3 models |
| 59 | + # azure_endpoint="https://<your-endpoint>.openai.azure.com/", If not provided, will read env variable AZURE_OPENAI_ENDPOINT |
| 60 | + # api_key=... # Can provide an API key directly. If missing read env variable AZURE_OPENAI_API_KEY |
| 61 | + # openai_api_version=..., # If not provided, will read env variable AZURE_OPENAI_API_VERSION |
| 62 | + ) |
| 63 | +
|
| 64 | + Embed single text: |
| 65 | + .. code-block:: python |
| 66 | +
|
| 67 | + input_text = "The meaning of life is 42" |
| 68 | + vector = embed.embed_query(input_text) |
| 69 | + print(vector[:3]) |
| 70 | +
|
| 71 | + .. code-block:: python |
| 72 | +
|
| 73 | + [-0.024603435769677162, -0.007543657906353474, 0.0039630369283258915] |
| 74 | +
|
| 75 | + Embed multiple texts: |
| 76 | + .. code-block:: python |
| 77 | +
|
| 78 | + input_texts = ["Document 1...", "Document 2..."] |
| 79 | + vectors = embed.embed_documents(input_texts) |
| 80 | + print(len(vectors)) |
| 81 | + # The first 3 coordinates for the first vector |
| 82 | + print(vectors[0][:3]) |
| 83 | +
|
| 84 | + .. code-block:: python |
| 85 | +
|
| 86 | + 2 |
| 87 | + [-0.024603435769677162, -0.007543657906353474, 0.0039630369283258915] |
| 88 | +
|
| 89 | + Async: |
| 90 | + .. code-block:: python |
| 91 | +
|
| 92 | + vector = await embed.aembed_query(input_text) |
| 93 | + print(vector[:3]) |
| 94 | +
|
| 95 | + # multiple: |
| 96 | + # await embed.aembed_documents(input_texts) |
| 97 | +
|
| 98 | + .. code-block:: python |
| 99 | +
|
| 100 | + [-0.009100092574954033, 0.005071679595857859, -0.0029193938244134188] |
| 101 | + """ # noqa: E501 |
29 | 102 |
|
30 | 103 | azure_endpoint: Union[str, None] = None |
31 | 104 | """Your Azure endpoint, including the resource. |
|
0 commit comments