|
| 1 | +""" |
| 2 | +Ragbits Chat Example: Code Planner |
| 3 | +
|
| 4 | +This example demonstrates how to use the ChatInterface with planning tools. |
| 5 | +
|
| 6 | +To run the script, execute the following command: |
| 7 | +
|
| 8 | + ```bash |
| 9 | + uv run ragbits api run examples.chat.code_planner:CodePlannerChat |
| 10 | + ``` |
| 11 | +""" |
| 12 | + |
| 13 | +from collections.abc import AsyncGenerator |
| 14 | + |
| 15 | +from pydantic import BaseModel |
| 16 | + |
| 17 | +from ragbits.agents import Agent, AgentOptions, ToolCall, ToolCallResult |
| 18 | +from ragbits.agents.tools.planning import PlanningState, create_planning_tools |
| 19 | +from ragbits.chat.interface import ChatInterface |
| 20 | +from ragbits.chat.interface.types import ChatContext, ChatResponseUnion, LiveUpdateType |
| 21 | +from ragbits.chat.interface.ui_customization import HeaderCustomization, UICustomization |
| 22 | +from ragbits.core.llms import LiteLLM |
| 23 | +from ragbits.core.prompt import ChatFormat, Prompt |
| 24 | + |
| 25 | + |
| 26 | +class CodePlannerInput(BaseModel): |
| 27 | + """ |
| 28 | + Input format for the CodePlannerPrompt. |
| 29 | + """ |
| 30 | + |
| 31 | + query: str |
| 32 | + |
| 33 | + |
| 34 | +class CodePlannerPrompt(Prompt[CodePlannerInput, str]): |
| 35 | + """ |
| 36 | + Prompt for a code planner agent. |
| 37 | + """ |
| 38 | + |
| 39 | + system_prompt = """ |
| 40 | + You are an expert software architect with planning capabilities. |
| 41 | +
|
| 42 | + For complex design requests: |
| 43 | + 1. Use `create_plan` to break down the architecture task into focused subtasks |
| 44 | + 2. Work through each task using `get_current_task` and `complete_task` |
| 45 | + 3. Keep working on tasks util there is nothing left |
| 46 | + 4. Build on context from completed tasks |
| 47 | +
|
| 48 | + Cover: technology stack, architecture patterns, scalability, and security. |
| 49 | + """ |
| 50 | + user_prompt = "{{ query }}" |
| 51 | + |
| 52 | + |
| 53 | +class CodePlannerChat(ChatInterface): |
| 54 | + """ |
| 55 | + Code planner agent with planning capabilities. |
| 56 | + """ |
| 57 | + |
| 58 | + ui_customization = UICustomization( |
| 59 | + header=HeaderCustomization(title="Code Planner", subtitle="Spec driven development", logo="📋"), |
| 60 | + welcome_message="Code planner", |
| 61 | + ) |
| 62 | + conversation_history = True |
| 63 | + show_usage = True |
| 64 | + |
| 65 | + def __init__(self) -> None: |
| 66 | + self.planning_state = PlanningState() |
| 67 | + self.agent = Agent( |
| 68 | + llm=LiteLLM("gpt-4.1-mini"), |
| 69 | + prompt=CodePlannerPrompt, |
| 70 | + tools=create_planning_tools(self.planning_state), |
| 71 | + default_options=AgentOptions(max_turns=50), |
| 72 | + keep_history=True, |
| 73 | + ) |
| 74 | + |
| 75 | + async def chat( |
| 76 | + self, |
| 77 | + message: str, |
| 78 | + history: ChatFormat, |
| 79 | + context: ChatContext, |
| 80 | + ) -> AsyncGenerator[ChatResponseUnion, None]: |
| 81 | + """ |
| 82 | + Chat implementation with planning capabilities. |
| 83 | +
|
| 84 | + Args: |
| 85 | + message: The current user message. |
| 86 | + history: Previous messages in the conversation. |
| 87 | + context: Additional context for the chat. |
| 88 | +
|
| 89 | + Yields: |
| 90 | + ChatResponse objects containing: |
| 91 | + - Text chunks for the response |
| 92 | + - Live updates showing planning progress |
| 93 | + - Plan items for planning tasks |
| 94 | + """ |
| 95 | + async for response in self.agent.run_streaming(CodePlannerInput(query=message)): |
| 96 | + match response: |
| 97 | + case str(): |
| 98 | + yield self.create_text_response(response) |
| 99 | + |
| 100 | + case ToolCall(): |
| 101 | + if response.name == "create_plan": |
| 102 | + yield self.create_live_update( |
| 103 | + update_id=response.id, |
| 104 | + type=LiveUpdateType.START, |
| 105 | + label="Creating plan...", |
| 106 | + ) |
| 107 | + |
| 108 | + case ToolCallResult(): |
| 109 | + if self.planning_state.plan: |
| 110 | + match response.name: |
| 111 | + case "create_plan": |
| 112 | + yield self.create_live_update( |
| 113 | + update_id=response.id, |
| 114 | + type=LiveUpdateType.FINISH, |
| 115 | + label="Plan created", |
| 116 | + description=f"{len(self.planning_state.plan.tasks)} tasks", |
| 117 | + ) |
| 118 | + for task in self.planning_state.plan.tasks: |
| 119 | + yield self.create_plan_item_response(task) |
| 120 | + |
| 121 | + case "get_current_task": |
| 122 | + if self.planning_state.plan.current_task: |
| 123 | + yield self.create_live_update( |
| 124 | + update_id=self.planning_state.plan.current_task.id, |
| 125 | + type=LiveUpdateType.START, |
| 126 | + label=self.planning_state.plan.current_task.description, |
| 127 | + ) |
| 128 | + yield self.create_plan_item_response(self.planning_state.plan.current_task) |
| 129 | + |
| 130 | + case "complete_task": |
| 131 | + if self.planning_state.plan.last_completed_task: |
| 132 | + yield self.create_live_update( |
| 133 | + update_id=self.planning_state.plan.last_completed_task.id, |
| 134 | + type=LiveUpdateType.FINISH, |
| 135 | + label=self.planning_state.plan.last_completed_task.description, |
| 136 | + description=self.planning_state.plan.last_completed_task.result, |
| 137 | + ) |
| 138 | + yield self.create_plan_item_response(self.planning_state.plan.last_completed_task) |
0 commit comments