Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agentic architectures #48

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Python Tests

on:
push:
branches: [ "gorilla" ]
pull_request:
branches: [ "gorilla" ]

jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.9'

- name: Install dependencies
run: pip install -r requirements.txt

# Step 4: Run tests
- name: Run pytest
run: pytest
186 changes: 186 additions & 0 deletions notebooks/pydantic-ai.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"🔭 OpenTelemetry Tracing Details 🔭\n",
"| Phoenix Project: default\n",
"| Span Processor: SimpleSpanProcessor\n",
"| Collector Endpoint: https://app.phoenix.arize.com/v1/traces\n",
"| Transport: HTTP\n",
"| Transport Headers: {'api_key': '****'}\n",
"| \n",
"| Using a default SpanProcessor. `add_span_processor` will overwrite this default.\n",
"| \n",
"| `register` has set this TracerProvider as the global OpenTelemetry default.\n",
"| To disable this behavior, call `register` with `set_global_tracer_provider=False`.\n",
"\n"
]
}
],
"source": [
"import os\n",
"from phoenix.otel import register\n",
"\n",
"# Add Phoenix API Key for tracing\n",
"PHOENIX_API_KEY = os.getenv(\"PHOENIX_API_KEY\")\n",
"os.environ[\"PHOENIX_CLIENT_HEADERS\"] = f\"api_key={PHOENIX_API_KEY}\"\n",
"\n",
"# configure the Phoenix tracer\n",
"tracer_provider = register(\n",
" endpoint=\"https://app.phoenix.arize.com/v1/traces\",\n",
") "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from openinference.instrumentation.openai import OpenAIInstrumentor\n",
"\n",
"OpenAIInstrumentor().instrument(tracer_provider=tracer_provider)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Failed to export batch code: 204, reason: \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"There are 5,280 feet in a mile.\n"
]
}
],
"source": [
"from pydantic_ai import Agent\n",
"\n",
"agent = Agent( \n",
" 'openai:gpt-4o',\n",
" system_prompt='Be concise, reply with one sentence.', \n",
")\n",
"\n",
"result = await agent.run('How many feet are in a mile?') \n",
"print(result.data)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Failed to export batch code: 204, reason: \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"greeting=\"Hello, Alice! It's great to see you!\"\n"
]
}
],
"source": [
"import nest_asyncio\n",
"nest_asyncio.apply()\n",
"\n",
"import asyncio\n",
"from dataclasses import dataclass\n",
"\n",
"from pydantic import BaseModel, Field\n",
"from pydantic_ai import Agent, RunContext\n",
"\n",
"\n",
"@dataclass\n",
"class NameContext:\n",
" \"\"\"Dependencies (context) for the conversation.\"\"\"\n",
" user_name: str\n",
"\n",
"\n",
"class GreetingResult(BaseModel):\n",
" \"\"\"Structured output from the AI.\"\"\"\n",
" greeting: str = Field(description=\"A short greeting to the user\")\n",
"\n",
"\n",
"greeting_agent = Agent(\n",
" model=\"openai:gpt-4o\",\n",
" deps_type=NameContext,\n",
" result_type=GreetingResult,\n",
" system_prompt=(\n",
" \"You are a personalized greeter AI. \"\n",
" \"Return a short greeting for the user.\"\n",
" ),\n",
")\n",
"\n",
"\n",
"@greeting_agent.system_prompt\n",
"async def add_user_name(ctx: RunContext[NameContext]) -> str:\n",
" return f\"The user's name is {ctx.deps.user_name!r}.\"\n",
"\n",
"\n",
"async def main():\n",
" deps = NameContext(user_name=\"Alice\")\n",
"\n",
" result = await greeting_agent.run(\n",
" \"Hi, can you greet me?\",\n",
" deps=deps\n",
" )\n",
"\n",
" print(result.data)\n",
"\n",
"await main()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"{\"messages\": [{\"role\": \"system\", \"content\": \"You are a personalized greeter AI. Return a short greeting for the user.\"}, {\"role\": \"system\", \"content\": \"The user's name is 'Alice'.\"}, {\"role\": \"user\", \"content\": \"Hi, can you greet me?\"}], \"model\": \"gpt-4o\", \"n\": 1, \"parallel_tool_calls\": true, \"stream\": false, \"tool_choice\": \"required\", \"tools\": [{\"type\": \"function\", \"function\": {\"name\": \"final_result\", \"description\": \"Structured output from the AI.\", \"parameters\": {\"properties\": {\"greeting\": {\"description\": \"A short greeting to the user\", \"title\": \"Greeting\", \"type\": \"string\"}}, \"required\": [\"greeting\"], \"title\": \"GreetingResult\", \"type\": \"object\"}}}]}\n",
"```"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==8.3.4
150 changes: 0 additions & 150 deletions src/lm/test-lm.py

This file was deleted.

Empty file added tests/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions tests/test_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# tests/test_tests.py

import sys

def test_basic_math():
"""
A trivial test to verify pytest is running.
"""
assert 1 + 1 == 2, "Basic math failed, so something is off!"

def test_python_version():
"""
This test checks if the Python version is at least 3.8.
Adjust the version requirement as needed.
"""
major, minor = sys.version_info[:2]
assert (major == 3 and minor >= 8), (
f"Expected Python 3.8+ but found Python {major}.{minor}."
)

def test_pytest_is_installed():
"""
A quick sanity check to ensure 'pytest' is recognized.
The fact that we're running this test at all typically
confirms pytest is installed, but let's be explicit.
"""
try:
import pytest # noqa: F401 # just to check import
except ImportError as e:
assert False, f"pytest not installed or not found: {e}"
Loading