Azure AI Inference Chat Models API.
Converts a sequence of BaseMessage to ChatRequestMessage.
Convert an inference message dict to generic message.
Azure AI Chat Completions Model.
This class has been deprecated in favor of AzureAIOpenAIApiChatModel.
The Azure AI model inference API (https://aka.ms/azureai/modelinference) provides a common layer to talk with most models deployed to Azure AI. This class providers inference for chat completions models supporting it. See documentation for the list of models supporting the API.
Examples:
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
from langchain_core.messages import HumanMessage, SystemMessage
model = AzureAIChatCompletionsModel(
endpoint="https://[your-service].services.ai.azure.com/models",
credential="your-api-key",
model="mistral-large-2407",
)
messages = [
SystemMessage(
content="Translate the following from English into Italian"
),
HumanMessage(content="hi!"),
]
model.invoke(messages)
For serverless endpoints running a single model, the model_name parameter
can be omitted:
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
from langchain_core.messages import HumanMessage, SystemMessage
model = AzureAIChatCompletionsModel(
endpoint="https://[your-service].inference.ai.azure.com",
credential="your-api-key",
)
messages = [
SystemMessage(
content="Translate the following from English into Italian"
),
HumanMessage(content="hi!"),
]
model.invoke(messages)
You can pass additional properties to the underlying model, including
temperature, top_p, presence_penalty, etc.
model = AzureAIChatCompletionsModel(
endpoint="https://[your-service].services.ai.azure.com/models",
credential="your-api-key",
model="mistral-large-2407",
temperature=0.5,
top_p=0.9,
)
Azure OpenAI models require to pass the route `openai/v1`.
```python
model = AzureAIChatCompletionsModel(
endpoint="https://[your-service].services.ai.azure.com/openai/v1",
model="gpt-4.1",
credential="your-api-key",
)
Structured Output:
To use structured output with Azure AI models, you can use the
with_structured_output method. This method supports the same methods
as the base class, including function_calling, json_mode, and
json_schema.
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.messages import HumanMessage
class Joke(BaseModel):
joke: str
model = AzureAIChatCompletionsModel(
endpoint="https://[your-service].services.ai.azure.com/models",
credential="your-api-key",
model="mistral-large-2407",
).with_structured_output(Joke, method="json_schema")
<!--ADMON:eyJ0eXBlIjoibm90ZSIsInRpdGxlIjoiTm90ZSIsImljb24iOiI8c3ZnIHdpZHRoPVwiMTRcIiBoZWlnaHQ9XCIxNFwiIHZpZXdCb3g9XCIwIDAgMTQgMTRcIiBmaWxsPVwiY3VycmVudENvbG9yXCIgeG1sbnM9XCJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2Z1wiPjxwYXRoIGZpbGwtcnVsZT1cImV2ZW5vZGRcIiBjbGlwLXJ1bGU9XCJldmVub2RkXCIgZD1cIk03IDEuM0MxMC4xNCAxLjMgMTIuNyAzLjg2IDEyLjcgN0MxMi43IDEwLjE0IDEwLjE0IDEyLjcgNyAxMi43QzUuNDg5MDggMTIuNjk3NCA0LjA0MDggMTIuMDk2IDIuOTcyNDEgMTEuMDI3NkMxLjkwNDAzIDkuOTU5MiAxLjMwMjY0IDguNTEwOTIgMS4zIDdDMS4zIDMuODYgMy44NiAxLjMgNyAxLjNaTTcgMEMzLjE0IDAgMCAzLjE0IDAgN0MwIDEwLjg2IDMuMTQgMTQgNyAxNEMxMC44NiAxNCAxNCAxMC44NiAxNCA3QzE0IDMuMTQgMTAuODYgMCA3IDBaTTggM0g2VjhIOFYzWk04IDlINlYxMUg4VjlaXCIvPjwvc3ZnPiJ9-->
Using `method="function_calling"` requires the model to support
function calling and `tool_choice". Use "json_mode" or
"json_schema" for best support.
<!--/ADMON-->
**Troubleshooting:**
To diagnostic issues with the model, you can enable debug logging:
```python
import sys
import logging
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
logger = logging.getLogger("azure")
# Set the desired logging level. logging.
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)
model = AzureAIChatCompletionsModel(
endpoint="https://[your-service].services.ai.azure.com/models",
credential="your-api-key",
model="mistral-large-2407",
client_kwargs={ "logging_enable": True }
)