From be7fa920fab2045a7872c490028fa46e56e06744 Mon Sep 17 00:00:00 2001 From: Marlene <57748216+marlenezw@users.noreply.github.com> Date: Thu, 20 Feb 2025 14:35:26 +0000 Subject: [PATCH] Partner: Azure AI Langchain Docs and Package Registry (#29879) This PR adds documentation for the Azure AI package in Langchain to the main mono-repo No issue connected or updated dependencies. Utilises existing tests and makes updates to the docs --------- Co-authored-by: Chester Curme --- docs/docs/integrations/chat/azure_ai.ipynb | 275 ++++++++++++++++++ docs/docs/integrations/providers/azure_ai.mdx | 59 ++++ libs/packages.yml | 6 + 3 files changed, 340 insertions(+) create mode 100644 docs/docs/integrations/chat/azure_ai.ipynb create mode 100644 docs/docs/integrations/providers/azure_ai.mdx diff --git a/docs/docs/integrations/chat/azure_ai.ipynb b/docs/docs/integrations/chat/azure_ai.ipynb new file mode 100644 index 00000000000..eb0ca3b5093 --- /dev/null +++ b/docs/docs/integrations/chat/azure_ai.ipynb @@ -0,0 +1,275 @@ +{ + "cells": [ + { + "cell_type": "raw", + "id": "afaf8039", + "metadata": {}, + "source": [ + "---\n", + "sidebar_label: AzureAIChatCompletionsModel\n", + "---" + ] + }, + { + "cell_type": "markdown", + "id": "e49f1e0d", + "metadata": {}, + "source": [ + "# AzureAIChatCompletionsModel\n", + "\n", + "This will help you getting started with AzureAIChatCompletionsModel [chat models](/docs/concepts/chat_models). For detailed documentation of all AzureAIChatCompletionsModel features and configurations head to the [API reference](https://python.langchain.com/api_reference/azure_ai/chat_models/langchain_azure_ai.chat_models.AzureAIChatCompletionsModel.html)\n", + "\n", + "The AzureAIChatCompletionsModel class uses the Azure AI Foundry SDK. AI Foundry has several chat models including AzureOpenAI, Cohere, Llama, Phi-3/4, and DeepSeek-R1 to name a few. You can find information about their latest models and their costs, context windows, and supported input types in the [Azure docs](https://learn.microsoft.com/azure/ai-studio/how-to/model-catalog-overview).\n", + "\n", + "\n", + "## Overview\n", + "### Integration details\n", + "\n", + "\n", + "| Class | Package | Local | Serializable | [JS support](https://v03.api.js.langchain.com/classes/_langchain_openai.AzureChatOpenAI.html) | Package downloads | Package latest |\n", + "| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n", + "| [AzureAIChatCompletionsModel](https://python.langchain.com/api_reference/azure_ai/chat_models/langchain_azure_ai.chat_models.AzureAIChatCompletionsModel.html) | [langchain-azure-ai](https://python.langchain.com/api_reference/langchain_azure_ai/index.html) | ❌ | ✅ | ✅ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-azure-ai?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-azure-ai?style=flat-square&label=%20) |\n", + "\n", + "### Model features\n", + "| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | Native async | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n", + "| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n", + "| ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ✅| \n", + "\n", + "## Setup\n", + "\n", + "To access AzureAIChatCompletionsModel models you'll need to create an [Azure account](https://azure.microsoft.com/pricing/purchase-options/azure-account), get an API key, and install the `langchain-azure-ai` integration package.\n", + "\n", + "### Credentials\n", + "\n", + "\n", + "Head to the [Azure docs](https://learn.microsoft.com/en-us/azure/ai-studio/how-to/develop/sdk-overview?tabs=sync&pivots=programming-language-python) to see how to create your deployment and generate an API key. Once your model is deployed you click the 'get endpoint' button in AI Foundry. This will show you your endpoint and api key. Once you've done this set the AZURE_INFERENCE_CREDENTIAL and AZURE_INFERENCE_ENDPOINT environment variables:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "433e8d2b-9519-4b49-b2c4-7ab65b046c94", + "metadata": {}, + "outputs": [], + "source": [ + "import getpass\n", + "import os\n", + "\n", + "if not os.getenv(\"AZURE_INFERENCE_CREDENTIAL\"):\n", + " os.environ[\"AZURE_INFERENCE_CREDENTIAL\"] = getpass.getpass(\n", + " \"Enter your AzureAIChatCompletionsModel API key: \"\n", + " )\n", + "\n", + "if not os.getenv(\"AZURE_INFERENCE_ENDPOINT\"):\n", + " os.environ[\"AZURE_INFERENCE_ENDPOINT\"] = getpass.getpass(\n", + " \"Enter your model endpoint: \"\n", + " )" + ] + }, + { + "cell_type": "markdown", + "id": "72ee0c4b-9764-423a-9dbf-95129e185210", + "metadata": {}, + "source": [ + "If you want to get automated tracing of your model calls you can also set your [LangSmith](https://docs.smith.langchain.com/) API key by uncommenting below:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a15d341e-3e26-4ca3-830b-5aab30ed66de", + "metadata": {}, + "outputs": [], + "source": [ + "# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")" + ] + }, + { + "cell_type": "markdown", + "id": "0730d6a1-c893-4840-9817-5e5251676d5d", + "metadata": {}, + "source": [ + "### Installation\n", + "\n", + "The LangChain AzureAIChatCompletionsModel integration lives in the `langchain-azure-ai` package:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "652d6238-1f87-422a-b135-f5abbb8652fc", + "metadata": {}, + "outputs": [], + "source": [ + "%pip install -qU langchain-azure-ai" + ] + }, + { + "cell_type": "markdown", + "id": "a38cde65-254d-4219-a441-068766c0d4b5", + "metadata": {}, + "source": [ + "## Instantiation\n", + "\n", + "Now we can instantiate our model object and generate chat completions:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel\n", + "\n", + "llm = AzureAIChatCompletionsModel(\n", + " model_name=\"gpt-4\",\n", + " temperature=0,\n", + " max_tokens=None,\n", + " timeout=None,\n", + " max_retries=2,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "2b4f3e15", + "metadata": {}, + "source": [ + "## Invocation" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "62e0dbc3", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "AIMessage(content=\"J'adore programmer.\", additional_kwargs={}, response_metadata={'model': 'gpt-4o-2024-05-13', 'token_usage': {'input_tokens': 31, 'output_tokens': 4, 'total_tokens': 35}, 'finish_reason': 'stop'}, id='run-c082dffd-b1de-4b3f-943f-863836663ddb-0', usage_metadata={'input_tokens': 31, 'output_tokens': 4, 'total_tokens': 35})" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "messages = [\n", + " (\n", + " \"system\",\n", + " \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n", + " ),\n", + " (\"human\", \"I love programming.\"),\n", + "]\n", + "ai_msg = llm.invoke(messages)\n", + "ai_msg" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "d86145b3-bfef-46e8-b227-4dda5c9c2705", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "J'adore programmer.\n" + ] + } + ], + "source": [ + "print(ai_msg.content)" + ] + }, + { + "cell_type": "markdown", + "id": "18e2bfc0-7e78-4528-a73f-499ac150dca8", + "metadata": {}, + "source": [ + "## Chaining\n", + "\n", + "We can [chain](/docs/how_to/sequence/) our model with a prompt template like so:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "AIMessage(content='Ich liebe Programmieren.', additional_kwargs={}, response_metadata={'model': 'gpt-4o-2024-05-13', 'token_usage': {'input_tokens': 26, 'output_tokens': 5, 'total_tokens': 31}, 'finish_reason': 'stop'}, id='run-01ba6587-6ff4-4554-8039-13204a7d95db-0', usage_metadata={'input_tokens': 26, 'output_tokens': 5, 'total_tokens': 31})" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from langchain_core.prompts import ChatPromptTemplate\n", + "\n", + "prompt = ChatPromptTemplate(\n", + " [\n", + " (\n", + " \"system\",\n", + " \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n", + " ),\n", + " (\"human\", \"{input}\"),\n", + " ]\n", + ")\n", + "\n", + "chain = prompt | llm\n", + "chain.invoke(\n", + " {\n", + " \"input_language\": \"English\",\n", + " \"output_language\": \"German\",\n", + " \"input\": \"I love programming.\",\n", + " }\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3", + "metadata": {}, + "source": [ + "## API reference\n", + "\n", + "For detailed documentation of all AzureAIChatCompletionsModel features and configurations head to the API reference: https://python.langchain.com/api_reference/azure_ai/chat_models/langchain_azure_ai.chat_models.AzureAIChatCompletionsModel.html" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "langchain-3-9", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.19" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/docs/integrations/providers/azure_ai.mdx b/docs/docs/integrations/providers/azure_ai.mdx new file mode 100644 index 00000000000..dd126d55e41 --- /dev/null +++ b/docs/docs/integrations/providers/azure_ai.mdx @@ -0,0 +1,59 @@ +# Azure AI + +All functionality related to [Azure AI Foundry](https://learn.microsoft.com/en-us/azure/developer/python/get-started) and its related projects. + +Integration packages for Azure AI, Dynamic Sessions, SQL Server are maintained in +the [langchain-azure](https://github.com/langchain-ai/langchain-azure) repository. + +## Chat models + +We recommend developers start with the (`langchain-azure-ai`) to access all the models available in [Azure AI Foundry](https://learn.microsoft.com/en-us/azure/ai-studio/how-to/model-catalog-overview). + +### Azure AI Chat Completions Model + +Access models like Azure OpenAI, DeepSeek R1, Cohere, Phi and Mistral using the `AzureAIChatCompletionsModel` class. + +```bash +pip install -U langchain-azure-ai +``` + +Configure your API key and Endpoint. + +```bash +export AZURE_INFERENCE_CREDENTIAL=your-api-key +export AZURE_INFERENCE_ENDPOINT=your-endpoint +``` + +```python +from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel + +llm = AzureAIChatCompletionsModel( + model_name="gpt-4o", + api_version="2024-05-01-preview", +) + +llm.invoke('Tell me a joke and include some emojis') +``` + +## Embedding models + +### Azure AI model inference for embeddings + +```bash +pip install -U langchain-azure-ai +``` + +Configure your API key and Endpoint. + +```bash +export AZURE_INFERENCE_CREDENTIAL=your-api-key +export AZURE_INFERENCE_ENDPOINT=your-endpoint +``` + +```python +from langchain_azure_ai.embeddings import AzureAIEmbeddingsModel + +embed_model = AzureAIEmbeddingsModel( + model_name="text-embedding-ada-002" +) +``` diff --git a/libs/packages.yml b/libs/packages.yml index cbdd22b3561..b0058a13269 100644 --- a/libs/packages.yml +++ b/libs/packages.yml @@ -230,6 +230,12 @@ packages: repo: langchain-ai/langchain-unstructured downloads: 118888 downloads_updated_at: '2025-02-13T20:29:06.035211+00:00' +- name: langchain-azure-ai + path: libs/azure-ai + repo: langchain-ai/langchain-azure + provider_page: azure_ai + downloads: 5835 + js: '@langchain/openai' - name: langchain-azure-dynamic-sessions path: libs/azure-dynamic-sessions repo: langchain-ai/langchain-azure