pydantic-ai-examples 0.4.10__tar.gz → 0.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pydantic-ai-examples might be problematic. Click here for more details.

Files changed (51) hide show
  1. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/PKG-INFO +6 -3
  2. pydantic_ai_examples-0.5.0/pydantic_ai_examples/data_analyst.py +107 -0
  3. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pyproject.toml +3 -0
  4. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/.gitignore +0 -0
  5. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/LICENSE +0 -0
  6. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/README.md +0 -0
  7. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/__main__.py +0 -0
  8. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/ag_ui/__init__.py +0 -0
  9. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/ag_ui/__main__.py +0 -0
  10. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/ag_ui/api/__init__.py +0 -0
  11. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/ag_ui/api/agentic_chat.py +0 -0
  12. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/ag_ui/api/agentic_generative_ui.py +0 -0
  13. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/ag_ui/api/human_in_the_loop.py +0 -0
  14. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/ag_ui/api/predictive_state_updates.py +0 -0
  15. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/ag_ui/api/shared_state.py +0 -0
  16. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/ag_ui/api/tool_based_generative_ui.py +0 -0
  17. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/bank_support.py +0 -0
  18. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/chat_app.html +0 -0
  19. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/chat_app.py +0 -0
  20. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/chat_app.ts +0 -0
  21. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/evals/__init__.py +0 -0
  22. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/evals/agent.py +0 -0
  23. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/evals/custom_evaluators.py +0 -0
  24. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/evals/datasets/time_range_v1.yaml +0 -0
  25. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/evals/datasets/time_range_v1_schema.json +0 -0
  26. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/evals/datasets/time_range_v2.yaml +0 -0
  27. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/evals/datasets/time_range_v2_schema.json +0 -0
  28. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/evals/example_01_generate_dataset.py +0 -0
  29. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/evals/example_02_add_custom_evaluators.py +0 -0
  30. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/evals/example_03_unit_testing.py +0 -0
  31. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/evals/example_04_compare_models.py +0 -0
  32. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/evals/models.py +0 -0
  33. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/flight_booking.py +0 -0
  34. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/py.typed +0 -0
  35. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/pydantic_model.py +0 -0
  36. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/question_graph.py +0 -0
  37. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/rag.py +0 -0
  38. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/roulette_wheel.py +0 -0
  39. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/slack_lead_qualifier/__init__.py +0 -0
  40. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/slack_lead_qualifier/agent.py +0 -0
  41. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/slack_lead_qualifier/app.py +0 -0
  42. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/slack_lead_qualifier/functions.py +0 -0
  43. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/slack_lead_qualifier/modal.py +0 -0
  44. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/slack_lead_qualifier/models.py +0 -0
  45. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/slack_lead_qualifier/slack.py +0 -0
  46. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/slack_lead_qualifier/store.py +0 -0
  47. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/sql_gen.py +0 -0
  48. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/stream_markdown.py +0 -0
  49. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/stream_whales.py +0 -0
  50. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/weather_agent.py +0 -0
  51. {pydantic_ai_examples-0.4.10 → pydantic_ai_examples-0.5.0}/pydantic_ai_examples/weather_agent_gradio.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pydantic-ai-examples
3
- Version: 0.4.10
3
+ Version: 0.5.0
4
4
  Summary: Examples of how to use Pydantic AI and what it can do.
5
5
  Author-email: Samuel Colvin <samuel@pydantic.dev>, Marcelo Trylesinski <marcelotryle@gmail.com>, David Montague <david@pydantic.dev>, Alex Hall <alex@pydantic.dev>, Douwe Maan <douwe@pydantic.dev>
6
6
  License-Expression: MIT
@@ -26,14 +26,17 @@ Classifier: Topic :: Internet
26
26
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
27
27
  Requires-Python: >=3.9
28
28
  Requires-Dist: asyncpg>=0.30.0
29
+ Requires-Dist: datasets>=4.0.0
29
30
  Requires-Dist: devtools>=0.12.2
31
+ Requires-Dist: duckdb>=1.3.2
30
32
  Requires-Dist: fastapi>=0.115.4
31
33
  Requires-Dist: gradio>=5.9.0; python_version > '3.9'
32
34
  Requires-Dist: logfire[asyncpg,fastapi,httpx,sqlite3]>=2.6
33
35
  Requires-Dist: mcp[cli]>=1.4.1; python_version >= '3.10'
34
36
  Requires-Dist: modal>=1.0.4
35
- Requires-Dist: pydantic-ai-slim[ag-ui,anthropic,groq,openai,vertexai]==0.4.10
36
- Requires-Dist: pydantic-evals==0.4.10
37
+ Requires-Dist: pandas>=2.2.3
38
+ Requires-Dist: pydantic-ai-slim[ag-ui,anthropic,groq,openai,vertexai]==0.5.0
39
+ Requires-Dist: pydantic-evals==0.5.0
37
40
  Requires-Dist: python-multipart>=0.0.17
38
41
  Requires-Dist: rich>=13.9.2
39
42
  Requires-Dist: uvicorn>=0.32.0
