Skip to main content

Amazon Bedrock Nova model integration for pydantic-ai

Project description

pydantic-ai-nova

Amazon Bedrock Nova model integration for pydantic-ai.

Installation

pip install pydantic-ai-nova

Requirements

  • Python 3.8 or higher
  • pydantic-ai
  • boto3
  • AWS credentials configured with access to Amazon Bedrock

Usage

from pydantic_ai_nova import AmazonNovaModel
from pydantic_ai import Agent, RunContext
from pydantic import BaseModel

# Define system prompts for better function calling
SYSTEM_PROMPTS = [
    {"text": "You are an AI assistant that helps users with their requests."},
    {"text": "Model Instructions:"},
    {"text": "- Do not assume any information. All required parameters for actions must come from the User."},
    {"text": "- If you are going to use a tool you should always generate a Thought within <thinking></thinking> tags before you invoke a function."},
    {"text": "- You have access to a weather function that can provide current weather information."},
    {"text": "- When users ask about weather, use the get_weather function instead of suggesting websites."}
]

# Define your function parameters
class WeatherParams(BaseModel):
    city: str

def get_weather(ctx: RunContext[WeatherParams]) -> str:
    """Get current weather for a city"""
    weather_data = {
        "tokyo": {
            "temp": 12,
            "condition": "Partly Cloudy",
            "humidity": 70
        }
    }
    
    city = ctx.params.city.lower()
    if city in weather_data:
        data = weather_data[city]
        return (
            f"Current weather in {city.title()}:\n"
            f"Temperature: {data['temp']}°C\n"
            f"Condition: {data['condition']}\n"
            f"Humidity: {data['humidity']}%"
        )
    return f"Sorry, weather data for {city} is not available."

# Initialize the model
model = AmazonNovaModel(
    model_id='amazon.nova-micro-v1:0',  # or your preferred Nova model
    region_name='us-east-1',  # your AWS region
    system_prompts=SYSTEM_PROMPTS  # customize model behavior
)

# Create an agent
agent = Agent(model=model)

# Register your function
agent._register_function(
    get_weather,
    WeatherParams,
    retries=3,
    prepare=None
)

# Run a simple query
response = agent.run_sync("How are you?")
print(response.data)

# Use function calling
response = agent.run_sync("What's the weather in Tokyo?")
print(response.data)

# Ask follow-up questions
response = agent.run_sync("Is it cold there?")
print(response.data)

Features

  • Full integration with pydantic-ai's Agent framework
  • Support for both streaming and non-streaming responses
  • Function calling support
  • Token usage tracking
  • AWS authentication handling through boto3

Configuration

The package uses boto3 for AWS authentication. Make sure you have your AWS credentials configured either through:

  • Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
  • AWS credentials file (~/.aws/credentials)
  • IAM role when running on AWS services

Development

To contribute to this project:

  1. Clone the repository
  2. Install development dependencies:
    pip install -e ".[dev]"
    
  3. Run tests:
    pytest
    

License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_ai_nova-0.2.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_ai_nova-0.2.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_ai_nova-0.2.0.tar.gz.

File metadata

  • Download URL: pydantic_ai_nova-0.2.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for pydantic_ai_nova-0.2.0.tar.gz
Algorithm Hash digest
SHA256 83d38fcfb2bc4b43823ef757f0b8822ec08cef94772ed4d6fec9c329653b9fba
MD5 d57b74a989a8f397eb94c2bcf67e437d
BLAKE2b-256 fd8b1e345858143af5e420ec4063ef721a203731c4d702f9fac220d4d5f6f804

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_ai_nova-0.2.0.tar.gz:

Publisher: workflow.yml on speetchai/speetchai-pydantic-ai-nova

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pydantic_ai_nova-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_ai_nova-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 90129f5e5f760155e2a28a09c2bec86cc972b35e51b7e29ae9486538e1f52aed
MD5 63fa84ccb1d72af29fd61b046daa1453
BLAKE2b-256 fcd2441eb69a6ad0e2e91ffc3f22f0a94abe2750b55db9903a12aca166c0bbbd

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_ai_nova-0.2.0-py3-none-any.whl:

Publisher: workflow.yml on speetchai/speetchai-pydantic-ai-nova

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page