langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
92.22k stars 14.72k forks source link

'ChatOllama' object has no attribute 'bind_tools' #20439

Open natecard opened 5 months ago

natecard commented 5 months ago

Checked other resources

Example Code

from dotenv import load_dotenv
from langchain_community.chat_models import ChatOllama
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
from transformers import AutoTokenizer
from langchain.tools import Tool

load_dotenv()
hf_token = os.environ.get("HF_TOKEN")
llm = ChatOllama(model="qwen:14b")
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen1.5-14B", token=hf_token)
search_wrapper = DuckDuckGoSearchAPIWrapper(region="en-us", max_results=5)
# Set up the DuckDuckGo search tool
search_tool = DuckDuckGoSearchRun(verbose=True, api_wrapper=search_wrapper)

# Set up the search tools list
search_tools = [
    Tool(
        name="Search",
        func=search_tool.run,
        description="Use the DuckDuckGo search engine to find information",
    ),
]

llm_with_tools = llm.bind_tools(tools=search_tools)

Error Message and Stack Trace (if applicable)

AttributeError                            Traceback (most recent call last)
Cell In[1], [line 26](vscode-notebook-cell:?execution_count=1&line=26)
     [17](vscode-notebook-cell:?execution_count=1&line=17) # Set up the search tools list
     [18](vscode-notebook-cell:?execution_count=1&line=18) search_tools = [
     [19](vscode-notebook-cell:?execution_count=1&line=19)     Tool(
     [20](vscode-notebook-cell:?execution_count=1&line=20)         name="Search",
   (...)
     [23](vscode-notebook-cell:?execution_count=1&line=23)     ),
     [24](vscode-notebook-cell:?execution_count=1&line=24) ]
---> [26](vscode-notebook-cell:?execution_count=1&line=26) llm_with_tools = llm.bind_tools(tools=search_tools)

AttributeError: 'ChatOllama' object has no attribute 'bind_tools'

Description

In the docs for ChatOllama it says there is the method bind_tools() here. When I attempt to run the Ollama instance and bind the search tool it throws the AttributeError saying it has no attribute 'bind_tools'

System Info

System Information

OS: Darwin OS Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000 Python Version: 3.12.2 | packaged by conda-forge | (main, Feb 16 2024, 20:54:21) [Clang 16.0.6 ]

Package Information

langchain_core: 0.1.40 langchain: 0.1.14 langchain_community: 0.0.31 langsmith: 0.1.38 langchain_experimental: 0.0.56 langchain_text_splitters: 0.0.1

Packages not installed (Not Necessarily a Problem)

The following packages were not found:

langgraph langserve

liugddx commented 5 months ago

You can refer to this https://python.langchain.com/docs/integrations/chat/

sepiatone commented 5 months ago

@natecard The documentation is out of sync with the code. The addition of the the bind_tools interface (#20360) to the BaseChatModel (which ChatOllama subclasses) was merged after the last release of langchain-core (#20332). With the next release of langchain-core you should see a slightly more informative "Not Implemented" message.

All of which is to say that please check the page sent by @liugddx - hopefully one of the models that supports the bind_tools interface right now is available to you and can be used in your project.

lalanikarim commented 4 months ago

@natecard ChatOllama or its underlying base class _OllamaCommon don't yet implement bind_tools. While implementing this function is pretty straight forward, using this code as reference, that alone won't be sufficient for the purposes of tool calling as neither the ChatOllama not the Ollama classes within langchain_community support tool calling directly at this time. There is an implementation within langchain_experimental.llms for OllamaFunctions which is a somewhat outdated implementation of tool calling and needs to be brought up to date if the intent is to use OpenAI style function calling. Worth checking out.

lalanikarim commented 4 months ago

Posted a PR to add bind_tools to OllamaFunctions https://github.com/langchain-ai/langchain/pull/20881

surya-apty commented 3 months ago

reference for javascript implementation for bindtool? cc @natecard @lalanikarim

dickreuter commented 3 months ago

There still is no bind_tools for ChatOllama in the latest version from today's github. Any suggestions how this can be implemented?

lalanikarim commented 3 months ago

There still is no bind_tools for ChatOllama in the latest version from today's github. Any suggestions how this can be implemented?

bind_tools is included in OllamaFunctions which is part of the langchain-experimental package. OllamaFunctions is an extension of ChatOllama for tool calling. The latest version of the published package is still missing some functionality, however a PR #22339 was approved and merged today which fixes that.

lalanikarim commented 3 months ago

Take a look at #22339 which should have addressed this issue. The PR was approved and merged yesterday but a release is yet to be cut from it and should happen in the next few days.

In the meantime, you may try and install langchain-experimental directly from langchain's source like this:

pip install git+https://github.com/langchain-ai/langchain.git\#egg=langchain-experimental\&subdirectory=libs/experimental

I hope this helps.

knelk commented 2 months ago

thanks! is it possible to use FewShotPrompting with OllamaFunctions?

Alan-Gomes commented 2 months ago

The Javascript SDK is also missing the implementation for tools using Ollama.

leobenkel commented 1 month ago

The official documentation says that ChatOllama supports bind_tools: https://python.langchain.com/v0.2/docs/integrations/chat/ollama/#tool-calling , is that true ? from my test it does not seem like it but i might have done something wrong.

yactouat commented 1 month ago

The official documentation says that ChatOllama supports bind_tools: https://python.langchain.com/v0.2/docs/integrations/chat/ollama/#tool-calling , is that true ? from my test it does not seem like it but i might have done something wrong.

are you sure you are pulling ChatOllama as in from langchain_ollama import ChatOllama with package langhain-ollama installed?

leobenkel commented 1 month ago

The official documentation says that ChatOllama supports bind_tools: python.langchain.com/v0.2/docs/integrations/chat/ollama#tool-calling , is that true ? from my test it does not seem like it but i might have done something wrong.

are you sure you are pulling ChatOllama as in from langchain_ollama import ChatOllama with package langhain-ollama installed?

hmm I think I had an issue with my installations, that's what i imported from at the start but it did not work, so i switched back to from langchain_community.chat_models.ollama import ChatOllama and then i forgot i did this change.

thanks !

laleph commented 2 weeks ago

The official documentation says that ChatOllama supports bind_tools: https://python.langchain.com/v0.2/docs/integrations/chat/ollama/#tool-calling , is that true ? from my test it does not seem like it but i might have done something wrong.

are you sure you are pulling ChatOllama as in from langchain_ollama import ChatOllama with package langhain-ollama installed?

@natecard I think this resolves the issue.