From the documentation, I expect to get Japanese results by specifying the follows:
params = {
"mkt": "ja-JP",
"setLang": "jp",
"q": search_term,
"count": count,
"textDecorations": True,
"textFormat": "HTML",
"X-MSEdge-ClientIP":"103.4.8.111",
"X-Search-Location":"lat:35.6;long:139.6;re:100",
}
# Even if IP and Location not set, mkt still doesn't work.
However, in the return header, the actual search market is still where I live:
print("Actual Used Market: ",header["BingAPIs-Market"]) # Actual Used Market: zh-CN
And the payload is also in the wrong language. Why is this happening? How should I change the market?
I see multiple questions on this forum, but none get properly answered.
For reference, this is the whole code I'm running:
"""Util that calls Bing Search."""
from typing import Any, Dict, List
import requests
from langchain_core.utils import get_from_dict_or_env
from pydantic import BaseModel
DEFAULT_BING_SEARCH_ENDPOINT = "https://api.bing.microsoft.com/v7.0/search"
class BingSearchAPIWrapper():
"""Wrapper for Bing Web Search API."""
bing_subscription_key: str
bing_search_url: str
k: int = 10
"""Additional keyword arguments to pass to the search request."""
def __init__(self, **kwargs):
self.validate_environment(kwargs)
def _bing_search_results(self, search_term: str, count: int) -> List[dict]:
headers = {"Ocp-Apim-Subscription-Key": self.bing_subscription_key}
# Accept-language: Use this header and the cc query parameter only if you specify multiple languages. Otherwise, use the mkt and setLang query parameters.
params = {
"mkt": "fr-FR",
"setLang": "fr",
"q": search_term,
"count": count,
"textDecorations": True,
"textFormat": "HTML",
}
response = requests.get(
self.bing_search_url,
headers=headers,
params=params, # type: ignore
)
response.raise_for_status()
search_results = response.json()
header=response.headers
# The market used by the request. The form is <languageCode>-<countryCode>. For example, en-US. This value may be different from the value you specify in the request's mkt query parameter if that market value is not listed in Market codes. The same is true if you specify values for cc and Accept-Language that can't be reconciled.
print("Actual Used Market: ",header["BingAPIs-Market"])
if "webPages" in search_results:
return search_results["webPages"]["value"]
return []
def validate_environment(self, values: Dict) -> Any:
"""Validate that api key and endpoint exists in environment."""
self.bing_subscription_key = get_from_dict_or_env(
values, "bing_subscription_key", "BING_SUBSCRIPTION_KEY"
)
self.bing_search_url = get_from_dict_or_env(
values,
"bing_search_url",
"BING_SEARCH_URL",
default=DEFAULT_BING_SEARCH_ENDPOINT,
)
def run(self, query: str) -> str:
"""Run query through BingSearch and parse result."""
snippets = []
results = self._bing_search_results(query, count=self.k)
if len(results) == 0:
return "No good Bing Search Result was found"
for result in results:
snippets.append(result["snippet"])
return " ".join(snippets)
def results(self, query: str, num_results: int) -> List[Dict]:
"""Run query through BingSearch and return metadata.
Args:
query: The query to search for.
num_results: The number of results to return.
Returns:
A list of dictionaries with the following keys:
snippet - The description of the result.
title - The title of the result.
link - The link to the result.
"""
metadata_results = []
results = self._bing_search_results(query, count=num_results)
if len(results) == 0:
return [{"Result": "No good Bing Search Result was found"}]
for result in results:
metadata_result = {
"snippet": result["snippet"],
"title": result["name"],
"link": result["url"],
}
metadata_results.append(metadata_result)
return metadata_results
search= BingSearchAPIWrapper()
print(search.results("Microsoft Azure",1))