Skip to content

Commit

Permalink
Merge pull request #948 from rylincoln/fixMissingImport1
Browse files Browse the repository at this point in the history
fix/bing-retriever: add missing import statement
  • Loading branch information
assafelovic authored Oct 24, 2024
2 parents 5a0fc2d + e3b89a7 commit 101bc7f
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions gpt_researcher/retrievers/bing/bing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import requests
import json
import logging


class BingSearch():
Expand All @@ -30,7 +31,8 @@ def get_api_key(self):
try:
api_key = os.environ["BING_API_KEY"]
except:
raise Exception("Bing API key not found. Please set the BING_API_KEY environment variable.")
raise Exception(
"Bing API key not found. Please set the BING_API_KEY environment variable.")
return api_key

def search(self, max_results=7) -> list[dict[str]]:
Expand All @@ -42,24 +44,23 @@ def search(self, max_results=7) -> list[dict[str]]:
print("Searching with query {0}...".format(self.query))
"""Useful for general internet search queries using the Bing API."""


# Search the query
url = "https://api.bing.microsoft.com/v7.0/search"

headers = {
'Ocp-Apim-Subscription-Key': self.api_key,
'Content-Type': 'application/json'
'Ocp-Apim-Subscription-Key': self.api_key,
'Content-Type': 'application/json'
}
params = {
"responseFilter" : "Webpages",
"responseFilter": "Webpages",
"q": self.query,
"count": max_results,
"setLang": "en-GB",
"textDecorations": False,
"textFormat": "HTML",
"safeSearch": "Strict"
}

resp = requests.get(url, headers=headers, params=params)

# Preprocess the results
Expand All @@ -69,7 +70,8 @@ def search(self, max_results=7) -> list[dict[str]]:
search_results = json.loads(resp.text)
results = search_results["webPages"]["value"]
except Exception as e:
logger.error(f"Error parsing Bing search results: {e}. Resulting in empty response.")
logger.error(
f"Error parsing Bing search results: {e}. Resulting in empty response.")
return []
if search_results is None:
logger.warning(f"No search results found for query: {self.query}")
Expand Down

0 comments on commit 101bc7f

Please sign in to comment.