Langchain / langgraph don’t call avaible search tools (Like ddg_search and tavily) [closed]

Estou estudando Pythn com Langchain, com o objetivo de criar um chatbot que responde perguntas pesquisando no duckduck go ou outra ferramenta de busca. Antes de chegar no chat realmente, eu fazendo alguns testes e, a IA simplesmente não usa as ferramentas que eu disponibilizei pra ela. a IA roda, mas não faz as pesquisas para conseguir responder.

Código:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from langchain import hub
from typing import Annotated
from langgraph.prebuilt import create_react_agent
from langgraph.graph import StateGraph
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode, tools_condition
from typing_extensions import TypedDict
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, AnyMessage
from langchain_community.chat_models import ChatLlamaCpp
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
from langchain_community.utilities.tavily_search import TavilySearchAPIWrapper
from langchain_community.tools.ddg_search.tool import DuckDuckGoSearchResults
from langchain_community.tools.tavily_search import TavilySearchResults
class State(TypedDict):
messages: Annotated[list, add_messages]
graph_builder = StateGraph(State)
model = ChatLlamaCpp(
#model_path="E:/DEV/brincandoDeAi/brincando-de-ai-python/llm/Qwen2-0.5B-Instruct.Q8_0.gguf", #Qwen 2-0 5B Instruct Q6 GUFF
model_path="E:/DEV/brincandoDeAi/brincando-de-ai-python/llm/Meta-Llama-3-8B-Instruct.Q6_K.gguf", #Meta Llama 3 8B Instruct Q6 GUFF
#model_path="E:/DEV/brincandoDeAi/brincando-de-ai-python/llm/TinyLlama-1.1B-Chat-v1.0-UltraQ-Imat-NEO1-Q8_0-imat.gguf", #TinyLlama 1 1.1B Chat Q6 GUFF
temperature=0.5,
top_p=1,
verbose=True,
n_ctx=10000
)
ddgSearch = DuckDuckGoSearchAPIWrapper()
ddg_search_tool = DuckDuckGoSearchResults(verbose=True, api_wrapper=ddgSearch)
search = TavilySearchAPIWrapper(tavily_api_key="tvly-X")
search_tool = TavilySearchResults(api_wrapper=search, max_results=2)
tools = [search_tool, ddg_search_tool]
model_tools = model.bind_tools(tools)
question = "What events occurred in July 2024?"
agent = create_react_agent(model=model_tools, tools=tools, debug=True)
def print_stream(stream):
for s in stream:
message = s["messages"][-1]
if isinstance(message, tuple):
print(message)
else:
message.pretty_print()
response = agent.invoke(
{
"messages": [
SystemMessage(content="You are a helpful assistant. You are a helpful AI. Answer the following question as best you can. In the end, show all links of the search tool if you used any."),
SystemMessage(content='''Use a tool at least one time. Call a tool using it name, for example: 'Action: duckduckgo_results_json'. Use the exactly following format in this order:
Question: the input question you must answer
Thought: you should always think about what to do,
Action: the action to take, should be one of your tools,
Action Input: the input to the action,
Observation: the result of the action,
... (this Thought/Action/Action Input/Observation can repeat N times in this exactly order),
Thought: I now know the final answer,
Final Answer: the final answer to the original Human input message
Begin!
'''),
HumanMessage(content=question)
]}
)
print("------------------------------------------------------------n------------------------------------------------------------n------------------------------------------------------------")
print(response)
</code>
<code>from langchain import hub from typing import Annotated from langgraph.prebuilt import create_react_agent from langgraph.graph import StateGraph from langgraph.graph.message import add_messages from langgraph.prebuilt import ToolNode, tools_condition from typing_extensions import TypedDict from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, AnyMessage from langchain_community.chat_models import ChatLlamaCpp from langchain_community.utilities import DuckDuckGoSearchAPIWrapper from langchain_community.utilities.tavily_search import TavilySearchAPIWrapper from langchain_community.tools.ddg_search.tool import DuckDuckGoSearchResults from langchain_community.tools.tavily_search import TavilySearchResults class State(TypedDict): messages: Annotated[list, add_messages] graph_builder = StateGraph(State) model = ChatLlamaCpp( #model_path="E:/DEV/brincandoDeAi/brincando-de-ai-python/llm/Qwen2-0.5B-Instruct.Q8_0.gguf", #Qwen 2-0 5B Instruct Q6 GUFF model_path="E:/DEV/brincandoDeAi/brincando-de-ai-python/llm/Meta-Llama-3-8B-Instruct.Q6_K.gguf", #Meta Llama 3 8B Instruct Q6 GUFF #model_path="E:/DEV/brincandoDeAi/brincando-de-ai-python/llm/TinyLlama-1.1B-Chat-v1.0-UltraQ-Imat-NEO1-Q8_0-imat.gguf", #TinyLlama 1 1.1B Chat Q6 GUFF temperature=0.5, top_p=1, verbose=True, n_ctx=10000 ) ddgSearch = DuckDuckGoSearchAPIWrapper() ddg_search_tool = DuckDuckGoSearchResults(verbose=True, api_wrapper=ddgSearch) search = TavilySearchAPIWrapper(tavily_api_key="tvly-X") search_tool = TavilySearchResults(api_wrapper=search, max_results=2) tools = [search_tool, ddg_search_tool] model_tools = model.bind_tools(tools) question = "What events occurred in July 2024?" agent = create_react_agent(model=model_tools, tools=tools, debug=True) def print_stream(stream): for s in stream: message = s["messages"][-1] if isinstance(message, tuple): print(message) else: message.pretty_print() response = agent.invoke( { "messages": [ SystemMessage(content="You are a helpful assistant. You are a helpful AI. Answer the following question as best you can. In the end, show all links of the search tool if you used any."), SystemMessage(content='''Use a tool at least one time. Call a tool using it name, for example: 'Action: duckduckgo_results_json'. Use the exactly following format in this order: Question: the input question you must answer Thought: you should always think about what to do, Action: the action to take, should be one of your tools, Action Input: the input to the action, Observation: the result of the action, ... (this Thought/Action/Action Input/Observation can repeat N times in this exactly order), Thought: I now know the final answer, Final Answer: the final answer to the original Human input message Begin! '''), HumanMessage(content=question) ]} ) print("------------------------------------------------------------n------------------------------------------------------------n------------------------------------------------------------") print(response) </code>
from langchain import hub
from typing import Annotated
from langgraph.prebuilt import create_react_agent
from langgraph.graph import StateGraph
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode, tools_condition
from typing_extensions import TypedDict
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, AnyMessage
from langchain_community.chat_models import ChatLlamaCpp
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
from langchain_community.utilities.tavily_search import TavilySearchAPIWrapper
from langchain_community.tools.ddg_search.tool import DuckDuckGoSearchResults
from langchain_community.tools.tavily_search  import TavilySearchResults

class State(TypedDict):
    messages: Annotated[list, add_messages]


graph_builder = StateGraph(State)

model = ChatLlamaCpp(
    #model_path="E:/DEV/brincandoDeAi/brincando-de-ai-python/llm/Qwen2-0.5B-Instruct.Q8_0.gguf", #Qwen 2-0 5B Instruct Q6 GUFF
    model_path="E:/DEV/brincandoDeAi/brincando-de-ai-python/llm/Meta-Llama-3-8B-Instruct.Q6_K.gguf", #Meta Llama 3 8B Instruct Q6 GUFF
    #model_path="E:/DEV/brincandoDeAi/brincando-de-ai-python/llm/TinyLlama-1.1B-Chat-v1.0-UltraQ-Imat-NEO1-Q8_0-imat.gguf", #TinyLlama 1 1.1B Chat Q6 GUFF
    temperature=0.5,
    top_p=1,
    verbose=True,
    n_ctx=10000
)
ddgSearch = DuckDuckGoSearchAPIWrapper()
ddg_search_tool = DuckDuckGoSearchResults(verbose=True, api_wrapper=ddgSearch)
search = TavilySearchAPIWrapper(tavily_api_key="tvly-X")
search_tool = TavilySearchResults(api_wrapper=search, max_results=2)
tools = [search_tool, ddg_search_tool]


model_tools = model.bind_tools(tools)
question = "What events occurred in July 2024?"

agent = create_react_agent(model=model_tools, tools=tools, debug=True)

def print_stream(stream):
    for s in stream:
        message = s["messages"][-1]
        if isinstance(message, tuple):
            print(message)
        else:
            message.pretty_print()

response = agent.invoke(
    {
    "messages": [
    SystemMessage(content="You are a helpful assistant. You are a helpful AI. Answer the following question as best you can. In the end, show all links of the search tool if you used any."),
    SystemMessage(content='''Use a tool at least one time. Call a tool using it name, for example: 'Action: duckduckgo_results_json'. Use the exactly following format in this order:
                Question: the input question you must answer
                Thought: you should always think about what to do,
                Action: the action to take, should be one of your tools,
                Action Input: the input to the action,
                Observation: the result of the action,
                ... (this Thought/Action/Action Input/Observation can repeat N times in this exactly order),
                Thought: I now know the final answer,
                Final Answer: the final answer to the original Human input message
                Begin!
                '''),
    HumanMessage(content=question)
    ]}
)

print("------------------------------------------------------------n------------------------------------------------------------n------------------------------------------------------------")
print(response)

My response is:
I'm happy to help! However, I have to inform you that the date you provided is in the future. As of now, we don't know what will happen on July 14th, 2024.nnBut if you're looking for information on past events involving Donald Trump, I'd be happy to help with that!"

So, it could not give a answer. but, if it make a search, it can answer.
How to fix it?
Using langchain 0.2v

I am reading the docs but i can’t figure it out

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