open-webui/backend/open_webui/retrieval/web/bing.py
Classic298 c48ac5163c
fix: pass subscription_key and endpoint in bing.py CLI search_bing() call (#24768)
The __main__ block called search_bing() with 4 positional arguments, but
the function requires 5 (subscription_key, endpoint, locale, query,
count). Running `python -m open_webui.retrieval.web.bing` raised a
TypeError and, before failing, silently misrouted every argument. Read
the key/endpoint from environment variables, matching config.py defaults.

Closes #24765

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-19 21:26:10 +04:00

77 lines
2.2 KiB
Python

import argparse
import logging
import os
from pprint import pprint
from typing import Optional
import requests
from open_webui.retrieval.web.main import SearchResult, get_filtered_results
log = logging.getLogger(__name__)
"""
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-web-search/overview
"""
def search_bing(
subscription_key: str,
endpoint: str,
locale: str,
query: str,
count: int,
filter_list: Optional[list[str]] = None,
) -> list[SearchResult]:
mkt = locale
params = {'q': query, 'mkt': mkt, 'count': count}
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
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
def main():
parser = argparse.ArgumentParser(description='Search Bing from the command line.')
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',
)
args = parser.parse_args()
results = search_bing(
os.environ.get('BING_SEARCH_V7_SUBSCRIPTION_KEY', ''),
os.environ.get(
'BING_SEARCH_V7_ENDPOINT', 'https://api.bing.microsoft.com/v7.0/search'
),
args.locale,
args.query,
args.count,
args.filter,
)
pprint(results)