赞
踩
基于本地的ChatGLM3-6B直接开发LangChain Function Call应用,发现其输出的action和action_input非常不稳定。
表现为生成的JSON格式回答非常容易出现不规范的情况,导致LangChain的Agent执行报错,或者进入死循环。
Langchain 作为最主流的大模型中间件开源框架,备受广大开发者的认可。
Langchain中具有一套完整的 Agent 思维,包括灵活,简单的Function Call开发框架。
ChatGLM3-6B 模型在同量级模型中有出色的Function Call能力。
但遗憾的是,其训练过程并没有与Langchain进行原生对齐。
这导致如果直接使用Langchian框架,将会遇到以下问题:
无法载入ChatGLM3-6B模型,Langchain中的 LLM模型 目前仅支持在线的几个主流模型,例如ChatGPT,Bard,Claude等
无法正常使用 Agent 的 Function Call 功能,ChatGLM3-6B的截断点与 Langchain 支持的并不同。
提示词不同,使用 Langchain 封装的 Agent 提示词完全无法胜任ChatGLM3-6B 的 Function Call 任务。
首先,要解决第一个痛点:ChatGLM3-6B 模型能够被 Langchain 读入并执行。
那么,我们就需要基于Langchain的LLM类完成ChatGLM3-6B的模型实现。
- class ChatGLM3(LLM):
- max_token: int = 8192
- do_sample: bool = False
- temperature: float = 0.8
- top_p = 0.8
- tokenizer: object = None
- model: object = None
- history: List = []
- tool_names: List = []
- has_search: bool = False
-
- def __init__(self):
- super().__init__()
-
- @property
- def _llm_type(self) -> str:
- return "ChatGLM3"

接着,我们要写入读入模型的方法,这与 Langchain 支持的在线模型不同,这里使用 Huggingface 的方式进行读入。
- def load_model(self, model_name_or_path=None):
- model_config = AutoConfig.from_pretrained(
- model_name_or_path,
- trust_remote_code=True
- )
- self.tokenizer = AutoTokenizer.from_pretrained(
- model_name_or_path,
- trust_remote_code=True
- )
- self.model = AutoModel.from_pretrained(
- model_name_or_path, config=model_config, trust_remote_code=True
- ).half().cuda()
其中包括工具的name,description,params等信息,可以被LangChain自动识别出来,加入到prompt中
- import abc
-
- from typing import Type
- from langchain.tools import BaseTool
- from pydantic import BaseModel, Field
-
-
- class CalculatorInput(BaseModel):
- calculation: str = Field(description="calculation to perform")
-
-
- class Calculator(BaseTool, abc.ABC):
- name = "Calculator"
- description = "Useful for when you need to calculate math problems"
- args_schema: Type[BaseModel] = CalculatorInput
-
- def __init__(self):
- super().__init__()
-
- def _run(self, calculation: str) -> str:
- calculation = calculation.replace("^", "**")
- if "sqrt" in calculation:
- calculation = calculation.replace("sqrt", "math.sqrt")
- elif "log" in calculation:
- calculation = calculation.replace("log", "math.log")
- return eval(calculation)

