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
36.65k stars 4.96k forks source link

SrplyApi tool not working #5338

Closed AndyMik90 closed 2 weeks ago

AndyMik90 commented 2 weeks ago

Self Checks

Dify version

0.6.11

Cloud or Self Hosted

Cloud, Self Hosted (Source)

Steps to reproduce

Trying to add API key to Serply.io tool gives: "Expecting value: line 1 column 1 (char 0)"

✔️ Expected Behavior

No response

❌ Actual Behavior

Expecting value: line 1 column 1 (char 0)

dosubot[bot] commented 2 weeks ago

The error message "Expecting value: line 1 column 1 (char 0)" typically indicates that the response from the API is empty or not in the expected JSON format. This can happen if the API key is incorrect or if there is an issue with the API service itself.

Here are some steps to debug and handle this issue:

  1. Check the API Key: Ensure that the API key you are using is correct and has the necessary permissions.

  2. Inspect the Response: Modify the run method to print out the response status code and content for debugging purposes.

def run(self, query: str, **kwargs: Any) -> str:
    """Run query through SerplyApi and parse result."""
    params = {"q": query, "hl": kwargs.get("hl", "en"), "gl": kwargs.get("gl", "US"), "num": kwargs.get("num", 10)}
    location = kwargs.get("location", "US")

    headers = {
        "X-API-KEY": self.serply_api_key,
        "X-User-Agent": kwargs.get("device", "desktop"),
        "X-Proxy-Location": location,
        "User-Agent": "Dify",
    }

    url = f"{BASE_URL}{urlencode(params)}"
    res = requests.get(url, headers=headers)

    # Debugging: Print response status and content
    print(f"Response Status Code: {res.status_code}")
    print(f"Response Content: {res.content}")

    if res.status_code != 200:
        raise Exception(f"API request failed with status code {res.status_code}")

    return res.json()
  1. Handle Empty Responses: Add a check to handle cases where the response is empty or not in the expected format.
def run(self, query: str, **kwargs: Any) -> str:
    """Run query through SerplyApi and parse result."""
    params = {"q": query, "hl": kwargs.get("hl", "en"), "gl": kwargs.get("gl", "US"), "num": kwargs.get("num", 10)}
    location = kwargs.get("location", "US")

    headers = {
        "X-API-KEY": self.serply_api_key,
        "X-User-Agent": kwargs.get("device", "desktop"),
        "X-Proxy-Location": location,
        "User-Agent": "Dify",
    }

    url = f"{BASE_URL}{urlencode(params)}"
    res = requests.get(url, headers=headers)

    if res.status_code != 200:
        raise Exception(f"API request failed with status code {res.status_code}")

    if not res.content:
        raise Exception("Received empty response from API")

    return res.json()
  1. Parse Results Correctly: Ensure that the parse_results method is correctly handling the response.
def run(self, query: str, **kwargs: Any) -> str:
    """Run query through SerplyApi and parse result."""
    params = {"q": query, "hl": kwargs.get("hl", "en"), "gl": kwargs.get("gl", "US"), "num": kwargs.get("num", 10)}
    location = kwargs.get("location", "US")

    headers = {
        "X-API-KEY": self.serply_api_key,
        "X-User-Agent": kwargs.get("device", "desktop"),
        "X-Proxy-Location": location,
        "User-Agent": "Dify",
    }

    url = f"{BASE_URL}{urlencode(params)}"
    res = requests.get(url, headers=headers)

    if res.status_code != 200:
        raise Exception(f"API request failed with status code {res.status_code}")

    if not res.content:
        raise Exception("Received empty response from API")

    res = res.json()
    return self.parse_results(res)

@staticmethod
def parse_results(res: dict) -> str:
    """Process response from Serply Job Search."""
    jobs = res.get("jobs", [])
    if not jobs:
        raise ValueError(f"Got error from Serply: {res}")

    string = []
    for job in jobs[:10]:
        try:
            string.append(
                "\n".join([
                    f"Position: {job['position']}",
                    f"Employer: {job['employer']}",
                    f"Location: {job['location']}",
                    f"Link: {job['link']}",
                    f"""Highest: {", ".join([h for h in job["highlights"]])}""",
                    "---",
                ])
            )
        except KeyError:
            continue

    content = "\n".join(string)
    return f"\nJobs results:\n {content}\n"

By adding these debugging steps, you can get more information about why the API request is failing and take appropriate action to resolve the issue [1][2][3].

To continue talking to Dosu, mention @dosu.

crazywoola commented 2 weeks ago

@googio @laipz8200 Can you guys take a look at this?

laipz8200 commented 2 weeks ago

@AndyMik90 I noticed that Serply.io's server is returning an error response. You might need to wait for them to fix the issue.

googio commented 2 weeks ago

@AndyMik90 @crazywoola This is most likely because the API Key isn’t active or our system flagged the account during signup. Please send an email to support@serply.io and mention authentication errors. We will review the account. Thank you

googio commented 2 weeks ago

@AndyMik90 Sorry about the issue. We've identified the error and pushed out an update. You should be able to Authenticate now. Please mark this as closed if this has resolved your issue. Thank you for identifying the bug.

laipz8200 commented 2 weeks ago

I've checked the fix. If anyone still has issues, just reopen this.