### Quick Start: Create Azure Chat Model with Environment Variables
Source: https://github.com/zpg6/langchain-azure-ai-inference-plus/blob/main/README.md
This example demonstrates how to initialize an Azure chat model using environment variables (AZURE_AI_ENDPOINT, AZURE_AI_API_KEY). It shows a basic interaction with the model using `HumanMessage` and `SystemMessage` and printing the response content.
```Python
from langchain_azure_ai_inference_plus import create_azure_chat_model
from langchain_core.messages import HumanMessage, SystemMessage
# Uses environment variables: AZURE_AI_ENDPOINT, AZURE_AI_API_KEY
llm = create_azure_chat_model(
model_name="Codestral-2501"
)
messages = [
SystemMessage(content="You are a helpful assistant."),
HumanMessage(content="What is the capital of France?")
]
response = llm.invoke(messages)
print(response.content)
# "The capital of France is Paris..."
```
--------------------------------
### Install langchain-azure-ai-inference-plus
Source: https://github.com/zpg6/langchain-azure-ai-inference-plus/blob/main/README.md
This snippet provides the command to install the langchain-azure-ai-inference-plus package using pip. It requires Python 3.10 or newer.
```Bash
pip install langchain-azure-ai-inference-plus
```
--------------------------------
### Quick Start: Create Azure Chat Model with Manual Credentials
Source: https://github.com/zpg6/langchain-azure-ai-inference-plus/blob/main/README.md
This snippet shows how to explicitly provide the endpoint and API key when creating an Azure chat model, bypassing the need for environment variables. This is useful for direct credential management within the code.
```Python
from langchain_azure_ai_inference_plus import create_azure_chat_model
llm = create_azure_chat_model(
model_name="gpt-4",
endpoint="https://your-resource.services.ai.azure.com/models",
api_key="your-api-key"
)
```
--------------------------------
### Automatic Reasoning Separation for Chat Models
Source: https://github.com/zpg6/langchain-azure-ai-inference-plus/blob/main/README.md
This example demonstrates how to configure a chat model to automatically separate reasoning from the main content using `reasoning_tags`. The reasoning part is then accessible via `result.additional_kwargs.get("reasoning")`, providing a clean output in `result.content`.
```Python
llm = create_azure_chat_model(
model_name="DeepSeek-R1",
reasoning_tags=["", ""] # ✨ Auto-separation
)
messages = [
SystemMessage(content="You are a helpful math tutor."),
HumanMessage(content="What's 15 * 23? Think step by step.")
]
result = llm.invoke(messages)
# Clean output without reasoning clutter
print(result.content)
# "15 * 23 equals 345."
# Access the reasoning separately
print(result.additional_kwargs.get("reasoning"))
# "Let me think about this step by step. 15 * 23 = 15 * 20 + 15 * 3..."
```
--------------------------------
### Integrate Azure AI Inference Plus with LangChain Chains
Source: https://github.com/zpg6/langchain-azure-ai-inference-plus/blob/main/README.md
Illustrates how to use `create_azure_chat_model` with LangChain's `ChatPromptTemplate` and `StrOutputParser` to build a simple conversational chain. This example highlights the seamless integration with existing LangChain components, allowing for robust LLM application development.
```python
from langchain_azure_ai_inference_plus import create_azure_chat_model
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
llm = create_azure_chat_model(
model_name="Codestral-2501"
)
# Create a reusable prompt template
joke_prompt = ChatPromptTemplate.from_messages([
("system", "You are a witty programmer who tells short, clever jokes."),
("human", "Tell me a joke about {topic}")
])
# Chain with string output parser
joke_chain = joke_prompt | llm | StrOutputParser()
# Use the chain multiple times with different topics
joke = joke_chain.invoke({"topic": "programming"})
print(f"Programming joke: {joke}")
```
--------------------------------
### Guaranteed Valid JSON Responses
Source: https://github.com/zpg6/langchain-azure-ai-inference-plus/blob/main/README.md
This example demonstrates the automatic JSON validation and retry mechanism. By setting `response_format="json_object"`, the model ensures that the output is always valid JSON, eliminating the need for manual error handling or try/catch blocks for parsing.
```Python
json_llm = create_azure_chat_model(
model_name="Codestral-2501",
response_format="json_object" # ✨ Auto-validation + retry
)
result = json_llm.invoke([
HumanMessage(content="Give me a JSON response about Tokyo")
])
# Always valid JSON, no try/catch needed!
import json
data = json.loads(result.content)
```
--------------------------------
### Set Up Environment Variables for Azure AI Inference Plus
Source: https://github.com/zpg6/langchain-azure-ai-inference-plus/blob/main/README.md
Provides instructions for setting up necessary environment variables (`AZURE_AI_ENDPOINT`, `AZURE_AI_API_KEY`) using a `.env` file. These credentials are required for authenticating with Azure AI services when using the `langchain-azure-ai-inference-plus` library.
```bash
AZURE_AI_ENDPOINT=https://your-resource.services.ai.azure.com/models
AZURE_AI_API_KEY=your-api-key-here
```
--------------------------------
### Migrate from LangChain Community AzureChatOpenAI to Inference Plus
Source: https://github.com/zpg6/langchain-azure-ai-inference-plus/blob/main/README.md
Explains the two simple steps to migrate existing LangChain applications using `AzureChatOpenAI` to `langchain-azure-ai-inference-plus`. It shows the change in import statement and how the model creation interface remains consistent, enabling a smooth transition to enhanced features.
```python
# Before
from langchain_community.chat_models import AzureChatOpenAI
# After
from langchain_azure_ai_inference_plus import create_azure_chat_model
# Create model (same interface, enhanced features)
llm = create_azure_chat_model(model_name="gpt-4")
```
--------------------------------
### Integrate Azure AI Embeddings with LangChain Vector Stores (FAISS)
Source: https://github.com/zpg6/langchain-azure-ai-inference-plus/blob/main/README.md
Demonstrates how to use the generated embeddings with a LangChain vector store like FAISS. It shows creating a vector store from documents and performing similarity searches, leveraging the `embeddings` object created previously for efficient retrieval.
```python
from langchain_community.vectorstores import FAISS
from langchain_core.documents import Document
# Create some sample documents
docs = [
Document(page_content="Python is a programming language", metadata={"source": "doc1"}),
Document(page_content="LangChain helps build LLM applications", metadata={"source": "doc2"}),
]
# Create vector store (automatically embeds documents)
vector_store = FAISS.from_documents(docs, embeddings)
# Perform similarity search
similar_docs = vector_store.similarity_search("programming language", k=1)
for doc in similar_docs:
print(f"Found: {doc.page_content}")
```
--------------------------------
### Python Project Dependencies
Source: https://github.com/zpg6/langchain-azure-ai-inference-plus/blob/main/requirements.txt
This snippet defines the essential Python libraries and their version constraints required for the 'langchain-azure-ai-inference-plus' project. It includes 'azure-ai-inference-plus' and 'langchain-core', specifying minimum versions to ensure a stable development environment.
```Python
azure-ai-inference-plus>=1.0.0
langchain-core>=0.1.0
```
--------------------------------
### Generate Embeddings with Azure AI Inference Plus and LangChain
Source: https://github.com/zpg6/langchain-azure-ai-inference-plus/blob/main/README.md
Shows how to use `create_azure_embeddings` to generate embeddings for documents and queries. It highlights the full LangChain embeddings support with automatic retry and batch processing capabilities, essential for semantic search and similarity tasks.
```python
from langchain_azure_ai_inference_plus import create_azure_embeddings
embeddings = create_azure_embeddings(
model_name="text-embedding-3-large"
)
# Example documents to embed
documents = [
"LangChain is a framework for developing applications powered by language models",
"Azure AI provides powerful embedding models for semantic search",
"Vector databases enable similarity search over embeddings"
]
# Generate embeddings for documents (batch processing)
doc_embeddings = embeddings.embed_documents(documents)
print(f"Generated {len(doc_embeddings)} embeddings with {len(doc_embeddings[0])} dimensions")
# Generate embedding for a query
query = "What is semantic search?"
query_embedding = embeddings.embed_query(query)
print(f"Query embedding: {len(query_embedding)} dimensions")
```
--------------------------------
### Manually Configure Azure AI Credentials in LangChain
Source: https://github.com/zpg6/langchain-azure-ai-inference-plus/blob/main/README.md
Illustrates how to explicitly pass `endpoint` and `api_key` parameters when creating `create_azure_chat_model` and `create_azure_embeddings` instances. This method is an alternative to using environment variables for credential management, offering direct control.
```python
from langchain_azure_ai_inference_plus import create_azure_chat_model, create_azure_embeddings
# Chat model
llm = create_azure_chat_model(
model_name="gpt-4",
endpoint="https://your-resource.services.ai.azure.com/models",
api_key="your-api-key"
)
# Embeddings
embeddings = create_azure_embeddings(
model_name="text-embedding-3-large",
endpoint="https://your-resource.services.ai.azure.com/models",
api_key="your-api-key"
)
```
--------------------------------
### Smart Automatic Retries for Chat Models
Source: https://github.com/zpg6/langchain-azure-ai-inference-plus/blob/main/README.md
This snippet highlights the built-in automatic retry mechanism with exponential backoff. When using `create_azure_chat_model`, transient failures are handled automatically without any additional configuration, improving the reliability of API calls.
```Python
# Automatically retries on failures - just works!
llm = create_azure_chat_model(model_name="Phi-4")
result = llm.invoke([HumanMessage(content="Tell me a joke")])
```
--------------------------------
### Automatic Reasoning Separation in JSON Mode
Source: https://github.com/zpg6/langchain-azure-ai-inference-plus/blob/main/README.md
This snippet illustrates how reasoning is automatically stripped when `response_format` is set to `json_object`, ensuring a clean JSON output. It integrates with LangChain's `ChatPromptTemplate` and `JsonOutputParser` to create a chain that produces validated JSON.
```Python
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import JsonOutputParser
json_llm = create_azure_chat_model(
model_name="DeepSeek-R1",
reasoning_tags=["", ""],
response_format="json_object" # ✨ Clean JSON guaranteed
)
# Create a prompt template
json_prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant that returns JSON."),
("human", "Give me information about {city} in JSON format with keys: name, country, population, famous_landmarks")
])
# Create output parser
json_parser = JsonOutputParser()
# Chain them together
chain = json_prompt | json_llm | json_parser
# Execute with variable substitution
result = chain.invoke({"city": "Paris"})
# Pure JSON - reasoning automatically stripped
print(f"Parsed JSON result: {result}")
print(f"Population: {result.get('population', 'N/A')}")
```
--------------------------------
### Configure Custom Retry for Azure AI Inference Plus Chat
Source: https://github.com/zpg6/langchain-azure-ai-inference-plus/blob/main/README.md
Demonstrates how to define custom retry logic for `AzureAIInferencePlusChat` using `RetryConfig`. It shows how to specify `max_retries`, `delay_seconds`, `exponential_backoff`, and custom callback functions for chat and JSON retries, providing fine-grained control over error handling.
```python
from langchain_azure_ai_inference_plus import AzureAIInferencePlusChat
from azure_ai_inference_plus import RetryConfig
def custom_chat_retry(attempt, max_retries, exception, delay):
print(f"🔄 Chat retry {attempt}/{max_retries}: {exception} (waiting {delay}s)")
def custom_json_retry(attempt, max_retries, message):
print(f"📝 JSON retry {attempt}/{max_retries}: {message}")
# Create custom retry config
custom_retry_config = RetryConfig(
max_retries=3,
delay_seconds=1.0,
exponential_backoff=True,
on_chat_retry=custom_chat_retry,
on_json_retry=custom_json_retry
)
llm = AzureAIInferencePlusChat(
model_name="Phi-4",
retry_config=custom_retry_config
)
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.