Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can ChatHuggingFace model perform Function Calling? #26620

Open
5 tasks done
Kirushikesh opened this issue Sep 18, 2024 · 0 comments
Open
5 tasks done

Can ChatHuggingFace model perform Function Calling? #26620

Kirushikesh opened this issue Sep 18, 2024 · 0 comments
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@Kirushikesh
Copy link
Contributor

Kirushikesh commented Sep 18, 2024

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

I was using the following code to demonstrate the tool-calling feature of HuggingFace model, but i got error.

from langchain_huggingface import ChatHuggingFace, HuggingFacePipeline

pipeline = HuggingFacePipeline.from_model_id(
    model_id="NousResearch/Hermes-2-Pro-Llama-3-8B",
    task="text-generation",
    pipeline_kwargs={
        "max_new_tokens": 500,
        "top_k": 50,
        "temperature": 0.1,
        "do_sample": True,
        "return_full_text": False
    },
    device=0
)

model = ChatHuggingFace(llm=pipeline)
from pydantic import BaseModel, Field
from langchain_core.tools import tool

class GetWeather(BaseModel):
    '''Get the current weather in a given location'''
    location: str = Field(description="The city and state,e.g. San Francisco, CA")

class GetPopulation(BaseModel):
    '''Get the current population in a given location'''
    location: str = Field(description="The city and state, e.g. San Francisco, CA")

model_with_tools = model.bind_tools([GetWeather, GetPopulation])
model_with_tools.invoke(
    "Which city is hotter today and which is bigger: LA or NY?",
)
model_with_tools.tool_calls

Error Message and Stack Trace (if applicable)

{
	"name": "AttributeError",
	"message": "'ChatHuggingFace' object has no attribute 'tool_calls'",
	"stack": "---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[7], line 1
----> 1 model_with_tools.tool_calls

File /dccstor/kirushikesh/.conda/testenv/lib/python3.10/site-packages/langchain_core/runnables/base.py:5704, in RunnableBinding.__getattr__(self, name)
   5703 def __getattr__(self, name: str) -> Any:
-> 5704     attr = getattr(self.bound, name)
   5706     if callable(attr) and (
   5707         config_param := inspect.signature(attr).parameters.get(\"config\")
   5708     ):
   5709         if config_param.kind == inspect.Parameter.KEYWORD_ONLY:

File /dccstor/kirushikesh/.conda/testenv/lib/python3.10/site-packages/pydantic/main.py:856, in BaseModel.__getattr__(self, item)
    853     return super().__getattribute__(item)  # Raises AttributeError if appropriate
    854 else:
    855     # this is the current error
--> 856     raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')

AttributeError: 'ChatHuggingFace' object has no attribute 'tool_calls'"
}

Description

Ok, i am try to use langchain library to play with the tool-calling capability of HuggingFace models. Recently, i got to know about the Hermes-LLM tool calling capability from this blog. I am a big fan of Langchain and i thought of doing the function calling with that model in Langchain. First i am not sure if its possible yet in Langchain, but when i checked this class definition i thought its possible and i tried to reproduce the same code.

The .bind_tools() is working from ChatHuggingFace, but when i tried to read the tool_calls from the response i was getting the error.

System Info

System Information

OS: Linux
OS Version: #1 SMP Thu Dec 7 03:06:13 EST 2023
Python Version: 3.10.4 (main, Mar 31 2022, 08:41:55) [GCC 7.5.0]

Package Information

langchain_core: 0.3.0
langchain: 0.3.0
langchain_community: 0.3.0
langsmith: 0.1.121
langchain_huggingface: 0.1.0
langchain_ibm: 0.2.0
langchain_openai: 0.2.0
langchain_text_splitters: 0.3.0

Optional packages not installed

langgraph
langserve

Other Dependencies

aiohttp: 3.10.5
async-timeout: 4.0.3
dataclasses-json: 0.6.7
httpx: 0.27.2
huggingface-hub: 0.24.7
ibm-watsonx-ai: 1.1.9
jsonpatch: 1.33
numpy: 1.26.4
openai: 1.45.1
orjson: 3.10.7
packaging: 24.1
pydantic: 2.9.2
pydantic-settings: 2.5.2
PyYAML: 6.0.2
requests: 2.32.2
sentence-transformers: 3.1.0
SQLAlchemy: 2.0.35
tenacity: 8.5.0
tiktoken: 0.7.0
tokenizers: 0.19.1
transformers: 4.44.2
typing-extensions: 4.12.2

@Kirushikesh Kirushikesh changed the title Can ChatHuggingFace model can do Function Calling? Can ChatHuggingFace model perform Function Calling? Sep 18, 2024
@dosubot dosubot bot added the 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature label Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

1 participant