Maplemx / Agently

[AI Agent Application Development Framework] - 🚀 Build AI agent native application in very few code 💬 Easy to interact with AI agent in code using structure data and chained-calls syntax 🧩 Enhance AI Agent using plugins instead of rebuild a whole new agent
http://agently.tech
Apache License 2.0
908 stars 100 forks source link

开两个agent在一个循环中,出现的问答bug #75

Closed ZohnSun closed 2 months ago

ZohnSun commented 2 months ago

我这里一共使用了三个agent:

  1. 路由agent:判断用户意图,是否需要调用制度查询;
  2. 闲聊agent,就是一个正常的agent,做了一些配置
  3. 带有知识库向量查询的agent;

``from agent_factory_api import agent_factory_zhipu from agent_tool.milvus_tool import get_search_result

路由模型-对用户问题进行基础判断-分发至正确的其他agent

def route_agent(user_input,history): route = ( agent_factory_zhipu.create_agent('rout') .set_settings("model.ZhipuAI.options", {"model": "glm-4"}) .set_role("role", "你是一个路由助手,您的工作是结合上下文帮助用户找到合适的Agent代理来回答用户的问题") .input(user_input) .chat_history(history) .output({ "intention": ("制度流程查询 | 绘图 | 闲聊 | 一般提问", "从'制度流程查询','绘图','闲聊','一般提问'中选择一项作为你对{user_input}的意图的判断结果") }).start() ) print('__') print(route) print('__') print('路由判断',route['intention']) return route['intention']

闲聊Agent

def get_agent_response_chatting(question:str,history): agent_standard_senior = ( agent_factory_zhipu.create_agent('standard_role') .set_settings("model.ZhipuAI.options", {"model": "glm-4"})

)
result=(
    # 全局配置
    agent_standard_senior.general("Output regulations", "根据提问方式匹配对应的输出语言,默认是中文")
    # 角色配置
    .role({
                "Name": "数智+",
                "Task": "作为同事,根据上下文为用户解答问题,提供想法",
                "Position":'销售管理运营中心运营助理',
                "Model":'由销售管理运营中心微调的基于Transformer编码器-解码器架构的大语言模型'
            })
    # user_info: agent需要了解的用户相关的信息
    .user_info("The user you are talking to is a colleague of yours who is not very familiar with the system and issues. He needs your assistance in searching for information and answering questions")
    # abstract: 对于之前对话(尤其是较长对话)的总结信息
    .abstract(None)
    # chat_history: 按照OpenAI消息列格式的对话记录list
    ## 支持:三种角色
    ## [{ "role": "system", "content": "" },
    ##  { "role": "assistant", "content": "" },
    ##  { "role": "user", "content": "" }]
    .chat_history(history)
    # input: 和本次请求相关的输入信息
    .input({
        "question": question
    })
    # info: 为本次请求提供的额外补充信息
    .info("用户的部门信息", ["", "", ""])
    .info("相关关键词", ["", ""])
    # instruct: 为本次请求提供的行动指导信息
    .instruct([
        "请使用{reply_style_expect}的回复风格,回复{question}提出的问题",
    ])
    # output: 对本次请求的输出提出格式和内容的要求
    .output({
        "reply": ("str", "对{question}的直接回复"),
        "next_questions": ([
            ("str",
             "根据{reply}内容,结合{user_info}提供的用户信息," +
             "给用户推荐的可以进一步提问的问题"
            )], "不少于3个"),
    })
    # start: 用于开始本次主要交互请求
    .start()
)
return result

制度流程查询Agent

def get_agent_response_processSystem(question,history): def print_streaming_content(data: str): print(data, end="") tool_info = { "tool_name": "流程制度查询工具", "desc": "从知识库文档中查询公司流程及制度", "args": { "context": ( "str", "要查询的相关制度流程内容,使用中文字符串" ) }, "func": get_search_result }