@@ -0,0 +1,107 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ import datasets
4
+ import duckdb
5
+ import pandas as pd
6
+
7
+ from pydantic_ai import Agent, ModelRetry, RunContext
8
+
9
+
10
+ @dataclass
11
+ class AnalystAgentDeps:
12
+ output: dict[str, pd.DataFrame] = field(default_factory=dict)
13
+
14
+ def store(self, value: pd.DataFrame) -> str:
15
+ """Store the output in deps and return the reference such as Out[1] to be used by the LLM."""
16
+ ref = f'Out[{len(self.output) + 1}]'
17
+ self.output[ref] = value
18
+ return ref
19
+
20
+ def get(self, ref: str) -> pd.DataFrame:
21
+ if ref not in self.output:
22
+ raise ModelRetry(
23
+ f'Error: {ref} is not a valid variable reference. Check the previous messages and try again.'
24
+ )
25
+ return self.output[ref]
26
+
27
+
28
+ analyst_agent = Agent(
29
+ 'openai:gpt-4o',
30
+ deps_type=AnalystAgentDeps,
31
+ instructions='You are a data analyst and your job is to analyze the data according to the user request.',
32
+ )
33
+
34
+
35
+ @analyst_agent.tool
36
+ def load_dataset(
37
+ ctx: RunContext[AnalystAgentDeps],
38
+ path: str,
39
+ split: str = 'train',
40
+ ) -> str:
41
+ """Load the `split` of dataset `dataset_name` from huggingface.
42
+
43
+ Args:
44
+ ctx: Pydantic AI agent RunContext
45
+ path: name of the dataset in the form of `<user_name>/<dataset_name>`
46
+ split: load the split of the dataset (default: "train")
47
+ """
48
+ # begin load data from hf
49
+ builder = datasets.load_dataset_builder(path) # pyright: ignore[reportUnknownMemberType]
50
+ splits: dict[str, datasets.SplitInfo] = builder.info.splits or {} # pyright: ignore[reportUnknownMemberType]
51
+ if split not in splits:
52
+ raise ModelRetry(
53
+ f'{split} is not valid for dataset {path}. Valid splits are {",".join(splits.keys())}'
54
+ )
55
+
56
+ builder.download_and_prepare() # pyright: ignore[reportUnknownMemberType]
57
+ dataset = builder.as_dataset(split=split)
58
+ assert isinstance(dataset, datasets.Dataset)
59
+ dataframe = dataset.to_pandas()
60
+ assert isinstance(dataframe, pd.DataFrame)
61
+ # end load data from hf
62
+
63
+ # store the dataframe in the deps and get a ref like "Out[1]"
64
+ ref = ctx.deps.store(dataframe)
65
+ # construct a summary of the loaded dataset
66
+ output = [
67
+ f'Loaded the dataset as `{ref}`.',
68
+ f'Description: {dataset.info.description}'
69
+ if dataset.info.description
70
+ else None,
71
+ f'Features: {dataset.info.features!r}' if dataset.info.features else None,
72
+ ]
73
+ return '\n'.join(filter(None, output))
74
+
75
+
76
+ @analyst_agent.tool
77
+ def run_duckdb(ctx: RunContext[AnalystAgentDeps], dataset: str, sql: str) -> str:
78
+ """Run DuckDB SQL query on the DataFrame.
79
+
80
+ Note that the virtual table name used in DuckDB SQL must be `dataset`.
81
+
82
+ Args:
83
+ ctx: Pydantic AI agent RunContext
84
+ dataset: reference string to the DataFrame
85
+ sql: the query to be executed using DuckDB
86
+ """
87
+ data = ctx.deps.get(dataset)
88
+ result = duckdb.query_df(df=data, virtual_table_name='dataset', sql_query=sql)
89
+ # pass the result as ref (because DuckDB SQL can select many rows, creating another huge dataframe)
90
+ ref = ctx.deps.store(result.df()) # pyright: ignore[reportUnknownMemberType]
91
+ return f'Executed SQL, result is `{ref}`'
92
+
93
+
94
+ @analyst_agent.tool
95
+ def display(ctx: RunContext[AnalystAgentDeps], name: str) -> str:
96
+ """Display at most 5 rows of the dataframe."""
97
+ dataset = ctx.deps.get(name)
98
+ return dataset.head().to_string() # pyright: ignore[reportUnknownMemberType]
99
+
100
+
101
+ if __name__ == '__main__':
102
+ deps = AnalystAgentDeps()
103
+ result = analyst_agent.run_sync(
104
+ user_prompt='Count how many negative comments are there in the dataset `cornell-movie-review-data/rotten_tomatoes`',
105
+ deps=deps,
106
+ )
107
+ print(result.output)
@@ -60,6 +60,9 @@ dependencies = [
60
60
  "gradio>=5.9.0; python_version>'3.9'",
61
61
  "mcp[cli]>=1.4.1; python_version >= '3.10'",
62
62
  "modal>=1.0.4",
63
+ "duckdb>=1.3.2",
64
+ "datasets>=4.0.0",
65
+ "pandas>=2.2.3",
63
66
  ]
64
67
 
65
68
  [tool.hatch.build.targets.wheel]