AzureAIChatCompletionsModel()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 }
)The API key or credential to use to connect to the service. If using a project
The name of the model to use for inference, if the endpoint is running more than one model. If not, this parameter is ignored.
The maximum number of tokens to generate in the response. If None, the default maximum tokens is used.
The temperature to use for sampling from the model. If None, the default temperature is used.
The top-p value to use for sampling from the model. If None, the default top-p value is used.
The presence penalty to use for sampling from the model. If None, the default presence penalty is used.
The frequency penalty to use for sampling from the model. If None, the default frequency penalty is used.
The stop token to use for stopping generation. If None, the default stop token is used.
The seed to use for random number generation. If None, the default seed is used.
Additional kwargs model parameters.
Initialize the Azure AI model inference client.
Bind tool-like objects to this chat model.
Model wrapper that returns outputs formatted to match the given schema.
Get the namespace of the langchain object.
Close the async client to prevent unclosed session warnings.
This method should be called to properly clean up HTTP connections when using async operations.