How to implement bind_tools to custom LLM from huggingface pipeline(Llama-3) for a custom agent

Example Code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> name = "meta-llama/Meta-Llama-3-8B-Instruct"
auth_token = ""
tokenizer = AutoTokenizer.from_pretrained(name,use_auth_token=auth_token)
bnb_config = BitsAndBytesConfig(
load_in_8bit=True,
)
model_config = AutoConfig.from_pretrained(
name,
use_auth_token=auth_token,
tempreature=0.1,
)
model = AutoModelForCausalLM.from_pretrained(
name,
trust_remote_code=True,
config=model_config,
quantization_config=bnb_config,
device_map='auto',
use_auth_token=auth_token,
)
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=4096, device_map="auto", streamer = streamer)
llm = HuggingFacePipeline(pipeline=pipe)
@tool
def some_custom_tool(input_string: str) -> str:
"""Executes some work and returns a success message if successfull else it return the error message"""
return "SUCCESS"
tools = [some_custom_tool]
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
f"""
You are an Assistant......
""",
),
("user", "{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad"),
]
)
llm_with_tools = llm.bind_tools(tools)
agent = (
{
"input": lambda x: x["input"],
"agent_scratchpad": lambda x: format_to_openai_tool_messages(
x["intermediate_steps"]
),
}
| prompt
| llm
| JsonOutputParser()
)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, return_intermediate_steps= True)
</code>
<code> name = "meta-llama/Meta-Llama-3-8B-Instruct" auth_token = "" tokenizer = AutoTokenizer.from_pretrained(name,use_auth_token=auth_token) bnb_config = BitsAndBytesConfig( load_in_8bit=True, ) model_config = AutoConfig.from_pretrained( name, use_auth_token=auth_token, tempreature=0.1, ) model = AutoModelForCausalLM.from_pretrained( name, trust_remote_code=True, config=model_config, quantization_config=bnb_config, device_map='auto', use_auth_token=auth_token, ) streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True) pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=4096, device_map="auto", streamer = streamer) llm = HuggingFacePipeline(pipeline=pipe) @tool def some_custom_tool(input_string: str) -> str: """Executes some work and returns a success message if successfull else it return the error message""" return "SUCCESS" tools = [some_custom_tool] prompt = ChatPromptTemplate.from_messages( [ ( "system", f""" You are an Assistant...... """, ), ("user", "{input}"), MessagesPlaceholder(variable_name="agent_scratchpad"), ] ) llm_with_tools = llm.bind_tools(tools) agent = ( { "input": lambda x: x["input"], "agent_scratchpad": lambda x: format_to_openai_tool_messages( x["intermediate_steps"] ), } | prompt | llm | JsonOutputParser() ) agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, return_intermediate_steps= True) </code>
    name = "meta-llama/Meta-Llama-3-8B-Instruct"
    
    auth_token = ""
    
    tokenizer = AutoTokenizer.from_pretrained(name,use_auth_token=auth_token)
    
    bnb_config = BitsAndBytesConfig(
        load_in_8bit=True,
    )
    
    model_config = AutoConfig.from_pretrained(
        name,
        use_auth_token=auth_token,
        tempreature=0.1,
    )
    
    model = AutoModelForCausalLM.from_pretrained(
        name,
        trust_remote_code=True,
        config=model_config,
        quantization_config=bnb_config,
        device_map='auto',
        use_auth_token=auth_token,
    )
    
    streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
    
    pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=4096, device_map="auto", streamer = streamer)
    llm = HuggingFacePipeline(pipeline=pipe)
    
    @tool
    def some_custom_tool(input_string: str) -> str:
        """Executes some work and returns a success message if successfull else it return the error message"""
        return "SUCCESS"
    
    
    tools = [some_custom_tool]
    prompt = ChatPromptTemplate.from_messages(
      [
          (
              "system",
              f"""
                  You are an Assistant......
              """,
          ),
          ("user", "{input}"),
          MessagesPlaceholder(variable_name="agent_scratchpad"),
      ]
    )
                
    llm_with_tools = llm.bind_tools(tools)
    
               
    
    agent = (
        {
            "input": lambda x: x["input"],
            "agent_scratchpad": lambda x: format_to_openai_tool_messages(
                x["intermediate_steps"]
            ),
        }
        | prompt
        | llm
        | JsonOutputParser()
    )
                
    
    agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, return_intermediate_steps= True)

Description

I am trying to bind a custom tool with the LLM just like ChatOpenAI but i am getting the following error. It looks like the bind_tools does exist in HuggingFacePipeline. Is there a way to bind a custom tool to an LLM from HuggingFacePipeline?

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

System Info:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>langchain==0.2.6
langchain-community==0.2.6
langchain-core==0.2.11
langchain-openai==0.1.14
langchain-text-splitters==0.2.2
Python 3.10.13
</code>
<code>langchain==0.2.6 langchain-community==0.2.6 langchain-core==0.2.11 langchain-openai==0.1.14 langchain-text-splitters==0.2.2 Python 3.10.13 </code>
langchain==0.2.6
langchain-community==0.2.6
langchain-core==0.2.11
langchain-openai==0.1.14
langchain-text-splitters==0.2.2
Python 3.10.13

I am doing this on Kaggle GPU t4x2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật