diff --git a/docs/notebooks/Llama_Stack_Agent_Workflows.ipynb b/docs/notebooks/Llama_Stack_Agent_Workflows.ipynb new file mode 100644 index 000000000..87d9a6030 --- /dev/null +++ b/docs/notebooks/Llama_Stack_Agent_Workflows.ipynb @@ -0,0 +1,226 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Llama Stack Agent Workflows\n", + "\n", + "This notebook contains minimal implementations of common agent workflows discussed in the blog post [Building Effective Agent Workflows](https://www.anthropic.com/research/building-effective-agents). Examples are copied from [anthropic-cookbook](https://github.com/anthropics/anthropic-cookbook/tree/main/patterns/agents). " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "MODEL_ID = \"meta-llama/Llama-3.3-70B-Instruct\"" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "from llama_stack_client import LlamaStackClient\n", + "from llama_stack_client.types.agent_create_params import AgentConfig\n", + "from llama_stack_client.lib.agents.react.agent import ReActAgent\n", + "from llama_stack_client.lib.agents.agent import Agent\n", + "from rich.pretty import pprint\n", + "import json\n", + "import uuid\n", + "\n", + "client = LlamaStackClient(base_url=\"http://localhost:8321\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 1. Basic Multi-LLM Workflow\n", + "\n", + "1. **Prompt Chaining**: Decomposes a task into sequential subtasks, where each step builds on previous results. \n", + "\n", + "2. **Parallelization**: Distributes independent subtasks acorss multiple LLMs for concurrent processing. \n", + "\n", + "3. **Routing**: Dynamically selects specialized LLM paths based on input characteristics. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 1.1 Prompt Chaining" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "========= Turn: 0 =========\n", + "92: customer satisfaction score\n", + "45%: revenue growth\n", + "23%: market share\n", + "5%: customer churn\n", + "43: new user acquisition cost\n", + "78%: product adoption rate\n", + "87: employee satisfaction\n", + "34%: operating margin\n", + "8%: customer churn (changed to reflect the original value for comparison)\n", + "\n", + "\n", + "========= Turn: 1 =========\n", + "92%: customer satisfaction\n", + "45%: revenue growth\n", + "23%: market share\n", + "5%: customer churn\n", + "0.043: new user acquisition cost\n", + "78%: product adoption rate\n", + "87%: employee satisfaction\n", + "34%: operating margin\n", + "8%: previous customer churn\n", + "\n", + "\n", + "========= Turn: 2 =========\n", + "92%: customer satisfaction\n", + "87%: employee satisfaction\n", + "78%: product adoption rate\n", + "45%: revenue growth\n", + "34%: operating margin\n", + "23%: market share\n", + "8%: previous customer churn\n", + "5%: customer churn\n", + "0.043: new user acquisition cost\n", + "\n", + "\n", + "========= Turn: 3 =========\n", + "| Metric | Value |\n", + "|:--|--:|\n", + "| Customer Satisfaction | 92% |\n", + "| Employee Satisfaction | 87% |\n", + "| Product Adoption Rate | 78% |\n", + "| Revenue Growth | 45% |\n", + "| Operating Margin | 34% |\n", + "| Market Share | 23% |\n", + "| Previous Customer Churn | 8% |\n", + "| Customer Churn | 5% |\n", + "| New User Acquisition Cost | 0.043 |\n", + "\n", + "\n" + ] + } + ], + "source": [ + "vanilla_agent_config = AgentConfig(\n", + " model=MODEL_ID,\n", + " instructions=\"\"\"\n", + " You are a helpful assistant capable of structuring data extraction and formatting. \n", + "\n", + " You will be given tasks to extract and format data from a performance report. Here is the report:\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", + " sampling_params={\n", + " \"strategy\": {\"type\": \"top_p\", \"temperature\": 1.0, \"top_p\": 0.9},\n", + " },\n", + " toolgroups=[],\n", + " tool_config={\n", + " \"tool_choice\": \"auto\",\n", + " \"tool_prompt_format\": \"python_list\",\n", + " },\n", + " input_shields=[],\n", + " output_shields=[],\n", + " enable_session_persistence=False,\n", + ")\n", + "\n", + "vanilla_agent = Agent(client, vanilla_agent_config)\n", + "\n", + "session_id_1 = vanilla_agent.create_session(session_name=f\"vanilla_agent_{uuid.uuid4()}\")\n", + "\n", + "prompts = [\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", + "for i, prompt in enumerate(prompts): \n", + " response = vanilla_agent.create_turn(\n", + " messages=[\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": prompt,\n", + " }\n", + " ],\n", + " session_id=session_id_1,\n", + " stream=False,\n", + " )\n", + " print(\"========= Turn: \", i, \"=========\")\n", + " print(response.output_message.content)\n", + " print(\"\\n\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "master", + "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.10.16" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}