pydantic-ai-examples 0.4.10__py3-none-any.whl → 0.4.11__py3-none-any.whl
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.
- pydantic_ai_examples/data_analyst.py +107 -0
- {pydantic_ai_examples-0.4.10.dist-info → pydantic_ai_examples-0.4.11.dist-info}/METADATA +6 -3
- {pydantic_ai_examples-0.4.10.dist-info → pydantic_ai_examples-0.4.11.dist-info}/RECORD +5 -4
- {pydantic_ai_examples-0.4.10.dist-info → pydantic_ai_examples-0.4.11.dist-info}/WHEEL +0 -0
- {pydantic_ai_examples-0.4.10.dist-info → pydantic_ai_examples-0.4.11.dist-info}/licenses/LICENSE +0 -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)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pydantic-ai-examples
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.11
|
|
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:
|
|
36
|
-
Requires-Dist: pydantic-
|
|
37
|
+
Requires-Dist: pandas>=2.2.3
|
|
38
|
+
Requires-Dist: pydantic-ai-slim[ag-ui,anthropic,groq,openai,vertexai]==0.4.11
|
|
39
|
+
Requires-Dist: pydantic-evals==0.4.11
|
|
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
|
|
@@ -3,6 +3,7 @@ pydantic_ai_examples/bank_support.py,sha256=vJL2zLEq19OztP1fUGG7_6cYHllvxvzkafFM
|
|
|
3
3
|
pydantic_ai_examples/chat_app.html,sha256=90XhxrpDAT09mPVTn9edEn8PqAD-tHxWkeeMz9r_okQ,2580
|
|
4
4
|
pydantic_ai_examples/chat_app.py,sha256=zrurOhwbjPDTYi9FkYZF3wMxc2AT9PFckM-xngQ5188,7105
|
|
5
5
|
pydantic_ai_examples/chat_app.ts,sha256=2KfZ2rJU2o0iCPjelyqEi5sH6vfemzWaa5Evx_VcAE4,3307
|
|
6
|
+
pydantic_ai_examples/data_analyst.py,sha256=vSpfIWpxNgaZqKx2DJD721ZA3QgSjaZkTixy5OHz9xY,3753
|
|
6
7
|
pydantic_ai_examples/flight_booking.py,sha256=hwz1ct0laIZQCb0cuhFYyrR-PBDUL2zc4CS39ytOwn0,7454
|
|
7
8
|
pydantic_ai_examples/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
9
|
pydantic_ai_examples/pydantic_model.py,sha256=EQsHBig2bvb0PG_2XDgP9Le5xJ4n8eZJzQDGQYhDykg,775
|
|
@@ -43,7 +44,7 @@ pydantic_ai_examples/slack_lead_qualifier/modal.py,sha256=f464AaeyP-n3UIfvEVVc4D
|
|
|
43
44
|
pydantic_ai_examples/slack_lead_qualifier/models.py,sha256=WTp6D2WCASXqrjPVT3vGgTSYATLPBM3_cjq9wvXMRao,1586
|
|
44
45
|
pydantic_ai_examples/slack_lead_qualifier/slack.py,sha256=VJVfMeUXYClWUJBLHNuaW8PB2sxjNzpTC-O_AJwcxQ4,833
|
|
45
46
|
pydantic_ai_examples/slack_lead_qualifier/store.py,sha256=04vB4eZWKk_Tx0b9K4QuVI1U24JEyJyBS4X76cui7OI,896
|
|
46
|
-
pydantic_ai_examples-0.4.
|
|
47
|
-
pydantic_ai_examples-0.4.
|
|
48
|
-
pydantic_ai_examples-0.4.
|
|
49
|
-
pydantic_ai_examples-0.4.
|
|
47
|
+
pydantic_ai_examples-0.4.11.dist-info/METADATA,sha256=Docxd8tyYNI3U9WesiQTEEx2-IjM2VEK1mK5iZFS2xI,2846
|
|
48
|
+
pydantic_ai_examples-0.4.11.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
49
|
+
pydantic_ai_examples-0.4.11.dist-info/licenses/LICENSE,sha256=vA6Jc482lEyBBuGUfD1pYx-cM7jxvLYOxPidZ30t_PQ,1100
|
|
50
|
+
pydantic_ai_examples-0.4.11.dist-info/RECORD,,
|
|
File without changes
|
{pydantic_ai_examples-0.4.10.dist-info → pydantic_ai_examples-0.4.11.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|