Skip to content

Instantly share code, notes, and snippets.

@pamelafox
Last active February 19, 2025 13:16
Show Gist options
  • Save pamelafox/4494f72e9c63e6f7bfca68b521cadcba to your computer and use it in GitHub Desktop.
Save pamelafox/4494f72e9c63e6f7bfca68b521cadcba to your computer and use it in GitHub Desktop.
Pydantic AI with Azure or GitHub Models
# pip install azure-identity
# pip install pydantic-ai
from openai import AsyncAzureOpenAI
from azure.identity.aio import (
AzureCliCredential,
get_bearer_token_provider,
)
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
azure_credential = AzureCliCredential()
token_provider = get_bearer_token_provider(azure_credential, "https://cognitiveservices.azure.com/.default")
client = AsyncAzureOpenAI(
azure_endpoint="https://cog-v3v4cax5h4fjk.openai.azure.com",
api_version='2024-07-01-preview',
azure_ad_token_provider=token_provider,
)
model = OpenAIModel('chat', openai_client=client) # Model name should actually be deployment name
agent = Agent(
model,
system_prompt='Be concise, reply with one sentence.',
)
result = agent.run_sync('Where does "hello world" come from?')
print(result.data)
"""
The first known use of "hello, world" was in a 1974 textbook about the C programming language.
"""
import os
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
model = OpenAIModel('gpt-4o', api_key=os.environ["GITHUB_TOKEN"], base_url="https://models.inference.ai.azure.com")
agent = Agent(
model,
system_prompt='Be concise, reply with one sentence.',
)
result = agent.run_sync('Where does "hello world" come from?')
print(result.data)
"""
The first known use of "hello, world" was in a 1974 textbook about the C programming language.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment