diff --git a/examples/tracing/pydantic-ai/pydantic_ai_tracing.ipynb b/examples/tracing/pydantic-ai/pydantic_ai_tracing.ipynb new file mode 100644 index 00000000..5a2c16c9 --- /dev/null +++ b/examples/tracing/pydantic-ai/pydantic_ai_tracing.ipynb @@ -0,0 +1,131 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "2722b419", + "metadata": {}, + "source": [ + "[![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", + "\n", + "\n", + "# Pydantic AI quickstart\n", + "\n", + "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)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "020c8f6a", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install pydantic-ai logfire" + ] + }, + { + "cell_type": "markdown", + "id": "75c2a473", + "metadata": {}, + "source": [ + "## 1. Set the environment variables" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "f3f4fa13", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "\n", + "os.environ[\"OPENAI_API_KEY\"] = \"YOUR_OPENAI_API_KEY_HERE\"\n", + "\n", + "os.environ[\"OTEL_EXPORTER_OTLP_ENDPOINT\"] = \"https://api.openlayer.com/v1/otel\"\n", + "os.environ[\"OTEL_EXPORTER_OTLP_HEADERS\"] = \"Authorization=Bearer YOUR_OPENLAYER_API_KEY_HERE, x-bt-parent=pipeline_id:YOUR_OPENLAYER_PIPELINE_ID_HERE\"" + ] + }, + { + "cell_type": "markdown", + "id": "9758533f", + "metadata": {}, + "source": [ + "## 2. Configure Logfire instrumentation (used by Pydantic AI)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "c35d9860-dc41-4f7c-8d69-cc2ac7e5e485", + "metadata": {}, + "outputs": [], + "source": [ + "import logfire\n", + "\n", + "logfire.configure(send_to_logfire=False)\n", + "logfire.instrument_pydantic_ai()" + ] + }, + { + "cell_type": "markdown", + "id": "72a6b954", + "metadata": {}, + "source": [ + "## 3. Use Agents as usual\n", + "\n", + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "e00c1c79", + "metadata": {}, + "outputs": [], + "source": [ + "from pydantic_ai import Agent" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "253a3ac8", + "metadata": {}, + "outputs": [], + "source": [ + "agent = Agent('openai:gpt-4o')\n", + "result = await agent.run('What is the capital of France?')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d1c37cfe", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "crewai-test", + "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.9.19" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}