A Python project template for building AI applications using Azure OpenAI services. This template includes:
- Ready-to-use Azure OpenAI integration
- Example implementation of AI agents using Swarm
- Development environment setup with modern Python tools
- Python 3.12 or higher
- uv package manager (installation guide)
- Azure subscription with OpenAI service access
- Azure OpenAI deployment with GPT model
- Install uv if you haven't already:
curl -LsSf https://astral.sh/uv/install.sh | sh
- Install and configure Azure CLI using homebrew:
brew update && brew install azure-cli
# Login to Azure and choose the subscription that enables you to make OpenAI LLM requests
az login
- Clone this repository:
git clone [email protected]:nulib/genai-template.git
cd genai-template
- Create and activate a virtual environment:
uv venv
source .venv/bin/activate # On Windows use: .venv\Scripts\activate
- Install dependencies:
uv sync
- Environment Variables:
Create a
.env
file in the project root with the following required variables:
AZURE_ENDPOINT=https://<your-resource-name>.openai.azure.com/
AZURE_API_VERSION=2024-08-01-preview
AZURE_ENDPOINT_SCOPE=https://cognitiveservices.azure.com/.default
AZURE_DEPLOYMENT_NAME=<your-model-deployment-name>
OPENSEARCH_ENDPOINT=<your-opensearch-endpoint>
OPENSEARCH_MODEL_ID=<your-opensearch-model-id>
Replace:
<your-resource-name>
with your Azure OpenAI resource name<your-model-deployment-name>
with your model deployment name (e.g., "gpt-4")<your-opensearch-endpoint>
with your OpenSearch domain endpoint<your-opensearch-model-id>
with your OpenSearch model ID
Note: The .env
file is already included in .gitignore
Run the OpenSearch client manually:
uv run opensearch_client.py
Run the langggraph
agents demo:
uv run langgraph
Run the swarm
agents demo:
uv run swarm
Note: check the pyproject.toml file
This project is organized around two main agent frameworks: langgraph_agents/
for graph-based workflows and swarm_agents/
for OpenAI's Swarm framework. Each framework directory contains its own implementation of agents, tools, and workflows. A cli/
directory provides one example of implementing command-line interface capabilities. Shared infrastructure components like the OpenAI client, OpenSearch integration, and utility functions reside in the root directory. Configuration is managed through pyproject.toml
and environment variables (.env
).
The project uses these dependencies:
azure-identity
(^1.19.0): Azure authenticationlangchain
(^0.3.7): LangChain frameworklangchain-openai
(^0.2.8): LangChain OpenAI integrationlanggraph
(^0.2.50): Graph-based workflow frameworkopenai
(^1.54.4): Azure OpenAI API clientopensearch-py
(^2.7.1): OpenSearch clientpython-dotenv
(^1.0.1): Environment configurationswarm
(latest): Agent implementation framework from OpenAIboto3
(^1.35.63): AWS SDK for Pythonrequests-aws4auth
(^0.4.3): AWS request signing
This project uses uv for dependency management. Common commands:
- Add a dependency:
uv add <package-name>
- Remove a dependency:
uv remove <package-name>
- Update dependencies:
uv sync
- Run a script:
uv run <script-name>
- Run a module:
uv run -m <module-name>
To install the langgraph
or swarm
as a local CLI tool, follow these steps:
# In any directory you choose:
uv venv langgraph
source langgraph/bin/activate
uv pip install git+https://github.com/nulib/genai-template
langgraph
# AzureOpenAI client initialized
# Session: Session(region_name='us-east-1')
# ...
# User:
Common issues and solutions:
-
Authentication Errors:
- Ensure you're logged in with
az login
andaws sso login
- Verify your Azure subscription has OpenAI access
- Check that your
.env
variables are correct
- Ensure you're logged in with
-
Model Deployment Issues:
- Verify your deployment name matches AZURE_DEPLOYMENT_NAME
- Ensure your Azure OpenAI service is properly configured
- Check that your Azure account has either
Cognitive Services OpenAI Contributor
orCognitive Services OpenAI User
role (see Azure RBAC documentation)
-
Package Installation Problems:
- Try removing the
.venv
directory and recreating it - Update uv
- Try removing the