Please read the PydanticAI docs for more information on how to use PydanticAI.
Create an Agent with PydanticAI
PydanticAI is a Python agent framework designed to make it AI Agent creation less painful to build for production grade applications with Generative AI.
Why would you use PydanticAI with PremAI?
If you work in an industry that needs fine-tuned agents for your use case, PydanticAI is a great tool to use as a complement to PremAI.
Pydantic makes it easy to create AI agents that can utilize tools without all the complexity of other agent frameworks.
Combined with Prem you can create very powerful and flexible agents that can be used in a production environment and are trained on your own data.
Prerequisites
pydantic-ai
library
- Prem API Key
- python 3.9+ (We have been primarily using uv to install packages!)
Install the Pydantic AI library
#pip
pip install "pydantic-ai-slim[openai]"
#uv
uv add "pydantic-ai-slim[openai]"
Import the Pydantic AI library
Make sure you have the following imports for the OpenAI provider and the OpenAI model.
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.providers.openai import OpenAIProvider
import os
import asyncio
Connect to the Prem AI API
premai_agent = OpenAIModel(
'claude-3.7-sonnet',#'gpt-4o-mini',gpt-4-o,claude-3.5-sonnet,claude-3.7-sonnet etc...
provider=OpenAIProvider(
base_url='https://studio.premai.io/api/v1',
api_key=os.getenv('PREMAI_API_KEY'),
)
)
Create the Agent and run it
agent = Agent(
premai_agent,
deps_type=str,
system_prompt="You are a helpful assistant that can answer questions about the world."
)
#you can also run this without asyncio.run_sync by using agent.run("Who is Stephen Curry?") check pydantic docs/reference for more info.
async def main():
response = await agent.run("Who is Stephen Curry?")
print(response.output) # This will print the agent's response
# Run the async function
asyncio.run(main())
Full Example
from pydantic_ai import Agent, RunContext
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.providers.openai import OpenAIProvider
import os
import asyncio
premai_agent = OpenAIModel(
'claude-3.7-sonnet',#'gpt-4o-mini',gpt-4-o,claude-3.5-sonnet,claude-3.7-sonnet etc...
provider=OpenAIProvider(
base_url='https://studio.premai.io/api/v1',
api_key=os.getenv('PREMAI_API_KEY'),
)
)
agent = Agent(
premai_agent,
deps_type=str,
system_prompt="You are a helpful assistant that can answer questions about the world."
)
#you can also run this without asyncio.run_sync by using agent.run("Who is Stephen Curry?") check pydantic docs/reference for more info.
async def main():
response = await agent.run("Who is Stephen Curry?")
print(response.output) # This will print the agent's response
# Run the async function
asyncio.run(main())