Skip to content

Commit

Permalink
import examples in CI, and other tweaks (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin authored Oct 28, 2024
1 parent 6d144eb commit 4ac52b3
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ jobs:
env:
COVERAGE_FILE: coverage/.coverage.${{ runner.os }}-py${{ matrix.python-version }}-all-extras

- run: uv run --frozen --all-extras --python ${{ matrix.python-version }} python tests/import_examples.py

- name: store coverage files
uses: actions/upload-artifact@v4
with:
Expand Down
15 changes: 5 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,11 @@ test:

.PHONY: test-all-python # Run tests on Python 3.9 to 3.13
test-all-python:
UV_PROJECT_ENVIRONMENT=.venv39 uv run --python 3.9 coverage run -m pytest
@mv .coverage .coverage.3.9
UV_PROJECT_ENVIRONMENT=.venv310 uv run --python 3.10 coverage run -m pytest
@mv .coverage .coverage.3.10
UV_PROJECT_ENVIRONMENT=.venv311 uv run --python 3.11 coverage run -m pytest
@mv .coverage .coverage.3.11
UV_PROJECT_ENVIRONMENT=.venv312 uv run --python 3.12 coverage run -m pytest
@mv .coverage .coverage.3.12
UV_PROJECT_ENVIRONMENT=.venv313 uv run --python 3.13 coverage run -m pytest
@mv .coverage .coverage.3.13
UV_PROJECT_ENVIRONMENT=.venv39 uv run --python 3.9 coverage run -p -m pytest
UV_PROJECT_ENVIRONMENT=.venv310 uv run --python 3.10 coverage run -p -m pytest
UV_PROJECT_ENVIRONMENT=.venv311 uv run --python 3.11 coverage run -p -m pytest
UV_PROJECT_ENVIRONMENT=.venv312 uv run --python 3.12 coverage run -p -m pytest
UV_PROJECT_ENVIRONMENT=.venv313 uv run --python 3.13 coverage run -p -m pytest
@uv run coverage combine
@uv run coverage report

Expand Down
8 changes: 3 additions & 5 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,17 @@ the search tool as a retriever with the Pydantic AI agent.
Logic for extracting sections from markdown files and a JSON file with that data is available in
[this gist](https://gist.github.com/samuelcolvin/4b5bb9bb163b1122ff17e29e48c10992).

[PostgreSQL with pgvector](https://github.com/pgvector/pgvector) is used as the search database.

The easiest way to download and run pgvector is using Docker:
[PostgreSQL with pgvector](https://github.com/pgvector/pgvector) is used as the search database, the easiest way to download and run pgvector is using Docker:

```bash
mkdir postgres-data
docker run --rm -e POSTGRES_PASSWORD=postgres -p 54320:5432 -v `pwd`/postgres-data:/var/lib/postgresql/data pgvector/pgvector:pg17
```

We run postgres port `54320` to avoid conflicts with any other postgres instances you may have running.
We run postgres on port `54320` to avoid conflicts with any other postgres instances you may have running.
We also mount the postgresql `data` directory locally to persist the data if you need to stop and restart the container.

Wit that running, we can then build the search database with (**WARNING**: this requires `OPENAI_API_KEY` and will calling the OpenAI embedding API around 300 times to generate embeddings for each section of the documentation):
With that running, we can build the search database with (**WARNING**: this requires the `OPENAI_API_KEY` env variable and will calling the OpenAI embedding API around 300 times to generate embeddings for each section of the documentation):

```bash
uv run --extra examples -m examples.rag build
Expand Down
2 changes: 1 addition & 1 deletion examples/rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from pydantic_ai.agent import Agent

# 'if-token-present' means nothing will be sent (and the example wil work) if you don't have logfire set up
logfire.configure()
logfire.configure(send_to_logfire='if-token-present')
logfire.instrument_asyncpg()


Expand Down
2 changes: 1 addition & 1 deletion examples/sql_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from pydantic_ai import Agent, CallContext, ModelRetry

# 'if-token-present' means nothing will be sent (and the example wil work) if you don't have logfire set up
logfire.configure()
logfire.configure(send_to_logfire='if-token-present')
logfire.instrument_asyncpg()

DB_SCHEMA = """
Expand Down
4 changes: 3 additions & 1 deletion examples/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
uv run --extra examples -m examples.weather
"""

from __future__ import annotations as _annotations

import asyncio
import os
from dataclasses import dataclass
Expand Down Expand Up @@ -116,7 +118,7 @@ async def get_weather(ctx: CallContext[Deps], lat: float, lng: float) -> dict[st
8000: 'Thunderstorm',
}
return {
'temperature': f'{values['temperatureApparent']:0.0f}°C',
'temperature': f'{values["temperatureApparent"]:0.0f}°C',
'description': code_lookup.get(values['weatherCode'], 'Unknown'),
}

Expand Down
3 changes: 1 addition & 2 deletions pydantic_ai/_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from pydantic import ConfigDict, TypeAdapter
from pydantic._internal import _decorators, _generate_schema, _typing_extra
from pydantic._internal._config import ConfigWrapper
from pydantic._internal._typing_extra import origin_is_union
from pydantic.fields import FieldInfo
from pydantic.json_schema import GenerateJsonSchema
from pydantic.plugin._schema_validator import create_schema_validator
Expand All @@ -32,7 +31,7 @@

def is_union(tp: Any) -> bool:
origin = get_origin(tp)
return origin_is_union(origin)
return _typing_extra.origin_is_union(origin)


class FunctionSchema(TypedDict):
Expand Down
14 changes: 14 additions & 0 deletions tests/import_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
Used to check that examples are at least valid syntax and can be imported without errors.
Called in CI.
"""

import os
from pathlib import Path

os.environ.update(OPENAI_API_KEY='fake-key', GEMINI_API_KEY='fake-key')

examples_dir = Path(__file__).parent.parent / 'examples'
for example in examples_dir.glob('*.py'):
__import__(f'examples.{example.stem}')

0 comments on commit 4ac52b3

Please sign in to comment.