|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Pydantic-AI Tools Integration Example |
| 4 | +
|
| 5 | +This example demonstrates how to use pydantic-ai Tool and FunctionToolset objects |
| 6 | +with proompt's PromptSection. It shows: |
| 7 | +- Creating pydantic-ai Tool objects |
| 8 | +- Creating a FunctionToolset |
| 9 | +- Mixing ToolContext and pydantic-ai tools |
| 10 | +- Using tools in a PromptSection |
| 11 | +""" |
| 12 | + |
| 13 | +from textwrap import dedent, indent |
| 14 | + |
| 15 | +from pydantic_ai import FunctionToolset, RunContext, Tool |
| 16 | + |
| 17 | +from proompt.base.context import ToolContext |
| 18 | +from proompt.base.prompt import BasePrompt, PromptSection |
| 19 | + |
| 20 | +# ===== DEFINE SOME TOOLS USING PYDANTIC-AI ===== |
| 21 | + |
| 22 | + |
| 23 | +def search_documents(query: str, max_results: int = 5) -> str: |
| 24 | + """Search through company documents.""" |
| 25 | + return f"Found {max_results} documents matching '{query}'" |
| 26 | + |
| 27 | + |
| 28 | +def get_company_metrics(metric_type: str) -> dict: |
| 29 | + """Retrieve company metrics.""" |
| 30 | + return {"revenue": 1000000, "users": 50000, "growth": 0.15} |
| 31 | + |
| 32 | + |
| 33 | +def calculate_percentage(numerator: float, denominator: float) -> float: |
| 34 | + """Calculate percentage from two numbers.""" |
| 35 | + if denominator == 0: |
| 36 | + return 0.0 |
| 37 | + return (numerator / denominator) * 100 |
| 38 | + |
| 39 | + |
| 40 | +# Create pydantic-ai Tool objects |
| 41 | +search_tool = Tool(search_documents, takes_ctx=False) |
| 42 | +metrics_tool = Tool(get_company_metrics, takes_ctx=False) |
| 43 | +calc_tool = Tool(calculate_percentage, takes_ctx=False) |
| 44 | + |
| 45 | +# Create a FunctionToolset |
| 46 | +analysis_toolset = FunctionToolset(tools=[search_tool, metrics_tool]) |
| 47 | + |
| 48 | + |
| 49 | +# You can also add tools using the decorator |
| 50 | +@analysis_toolset.tool |
| 51 | +def summarize_data(ctx: RunContext, data: str) -> str: |
| 52 | + """Summarize the provided data.""" |
| 53 | + return f"Summary of {len(data)} characters of data" |
| 54 | + |
| 55 | + |
| 56 | +# ===== DEFINE A TRADITIONAL PROOMPT TOOL ===== |
| 57 | + |
| 58 | + |
| 59 | +def format_report(data: dict) -> str: |
| 60 | + """Format data into a readable report.""" |
| 61 | + return "\n".join(f"{k}: {v}" for k, v in data.items()) |
| 62 | + |
| 63 | + |
| 64 | +proompt_tool = ToolContext(format_report) |
| 65 | + |
| 66 | + |
| 67 | +# ===== CREATE A PROMPT SECTION WITH MIXED TOOLS ===== |
| 68 | + |
| 69 | + |
| 70 | +class AnalysisSection(PromptSection): |
| 71 | + """A section that uses both proompt and pydantic-ai tools.""" |
| 72 | + |
| 73 | + def formatter(self) -> str: |
| 74 | + tools_list = "\n" + "\n".join(f"- {tool.tool_name}: {tool.tool_description}" for tool in self.tools) |
| 75 | + return dedent(f"""\ |
| 76 | + ## ANALYSIS TOOLS |
| 77 | + |
| 78 | + You have access to the following tools: |
| 79 | + {indent(tools_list, " " * 12)} |
| 80 | + |
| 81 | + Use these tools to gather and analyze data for your report. |
| 82 | + """).strip() |
| 83 | + |
| 84 | + def render(self) -> str: |
| 85 | + return self.formatter() |
| 86 | + |
| 87 | + |
| 88 | +# ===== CREATE THE PROMPT ===== |
| 89 | + |
| 90 | + |
| 91 | +class MixedToolsPrompt(BasePrompt): |
| 92 | + """Example prompt using both proompt and pydantic-ai tools.""" |
| 93 | + |
| 94 | + def render(self) -> str: |
| 95 | + return "\n\n".join(section.render() for section in self.sections) |
| 96 | + |
| 97 | + |
| 98 | +# ===== DEMONSTRATE THE INTEGRATION ===== |
| 99 | + |
| 100 | + |
| 101 | +def main(): |
| 102 | + print("=" * 80) |
| 103 | + print("Pydantic-AI Tools Integration Demo") |
| 104 | + print("=" * 80) |
| 105 | + print() |
| 106 | + |
| 107 | + # Method 1: Pass individual pydantic-ai Tool objects |
| 108 | + print("Method 1: Individual Tool objects") |
| 109 | + print("-" * 80) |
| 110 | + section1 = AnalysisSection(tools=[search_tool, metrics_tool, calc_tool]) |
| 111 | + print(f"Tools in section: {len(section1.tools)}") |
| 112 | + print() |
| 113 | + |
| 114 | + # Method 2: Pass a FunctionToolset (tools are extracted automatically) |
| 115 | + print("Method 2: FunctionToolset") |
| 116 | + print("-" * 80) |
| 117 | + section2 = AnalysisSection(tools=[analysis_toolset]) |
| 118 | + print(f"Tools in section: {len(section2.tools)}") |
| 119 | + print("Tool names:", [t.tool_name for t in section2.tools]) |
| 120 | + print() |
| 121 | + |
| 122 | + # Method 3: Mix ToolContext and pydantic-ai tools |
| 123 | + print("Method 3: Mixed tools (ToolContext + pydantic-ai Tool + FunctionToolset)") |
| 124 | + print("-" * 80) |
| 125 | + section3 = AnalysisSection( |
| 126 | + tools=[ |
| 127 | + proompt_tool, # Traditional proompt tool |
| 128 | + calc_tool, # Individual pydantic-ai Tool |
| 129 | + analysis_toolset, # FunctionToolset (extracts multiple tools) |
| 130 | + ] |
| 131 | + ) |
| 132 | + print(f"Tools in section: {len(section3.tools)}") |
| 133 | + print("Tool names:", [t.tool_name for t in section3.tools]) |
| 134 | + print() |
| 135 | + |
| 136 | + # Create a full prompt |
| 137 | + print("Full Prompt Output:") |
| 138 | + print("=" * 80) |
| 139 | + prompt = MixedToolsPrompt(section3) |
| 140 | + print(prompt.render()) |
| 141 | + print() |
| 142 | + |
| 143 | + # Demonstrate add_tools method |
| 144 | + print("Using add_tools method:") |
| 145 | + print("-" * 80) |
| 146 | + section4 = AnalysisSection() |
| 147 | + print(f"Initial tools: {len(section4.tools)}") |
| 148 | + |
| 149 | + section4.add_tools(proompt_tool) |
| 150 | + print(f"After adding ToolContext: {len(section4.tools)}") |
| 151 | + |
| 152 | + section4.add_tools(search_tool, metrics_tool) |
| 153 | + print(f"After adding pydantic-ai Tools: {len(section4.tools)}") |
| 154 | + |
| 155 | + section4.add_tools(analysis_toolset) |
| 156 | + print(f"After adding FunctionToolset: {len(section4.tools)}") |
| 157 | + |
| 158 | + print("Final tool names:", [t.tool_name for t in section4.tools]) |
| 159 | + print() |
| 160 | + |
| 161 | + # Show tool rendering |
| 162 | + print("Individual Tool Rendering:") |
| 163 | + print("=" * 80) |
| 164 | + for tool in section3.tools[:3]: # Show first 3 tools |
| 165 | + print(f"\n{tool.tool_name}:") |
| 166 | + print("-" * 40) |
| 167 | + print(tool) # automatically uses __str__() to render |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == "__main__": |
| 171 | + main() |
0 commit comments