跳到主要内容

agentchat.contrib.math_user_proxy_agent

MathUserProxyAgent

class MathUserProxyAgent(UserProxyAgent)

(实验性)一个可以处理数学问题的MathChat代理。

MAX_CONSECUTIVE_AUTO_REPLY

最大连续自动回复次数(未来可能会有变动)

__init__

def __init__(name: Optional[str] = "MathChatAgent",
is_termination_msg: Optional[Callable[
[Dict], bool]] = _is_termination_msg_mathchat,
human_input_mode: Literal["ALWAYS", "NEVER",
"TERMINATE"] = "NEVER",
default_auto_reply: Optional[Union[str, Dict,
None]] = DEFAULT_REPLY,
max_invalid_q_per_step=3,
**kwargs)

参数:

  • name str - 代理的名称
  • is_termination_msg function - 一个函数,接收一个字典形式的消息,并返回一个布尔值,指示接收到的消息是否是终止消息。 该字典可以包含以下键:"content", "role", "name", "function_call"。
  • human_input_mode str - 是否在每次收到消息时询问人工输入。 可能的值是 "ALWAYS"、"TERMINATE"、"NEVER"。 (1) 当为 "ALWAYS" 时,代理每次收到消息都会提示人工输入。 在此模式下,当人工输入为 "exit" 或当 is_termination_msg 为 True 且没有人工输入时,对话停止。 (2) 当为 "TERMINATE" 时,代理仅在收到终止消息或自动回复次数达到 max_consecutive_auto_reply 时提示人工输入。 (3) (默认) 当为 "NEVER" 时,代理将永远不会提示人工输入。在此模式下,当自动回复次数达到 max_consecutive_auto_reply 或 is_termination_msg 为 True 时,对话停止。
  • default_auto_reply str or dict or None - 当没有生成代码执行或基于llm的回复时的默认自动回复消息。
  • max_invalid_q_per_step int - (新增)每步最大无效查询数。
  • **kwargs dict - UserProxyAgent 中的其他 kwargs。

消息生成器

@staticmethod
def message_generator(sender, recipient, context)

为助理代理生成一个提示,包含给定的问题和提示。

参数:

  • sender Agent - 消息的发送者。
  • recipient Agent - 消息的接收者。
  • context dict - 包含以下字段的字典:
  • problem str - 需要解决的问题。
  • prompt_type str, Optional - the type of the prompt. Possible values are "default", "python", "wolfram". (1) "default": the prompt that allows the agent to choose between 3 ways to solve a problem:
    1. 编写一个Python程序直接解决它。
    2. 直接解决它而不使用python。
    3. 逐步使用python解决它。 (2) "python": 来自"default"提示的第三种方式的简化提示,要求助手逐步使用python解决问题。 (3) "two_tools": 类似于"python"提示的简化提示,但允许模型在Python和Wolfram Alpha之间选择来解决问题。
  • customized_prompt str, 可选 - 使用的自定义提示。如果它不是None,则忽略prompt_type。

返回:

  • str - 生成的提示,准备好发送给助手代理。

execute_one_python_code

def execute_one_python_code(pycode)

执行python代码块。

之前的Python代码将会保存并与新代码一起执行。 如果需要,“print”函数也会添加到代码的最后一行。

执行一个wolfram查询

def execute_one_wolfram_query(query: str)

运行一个wolfram查询并返回输出。

参数:

  • query - 查询的字符串。

返回:

  • output - 包含查询输出的字符串。
  • is_success - 布尔值,表示查询是否成功。

从字典或环境中获取

def get_from_dict_or_env(data: Dict[str, Any],
key: str,
env_key: str,
default: Optional[str] = None) -> str

从字典或环境变量中获取一个值。

WolframAlphaAPIWrapper

class WolframAlphaAPIWrapper(BaseModel)

Wolfram Alpha 的封装器。

使用文档:

  1. 前往 wolfram alpha 并注册一个开发者账户
  2. 创建一个应用并获取您的APP ID
  3. 将您的APP ID保存到WOLFRAM_ALPHA_APPID环境变量中
  4. pip 安装 wolframalpha

wolfram_client

配置

class Config()

此pydantic对象的配置。

validate_environment

@root_validator(skip_on_failure=True)
def validate_environment(cls, values: Dict) -> Dict

验证环境中是否存在api密钥和python包。

运行

def run(query: str) -> Tuple[str, bool]

通过WolframAlpha运行查询并解析结果。