1313from langgraph .graph .message import add_messages
1414from pydantic import BaseModel , Field
1515
16+ from comps .cores .telemetry .opea_telemetry import opea_telemetry , tracer
17+
1618from ...global_var import threads_global_kv
1719from ...utils import has_multi_tool_inputs , tool_renderer
1820from ..base_agent import BaseAgent
@@ -54,6 +56,7 @@ class PlanStepChecker:
5456 str: A decision for whether we should use this plan or not
5557 """
5658
59+ @opea_telemetry
5760 def __init__ (self , llm , is_vllm = False ):
5861 class grade (BaseModel ):
5962 binary_score : str = Field (description = "executable score 'yes' or 'no'" )
@@ -66,9 +69,15 @@ class grade(BaseModel):
6669 output_parser = PydanticToolsParser (tools = [grade ], first_tool_only = True )
6770 self .chain = plan_check_prompt | llm | output_parser
6871
72+ @opea_telemetry
73+ def __llm_invoke__ (self , state ):
74+ scored_result = self .chain .invoke (state )
75+ return scored_result
76+
77+ @opea_telemetry
6978 def __call__ (self , state ):
7079 # print("---CALL PlanStepChecker---")
71- scored_result = self .chain . invoke (state )
80+ scored_result = self .__llm_invoke__ (state )
7281 score = scored_result .binary_score
7382 print (f"Task is { state ['context' ]} , Score is { score } " )
7483 if score .startswith ("yes" ):
@@ -79,6 +88,7 @@ def __call__(self, state):
7988
8089# Define workflow Node
8190class Planner :
91+ @opea_telemetry
8292 def __init__ (self , llm , plan_checker = None , is_vllm = False ):
8393 if is_vllm :
8494 llm = llm .bind_tools ([Plan ], tool_choice = {"function" : {"name" : Plan .__name__ }})
@@ -88,6 +98,12 @@ def __init__(self, llm, plan_checker=None, is_vllm=False):
8898 self .llm = planner_prompt | llm | output_parser
8999 self .plan_checker = plan_checker
90100
101+ @opea_telemetry
102+ def __llm_invoke__ (self , messages ):
103+ plan = self .llm .invoke (messages )
104+ return plan
105+
106+ @opea_telemetry
91107 def __call__ (self , state ):
92108 print ("---CALL Planner---" )
93109 input = state ["messages" ][- 1 ].content
@@ -96,7 +112,7 @@ def __call__(self, state):
96112 while not success :
97113 while not success :
98114 try :
99- plan = self .llm . invoke ({"messages" : [("user" , state ["messages" ][- 1 ].content )]})
115+ plan = self .__llm_invoke__ ({"messages" : [("user" , state ["messages" ][- 1 ].content )]})
100116 print ("Generated plan: " , plan )
101117 success = True
102118 except OutputParserException as e :
@@ -116,6 +132,7 @@ def __call__(self, state):
116132
117133
118134class Executor :
135+ @opea_telemetry
119136 def __init__ (self , llm , tools = []):
120137 prompt = hwchase17_react_prompt
121138 if has_multi_tool_inputs (tools ):
@@ -126,6 +143,7 @@ def __init__(self, llm, tools=[]):
126143 agent = agent_chain , tools = tools , handle_parsing_errors = True , max_iterations = 50
127144 )
128145
146+ @opea_telemetry
129147 def __call__ (self , state ):
130148 print ("---CALL Executor---" )
131149 plan = state ["plan" ]
@@ -151,6 +169,7 @@ def __call__(self, state):
151169
152170
153171class AnswerMaker :
172+ @opea_telemetry
154173 def __init__ (self , llm , is_vllm = False ):
155174 if is_vllm :
156175 llm = llm .bind_tools ([Response ], tool_choice = {"function" : {"name" : Response .__name__ }})
@@ -159,13 +178,19 @@ def __init__(self, llm, is_vllm=False):
159178 output_parser = PydanticToolsParser (tools = [Response ], first_tool_only = True )
160179 self .llm = answer_make_prompt | llm | output_parser
161180
181+ @opea_telemetry
182+ def __llm_invoke__ (self , state ):
183+ output = self .llm .invoke (state )
184+ return output
185+
186+ @opea_telemetry
162187 def __call__ (self , state ):
163188 print ("---CALL AnswerMaker---" )
164189 success = False
165190 # sometime, LLM will not provide accurate steps per ask, try more than one time until success
166191 while not success :
167192 try :
168- output = self .llm . invoke (state )
193+ output = self .__llm_invoke__ (state )
169194 print ("Generated response: " , output .response )
170195 success = True
171196 except OutputParserException as e :
@@ -183,6 +208,7 @@ class FinalAnswerChecker:
183208 str: A decision for whether we should use this plan or not
184209 """
185210
211+ @opea_telemetry
186212 def __init__ (self , llm , is_vllm = False ):
187213 class grade (BaseModel ):
188214 binary_score : str = Field (description = "executable score 'yes' or 'no'" )
@@ -194,9 +220,15 @@ class grade(BaseModel):
194220 output_parser = PydanticToolsParser (tools = [grade ], first_tool_only = True )
195221 self .chain = answer_check_prompt | llm | output_parser
196222
223+ @opea_telemetry
224+ def __llm_invoke__ (self , state ):
225+ output = self .chain .invoke (state )
226+ return output
227+
228+ @opea_telemetry
197229 def __call__ (self , state ):
198230 print ("---CALL FinalAnswerChecker---" )
199- scored_result = self .chain . invoke (state )
231+ scored_result = self .__llm_invoke__ (state )
200232 score = scored_result .binary_score
201233 print (f"Answer is { state ['response' ]} , Grade of good response is { score } " )
202234 if score .startswith ("yes" ):
@@ -206,19 +238,26 @@ def __call__(self, state):
206238
207239
208240class Replanner :
241+ @opea_telemetry
209242 def __init__ (self , llm , answer_checker = None ):
210243 llm = llm .bind_tools ([Plan ])
211244 output_parser = PydanticToolsParser (tools = [Plan ], first_tool_only = True )
212245 self .llm = replanner_prompt | llm | output_parser
213246 self .answer_checker = answer_checker
214247
248+ @opea_telemetry
249+ def __llm_invoke__ (self , state ):
250+ output = self .llm .invoke (state )
251+ return output
252+
253+ @opea_telemetry
215254 def __call__ (self , state ):
216255 print ("---CALL Replanner---" )
217256 success = False
218257 # sometime, LLM will not provide accurate steps per ask, try more than one time until success
219258 while not success :
220259 try :
221- output = self .llm . invoke (state )
260+ output = self .__llm_invoke__ (state )
222261 success = True
223262 print ("Replan: " , output )
224263 except OutputParserException as e :
@@ -230,6 +269,7 @@ def __call__(self, state):
230269
231270
232271class PlanExecuteAgentWithLangGraph (BaseAgent ):
272+ @opea_telemetry
233273 def __init__ (self , args , with_memory = False , ** kwargs ):
234274 super ().__init__ (args , local_vars = globals (), ** kwargs )
235275
@@ -260,6 +300,7 @@ def __init__(self, args, with_memory=False, **kwargs):
260300 def prepare_initial_state (self , query ):
261301 return {"messages" : [("user" , query )]}
262302
303+ @opea_telemetry
263304 async def stream_generator (self , query , config , thread_id = None ):
264305 initial_state = self .prepare_initial_state (query )
265306 if thread_id is not None :
@@ -282,6 +323,7 @@ async def stream_generator(self, query, config, thread_id=None):
282323 yield f"data: { repr (event )} \n \n "
283324 yield "data: [DONE]\n \n "
284325
326+ @opea_telemetry
285327 async def non_streaming_run (self , query , config ):
286328 initial_state = self .prepare_initial_state (query )
287329 try :
0 commit comments