@@ -21,7 +21,7 @@ def ollama_generate(prompt: str, is_json_response: bool = True):
2121 payload = {
2222 "model" : OLLAMA_MODEL ,
2323 "prompt" : prompt ,
24- "stream" : False ,
24+ "stream" : True ,
2525 }
2626
2727 if is_json_response :
@@ -52,6 +52,7 @@ def create_message(role: str, content: str ) -> dict:
5252 "content" : content ,
5353 "timestamp" : f"{ datetime .now ()} "
5454 }
55+
5556system_message = create_message (
5657 "system" ,
5758 """
@@ -114,10 +115,24 @@ def validate_tool_args(tool_name: str, args: dict) -> tuple[bool, str]:
114115 return True , "Valid"
115116 except jsonschema .ValidationError as e :
116117 return False , e .message
117-
118+
119+ def run_planning_agent (user_prompt : str ):
120+ planning_prompt = f"""
121+ You are a planning agent that creates a step-by-step plan to answer the user's question.
122+ Your response should be a JSON object with a "plan" field that contains an array of steps.
123+ Each step should be a string describing a single action or thought process that leads towards
124+ answering the user's question. The plan should be detailed and cover all necessary steps to arrive at the final answer.
125+ The user's question is: "{ user_prompt } ".
126+ """
127+ return ollama_generate (planning_prompt )
128+
118129
119130def run_agent (user_prompt : str , max_steps : int = 5 ):
120- conversation_context .append (create_message ("user" , user_prompt ))
131+ plan = run_planning_agent (user_prompt )
132+ conversation_context .append (create_message ("plan" , plan ))
133+ print (f"\n [plan]: Generated plan:\n { plan } \n " )
134+
135+ conversation_context .append (create_message ("[plan]" , plan ))
121136
122137 for step in range (max_steps ):
123138 context = parse_conversation_context ()
@@ -145,19 +160,19 @@ def run_agent(user_prompt: str, max_steps: int = 5):
145160 is_valid , validation_msg = validate_tool_args (tool_name , arguments )
146161
147162 if not is_valid :
148- observation = f"Tool '{ tool_name } ' validation failed: { validation_msg } "
149- print (f"[observation ]: { observation } " )
150- conversation_context .append (create_message ("observation" , observation ))
163+ tool_call_respose = f"Tool '{ tool_name } ' validation failed: { validation_msg } "
164+ print (f"[tool_call ]: { tool_call_respose } " )
165+ conversation_context .append (create_message ("observation" , tool_call_respose ))
151166 continue
152167
153168 try :
154169 tool_result = TOOLS [tool_name ](** arguments )
155- observation = f"Tool '{ tool_name } ' with arguments { arguments } returned: { tool_result } "
170+ tool_call_respose = f"Tool '{ tool_name } ' with arguments { arguments } returned: { tool_result } "
156171 except Exception as e :
157- observation = f"Tool '{ tool_name } ' failed with error: { str (e )} "
172+ tool_call_respose = f"Tool '{ tool_name } ' failed with error: { str (e )} "
158173
159- print (f"[observation ]: { observation } " )
160- conversation_context .append (create_message ("observation " , observation ))
174+ print (f"[tool_call ]: { tool_call_respose } " )
175+ conversation_context .append (create_message ("tool_call " , tool_call_respose ))
161176 continue
162177
163178 else :
0 commit comments