chatchat-space / Langchain-Chatchat

Langchain-Chatchat(原Langchain-ChatGLM)基于 Langchain 与 ChatGLM, Qwen 与 Llama 等语言模型的 RAG 与 Agent 应用 | Langchain-Chatchat (formerly langchain-ChatGLM), local knowledge based LLM (like ChatGLM, Qwen and Llama) RAG and Agent app with langchain
Apache License 2.0
31.93k stars 5.56k forks source link

建议增加 `create_structured_glm4_chat_agent` 或类似方法,使glm4 agent可以直接离线使用 #4707

Open XDcsy opened 3 months ago

XDcsy commented 3 months ago

目前在 agents_registry.py 中,预置了 glm3qwen 的 agent 模板,但对 glm4 系列的模型,会走到调用 hub.pull("hwchase17/structured-chat-agent") 的分支,这里需要互联网连接。

因为我们部署在离线环境,搭配glm4时,chat和知识库都在部署完成后都直接可用,只有agent还需要手动从外网pull这个prompt导入进来。

鉴于项目已经为 glm3 提供了agent 模板,是否考虑为 glm4 也提供一个,或者直接将这个通用的prompt内置?

    if "glm3" in llm.model_name.lower():
        agent = create_structured_glm3_chat_agent(llm=llm, tools=tools)
        ......

    elif "qwen" in llm.model_name.lower():
        return create_structured_qwen_chat_agent(llm=llm, tools=tools, callbacks=callbacks)

    else:
        if prompt is not None:
            prompt = ChatPromptTemplate.from_messages([SystemMessage(content=prompt)])
        else:
            prompt = hub.pull("hwchase17/structured-chat-agent")  # default prompt
LeeGitHub1 commented 2 months ago

请问你的glm4可以调用tools吗,我的会发生这种情况https://github.com/chatchat-space/Langchain-Chatchat/issues/4712

Unexpectedlyc commented 2 months ago

这个直接下载到本地就好了,我已经实现了

diaozhengang commented 2 months ago

请问你是怎么下载到本地的?langchain.hub.pull的时候不知道下哪去了

Unexpectedlyc commented 2 months ago

/> 请问你是怎么下载到本地的?langchain.hub.pull的时候不知道下哪去了 我直接写死在代码里了,也没空做优化,平时太忙了,在libs\chatchat-server\chatchat\server\agent\agent_factory\agents_registry.py文件中添加下面的函数

def get_prompt():
    prompt_dict={
                'commit_hash': 'ea510f70a5872eb0f41a4e3b7bb004d5711dc127adee08329c664c6c8be5f13c',
                'manifest':
                        {
                            'id': ['langchain', 'prompts', 'chat', 'ChatPromptTemplate'],
                            'lc': 1, 'type': 'constructor',
                            'kwargs':
                                {
                                    'messages':[
                                                {'id': ['langchain', 'prompts', 'chat', 'SystemMessagePromptTemplate'],
                                                 'lc': 1,
                                                 'type': 'constructor',
                                                 'kwargs': {'prompt':
                                                                {'id': ['langchain', 'prompts', 'prompt', 'PromptTemplate'],
                                                                 'lc': 1,
                                                                 'type': 'constructor',
                                                                 'kwargs': {
                                                                            'template': 'Respond to the human as helpfully and accurately as '
                                                                            'possible. You have access to the following tools:\n\n{'
                                                                            'tools}\n\nUse a json blob to specify a tool by providing '
                                                                            'an action key (tool name) and an action_input key (tool '
                                                                            'input).\n\nValid "action" values: "Final Answer" or {'
                                                                            'tool_names}\n\nProvide only ONE action per $JSON_BLOB, '
                                                                            'as shown:\n\n```\n{{\n  "action": $TOOL_NAME,'
                                                                            '\n  "action_input": $INPUT\n}}\n```\n\nFollow this '
                                                                            'format:\n\nQuestion: input question to answer\nThought: '
                                                                            'consider previous and subsequent '
                                                                            'steps\nAction:\n```\n$JSON_BLOB\n```\nObservation: action '
                                                                            'result\n... (repeat Thought/Action/Observation N '
                                                                            'times)\nThought: I know what to respond\nAction:\n```\n{{'
                                                                            '\n  "action": "Final Answer",\n  "action_input": "Final '
                                                                            'response to human"\n}}\n\nBegin! Reminder to ALWAYS '
                                                                            'respond with a valid json blob of a single action. Use '
                                                                            'tools if necessary. Respond directly if appropriate. '
                                                                            'Format is Action:```$JSON_BLOB```then Observation',
                                                                            'input_variables': ['tool_names', 'tools'],
                                                                            'template_format':'f-string',
                                                                            'partial_variables': {}
                                                                            }
                                                                }
                                                            }
                                                 },
                                                {'id': ['langchain','prompts','chat','MessagesPlaceholder'],
                                                 'lc': 1,
                                                 'type': 'constructor',
                                                 'kwargs': {'optional': True, 'variable_name': 'chat_history'}},
                                                {'id': ['langchain', 'prompts', 'chat', 'HumanMessagePromptTemplate'],
                                                 'lc': 1, 'type': 'constructor',
                                                 'kwargs': {'prompt':
                                                                {'id': ['langchain', 'prompts', 'prompt', 'PromptTemplate'],
                                                                 'lc': 1, 'type': 'constructor',
                                                                 'kwargs': {
                                                                            'template': '{input}\n\n{agent_scratchpad}\n (reminder to respond in a JSON blob no matter what)',
                                                                            'input_variables': ['agent_scratchpad', 'input'],
                                                                            'template_format': 'f-string',
                                                                            'partial_variables': {}
                                                                            }
                                                                 }
                                                            }
                                                 }
                                                ],
                                    'input_variables': ['agent_scratchpad', 'chat_history', 'input', 'tool_names', 'tools']
                                }
                        },
                'examples': []
                }
    resp=json.dumps(prompt_dict["manifest"])
    return loads(resp)

然后再这个地方调用就行 企业微信截图_17247271676276

github-actions[bot] commented 1 month ago

这个问题已经被标记为 stale ,因为它已经超过 30 天没有任何活动。