然后,就是非常重要的一环。由于我们的工具调用和观察抓取与 Langchain 并不相同,我们需要对 Langchain 的提示词进行修改,并配上我们的提示词。
我们先从AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION
模板中截取到You have access to the following tools:\n\n")
的关键词,并在合理插入已经注册的工具类型。
- tool_prompts = prompt.split(
- "You have access to the following tools:\n\n")[1].split("\n\nUse a json blob")[0].split("\n")
-
- tool_names = [tool.split(":")[0] for tool in tool_prompts]
- self.tool_names = tool_name
由于ChatGLM3-6B拥有Observation角色,这与Langchain中原本设定的Observation截断并不相同,因此,在这里,我们需要做提取,在这段代码中,我们需要对原本Langchain中的Observation进行截断,并补
充上我们的工具观察的结果。
- def _extract_observation(self, prompt: str):
- return_json = prompt.split("Observation: ")[-1].split("\nThought:")[0]
- self.history.append({
- "role": "observation",
- "content": return_json
- })
- return
在这,我们还需要对执行工具进行截断和填充,使得其能满足ChatGLM3-6B的思维模式
- def _extract_tool(self):
- if len(self.history[-1]["metadata"]) > 0:
- metadata = self.history[-1]["metadata"]
- content = self.history[-1]["content"]
- if "tool_call" in content:
- for tool in self.tool_names:
- if tool in metadata:
- input_para = content.split("='")[-1].split("'")[0]
- action_json = {
- "action": tool,
- "action_input": input_para
- }
- self.has_search = True
- return f"""
- Action:
- ```
- {json.dumps(action_json, ensure_ascii=False)}
- ```"""
- final_answer_json = {
- "action": "Final Answer",
- "action_input": self.history[-1]["content"]
- }
- self.has_search = False
- return f"""
- Action:
- ```
- {json.dumps(final_answer_json, ensure_ascii=False)}
- ```"""

由于ChatgGLM3-6B的思维方式并没有Action:
字段,而这是langchain的截断点,因此,我们需要对其进行补充,使得Langchain能知道,此时模型进入调用工具阶段。
最后,我们要基于Langchain的构造,重写_call函数,包括历史记录,提示词等拼接内容。
- def _call(self, prompt: str, history: List = [], stop: Optional[List[str]] = ["<|user|>"]):
- if not self.has_search:
- self.history, query = self._tool_history(prompt)
- else:
- self._extract_observation(prompt)
- query = ""
- _, self.history = self.model.chat(
- self.tokenizer,
- query,
- history=self.history,
- do_sample=self.do_sample,
- max_length=self.max_token,
- temperature=self.temperature,
- )
- response = self._extract_tool()
- history.append((prompt, response))
- return response

在完成了上述工作之后,我们就已经拥有了支持Langchain的ChatGLM3-6B模型,我们在main.py
中对其进行了简单调用
- if __name__ == "__main__":
- llm = ChatGLM3()
- llm.load_model(MODEL_PATH)
- prompt = hub.pull("hwchase17/structured-chat-agent")
-
- # for single parameter without history
-
- tools = [Calculator()]
- agent = create_structured_chat_agent(llm=llm, tools=tools, prompt=prompt)
- agent_executor = AgentExecutor(agent=agent, tools=tools)
- ans = agent_executor.invoke({"input": "34 * 34"})
- print(ans)
到此为止,你已经简单实现了使用LangChain调用ChatGLM3-6B模型来实现工具调用和其他基本用法。但是,在更多探索之前,请一定要看这部分的内容。这将能为你之后的开发减少不必要的麻烦。
在官方的实现方案中,暂时不能解决在工具中仍然需要调用正常的LLMChain的操作,这意味着你在工具的设计中不能再次调用大模型进行更多操作,例如参数解析等,典型的错误例子为
LLMMathChain
如果使用官方Demo调用这个工具,则必然遇到以下错误:
line 120, in _process_llm_result
raise ValueError(f"unknown format from LLM: {llm_output}")
ValueError: unknown format from LLM: Action:
{"action": "Calculator", "action_input": "23*23"}
这是因为在模型构建的过程中,模型会解析到tools,而在tools中的常规调用模型并没有修改模型的系统提示词,模型还会尝试调用工具,这在常规的Chain中是错误的。
ChatGLM3-6B必须使用结构化的Agent,在Langchain中,我们只适配了AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION
如果使用LLMSingleActionAgent
来构建,stop
参数无效。
使用Tool.from_function
时,args_schema
无效。
每次创建一个新的Tools,都必须有同名的yaml,或者自己实现传入格式化的工具说明。
官方的OpenAI API格式的demo,目前无法适配Langchain的工具。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。