|
6 | 6 | # @Email : lc299034@antgroup.com |
7 | 7 | # @FileName: agent.py |
8 | 8 | """The definition of agent paradigm.""" |
| 9 | +import json |
9 | 10 | from abc import abstractmethod |
10 | 11 | from datetime import datetime |
11 | 12 | from typing import Optional |
12 | 13 |
|
| 14 | +from langchain_core.utils.json import parse_json_markdown |
| 15 | + |
13 | 16 | from agentuniverse.agent.agent_model import AgentModel |
14 | 17 | from agentuniverse.agent.input_object import InputObject |
15 | 18 | from agentuniverse.agent.output_object import OutputObject |
16 | 19 | from agentuniverse.agent.plan.planner.planner import Planner |
17 | 20 | from agentuniverse.agent.plan.planner.planner_manager import PlannerManager |
| 21 | +from agentuniverse.base.annotation.trace import trace_agent |
18 | 22 | from agentuniverse.base.component.component_base import ComponentBase |
19 | 23 | from agentuniverse.base.component.component_enum import ComponentEnum |
20 | 24 | from agentuniverse.base.config.application_configer.application_config_manager \ |
21 | 25 | import ApplicationConfigManager |
22 | 26 | from agentuniverse.base.config.component_configer.configers.agent_configer \ |
23 | 27 | import AgentConfiger |
| 28 | +from agentuniverse.base.util.logging.logging_util import LOGGER |
24 | 29 | from agentuniverse.llm.llm import LLM |
25 | 30 |
|
26 | 31 |
|
@@ -66,6 +71,7 @@ def parse_result(self, planner_result: dict) -> dict: |
66 | 71 | """ |
67 | 72 | pass |
68 | 73 |
|
| 74 | + @trace_agent |
69 | 75 | def run(self, **kwargs) -> OutputObject: |
70 | 76 | """Agent instance running entry. |
71 | 77 |
|
@@ -156,3 +162,35 @@ def initialize_by_component_configer(self, component_configer: AgentConfiger) -> |
156 | 162 | plan=plan, memory=memory, action=action) |
157 | 163 | self.agent_model = agent_model |
158 | 164 | return self |
| 165 | + |
| 166 | + def langchain_run(self, input: str, callbacks=None, **kwargs): |
| 167 | + """Run the agent model using LangChain.""" |
| 168 | + try: |
| 169 | + parse_result = parse_json_markdown(input) |
| 170 | + except Exception as e: |
| 171 | + LOGGER.error(f"langchain run parse_json_markdown error,input(parse_result) error({str(e)})") |
| 172 | + return "Error , Your Action Input is not a valid JSON string" |
| 173 | + output_object = self.run(**parse_result, callbacks=callbacks, **kwargs) |
| 174 | + result_dict = {} |
| 175 | + for key in self.output_keys(): |
| 176 | + result_dict[key] = output_object.get_data(key) |
| 177 | + return result_dict |
| 178 | + |
| 179 | + def as_langchain_tool(self): |
| 180 | + """Convert to LangChain tool.""" |
| 181 | + from langchain.agents.tools import Tool |
| 182 | + format_dict = {} |
| 183 | + for key in self.input_keys(): |
| 184 | + format_dict.setdefault(key, "input val") |
| 185 | + format_str = json.dumps(format_dict) |
| 186 | + |
| 187 | + args_description = f""" |
| 188 | + to use this tool,your input must be a json string,must contain all keys of {self.input_keys()}, |
| 189 | + and the value of the key must be a json string,the format of the json string is as follows: |
| 190 | + ```{format_str}``` |
| 191 | + """ |
| 192 | + return Tool( |
| 193 | + name=self.agent_model.info.get("name"), |
| 194 | + func=self.langchain_run, |
| 195 | + description=self.agent_model.info.get("description") + args_description |
| 196 | + ) |
0 commit comments