Skip to content

Commit f704792

Browse files
docs: add Pydantic AI notebook example
1 parent 17256c9 commit f704792

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "2722b419",
6+
"metadata": {},
7+
"source": [
8+
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/openlayer-ai/openlayer-python/blob/main/examples/tracing/pydantic-ai/pydantic_ai_tracing.ipynb)\n",
9+
"\n",
10+
"\n",
11+
"# <a id=\"top\">Pydantic AI quickstart</a>\n",
12+
"\n",
13+
"This notebook shows how to trace Pydantic AI Agents with Openlayer. The integration is done via the Openlayer's [OpenTelemetry endpoint](https://www.openlayer.com/docs/integrations/opentelemetry)."
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"id": "020c8f6a",
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"!pip install pydantic-ai logfire"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"id": "75c2a473",
29+
"metadata": {},
30+
"source": [
31+
"## 1. Set the environment variables"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 1,
37+
"id": "f3f4fa13",
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"import os\n",
42+
"\n",
43+
"os.environ[\"OPENAI_API_KEY\"] = \"YOUR_OPENAI_API_KEY_HERE\"\n",
44+
"\n",
45+
"os.environ[\"OTEL_EXPORTER_OTLP_ENDPOINT\"] = \"https://api.openlayer.com/v1/otel\"\n",
46+
"os.environ[\"OTEL_EXPORTER_OTLP_HEADERS\"] = \"Authorization=Bearer YOUR_OPENLAYER_API_KEY_HERE, x-bt-parent=pipeline_id:YOUR_OPENLAYER_PIPELINE_ID_HERE\""
47+
]
48+
},
49+
{
50+
"cell_type": "markdown",
51+
"id": "9758533f",
52+
"metadata": {},
53+
"source": [
54+
"## 2. Configure Logfire instrumentation (used by Pydantic AI)"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 2,
60+
"id": "c35d9860-dc41-4f7c-8d69-cc2ac7e5e485",
61+
"metadata": {},
62+
"outputs": [],
63+
"source": [
64+
"import logfire\n",
65+
"\n",
66+
"logfire.configure(send_to_logfire=False)\n",
67+
"logfire.instrument_pydantic_ai()"
68+
]
69+
},
70+
{
71+
"cell_type": "markdown",
72+
"id": "72a6b954",
73+
"metadata": {},
74+
"source": [
75+
"## 3. Use Agents as usual\n",
76+
"\n",
77+
"That's it! Now you can continue using Agents as usual. The trace data is automatically exported to Openlayer and you can start creating tests around it."
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 3,
83+
"id": "e00c1c79",
84+
"metadata": {},
85+
"outputs": [],
86+
"source": [
87+
"from pydantic_ai import Agent"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": null,
93+
"id": "253a3ac8",
94+
"metadata": {},
95+
"outputs": [],
96+
"source": [
97+
"agent = Agent('openai:gpt-4o')\n",
98+
"result = await agent.run('What is the capital of France?')"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": null,
104+
"id": "d1c37cfe",
105+
"metadata": {},
106+
"outputs": [],
107+
"source": []
108+
}
109+
],
110+
"metadata": {
111+
"kernelspec": {
112+
"display_name": "crewai-test",
113+
"language": "python",
114+
"name": "python3"
115+
},
116+
"language_info": {
117+
"codemirror_mode": {
118+
"name": "ipython",
119+
"version": 3
120+
},
121+
"file_extension": ".py",
122+
"mimetype": "text/x-python",
123+
"name": "python",
124+
"nbconvert_exporter": "python",
125+
"pygments_lexer": "ipython3",
126+
"version": "3.9.19"
127+
}
128+
},
129+
"nbformat": 4,
130+
"nbformat_minor": 5
131+
}

0 commit comments

Comments
 (0)