langchain-ai / langchain

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

TavilySearch parameters don't change the output. #24265

Closed dsolito closed 3 weeks ago

dsolito commented 4 months ago

Checked other resources

Example Code

from langchain_community.tools.tavily_search import TavilySearchResults, TavilyAnswer

tool_with_raw = TavilySearchResults(include_raw_content=True, max_results=1)
tool_with_raw_and_answer = TavilySearchResults(include_raw_content=True, include_answer=True, max_results=1)
tool_without_raw = TavilySearchResults(include_raw_content=False, max_results=1)

r1=tool_with_raw.invoke({'query': 'how to cook a steak?'})
print(r1)

r2=tool_without_raw.invoke({'query': 'how to cook a steak?'})
print(r2)

r3=tool_with_raw_and_answer.invoke({'query': 'how to cook a steak?'})
print(r3)
[
    {
        'url': 'https://www.onceuponachef.com/recipes/how-to-cook-steak-on-the-stovetop.html',
        'content': 'Pan-Seared Steaks\nPan-searing is the best way to cook a steak, and it’s also the easiest!\nIngredients\nInstructions\nPair 
with\nNutrition Information\nPowered by\nThis website is written and produced for informational purposes only. When I do this again I will do for 5 
minutes but will turn off the heat on my cast Iron frying pan 2 minutes before and add butter and rosemary and garlic to get the steak more to our 
liking.\n I got a ribeye steak, heated the pan to the top heat and did everything like you mentioned, but after three minutes the steak was burned, 
on the other side the same happened. After doing some more research, I find you have to bring the steak to room temperature before you cook it and 
yiu have to snip the fat around the edges to keep it from curling. 22 Quick and Easy Recipes in 30 Minutes (or less) + 5 Chef Secrets To Make You A 
Better Cook!\nFind a Recipe\nHow To Cook Steak On The Stovetop\nThis post may contain affiliate links.'
    }
]
>>> r2=tool_without_raw.invoke({'query': 'how to cook a steak?'})
>>> print(r2)
[
    {
        'url': 'https://www.onceuponachef.com/recipes/how-to-cook-steak-on-the-stovetop.html',
        'content': 'Pan-Seared Steaks\nPan-searing is the best way to cook a steak, and it’s also the easiest!\nIngredients\nInstructions\nPair 
with\nNutrition Information\nPowered by\nThis website is written and produced for informational purposes only. When I do this again I will do for 5 
minutes but will turn off the heat on my cast Iron frying pan 2 minutes before and add butter and rosemary and garlic to get the steak more to our 
liking.\n I got a ribeye steak, heated the pan to the top heat and did everything like you mentioned, but after three minutes the steak was burned, 
on the other side the same happened. After doing some more research, I find you have to bring the steak to room temperature before you cook it and 
yiu have to snip the fat around the edges to keep it from curling. 22 Quick and Easy Recipes in 30 Minutes (or less) + 5 Chef Secrets To Make You A 
Better Cook!\nFind a Recipe\nHow To Cook Steak On The Stovetop\nThis post may contain affiliate links.'
    }
]
>>> r3=tool_with_raw_and_answer.invoke({'query': 'how to cook a steak?'})
>>> print(r3)
[
    {
        'url': 'https://www.onceuponachef.com/recipes/how-to-cook-steak-on-the-stovetop.html',
        'content': 'Pan-Seared Steaks\nPan-searing is the best way to cook a steak, and it’s also the easiest!\nIngredients\nInstructions\nPair 
with\nNutrition Information\nPowered by\nThis website is written and produced for informational purposes only. When I do this again I will do for 5 
minutes but will turn off the heat on my cast Iron frying pan 2 minutes before and add butter and rosemary and garlic to get the steak more to our 
liking.\n I got a ribeye steak, heated the pan to the top heat and did everything like you mentioned, but after three minutes the steak was burned, 
on the other side the same happened. After doing some more research, I find you have to bring the steak to room temperature before you cook it and 
yiu have to snip the fat around the edges to keep it from curling. 22 Quick and Easy Recipes in 30 Minutes (or less) + 5 Chef Secrets To Make You A 
Better Cook!\nFind a Recipe\nHow To Cook Steak On The Stovetop\nThis post may contain affiliate links.'
    }
]

