@@ -128,26 +128,74 @@ ajet:
128128
129129**Workflow:** `tutorial/example_math_agent/math_agent.py`
130130
131- ` ` ` python title="Workflow Sketch"
132- self.toolkit = Toolkit()
133- self.toolkit.register_tool_function(execute_python_code)
134-
135- self.agent = ReActAgent(
136- name="math_react_agent",
137- sys_prompt=system_prompt,
138- model=tuner.as_agentscope_model(), # trainer-managed model wrapper
139- formatter=DashScopeChatFormatter(),
140- toolkit=self.toolkit,
141- memory=InMemoryMemory(),
142- )
143-
144- msg = Msg("user", init_messages[0]["content"], role="user")
145- result = await self.agent.reply(msg)
146- final_answer = extract_final_answer(result)
147-
148- # IMPORTANT: provide final answer to the judge via WorkflowOutput metadata
149- return WorkflowOutput(reward=None, metadata={"final_answer": final_answer})
150- ` ` `
131+ === "AgentScope"
132+
133+ ` ` ` python title="Workflow Sketch"
134+ self.toolkit = Toolkit()
135+ self.toolkit.register_tool_function(execute_python_code)
136+
137+ self.agent = ReActAgent(
138+ name="math_react_agent",
139+ sys_prompt=system_prompt,
140+ model=model_tuner, # trainer-managed model wrapper
141+ formatter=DashScopeChatFormatter(),
142+ toolkit=self.toolkit,
143+ memory=InMemoryMemory(),
144+ )
145+
146+ msg = Msg("user", init_messages[0]["content"], role="user")
147+ result = await self.agent.reply(msg)
148+ final_answer = extract_final_answer(result)
149+
150+ # IMPORTANT: provide final answer to the judge via WorkflowOutput metadata
151+ return WorkflowOutput(reward=None, metadata={"final_answer": final_answer})
152+ ` ` `
153+
154+ === "Langchain"
155+
156+ ` ` ` python
157+
158+ class ExampleMathLearn(Workflow):
159+
160+ name: str = "math_agent_workflow"
161+ system_prompt: str = dedent("""
162+ You are an agent specialized in solving math problems.
163+ Please solve the math problem given to you.
164+ You can write and execute Python code to perform calculation or verify your answer.
165+ You should return your final answer within \\ boxed{{}}.
166+ """)
167+
168+ async def execute(self, workflow_task: WorkflowTask, tuner: AjetTuner) -> WorkflowOutput: # type: ignore
169+ # tuner to api key
170+ url_and_apikey = tuner.as_oai_baseurl_apikey()
171+ base_url = url_and_apikey.base_url
172+ api_key = url_and_apikey.api_key
173+
174+ from langchain_openai import ChatOpenAI
175+ llm=ChatOpenAI(
176+ base_url=base_url,
177+ api_key=lambda:api_key,
178+ )
179+ agent=create_agent(
180+ model=llm,
181+ system_prompt=self.system_prompt,
182+ )
183+
184+ # take out query
185+ query = workflow_task.task.main_query
186+
187+ response = agent.invoke({
188+ "messages": [
189+ {
190+ "role": "user",
191+ "content": query
192+ }
193+ ],
194+ })
195+
196+ final_answer = response['messages'][-1].content
197+ return WorkflowOutput(reward=None, metadata={"final_answer": final_answer})
198+ ` ` `
151199
152200!!! warning "Important"
153201 Always provide the final answer via `WorkflowOutput.metadata` so the judge can score it.
0 commit comments