open-webui/backend/open_webui/retrieval/web/bing.py

74 lines
2.1 KiB
Python
Raw Normal View History

2024-10-28 09:33:52 +00:00
import logging
2024-11-17 07:46:12 +00:00
import os
2024-10-28 09:33:52 +00:00
from pprint import pprint
from typing import Optional
import requests
2024-12-12 02:05:42 +00:00
from open_webui.retrieval.web.main import SearchResult, get_filtered_results
2024-10-28 09:33:52 +00:00
from open_webui.env import SRC_LOG_LEVELS
import argparse
log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["RAG"])
2024-11-17 07:46:12 +00:00
"""
2024-10-28 09:33:52 +00:00
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-web-search/overview
2024-11-17 07:46:12 +00:00
"""
2024-10-28 09:33:52 +00:00
def search_bing(
2024-11-17 07:46:12 +00:00
subscription_key: str,
endpoint: str,
locale: str,
query: str,
count: int,
filter_list: Optional[list[str]] = None,
2024-10-28 09:33:52 +00:00
) -> list[SearchResult]:
mkt = locale
2024-11-17 07:46:12 +00:00
params = {"q": query, "mkt": mkt, "answerCount": count}
headers = {"Ocp-Apim-Subscription-Key": subscription_key}
2024-10-28 09:33:52 +00:00
try:
response = requests.get(endpoint, headers=headers, params=params)
response.raise_for_status()
json_response = response.json()
results = json_response.get("webPages", {}).get("value", [])
if filter_list:
results = get_filtered_results(results, filter_list)
return [
SearchResult(
link=result["url"],
title=result.get("name"),
snippet=result.get("snippet"),
)
for result in results
]
except Exception as ex:
log.error(f"Error: {ex}")
raise ex
2024-11-17 07:46:12 +00:00
2024-10-28 09:33:52 +00:00
def main():
parser = argparse.ArgumentParser(description="Search Bing from the command line.")
2024-11-17 07:46:12 +00:00
parser.add_argument(
"query",
type=str,
default="Top 10 international news today",
help="The search query.",
)
parser.add_argument(
"--count", type=int, default=10, help="Number of search results to return."
)
parser.add_argument(
"--filter", nargs="*", help="List of filters to apply to the search results."
)
parser.add_argument(
"--locale",
type=str,
default="en-US",
help="The locale to use for the search, maps to market in api",
)
2024-10-28 09:33:52 +00:00
args = parser.parse_args()
2024-11-17 07:46:12 +00:00
2024-10-28 09:33:52 +00:00
results = search_bing(args.locale, args.query, args.count, args.filter)
2024-11-17 07:46:12 +00:00
pprint(results)