创建该生命周期智能体:考虑到文档查询用量,使用便宜的模型--穷啊

agent_standard_lower = (
    agent_factory_zhipu.create_agent('knowledge_base_agent')
    .set_settings("model.ZhipuAI.options", {"model": "glm-3-turbo"})

)
# doc=get_search_result(question)
result=(
    # 全局配置
    agent_standard_lower.general("Output regulations", "根据提问方式匹配对应的输出语言,默认是中文")
    # 工具配置
    .register_tool(
        tool_name=tool_info["tool_name"],
        desc=tool_info["desc"],
        args=tool_info["args"],
        func=tool_info["func"],
    )
    # 角色配置
    .role({
                "Name": "数智+",
                "Task": "作为同事,根据上下文为用户查询流程制度和文档",
                "Position":'条理化总结制度文档的内容'
            })
    # user_info: agent需要了解的用户相关的信息
    .user_info("对公司制度流程不清楚,需要完整的了解其内容")
    # abstract: 对于之前对话(尤其是较长对话)的总结信息
    .abstract(None)
    # chat_history: 按照OpenAI消息列格式的对话记录list
    ## 支持:三种角色
    ## [{ "role": "system", "content": "" },
    ##  { "role": "assistant", "content": "" },
    ##  { "role": "user", "content": "" }]
    .chat_history(history)
    # input: 和本次请求相关的输入信息
    .input({
        "question": question,
    })
    # info: 为本次请求提供的额外补充信息
    # .info("用户的部门信息", ["", "", ""])
    # .info("相关关键词", ["", ""])
    # .info("document_info", doc)
    # instruct: 为本次请求提供的行动指导信息
    .instruct([
        "请从{document_info}查询结果中找{question}的答案,条理化总结知识,同事告知文档来源Document_title",
    ])
    # output: 对本次请求的输出提出格式和内容的要求
    # .output({
    #     "reply": ("str", "请条目化的输出问题相关文档的内容,使用换行符分割"),
    #     "document_name": ([
    #         ("str",
    #          "流程制度文档中Document_title名称"
    #         )],)
    # })
    # start: 用于开始本次主要交互请求
    .segment(
        "required_info_list",
        [
            {
                "知识对象": ("str", "回答{input}问题时,需要了解相关知识的具体对象"),
                "已知信息": ("str", "根据之前所有对话历史,总结已知信息"),
                "是否完备": ("bool", "判断你是否确信自己拥有{知识对象}的关键知识或信息,如果不了解,输出false"),
                "关键知识点或信息补充": ("str", "如果{是否完备}==false,给出需要了解的关键知识或需要用户提供的信息补充,否则输出空字符串''"),
            }
        ],
    )
    .segment(
        "certain_reply",
        "根据{required_info_list}给出回复,展开详细陈述自己了解的关键知识点内容",
        print_streaming_content,
        is_streaming=True,
    )
    .segment(
        "uncertain_reply",
        "根据{required_info_list}的信息,向用户说明自己不了解的信息,请用户提供或自行查找",
        print_streaming_content,
        is_streaming=True,
    )
    .segment(
        "next_topic_suggestions",
        "根据之前所有生成内容的信息,给出接下来可以进一步讨论的问题或话题建议,如果没有则输出空字符串''",
        print_streaming_content,
        is_streaming=True,
    )
    # .on_delta(lambda data: print(data, end=""))
    .start()
)
return

def ai_response(question,history): if route_agent(question,history)=='制度流程查询': print('流程查询') return get_agent_response_processSystem(question,history) else: print('定位失败') return get_agent_response_chatting(question,history)

if name == 'main':

print(get_agent_response_processSystem('信息系统建设管理办法有哪些内容'))

history=[]# { "role": "system", "content": "" }
while True:
    question=input('请输入问题:')

    result=ai_response(question,history)
    history.append({"role": "user", "content": question})
    print('[ai:]')
    print(result)
    history.append({"role": "assistant", "content": result})