77from pydantic import BaseModel
88from pydantic_ai import Agent
99from pydantic_ai .models .anthropic import AnthropicModel
10+ from pydantic_ai .result import RunResult
1011from typing_extensions import Any , Dict , Optional , Union
1112
1213from patchwork .common .client .llm .utils import example_json_to_base_model
@@ -31,14 +32,14 @@ class Config:
3132
3233class AgenticStrategyV2 :
3334 def __init__ (
34- self ,
35- api_key : str ,
36- template_data : dict [str , str ],
37- system_prompt_template : str ,
38- user_prompt_template : str ,
39- agent_configs : list [AgentConfig ],
40- example_json : Union [str , dict [str , Any ]] = '{"output":"output text"}' ,
41- limit : Optional [int ] = None ,
35+ self ,
36+ api_key : str ,
37+ template_data : dict [str , str ],
38+ system_prompt_template : str ,
39+ user_prompt_template : str ,
40+ agent_configs : list [AgentConfig ],
41+ example_json : Union [str , dict [str , Any ]] = '{"output":"output text"}' ,
42+ limit : Optional [int ] = None ,
4243 ):
4344 self .__limit = limit
4445 self .__template_data = template_data
@@ -66,6 +67,19 @@ def __init__(
6667
6768 self .__agents .append (agent )
6869
70+ self .__request_tokens = 0
71+ self .__response_tokens = 0
72+
73+ def reset (self ):
74+ self .__request_tokens = 0
75+ self .__response_tokens = 0
76+
77+ def usage (self ):
78+ return {
79+ "request_tokens" : self .__request_tokens ,
80+ "response_tokens" : self .__response_tokens ,
81+ }
82+
6983 def execute (self , limit : Optional [int ] = None ) -> dict :
7084 agents_result = dict ()
7185 loop = asyncio .new_event_loop ()
@@ -75,8 +89,13 @@ def execute(self, limit: Optional[int] = None) -> dict:
7589 message_history = None
7690 agent_output = None
7791 for i in range (limit or self .__limit or sys .maxsize ):
78- agent_output = loop .run_until_complete (agent .run (user_message , message_history = message_history ))
92+ agent_output : RunResult [Any ] = loop .run_until_complete (
93+ agent .run (user_message , message_history = message_history )
94+ )
7995 message_history = agent_output .all_messages ()
96+ self .__request_tokens += agent_output .usage ().request_tokens or 0
97+ self .__response_tokens += agent_output .usage ().response_tokens or 0
98+
8099 if getattr (agent_output .data , _COMPLETION_FLAG_ATTRIBUTE , False ):
81100 break
82101 user_message = "Please continue"
@@ -88,27 +107,38 @@ def execute(self, limit: Optional[int] = None) -> dict:
88107 return dict ()
89108
90109 if len (agents_result ) == 1 :
91- final_result = loop .run_until_complete (self .__summariser .run (
92- "From the actions taken by the assistant. Please give me the result." ,
93- message_history = next (v for _ , v in agents_result .items ()).all_messages (),
94- ))
110+ final_result = loop .run_until_complete (
111+ self .__summariser .run (
112+ "From the actions taken by the assistant. Please give me the result." ,
113+ message_history = next (v for _ , v in agents_result .items ()).all_messages (),
114+ )
115+ )
95116 else :
96117 agent_summaries = []
97118 for agent_result in agents_result .values ():
98- agent_summary_result = loop .run_until_complete (self .__summariser .run (
99- "From the actions taken by the assistant. Please give me the result." ,
100- message_history = agent_result .all_messages (),
101- ))
119+ agent_summary_result = loop .run_until_complete (
120+ self .__summariser .run (
121+ "From the actions taken by the assistant. Please give me the result." ,
122+ message_history = agent_result .all_messages (),
123+ )
124+ )
125+ self .__request_token += agent_summary_result .usage ().request_tokens or 0
126+ self .__response_token += agent_summary_result .usage ().response_tokens or 0
127+
102128 agent_summary = getattr (agent_summary_result .data , _MESSAGE_ATTRIBUTE , None )
103129 if agent_summary is None :
104130 continue
105131
106132 agent_summaries .append (agent_summary )
107133 agent_summary_list = "\n * " + "\n * " .join (agent_summaries )
108- final_result = loop .run_until_complete (self .__summariser .run (
109- "Please give me the result from the following summary of what the assistants have done."
110- + agent_summary_list ,
111- ))
134+ final_result = loop .run_until_complete (
135+ self .__summariser .run (
136+ "Please give me the result from the following summary of what the assistants have done."
137+ + agent_summary_list ,
138+ )
139+ )
140+ self .__request_tokens += final_result .usage ().request_tokens or 0
141+ self .__response_tokens += final_result .usage ().response_tokens or 0
112142
113143 loop .close ()
114144 return final_result .data .dict ()
0 commit comments