Why isn't mkt working in bing search api?

Henry Jin 5 Reputation points
2024-11-28T10:02:21.3766667+00:00

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))
Bing Web Search
Bing Web Search
A Bing service that gives you enhanced search details from billions of web documents.
162 questions
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,206 questions
{count} votes

1 answer

Sort by: Most helpful
  1. romungi-MSFT 47,236 Reputation points Microsoft Employee
    2024-11-28T16:04:37.2733333+00:00

    @Henry Jin I think when you are using setLang in the request as a parameter it accepts the code in ISO 639-1 language code, if you set it incorrectly then it defaults to English. This is documented here.

    If setlang is not valid (for example, zh) or Bing doesn’t support the language (for example, af, af-na), Bing defaults to en (English).

    For Japan it could be ja as per the wiki reference.

    I also see that the headers are used as params in your request. X-MSEdge-ClientIP and X-Search-Location are part of params, try to add them to headers along with setlang change and retry the search.

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.