使用Ollama Llama-2时遇AttributeError:无bind_functions属性
解决Ollama Llama-2模型调用bind_functions报错问题
报错原因:LangChain的ChatOllama类没有bind_functions方法,该方法是OpenAI集成专属的。要在Ollama模型上实现函数/工具绑定,需使用LangChain为Ollama提供的bind_tools方法,并调整对应的输出解析器。
修改步骤及代码示例:
导入正确的依赖类
确保使用langchain_ollama.ChatOllama(旧版langchain.llms.Ollama不支持工具调用),同时替换输出解析器为适配Ollama的类型:from langchain_ollama import ChatOllama from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_core.output_parsers import JsonOutputToolsParser替换绑定方法与解析器
将llm.bind_functions改为llm.bind_tools,并使用JsonOutputToolsParser解析工具调用输出:修改后的完整代码:
# Setup your supervisor, conditional logic, and integrate the nodes into the workflow members = ["Web_Searcher", "Web_Searcher_Quality","Blog_Searcher", "Blog_Searcher_Quality", "Content_Writer"] system_prompt = ( """As a supervisor, your role is to oversee a dialogue between these workers: {members}. and execute all the agents one by one, determine which worker should take the next action. Each worker is responsible for executing a specific task and reporting back their findings and progress. Once all tasks are complete, indicate with 'FINISH'.""" ) options = ["FINISH"] + members function_def = { "name": "route", "description": "Select the next role.", "parameters": { "type": "object", "properties": {"next": {"type": "string", "enum": options}}, "required": ["next"], }, } prompt = ChatPromptTemplate.from_messages([ ("system", system_prompt), MessagesPlaceholder(variable_name="messages"), ("system", "Given the conversation above, who should act next? Or should we FINISH? Select one of: {options}"), ]).partial(options=str(options), members=", ".join(members)) # 初始化Ollama模型 llm = ChatOllama(model="llama2", temperature=0) # 使用bind_tools替代bind_functions,并用JsonOutputToolsParser解析输出 manager_chain = (prompt | llm.bind_tools(tools=[function_def], tool_choice="route") | JsonOutputToolsParser())关键说明
bind_tools是LangChain为支持工具调用的模型(含Ollama)提供的统一绑定方法,替代OpenAI专属的bind_functionsJsonOutputToolsParser适配Ollama工具调用的输出格式,可正确提取工具参数- 需确保LangChain版本在0.1.10及以上,
bind_tools是较新版本新增的功能
内容的提问来源于stack exchange,提问作者Prangya Paramita
相关产品推荐
相关产品推荐

