mirror of
https://github.com/anthropics/claude-cookbooks.git
synced 2025-10-06 01:00:28 +03:00
cookbook for 'Building Effective Agents'
This commit is contained in:
20
patterns/agents/README.md
Normal file
20
patterns/agents/README.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# Building Effective Agents Cookbook
|
||||
|
||||
Reference implementation for [Building Effective Agents](https://anthropic.com/research/building-effective-agents) by Erik Schluntz and Barry Zhang.
|
||||
|
||||
This repository contains example minimal implementations of common agent workflows discussed in the blog:
|
||||
|
||||
- Basic Building Blocks
|
||||
- Prompt Chaining
|
||||
- Routing
|
||||
- Multi-LLM Parallelization
|
||||
- Advanced Workflows
|
||||
- Orchestrator-Subagents
|
||||
- Evaluator-Optimizer
|
||||
|
||||
## Getting Started
|
||||
See the Jupyter notebooks for detailed examples:
|
||||
|
||||
- [Basic Workflows](basic_workflows.ipynb)
|
||||
- [Evaluator-Optimizer Workflow](evaluator_optimizer.ipynb)
|
||||
- [Orchestrator-Workers Workflow](orchestrator_workers.ipynb)
|
||||
806
patterns/agents/basic_workflows.ipynb
Normal file
806
patterns/agents/basic_workflows.ipynb
Normal file
@@ -0,0 +1,806 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Basic Multi-LLM Workflows\n",
|
||||
"\n",
|
||||
"This notebook demonstrates three simple multi-LLM workflows. They trade off cost or latency for potentially improved task performances:\n",
|
||||
"\n",
|
||||
"1. **Prompt-Chaining**: Decomposes a task into sequential subtasks, where each step builds on previous results\n",
|
||||
"2. **Parallelization**: Distributes independent subtasks across multiple LLMs for concurrent processing\n",
|
||||
"3. **Routing**: Dynamically selects specialized LLM paths based on input characteristics\n",
|
||||
"\n",
|
||||
"Note: These are sample implementations meant to demonstrate core concepts - not production code."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from concurrent.futures import ThreadPoolExecutor\n",
|
||||
"from typing import List, Dict, Callable\n",
|
||||
"from util import llm_call, extract_xml"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def chain(input: str, prompts: List[str]) -> str:\n",
|
||||
" \"\"\"Chain multiple LLM calls sequentially, passing results between steps.\"\"\"\n",
|
||||
" result = input\n",
|
||||
" for i, prompt in enumerate(prompts, 1):\n",
|
||||
" print(f\"\\nStep {i}:\")\n",
|
||||
" result = llm_call(f\"{prompt}\\nInput: {result}\")\n",
|
||||
" print(result)\n",
|
||||
" return result\n",
|
||||
"\n",
|
||||
"def parallel(prompt: str, inputs: List[str], n_workers: int = 3) -> List[str]:\n",
|
||||
" \"\"\"Process multiple inputs concurrently with the same prompt.\"\"\"\n",
|
||||
" with ThreadPoolExecutor(max_workers=n_workers) as executor:\n",
|
||||
" futures = [executor.submit(llm_call, f\"{prompt}\\nInput: {x}\") for x in inputs]\n",
|
||||
" return [f.result() for f in futures]\n",
|
||||
"\n",
|
||||
"def route(input: str, routes: Dict[str, str]) -> str:\n",
|
||||
" \"\"\"Route input to specialized prompt using content classification.\"\"\"\n",
|
||||
" # First determine appropriate route using LLM with chain-of-thought\n",
|
||||
" print(f\"\\nAvailable routes: {list(routes.keys())}\")\n",
|
||||
" selector_prompt = f\"\"\"\n",
|
||||
" Analyze the input and select the most appropriate support team from these options: {list(routes.keys())}\n",
|
||||
" First explain your reasoning, then provide your selection in this XML format:\n",
|
||||
"\n",
|
||||
" <reasoning>\n",
|
||||
" Brief explanation of why this ticket should be routed to a specific team.\n",
|
||||
" Consider key terms, user intent, and urgency level.\n",
|
||||
" </reasoning>\n",
|
||||
"\n",
|
||||
" <selection>\n",
|
||||
" The chosen team name\n",
|
||||
" </selection>\n",
|
||||
"\n",
|
||||
" Input: {input}\"\"\".strip()\n",
|
||||
" \n",
|
||||
" route_response = llm_call(selector_prompt)\n",
|
||||
" reasoning = extract_xml(route_response, 'reasoning')\n",
|
||||
" route_key = extract_xml(route_response, 'selection').strip().lower()\n",
|
||||
" \n",
|
||||
" print(\"Routing Analysis:\")\n",
|
||||
" print(reasoning)\n",
|
||||
" print(f\"\\nSelected route: {route_key}\")\n",
|
||||
" \n",
|
||||
" # Process input with selected specialized prompt\n",
|
||||
" selected_prompt = routes[route_key]\n",
|
||||
" return llm_call(f\"{selected_prompt}\\nInput: {input}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Example Usage\n",
|
||||
"\n",
|
||||
"Below are practical examples demonstrating each workflow:\n",
|
||||
"1. Chain workflow for structured data extraction and formatting\n",
|
||||
"2. Parallelization workflow for stakeholder impact analysis\n",
|
||||
"3. Route workflow for customer support ticket handling"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"Input text:\n",
|
||||
"\n",
|
||||
"Q3 Performance Summary:\n",
|
||||
"Our customer satisfaction score rose to 92 points this quarter.\n",
|
||||
"Revenue grew by 45% compared to last year.\n",
|
||||
"Market share is now at 23% in our primary market.\n",
|
||||
"Customer churn decreased to 5% from 8%.\n",
|
||||
"New user acquisition cost is $43 per user.\n",
|
||||
"Product adoption rate increased to 78%.\n",
|
||||
"Employee satisfaction is at 87 points.\n",
|
||||
"Operating margin improved to 34%.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Step 1:\n",
|
||||
"92: customer satisfaction points\n",
|
||||
"45%: revenue growth\n",
|
||||
"23%: market share\n",
|
||||
"5%: customer churn\n",
|
||||
"8%: previous customer churn\n",
|
||||
"$43: user acquisition cost\n",
|
||||
"78%: product adoption rate\n",
|
||||
"87: employee satisfaction points\n",
|
||||
"34%: operating margin\n",
|
||||
"\n",
|
||||
"Step 2:\n",
|
||||
"92%: customer satisfaction\n",
|
||||
"45%: revenue growth\n",
|
||||
"23%: market share\n",
|
||||
"5%: customer churn\n",
|
||||
"8%: previous customer churn\n",
|
||||
"43.00: user acquisition cost\n",
|
||||
"78%: product adoption rate\n",
|
||||
"87%: employee satisfaction\n",
|
||||
"34%: operating margin\n",
|
||||
"\n",
|
||||
"Step 3:\n",
|
||||
"Here are the lines sorted in descending order by numerical value:\n",
|
||||
"\n",
|
||||
"92%: customer satisfaction\n",
|
||||
"87%: employee satisfaction\n",
|
||||
"78%: product adoption rate\n",
|
||||
"45%: revenue growth\n",
|
||||
"43.00: user acquisition cost\n",
|
||||
"34%: operating margin\n",
|
||||
"23%: market share\n",
|
||||
"8%: previous customer churn\n",
|
||||
"5%: customer churn\n",
|
||||
"\n",
|
||||
"Step 4:\n",
|
||||
"| Metric | Value |\n",
|
||||
"|:--|--:|\n",
|
||||
"| Customer Satisfaction | 92% |\n",
|
||||
"| Employee Satisfaction | 87% |\n",
|
||||
"| Product Adoption Rate | 78% |\n",
|
||||
"| Revenue Growth | 45% |\n",
|
||||
"| User Acquisition Cost | 43.00 |\n",
|
||||
"| Operating Margin | 34% |\n",
|
||||
"| Market Share | 23% |\n",
|
||||
"| Previous Customer Churn | 8% |\n",
|
||||
"| Customer Churn | 5% |\n",
|
||||
"| Metric | Value |\n",
|
||||
"|:--|--:|\n",
|
||||
"| Customer Satisfaction | 92% |\n",
|
||||
"| Employee Satisfaction | 87% |\n",
|
||||
"| Product Adoption Rate | 78% |\n",
|
||||
"| Revenue Growth | 45% |\n",
|
||||
"| User Acquisition Cost | 43.00 |\n",
|
||||
"| Operating Margin | 34% |\n",
|
||||
"| Market Share | 23% |\n",
|
||||
"| Previous Customer Churn | 8% |\n",
|
||||
"| Customer Churn | 5% |\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Example 1: Chain workflow for structured data extraction and formatting\n",
|
||||
"# Each step progressively transforms raw text into a formatted table\n",
|
||||
"\n",
|
||||
"data_processing_steps = [\n",
|
||||
" \"\"\"Extract only the numerical values and their associated metrics from the text.\n",
|
||||
" Format each as 'value: metric' on a new line.\n",
|
||||
" Example format:\n",
|
||||
" 92: customer satisfaction\n",
|
||||
" 45%: revenue growth\"\"\",\n",
|
||||
" \n",
|
||||
" \"\"\"Convert all numerical values to percentages where possible.\n",
|
||||
" If not a percentage or points, convert to decimal (e.g., 92 points -> 92%).\n",
|
||||
" Keep one number per line.\n",
|
||||
" Example format:\n",
|
||||
" 92%: customer satisfaction\n",
|
||||
" 45%: revenue growth\"\"\",\n",
|
||||
" \n",
|
||||
" \"\"\"Sort all lines in descending order by numerical value.\n",
|
||||
" Keep the format 'value: metric' on each line.\n",
|
||||
" Example:\n",
|
||||
" 92%: customer satisfaction\n",
|
||||
" 87%: employee satisfaction\"\"\",\n",
|
||||
" \n",
|
||||
" \"\"\"Format the sorted data as a markdown table with columns:\n",
|
||||
" | Metric | Value |\n",
|
||||
" |:--|--:|\n",
|
||||
" | Customer Satisfaction | 92% |\"\"\"\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"report = \"\"\"\n",
|
||||
"Q3 Performance Summary:\n",
|
||||
"Our customer satisfaction score rose to 92 points this quarter.\n",
|
||||
"Revenue grew by 45% compared to last year.\n",
|
||||
"Market share is now at 23% in our primary market.\n",
|
||||
"Customer churn decreased to 5% from 8%.\n",
|
||||
"New user acquisition cost is $43 per user.\n",
|
||||
"Product adoption rate increased to 78%.\n",
|
||||
"Employee satisfaction is at 87 points.\n",
|
||||
"Operating margin improved to 34%.\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"print(\"\\nInput text:\")\n",
|
||||
"print(report)\n",
|
||||
"formatted_result = chain(report, data_processing_steps)\n",
|
||||
"print(formatted_result)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"MARKET IMPACT ANALYSIS FOR CUSTOMERS\n",
|
||||
"============================================\n",
|
||||
"\n",
|
||||
"HIGH PRIORITY IMPACTS\n",
|
||||
"--------------------\n",
|
||||
"1. Price Sensitivity\n",
|
||||
"- Rising inflation and costs likely to reduce purchasing power\n",
|
||||
"- Increased competition for value-oriented products\n",
|
||||
"- Risk of customers trading down to lower-cost alternatives\n",
|
||||
"\n",
|
||||
"Recommended Actions:\n",
|
||||
"• Introduce tiered pricing options\n",
|
||||
"• Develop value-focused product lines\n",
|
||||
"• Implement loyalty programs with cost-saving benefits\n",
|
||||
"• Highlight total cost of ownership benefits\n",
|
||||
"\n",
|
||||
"2. Technology Demands\n",
|
||||
"- Accelerating tech advancement creating higher expectations\n",
|
||||
"- Growing demand for smart/connected features\n",
|
||||
"- Integration with mobile devices and apps becoming standard\n",
|
||||
"\n",
|
||||
"Recommended Actions:\n",
|
||||
"• Accelerate digital transformation initiatives\n",
|
||||
"• Invest in user experience improvements\n",
|
||||
"• Develop smart product features\n",
|
||||
"• Create technology roadmap aligned with customer needs\n",
|
||||
"\n",
|
||||
"MEDIUM PRIORITY IMPACTS\n",
|
||||
"----------------------\n",
|
||||
"3. Environmental Consciousness\n",
|
||||
"- Growing demand for sustainable products\n",
|
||||
"- Increased scrutiny of environmental practices\n",
|
||||
"- Willingness to pay premium for eco-friendly options\n",
|
||||
"\n",
|
||||
"Recommended Actions:\n",
|
||||
"• Develop eco-friendly product lines\n",
|
||||
"• Improve sustainability messaging\n",
|
||||
"• Create transparent environmental reporting\n",
|
||||
"• Consider green certification programs\n",
|
||||
"\n",
|
||||
"MONITORING & IMPLEMENTATION\n",
|
||||
"--------------------------\n",
|
||||
"1. Track customer satisfaction metrics\n",
|
||||
"2. Conduct regular market surveys\n",
|
||||
"3. Monitor competitor responses\n",
|
||||
"4. Review pricing strategies quarterly\n",
|
||||
"5. Measure environmental impact improvements\n",
|
||||
"\n",
|
||||
"SUCCESS METRICS\n",
|
||||
"--------------\n",
|
||||
"• Customer retention rates\n",
|
||||
"• Market share\n",
|
||||
"• Sustainability goals met\n",
|
||||
"• Customer satisfaction scores\n",
|
||||
"• Technology adoption rates\n",
|
||||
"\n",
|
||||
"This analysis should be reviewed quarterly and adjusted based on market conditions and customer feedback.\n",
|
||||
"MARKET IMPACT ANALYSIS FOR EMPLOYEES\n",
|
||||
"\n",
|
||||
"Priority 1: Job Security Concerns\n",
|
||||
"Impacts:\n",
|
||||
"• Market volatility creating uncertainty about positions\n",
|
||||
"• Potential restructuring or role changes\n",
|
||||
"• Stress affecting productivity and morale\n",
|
||||
"\n",
|
||||
"Recommended Actions:\n",
|
||||
"- Provide regular, transparent communications about company stability\n",
|
||||
"- Create clear retention plans for key talent\n",
|
||||
"- Establish severance/transition support programs if needed\n",
|
||||
"- Develop internal mobility programs\n",
|
||||
"\n",
|
||||
"Priority 2: Skills Gap & Development\n",
|
||||
"Impacts:\n",
|
||||
"• Current skills becoming outdated due to market changes\n",
|
||||
"• New technologies/processes requiring different capabilities\n",
|
||||
"• Competition for skilled workers increasing\n",
|
||||
"\n",
|
||||
"Recommended Actions:\n",
|
||||
"- Conduct skills gap analysis\n",
|
||||
"- Implement targeted training programs\n",
|
||||
"- Offer tuition reimbursement/certification support\n",
|
||||
"- Create mentorship programs\n",
|
||||
"- Partner with educational institutions\n",
|
||||
"\n",
|
||||
"Priority 3: Strategic Direction & Leadership\n",
|
||||
"Impacts:\n",
|
||||
"• Unclear career paths in changing environment\n",
|
||||
"• Uncertainty about future company direction\n",
|
||||
"• Need for new leadership approaches\n",
|
||||
"\n",
|
||||
"Recommended Actions:\n",
|
||||
"- Develop and communicate clear strategic roadmap\n",
|
||||
"- Create detailed career progression frameworks\n",
|
||||
"- Increase leadership visibility and accessibility\n",
|
||||
"- Establish regular town halls/feedback sessions\n",
|
||||
"- Implement change management programs\n",
|
||||
"\n",
|
||||
"Implementation Timeline:\n",
|
||||
"Short-term (0-3 months):\n",
|
||||
"- Begin communication initiatives\n",
|
||||
"- Start skills assessment\n",
|
||||
"- Launch feedback channels\n",
|
||||
"\n",
|
||||
"Medium-term (3-12 months):\n",
|
||||
"- Roll out training programs\n",
|
||||
"- Implement career frameworks\n",
|
||||
"- Develop retention strategies\n",
|
||||
"\n",
|
||||
"Long-term (12+ months):\n",
|
||||
"- Build sustainable talent development\n",
|
||||
"- Create lasting cultural changes\n",
|
||||
"- Monitor and adjust programs\n",
|
||||
"\n",
|
||||
"Success Metrics:\n",
|
||||
"• Employee retention rates\n",
|
||||
"• Skills acquisition levels\n",
|
||||
"• Employee satisfaction scores\n",
|
||||
"• Internal promotion rates\n",
|
||||
"• Productivity measures\n",
|
||||
"MARKET IMPACT ANALYSIS FOR INVESTORS\n",
|
||||
"===============================\n",
|
||||
"\n",
|
||||
"Primary Impacts\n",
|
||||
"--------------\n",
|
||||
"1. Growth Expectations\n",
|
||||
"- Market volatility may reduce short-term returns\n",
|
||||
"- Potential slowdown in expansion opportunities\n",
|
||||
"- Competition for investment capital increasing\n",
|
||||
"\n",
|
||||
"2. Cost Control Concerns \n",
|
||||
"- Rising operational costs due to inflation\n",
|
||||
"- Supply chain disruptions affecting margins\n",
|
||||
"- Higher borrowing costs from interest rates\n",
|
||||
"\n",
|
||||
"3. Risk Profile\n",
|
||||
"- Increased market uncertainty\n",
|
||||
"- Regulatory changes creating compliance costs\n",
|
||||
"- Geopolitical tensions affecting global investments\n",
|
||||
"\n",
|
||||
"Recommended Actions (Prioritized)\n",
|
||||
"-------------------------------\n",
|
||||
"Immediate Priority:\n",
|
||||
"1. Strengthen risk management protocols\n",
|
||||
"- Implement enhanced monitoring systems\n",
|
||||
"- Diversify investment portfolio\n",
|
||||
"- Develop contingency plans for market shifts\n",
|
||||
"\n",
|
||||
"Medium Priority:\n",
|
||||
"2. Optimize cost structure\n",
|
||||
"- Review operational efficiency\n",
|
||||
"- Identify automation opportunities\n",
|
||||
"- Renegotiate supplier contracts\n",
|
||||
"\n",
|
||||
"3. Enhance communication\n",
|
||||
"- Increase transparency in reporting\n",
|
||||
"- Regular stakeholder updates\n",
|
||||
"- Clear messaging on strategy adjustments\n",
|
||||
"\n",
|
||||
"Long-term Priority:\n",
|
||||
"4. Strategic repositioning\n",
|
||||
"- Explore new growth markets\n",
|
||||
"- Invest in innovation\n",
|
||||
"- Build resilience through diversification\n",
|
||||
"\n",
|
||||
"Key Performance Metrics to Monitor\n",
|
||||
"--------------------------------\n",
|
||||
"1. Return on Investment (ROI)\n",
|
||||
"2. Cost-to-Income Ratio\n",
|
||||
"3. Risk-adjusted returns\n",
|
||||
"4. Market share growth\n",
|
||||
"5. Operating margins\n",
|
||||
"\n",
|
||||
"Success Criteria\n",
|
||||
"---------------\n",
|
||||
"- Maintained or improved ROI\n",
|
||||
"- Effective cost management\n",
|
||||
"- Risk levels within acceptable parameters\n",
|
||||
"- Clear communication with stakeholders\n",
|
||||
"- Strategic objectives met\n",
|
||||
"MARKET IMPACT ANALYSIS FOR SUPPLIERS\n",
|
||||
"\n",
|
||||
"Priority 1: Capacity Constraints\n",
|
||||
"Impacts:\n",
|
||||
"• Reduced ability to meet customer demand\n",
|
||||
"• Risk of losing market share to competitors\n",
|
||||
"• Increased operational costs from running at maximum capacity\n",
|
||||
"\n",
|
||||
"Recommended Actions:\n",
|
||||
"- Invest in capacity expansion where financially viable\n",
|
||||
"- Implement advanced scheduling and forecasting systems\n",
|
||||
"- Develop partnerships with backup suppliers\n",
|
||||
"- Consider outsourcing non-critical components\n",
|
||||
"\n",
|
||||
"Priority 2: Price Pressures\n",
|
||||
"Impacts:\n",
|
||||
"• Squeezed profit margins\n",
|
||||
"• Difficulty maintaining quality standards\n",
|
||||
"• Risk of losing customers to lower-cost alternatives\n",
|
||||
"\n",
|
||||
"Recommended Actions:\n",
|
||||
"- Review and optimize cost structure\n",
|
||||
"- Negotiate long-term contracts with key customers\n",
|
||||
"- Explore automation and efficiency improvements\n",
|
||||
"- Develop value-added services to justify pricing\n",
|
||||
"\n",
|
||||
"Priority 3: Technology Transitions\n",
|
||||
"Impacts:\n",
|
||||
"• Need for new equipment and processes\n",
|
||||
"• Workforce skill gaps\n",
|
||||
"• Investment requirements\n",
|
||||
"• Risk of obsolescence\n",
|
||||
"\n",
|
||||
"Recommended Actions:\n",
|
||||
"- Create technology roadmap and investment timeline\n",
|
||||
"- Implement training programs for workforce\n",
|
||||
"- Seek strategic partnerships with tech providers\n",
|
||||
"- Consider R&D investments in key areas\n",
|
||||
"\n",
|
||||
"RISK MITIGATION STRATEGIES\n",
|
||||
"\n",
|
||||
"Short-term:\n",
|
||||
"• Improve communication with customers about constraints\n",
|
||||
"• Implement cost control measures\n",
|
||||
"• Begin essential technology upgrades\n",
|
||||
"\n",
|
||||
"Medium-term:\n",
|
||||
"• Develop strategic capacity expansion plans\n",
|
||||
"• Build relationships with alternative suppliers\n",
|
||||
"• Invest in workforce development\n",
|
||||
"\n",
|
||||
"Long-term:\n",
|
||||
"• Transform operations for digital age\n",
|
||||
"• Build sustainable competitive advantages\n",
|
||||
"• Establish innovation capabilities\n",
|
||||
"\n",
|
||||
"MONITORING METRICS\n",
|
||||
"1. Capacity utilization rates\n",
|
||||
"2. Price-to-cost margins\n",
|
||||
"3. Technology adoption rates\n",
|
||||
"4. Customer satisfaction scores\n",
|
||||
"5. Market share trends\n",
|
||||
"\n",
|
||||
"This analysis should be reviewed quarterly and adjusted based on market conditions.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Example 2: Parallelization workflow for stakeholder impact analysis\n",
|
||||
"# Process impact analysis for multiple stakeholder groups concurrently\n",
|
||||
"\n",
|
||||
"stakeholders = [\n",
|
||||
" \"\"\"Customers:\n",
|
||||
" - Price sensitive\n",
|
||||
" - Want better tech\n",
|
||||
" - Environmental concerns\"\"\",\n",
|
||||
" \n",
|
||||
" \"\"\"Employees:\n",
|
||||
" - Job security worries\n",
|
||||
" - Need new skills\n",
|
||||
" - Want clear direction\"\"\",\n",
|
||||
" \n",
|
||||
" \"\"\"Investors:\n",
|
||||
" - Expect growth\n",
|
||||
" - Want cost control\n",
|
||||
" - Risk concerns\"\"\",\n",
|
||||
" \n",
|
||||
" \"\"\"Suppliers:\n",
|
||||
" - Capacity constraints\n",
|
||||
" - Price pressures\n",
|
||||
" - Tech transitions\"\"\"\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"impact_results = parallel(\n",
|
||||
" \"\"\"Analyze how market changes will impact this stakeholder group.\n",
|
||||
" Provide specific impacts and recommended actions.\n",
|
||||
" Format with clear sections and priorities.\"\"\",\n",
|
||||
" stakeholders\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"for result in impact_results:\n",
|
||||
" print(result)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Processing support tickets...\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Ticket 1:\n",
|
||||
"----------------------------------------\n",
|
||||
"Subject: Can't access my account\n",
|
||||
" Message: Hi, I've been trying to log in for the past hour but keep getting an 'invalid password' error. \n",
|
||||
" I'm sure I'm using the right password. Can you help me regain access? This is urgent as I need to \n",
|
||||
" submit a report by end of day.\n",
|
||||
" - John\n",
|
||||
"\n",
|
||||
"Response:\n",
|
||||
"----------------------------------------\n",
|
||||
"\n",
|
||||
"Available routes: ['billing', 'technical', 'account', 'product']\n",
|
||||
"Routing Analysis:\n",
|
||||
"\n",
|
||||
"This issue is clearly related to account access and authentication problems. The user is experiencing login difficulties with their password, which is a core account security and access issue. While there might be technical aspects involved, the primary concern is account access restoration. The urgency expressed by the user and the nature of the problem (password/login issues) makes this a typical account support case. Account team specialists are best equipped to handle password resets, account verification, and access restoration while following proper security protocols.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Selected route: account\n",
|
||||
"Account Support Response:\n",
|
||||
"\n",
|
||||
"Dear John,\n",
|
||||
"\n",
|
||||
"I understand you're experiencing login issues and have an urgent deadline. We'll need to verify your identity before proceeding with account recovery.\n",
|
||||
"\n",
|
||||
"Immediate Steps to Take:\n",
|
||||
"1. Clear your browser cache and cookies\n",
|
||||
"2. Ensure CAPS LOCK is not enabled\n",
|
||||
"3. Try accessing from a private/incognito window\n",
|
||||
"\n",
|
||||
"If still unsuccessful, initiate account recovery:\n",
|
||||
"1. Visit the secure password reset page: [Reset Portal URL]\n",
|
||||
"2. Click \"Forgot Password\"\n",
|
||||
"3. Enter your email address\n",
|
||||
"4. Follow verification instructions sent to your registered email\n",
|
||||
"5. Choose a new strong password\n",
|
||||
"\n",
|
||||
"SECURITY VERIFICATION REQUIRED:\n",
|
||||
"To protect your account, you'll need to provide:\n",
|
||||
"- Account email address\n",
|
||||
"- Date of last successful login\n",
|
||||
"- Any two-factor authentication backup codes (if enabled)\n",
|
||||
"\n",
|
||||
"⚠️ IMPORTANT SECURITY WARNINGS:\n",
|
||||
"- Never share passwords or verification codes\n",
|
||||
"- Beware of similar-looking phishing emails\n",
|
||||
"- Use unique passwords for each account\n",
|
||||
"\n",
|
||||
"Expected Resolution Time:\n",
|
||||
"- Password reset: 5-10 minutes\n",
|
||||
"- Account verification: Up to 1 hour\n",
|
||||
"- Support team response: Within 2 hours\n",
|
||||
"\n",
|
||||
"If you cannot complete these steps or don't receive verification emails, contact our security team at [secure contact information] with your account details.\n",
|
||||
"\n",
|
||||
"Regards,\n",
|
||||
"Account Security Team\n",
|
||||
"\n",
|
||||
"P.S. Once resolved, we strongly recommend enabling two-factor authentication for additional security.\n",
|
||||
"\n",
|
||||
"Ticket 2:\n",
|
||||
"----------------------------------------\n",
|
||||
"Subject: Unexpected charge on my card\n",
|
||||
" Message: Hello, I just noticed a charge of $49.99 on my credit card from your company, but I thought\n",
|
||||
" I was on the $29.99 plan. Can you explain this charge and adjust it if it's a mistake?\n",
|
||||
" Thanks,\n",
|
||||
" Sarah\n",
|
||||
"\n",
|
||||
"Response:\n",
|
||||
"----------------------------------------\n",
|
||||
"\n",
|
||||
"Available routes: ['billing', 'technical', 'account', 'product']\n",
|
||||
"Routing Analysis:\n",
|
||||
"\n",
|
||||
"This is clearly a billing-related inquiry for several reasons:\n",
|
||||
"1. The user is questioning a specific charge amount ($49.99)\n",
|
||||
"2. They're comparing it to their expected plan price ($29.99)\n",
|
||||
"3. They're requesting explanation of charges and potential adjustment\n",
|
||||
"4. The issue involves credit card charges and pricing plans\n",
|
||||
"5. There's no mention of technical issues, product features, or account access problems\n",
|
||||
"\n",
|
||||
"This is a straightforward billing dispute that requires access to payment records and the ability to process refunds or adjustments if needed.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Selected route: billing\n",
|
||||
"Billing Support Response:\n",
|
||||
"\n",
|
||||
"Dear Sarah,\n",
|
||||
"\n",
|
||||
"I understand your concern about the unexpected charge of $49.99 when you were expecting to be billed $29.99.\n",
|
||||
"\n",
|
||||
"After reviewing the billing details, this difference typically occurs when an account transitions from our Basic plan ($29.99) to our Premium plan, often due to either:\n",
|
||||
"- An automatic upgrade based on usage\n",
|
||||
"- A plan change request that may have been initiated\n",
|
||||
"- The end of a promotional period\n",
|
||||
"\n",
|
||||
"Here are the next steps to resolve this:\n",
|
||||
"\n",
|
||||
"1. Within 1 business day: I will conduct a detailed audit of your account history to confirm when and why the plan change occurred\n",
|
||||
"2. By tomorrow: You'll receive an email with the complete billing breakdown\n",
|
||||
"3. Within 48 hours: If we confirm this was an error, we'll process a refund of $20 (the difference between plans)\n",
|
||||
"\n",
|
||||
"Payment Options:\n",
|
||||
"- If this charge was indeed incorrect, we can either:\n",
|
||||
" * Issue a direct refund to your credit card (3-5 business days)\n",
|
||||
" * Apply a $20 credit to your next billing cycle\n",
|
||||
"\n",
|
||||
"Please let me know your preferred refund method if we determine this was charged in error.\n",
|
||||
"\n",
|
||||
"Is there anything else you need clarification on regarding your billing?\n",
|
||||
"\n",
|
||||
"Best regards,\n",
|
||||
"Billing Support Team\n",
|
||||
"\n",
|
||||
"Ticket 3:\n",
|
||||
"----------------------------------------\n",
|
||||
"Subject: How to export data?\n",
|
||||
" Message: I need to export all my project data to Excel. I've looked through the docs but can't\n",
|
||||
" figure out how to do a bulk export. Is this possible? If so, could you walk me through the steps?\n",
|
||||
" Best regards,\n",
|
||||
" Mike\n",
|
||||
"\n",
|
||||
"Response:\n",
|
||||
"----------------------------------------\n",
|
||||
"\n",
|
||||
"Available routes: ['billing', 'technical', 'account', 'product']\n",
|
||||
"Routing Analysis:\n",
|
||||
"\n",
|
||||
"This is clearly a technical/functional question about using a product feature (data export). The user is requesting specific instructions on how to perform a task within the system. Key terms like \"export,\" \"data,\" \"bulk export,\" and the request for step-by-step guidance indicate this is a technical how-to question. This isn't related to billing, account management, or product feedback - it's about understanding how to use an existing feature.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Selected route: technical\n",
|
||||
"Technical Support Response:\n",
|
||||
"\n",
|
||||
"I'll help you export your project data to Excel. Here's the step-by-step process:\n",
|
||||
"\n",
|
||||
"Steps to Export Data:\n",
|
||||
"1. Log into your project dashboard\n",
|
||||
"2. Navigate to \"Project Settings\" in the top right corner\n",
|
||||
"3. Select \"Data Management\" from the dropdown menu\n",
|
||||
"4. Click the \"Export\" tab\n",
|
||||
"5. Choose \"Bulk Export\" option\n",
|
||||
"6. Select data range and specific data fields you want to export\n",
|
||||
"7. Choose \"Excel (.xlsx)\" as the export format\n",
|
||||
"8. Click \"Generate Export\"\n",
|
||||
"9. Wait for the system to process (may take several minutes for large datasets)\n",
|
||||
"10. Download the exported file when complete\n",
|
||||
"\n",
|
||||
"System Requirements:\n",
|
||||
"- Supported browsers: Chrome 90+, Firefox 88+, Edge 91+\n",
|
||||
"- Minimum 4GB RAM recommended for large exports\n",
|
||||
"- Stable internet connection\n",
|
||||
"- Excel 2016 or later to open exported files\n",
|
||||
"\n",
|
||||
"Common Issues & Workarounds:\n",
|
||||
"A. If export times out:\n",
|
||||
" - Try reducing the date range\n",
|
||||
" - Export in smaller batches\n",
|
||||
" - Use off-peak hours\n",
|
||||
"\n",
|
||||
"B. If download fails:\n",
|
||||
" - Clear browser cache\n",
|
||||
" - Use incognito/private mode\n",
|
||||
" - Try a different supported browser\n",
|
||||
"\n",
|
||||
"Escalation Path:\n",
|
||||
"If you continue experiencing issues after trying the above steps, please:\n",
|
||||
"1. Contact our technical support team at support@company.com\n",
|
||||
"2. Include your project ID and export error messages\n",
|
||||
"3. For urgent assistance, call our support hotline: 1-800-TECH-HELP\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Example 3: Route workflow for customer support ticket handling\n",
|
||||
"# Route support tickets to appropriate teams based on content analysis\n",
|
||||
"\n",
|
||||
"support_routes = {\n",
|
||||
" \"billing\": \"\"\"You are a billing support specialist. Follow these guidelines:\n",
|
||||
" 1. Always start with \"Billing Support Response:\"\n",
|
||||
" 2. First acknowledge the specific billing issue\n",
|
||||
" 3. Explain any charges or discrepancies clearly\n",
|
||||
" 4. List concrete next steps with timeline\n",
|
||||
" 5. End with payment options if relevant\n",
|
||||
" \n",
|
||||
" Keep responses professional but friendly.\n",
|
||||
" \n",
|
||||
" Input: \"\"\",\n",
|
||||
" \n",
|
||||
" \"technical\": \"\"\"You are a technical support engineer. Follow these guidelines:\n",
|
||||
" 1. Always start with \"Technical Support Response:\"\n",
|
||||
" 2. List exact steps to resolve the issue\n",
|
||||
" 3. Include system requirements if relevant\n",
|
||||
" 4. Provide workarounds for common problems\n",
|
||||
" 5. End with escalation path if needed\n",
|
||||
" \n",
|
||||
" Use clear, numbered steps and technical details.\n",
|
||||
" \n",
|
||||
" Input: \"\"\",\n",
|
||||
" \n",
|
||||
" \"account\": \"\"\"You are an account security specialist. Follow these guidelines:\n",
|
||||
" 1. Always start with \"Account Support Response:\"\n",
|
||||
" 2. Prioritize account security and verification\n",
|
||||
" 3. Provide clear steps for account recovery/changes\n",
|
||||
" 4. Include security tips and warnings\n",
|
||||
" 5. Set clear expectations for resolution time\n",
|
||||
" \n",
|
||||
" Maintain a serious, security-focused tone.\n",
|
||||
" \n",
|
||||
" Input: \"\"\",\n",
|
||||
" \n",
|
||||
" \"product\": \"\"\"You are a product specialist. Follow these guidelines:\n",
|
||||
" 1. Always start with \"Product Support Response:\"\n",
|
||||
" 2. Focus on feature education and best practices\n",
|
||||
" 3. Include specific examples of usage\n",
|
||||
" 4. Link to relevant documentation sections\n",
|
||||
" 5. Suggest related features that might help\n",
|
||||
" \n",
|
||||
" Be educational and encouraging in tone.\n",
|
||||
" \n",
|
||||
" Input: \"\"\"\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"# Test with different support tickets\n",
|
||||
"tickets = [\n",
|
||||
" \"\"\"Subject: Can't access my account\n",
|
||||
" Message: Hi, I've been trying to log in for the past hour but keep getting an 'invalid password' error. \n",
|
||||
" I'm sure I'm using the right password. Can you help me regain access? This is urgent as I need to \n",
|
||||
" submit a report by end of day.\n",
|
||||
" - John\"\"\",\n",
|
||||
" \n",
|
||||
" \"\"\"Subject: Unexpected charge on my card\n",
|
||||
" Message: Hello, I just noticed a charge of $49.99 on my credit card from your company, but I thought\n",
|
||||
" I was on the $29.99 plan. Can you explain this charge and adjust it if it's a mistake?\n",
|
||||
" Thanks,\n",
|
||||
" Sarah\"\"\",\n",
|
||||
" \n",
|
||||
" \"\"\"Subject: How to export data?\n",
|
||||
" Message: I need to export all my project data to Excel. I've looked through the docs but can't\n",
|
||||
" figure out how to do a bulk export. Is this possible? If so, could you walk me through the steps?\n",
|
||||
" Best regards,\n",
|
||||
" Mike\"\"\"\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"print(\"Processing support tickets...\\n\")\n",
|
||||
"for i, ticket in enumerate(tickets, 1):\n",
|
||||
" print(f\"\\nTicket {i}:\")\n",
|
||||
" print(\"-\" * 40)\n",
|
||||
" print(ticket)\n",
|
||||
" print(\"\\nResponse:\")\n",
|
||||
" print(\"-\" * 40)\n",
|
||||
" response = route(ticket, support_routes)\n",
|
||||
" print(response)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "py311",
|
||||
"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.11.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
163
patterns/agents/evaluator_optimizer.ipynb
Normal file
163
patterns/agents/evaluator_optimizer.ipynb
Normal file
@@ -0,0 +1,163 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Evaluator-Optimizer Workflow\n",
|
||||
"In this workflow, one LLM call generates a response while another provides evaluation and feedback in a loop.\n",
|
||||
"\n",
|
||||
"### When to use this workflow\n",
|
||||
"This workflow is particularly effective when we have:\n",
|
||||
"\n",
|
||||
"- Clear evaluation criteria\n",
|
||||
"- Value from iterative refinement\n",
|
||||
"\n",
|
||||
"The two signs of good fit are:\n",
|
||||
"\n",
|
||||
"- LLM responses can be demonstrably improved when feedback is provided\n",
|
||||
"- The LLM can provide meaningful feedback itself"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from util import llm_call, extract_xml\n",
|
||||
"\n",
|
||||
"def generate(prompt: str, task: str, context: str = \"\") -> tuple[str, str]:\n",
|
||||
" \"\"\"Generate and improve a solution based on feedback.\"\"\"\n",
|
||||
" full_prompt = f\"{prompt}\\n{context}\\nTask: {task}\" if context else f\"{prompt}\\nTask: {task}\"\n",
|
||||
" response = llm_call(full_prompt)\n",
|
||||
" thoughts = extract_xml(response, \"thoughts\")\n",
|
||||
" result = extract_xml(response, \"response\")\n",
|
||||
" \n",
|
||||
" print(\"\\n=== GENERATION START ===\")\n",
|
||||
" print(f\"Thoughts:\\n{thoughts}\\n\")\n",
|
||||
" print(f\"Generated:\\n{result}\")\n",
|
||||
" print(\"=== GENERATION END ===\\n\")\n",
|
||||
" \n",
|
||||
" return thoughts, result\n",
|
||||
"\n",
|
||||
"def evaluate(prompt: str, content: str, task: str) -> tuple[str, str]:\n",
|
||||
" \"\"\"Evaluate if a solution meets requirements.\"\"\"\n",
|
||||
" full_prompt = f\"{prompt}\\nOriginal task: {task}\\nContent to evaluate: {content}\"\n",
|
||||
" response = llm_call(full_prompt)\n",
|
||||
" evaluation = extract_xml(response, \"evaluation\")\n",
|
||||
" feedback = extract_xml(response, \"feedback\")\n",
|
||||
" \n",
|
||||
" print(\"=== EVALUATION START ===\")\n",
|
||||
" print(f\"Status: {evaluation}\")\n",
|
||||
" print(f\"Feedback: {feedback}\")\n",
|
||||
" print(\"=== EVALUATION END ===\\n\")\n",
|
||||
" \n",
|
||||
" return evaluation, feedback\n",
|
||||
"\n",
|
||||
"def loop(task: str, evaluator_prompt: str, generator_prompt: str) -> tuple[str, list[dict]]:\n",
|
||||
" \"\"\"Keep generating and evaluating until requirements are met.\"\"\"\n",
|
||||
" memory = []\n",
|
||||
" chain_of_thought = []\n",
|
||||
" \n",
|
||||
" thoughts, result = generate(generator_prompt, task)\n",
|
||||
" memory.append(result)\n",
|
||||
" chain_of_thought.append({\"thoughts\": thoughts, \"result\": result})\n",
|
||||
" \n",
|
||||
" while True:\n",
|
||||
" evaluation, feedback = evaluate(evaluator_prompt, result, task)\n",
|
||||
" if evaluation == \"PASS\":\n",
|
||||
" return result, chain_of_thought\n",
|
||||
" \n",
|
||||
" context = \"\\n\".join([\n",
|
||||
" \"Previous attempts:\",\n",
|
||||
" *[f\"- {m}\" for m in memory],\n",
|
||||
" f\"\\nFeedback: {feedback}\"\n",
|
||||
" ])\n",
|
||||
" \n",
|
||||
" thoughts, result = generate(generator_prompt, task, context)\n",
|
||||
" memory.append(result)\n",
|
||||
" chain_of_thought.append({\"thoughts\": thoughts, \"result\": result})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Example Use Case: Iterative coding loop\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"evaluator_prompt = \"\"\"\n",
|
||||
"Evaluate this following code implementation for:\n",
|
||||
"1. code correctness\n",
|
||||
"2. time complexity\n",
|
||||
"3. style and best practices\n",
|
||||
"\n",
|
||||
"You should be evaluating only and not attemping to solve the task.\n",
|
||||
"Only output \"PASS\" if all criteria are met and you have no further suggestions for improvements.\n",
|
||||
"Output your evaluation concisely in the following format.\n",
|
||||
"\n",
|
||||
"<evaluation>PASS, NEEDS_IMPROVEMENT, or FAIL</evaluation>\n",
|
||||
"<feedback>\n",
|
||||
"What needs improvement and why.\n",
|
||||
"</feedback>\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"generator_prompt = \"\"\"\n",
|
||||
"Your goal is to complete the task based on <user input>. If there are feedback \n",
|
||||
"from your previous generations, you should reflect on them to improve your solution\n",
|
||||
"\n",
|
||||
"Output your answer concisely in the following format: \n",
|
||||
"\n",
|
||||
"<thoughts>\n",
|
||||
"[Your understanding of the task and feedback and how you plan to improve]\n",
|
||||
"</thoughts>\n",
|
||||
"\n",
|
||||
"<response>\n",
|
||||
"[Your code implementation here]\n",
|
||||
"</response>\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"task = \"\"\"\n",
|
||||
"<user input>\n",
|
||||
"Implement a Stack with:\n",
|
||||
"1. push(x)\n",
|
||||
"2. pop()\n",
|
||||
"3. getMin()\n",
|
||||
"All operations should be O(1).\n",
|
||||
"</user input>\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"loop(task, evaluator_prompt, generator_prompt)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "py311",
|
||||
"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.11.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
284
patterns/agents/orchestrator_workers.ipynb
Normal file
284
patterns/agents/orchestrator_workers.ipynb
Normal file
@@ -0,0 +1,284 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Orchestrator-Workers Workflow\n",
|
||||
"In this workflow, a central LLM dynamically breaks down tasks, delegates them to worker LLMs, and synthesizes their results.\n",
|
||||
"\n",
|
||||
"### When to use this workflow\n",
|
||||
"This workflow is well-suited for complex tasks where you can't predict the subtasks needed. The key difference from simple parallelization is its flexibility—subtasks aren't pre-defined, but determined by the orchestrator based on the specific input."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from typing import Dict, List, Optional\n",
|
||||
"from util import llm_call, extract_xml\n",
|
||||
"\n",
|
||||
"def parse_tasks(tasks_xml: str) -> List[Dict]:\n",
|
||||
" \"\"\"Parse XML tasks into a list of task dictionaries.\"\"\"\n",
|
||||
" tasks = []\n",
|
||||
" current_task = {}\n",
|
||||
" \n",
|
||||
" for line in tasks_xml.split('\\n'):\n",
|
||||
" line = line.strip()\n",
|
||||
" if not line:\n",
|
||||
" continue\n",
|
||||
" \n",
|
||||
" if line.startswith(\"<task>\"):\n",
|
||||
" current_task = {}\n",
|
||||
" elif line.startswith(\"<type>\"):\n",
|
||||
" current_task[\"type\"] = line[6:-7].strip()\n",
|
||||
" elif line.startswith(\"<description>\"):\n",
|
||||
" current_task[\"description\"] = line[12:-13].strip()\n",
|
||||
" elif line.startswith(\"</task>\"):\n",
|
||||
" if \"description\" in current_task:\n",
|
||||
" if \"type\" not in current_task:\n",
|
||||
" current_task[\"type\"] = \"default\"\n",
|
||||
" tasks.append(current_task)\n",
|
||||
" \n",
|
||||
" return tasks\n",
|
||||
"\n",
|
||||
"class FlexibleOrchestrator:\n",
|
||||
" \"\"\"Break down tasks and run them in parallel using worker LLMs.\"\"\"\n",
|
||||
" \n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" orchestrator_prompt: str,\n",
|
||||
" worker_prompt: str,\n",
|
||||
" ):\n",
|
||||
" \"\"\"Initialize with prompt templates.\"\"\"\n",
|
||||
" self.orchestrator_prompt = orchestrator_prompt\n",
|
||||
" self.worker_prompt = worker_prompt\n",
|
||||
"\n",
|
||||
" def _format_prompt(self, template: str, **kwargs) -> str:\n",
|
||||
" \"\"\"Format a prompt template with variables.\"\"\"\n",
|
||||
" try:\n",
|
||||
" return template.format(**kwargs)\n",
|
||||
" except KeyError as e:\n",
|
||||
" raise ValueError(f\"Missing required prompt variable: {e}\")\n",
|
||||
"\n",
|
||||
" def process(self, task: str, context: Optional[Dict] = None) -> Dict:\n",
|
||||
" \"\"\"Process task by breaking it down and running subtasks in parallel.\"\"\"\n",
|
||||
" context = context or {}\n",
|
||||
" \n",
|
||||
" # Step 1: Get orchestrator response\n",
|
||||
" orchestrator_input = self._format_prompt(\n",
|
||||
" self.orchestrator_prompt,\n",
|
||||
" task=task,\n",
|
||||
" **context\n",
|
||||
" )\n",
|
||||
" orchestrator_response = llm_call(orchestrator_input)\n",
|
||||
" \n",
|
||||
" # Parse orchestrator response\n",
|
||||
" analysis = extract_xml(orchestrator_response, \"analysis\")\n",
|
||||
" tasks_xml = extract_xml(orchestrator_response, \"tasks\")\n",
|
||||
" tasks = parse_tasks(tasks_xml)\n",
|
||||
" \n",
|
||||
" print(\"\\n=== ORCHESTRATOR OUTPUT ===\")\n",
|
||||
" print(f\"\\nANALYSIS:\\n{analysis}\")\n",
|
||||
" print(f\"\\nTASKS:\\n{tasks}\")\n",
|
||||
" \n",
|
||||
" # Step 2: Process each task\n",
|
||||
" worker_results = []\n",
|
||||
" for task_info in tasks:\n",
|
||||
" worker_input = self._format_prompt(\n",
|
||||
" self.worker_prompt,\n",
|
||||
" original_task=task,\n",
|
||||
" task_type=task_info['type'],\n",
|
||||
" task_description=task_info['description'],\n",
|
||||
" **context\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" worker_response = llm_call(worker_input)\n",
|
||||
" result = extract_xml(worker_response, \"response\")\n",
|
||||
" \n",
|
||||
" worker_results.append({\n",
|
||||
" \"type\": task_info[\"type\"],\n",
|
||||
" \"description\": task_info[\"description\"],\n",
|
||||
" \"result\": result\n",
|
||||
" })\n",
|
||||
" \n",
|
||||
" print(f\"\\n=== WORKER RESULT ({task_info['type']}) ===\\n{result}\\n\")\n",
|
||||
" \n",
|
||||
" return {\n",
|
||||
" \"analysis\": analysis,\n",
|
||||
" \"worker_results\": worker_results,\n",
|
||||
" }\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Example Use Case: Marketing Variation Generation\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"=== ORCHESTRATOR OUTPUT ===\n",
|
||||
"\n",
|
||||
"ANALYSIS:\n",
|
||||
"\n",
|
||||
"This task requires creating marketing copy for an eco-friendly water bottle, which presents multiple angles for effective communication. The key challenge is balancing environmental benefits with practical features while maintaining appeal to different consumer segments.\n",
|
||||
"\n",
|
||||
"Key variations would be valuable because:\n",
|
||||
"1. Technical buyers need specific details about materials and environmental impact\n",
|
||||
"2. Lifestyle-focused consumers respond better to emotional benefits and storytelling\n",
|
||||
"3. Different tones can target distinct market segments while promoting the same core product\n",
|
||||
"\n",
|
||||
"The technical approach serves those who make purchase decisions based on specifications and measurable impact, while the conversational approach connects with those who buy based on lifestyle alignment and emotional resonance.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"TASKS:\n",
|
||||
"[{'type': 'formal', 'description': '>Create a specification-focused description highlighting material composition, environmental certifications, capacity measurements, and quantifiable eco-impact (e.g., \"plastic bottles saved per year\"). Include technical details about manufacturing process and recycling capabilities.<'}, {'type': 'conversational', 'description': '>Develop a narrative-style description that focuses on the user experience, lifestyle benefits, and emotional connection to environmental protection. Use relatable scenarios and casual language to help readers envision the bottle as part of their daily routine.<'}, {'type': 'hybrid', 'description': '>Combine emotional appeal with key specifications by weaving technical details into a story-driven format. Balance environmental impact statistics with aspirational messaging about sustainability lifestyle choices.<'}]\n",
|
||||
"\n",
|
||||
"=== WORKER RESULT (formal) ===\n",
|
||||
"\n",
|
||||
"Introducing the EcoVessel Pro Series: A precision-engineered hydration solution crafted from 100% post-consumer recycled stainless steel, certified by the Global Recycled Standard (GRS).\n",
|
||||
"\n",
|
||||
"Technical Specifications:\n",
|
||||
"• Capacity: 750ml / 25.4 fl oz\n",
|
||||
"• Material: 18/8 food-grade recycled stainless steel (304 grade)\n",
|
||||
"• Wall thickness: 2mm double-wall vacuum insulation\n",
|
||||
"• Temperature retention: 24 hours cold / 12 hours hot\n",
|
||||
"• Weight: 340g / 12 oz (empty)\n",
|
||||
"\n",
|
||||
"Environmental Certifications:\n",
|
||||
"• Carbon Neutral Product certified by Climate Partner\n",
|
||||
"• BPA-free verification from NSF International\n",
|
||||
"• ISO 14001 Environmental Management certification\n",
|
||||
"\n",
|
||||
"Manufacturing Process:\n",
|
||||
"Manufactured using hydroelectric power in our carbon-neutral facility, each vessel undergoes a proprietary eco-sanitization process utilizing steam-based sterilization, eliminating chemical cleaning agents. The powder coating is applied through a zero-waste electrostatic process.\n",
|
||||
"\n",
|
||||
"Environmental Impact Metrics:\n",
|
||||
"• Eliminates approximately 167 single-use plastic bottles annually per user\n",
|
||||
"• 87% lower carbon footprint compared to traditional bottle manufacturing\n",
|
||||
"• 100% recyclable at end-of-life through our closed-loop recycling program\n",
|
||||
"• Saves 2,920 liters of water annually through eliminated plastic bottle production\n",
|
||||
"\n",
|
||||
"Each unit includes a digital tracking code for real-time impact monitoring and verification of authenticity. Engineered for a minimum 10-year service life under normal usage conditions.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"=== WORKER RESULT (conversational) ===\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"=== WORKER RESULT (hybrid) ===\n",
|
||||
"\n",
|
||||
"Meet the AquaVerde Elite - where your daily hydration ritual becomes a powerful statement for our planet's future.\n",
|
||||
"\n",
|
||||
"Imagine starting your day knowing that every sip you take helps prevent up to 167 single-use plastic bottles from entering our oceans annually. The AquaVerde Elite isn't just a water bottle; it's your personal ambassador in the fight against plastic pollution, crafted from aerospace-grade recycled stainless steel that's been given a second life.\n",
|
||||
"\n",
|
||||
"Built to accompany you through life's adventures, this 24oz companion features our innovative ThermaLock™ technology, maintaining your cold drinks frosty for 24 hours or your hot beverages steaming for 12 hours. The double-wall vacuum insulation isn't just about performance - it's engineered to use 30% less material than conventional designs while delivering superior temperature retention.\n",
|
||||
"\n",
|
||||
"The bottle's sleek silhouette houses thoughtful details that enhance your daily experience: a leak-proof AutoSeal cap that operates with one hand, a built-in carrying loop made from recycled climbing rope, and our signature CloudTouch™ exterior finish that's both grippy and gorgeous. Available in four nature-inspired colors (Ocean Deep, Forest Canopy, Desert Dawn, and Mountain Mist), each bottle's finish is created using a water-based, zero-VOC coating process.\n",
|
||||
"\n",
|
||||
"But perhaps the most beautiful feature is what you don't see - every AquaVerde Elite helps fund clean water projects in developing communities, with 2% of each purchase supporting water conservation initiatives worldwide. Your choice to carry the AquaVerde Elite isn't just about staying hydrated; it's about being part of a global movement toward a more sustainable future.\n",
|
||||
"\n",
|
||||
"Specifications that matter:\n",
|
||||
"• Capacity: 24oz/710ml\n",
|
||||
"• Weight: 12.8oz\n",
|
||||
"• Materials: 90% recycled 18/8 stainless steel\n",
|
||||
"• BPA-free, phthalate-free\n",
|
||||
"• Dishwasher safe\n",
|
||||
"• Lifetime warranty\n",
|
||||
"\n",
|
||||
"Join the growing community of AquaVerde carriers who've collectively prevented over 2 million single-use bottles from entering our ecosystems. Because every drop counts, and every choice matters.\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"ORCHESTRATOR_PROMPT = \"\"\"\n",
|
||||
"Analyze this task and break it down into 2-3 distinct approaches:\n",
|
||||
"\n",
|
||||
"Task: {task}\n",
|
||||
"\n",
|
||||
"Return your response in this format:\n",
|
||||
"\n",
|
||||
"<analysis>\n",
|
||||
"Explain your understanding of the task and which variations would be valuable.\n",
|
||||
"Focus on how each approach serves different aspects of the task.\n",
|
||||
"</analysis>\n",
|
||||
"\n",
|
||||
"<tasks>\n",
|
||||
" <task>\n",
|
||||
" <type>formal</type>\n",
|
||||
" <description>Write a precise, technical version that emphasizes specifications</description>\n",
|
||||
" </task>\n",
|
||||
" <task>\n",
|
||||
" <type>conversational</type>\n",
|
||||
" <description>Write an engaging, friendly version that connects with readers</description>\n",
|
||||
" </task>\n",
|
||||
"</tasks>\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"WORKER_PROMPT = \"\"\"\n",
|
||||
"Generate content based on:\n",
|
||||
"Task: {original_task}\n",
|
||||
"Style: {task_type}\n",
|
||||
"Guidelines: {task_description}\n",
|
||||
"\n",
|
||||
"Return your response in this format:\n",
|
||||
"\n",
|
||||
"<response>\n",
|
||||
"Your content here, maintaining the specified style and fully addressing requirements.\n",
|
||||
"</response>\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"orchestrator = FlexibleOrchestrator(\n",
|
||||
" orchestrator_prompt=ORCHESTRATOR_PROMPT,\n",
|
||||
" worker_prompt=WORKER_PROMPT,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"results = orchestrator.process(\n",
|
||||
" task=\"Write a product description for a new eco-friendly water bottle\",\n",
|
||||
" context={\n",
|
||||
" \"target_audience\": \"environmentally conscious millennials\",\n",
|
||||
" \"key_features\": [\"plastic-free\", \"insulated\", \"lifetime warranty\"]\n",
|
||||
" }\n",
|
||||
")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "py311",
|
||||
"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.11.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
42
patterns/agents/util.py
Normal file
42
patterns/agents/util.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from anthropic import Anthropic
|
||||
import os
|
||||
import re
|
||||
|
||||
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
|
||||
|
||||
def llm_call(prompt: str, system_prompt: str = "", model="claude-3-5-sonnet-20241022") -> str:
|
||||
"""
|
||||
Calls the model with the given prompt and returns the response.
|
||||
|
||||
Args:
|
||||
prompt (str): The user prompt to send to the model.
|
||||
system_prompt (str, optional): The system prompt to send to the model. Defaults to "".
|
||||
model (str, optional): The model to use for the call. Defaults to "claude-3-5-sonnet-20241022".
|
||||
|
||||
Returns:
|
||||
str: The response from the language model.
|
||||
"""
|
||||
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
|
||||
messages = [{"role": "user", "content": prompt}]
|
||||
response = client.messages.create(
|
||||
model=model,
|
||||
max_tokens=4096,
|
||||
system=system_prompt,
|
||||
messages=messages,
|
||||
temperature=0.1,
|
||||
)
|
||||
return response.content[0].text
|
||||
|
||||
def extract_xml(text: str, tag: str) -> str:
|
||||
"""
|
||||
Extracts the content of the specified XML tag from the given text. Used for parsing structured responses
|
||||
|
||||
Args:
|
||||
text (str): The text containing the XML.
|
||||
tag (str): The XML tag to extract content from.
|
||||
|
||||
Returns:
|
||||
str: The content of the specified XML tag, or an empty string if the tag is not found.
|
||||
"""
|
||||
match = re.search(f'<{tag}>(.*?)</{tag}>', text, re.DOTALL)
|
||||
return match.group(1) if match else ""
|
||||
Reference in New Issue
Block a user