langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
46.75k stars 6.61k forks source link

TavilySearch Unable to connect, reporting a 400 error #8772

Closed ceozyg3 closed 3 days ago

ceozyg3 commented 3 days ago

Self Checks

Dify version

0.8.3

Cloud or Self Hosted

Cloud

Steps to reproduce

Hello, I am using your DIY to create an AI application. I intend to call TavilySearch in dify.ai. But after multiple attempts, the following errors were displayed.

Failed to invoke tool: 400 Client Error: Bad Request for url: https://api.tavily.com/search

At present, I cannot find the reason. How can I use this API correctly?

Attached is the attached image: Tavi搜索报错

Thank!

✔️ Expected Behavior

Can use TavilySearch's search function normally in DifyAI

❌ Actual Behavior

Failed to invoke tool: 400 Client Error: Bad Request for url: https://api.tavily.com/search

dosubot[bot] commented 3 days ago

@ceozyg3 Hello there! I'm here to help you with bug fixes, answer questions, and guide you on becoming a contributor. Let's work together to resolve the issue you're facing.

To resolve the 400 Client Error when using TavilySearch in DifyAI, ensure the following:

  1. Valid Credentials: Verify that the tavily_api_key in your credentials is correct and properly formatted. The _validate_credentials method in the TavilyProvider class checks the credentials by making a sample query. Incorrect credentials will raise a ToolProviderCredentialValidationError [1].

  2. Correct Search Parameters: Ensure that the search parameters are correctly formatted. For example, exclude_domains and include_domains should be lists, not strings, unless they are empty or set to "None" [2].

  3. Rate Limits: Check if you have exceeded the rate limits for the TavilySearch API. Exceeding the rate limit can also cause a 400 error [3].

Here is a sample code snippet for reference:

class TavilyProvider(BuiltinToolProviderController):
    def _validate_credentials(self, credentials: dict[str, Any]) -> None:
        try:
            TavilySearchTool().fork_tool_runtime(
                runtime={
                    "credentials": credentials,
                }
            ).invoke(
                user_id="",
                tool_parameters={
                    "query": "Sachin Tendulkar",
                    "search_depth": "basic",
                    "include_answer": True,
                    "include_images": False,
                    "include_raw_content": False,
                    "max_results": 5,
                    "include_domains": "",
                    "exclude_domains": "",
                },
            )
        except Exception as e:
            raise ToolProviderCredentialValidationError(str(e))

Ensure your credentials dictionary contains the required tavily_api_key field and that it is correct. Also, make sure the search parameters are formatted correctly. If the issue persists, check the specific error message returned by the exception for more details on what might be wrong.

To continue talking to Dosu, mention @dosu.

ceozyg3 commented 3 days ago

Thank you for your reply! But I couldn't find any further adjustments on the page. Obviously, these configurations are clear at a glance. Could you please help me take a look and guide me on how to adjust them? √The API key is correct √ Has not exceeded the speed limit of TavilySearch API √The TavilySearch official website shows that these deductions were successful, but they all reported an error of 400

搜索错误2

dosubot[bot] commented 3 days ago

To resolve the 400 Client Error for TavilySearch in DifyAI, you can adjust the parameters as follows:

  1. Ensure the query parameter is provided and is a valid string.
  2. Adjust optional parameters to valid values:
    • search_depth: Set to either basic or advanced.
    • include_images: Set to true or false.
    • include_answer: Set to true or false.
    • include_raw_content: Set to true or false.
    • max_results: Ensure it is a number between 1 and 20.
    • include_domains: Provide a valid list of domains if needed.
    • exclude_domains: Provide a valid list of domains if needed.

Here is an example of how to set these parameters correctly:

parameters:
  query: "example search query"
  search_depth: "basic"  # or "advanced"
  include_images: false  # or true
  include_answer: false  # or true
  include_raw_content: false  # or true
  max_results: 5  # any number between 1 and 20
  include_domains: "example.com,anotherexample.com"  # optional
  exclude_domains: "excludedexample.com"  # optional

Additionally, ensure that the include_domains and exclude_domains parameters are correctly formatted as lists. If they are provided as strings, they should be split into lists:

if (
    "exclude_domains" in params
    and isinstance(params["exclude_domains"], str)
    and params["exclude_domains"] != "None"
):
    params["exclude_domains"] = params["exclude_domains"].split()
else:
    params["exclude_domains"] = []

if (
    "include_domains" in params
    and isinstance(params["include_domains"], str)
    and params["include_domains"] != "None"
):
    params["include_domains"] = params["include_domains"].split()
else:
    params["include_domains"] = []

Make sure all required parameters are provided and optional parameters are within their valid ranges to avoid the 400 Client Error [1][2].

To continue talking to Dosu, mention @dosu.

ceozyg3 commented 3 days ago

May I ask if there is a human customer service available? I have already set up everything you said correctly, is there still an error

hjlarry commented 3 days ago

I can't reproduce your problem, and I also use the cloud version of dify:

image

ceozyg3 commented 3 days ago

I just inputted Tavi-API without making any other configurations. What configurations do I need to make to succeed?

ceozyg3 commented 3 days ago

搜索测试1 搜索测试2 搜索测试3

hjlarry commented 3 days ago

I haven't do anything else, just use the default settings

ceozyg3 commented 3 days ago

That's too strange. It's been like this since yesterday

ceozyg3 commented 3 days ago

Are you using Tavi's free API? A plan of free month/1000?

hjlarry commented 3 days ago

Are you using Tavi's free API? A plan of free month/1000?

yes