Error Message and Stack Trace (if applicable)

No response

Description

Hello, I cannot get all the informations requested from the parameters. Seems that only max_result is kept. I can understand that there is two classes (TavilySearchResults, TavilyAnswer) but if we can initiate TavilySearchResults with API options why to keep the two classes? D

System Info

System Information

OS: Darwin OS Version: Darwin Kernel Version 23.3.0: Wed Dec 20 21:28:58 PST 2023; root:xnu-10002.81.5~7/RELEASE_X86_64 Python Version: 3.12.2 | packaged by conda-forge | (main, Feb 16 2024, 21:00:12) [Clang 16.0.6 ]

Package Information

langchain_core: 0.2.11 langchain: 0.2.6 langchain_community: 0.2.6 langsmith: 0.1.83 langchain_openai: 0.1.14 langchain_text_splitters: 0.2.2 langgraph: 0.1.5 langserve: 0.2.2

dsolito commented 4 months ago

I tried with the python SDK and this is what I get :

from tavily import TavilyClient
tavily = TavilyClient(api_key=API_KEY)
print(tavily.search(query='how to cook a steak?', max_results=1, include_raw_content=True))

{
    'query': 'how to cook a steak?',
    'follow_up_questions': None,
    'answer': None,
    'images': [],
    'results': [
        {
            'title': 'How To Cook Steak On The Stovetop - Once Upon a Chef',
            'url': 'https://www.onceuponachef.com/recipes/how-to-cook-steak-on-the-stovetop.html',
            'content': 'Pan-Seared Steaks\nPan-searing is the best way to cook a steak, and it’s also the easiest!\nIngredients\nInstructions\nPair 
with\nNutrition Information\nPowered by\nThis website is written and produced for informational purposes only. When I do this again I will do for 5 
minutes but will turn off the heat on my cast Iron frying pan 2 minutes before and add butter and rosemary and garlic to get the steak more to our 
liking.\n I got a ribeye steak, heated the pan to the top heat and did everything like you mentioned, but after three minutes the steak was burned, 
on the other side the same happened. After doing some more research, I find you have to bring the steak to room temperature before you cook it and 
yiu have to snip the fat around the edges to keep it from curling. 22 Quick and Easy Recipes in 30 Minutes (or less) + 5 Chef Secrets To Make You A 
Better Cook!\nFind a Recipe\nHow To Cook Steak On The Stovetop\nThis post may contain affiliate links.',
            'score': 0.98533,
            'raw_content': "22 Quick and Easy Recipes in 30 Minutes (or less) + 5 Chef Secrets To Make You A Better Cook!\nFind a Recipe\nHow To ... (text deleted)"
        }
    ],
    'response_time': 0.95
}
2020uce0047 commented 4 months ago

Hello @dsolito

It seems the two classes are supposed to used in order to build tool calling agents. And, creating two separate classes keeps it less confusing for the llms to choose either to get the current events from the TavilySearchResults or directly the answer from TavilyAnswer, based on the prompt instruction.

dsolito commented 4 months ago

Hi @2020uce0047 Could make sense but why implement tavily Api arguments (include_raw_content, include_answer...)to the classes then?

2020uce0047 commented 4 months ago

Hi @dsolito I see the implementation both classes are using the same function raw_results to get the output json from the API which accepts all the Api arguments. However, the TavilySearchResults class does an extra cleaning through clean_results to return only the content and url.

isahers1 commented 4 months ago

Working on this now

isahers1 commented 4 months ago

https://github.com/langchain-ai/langchain/pull/24376