Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serpapi的配置项中增加搜索引擎的相关配置,以支持更多的搜索引擎如:bing, baidu 等 #957

Closed
redlion99 opened this issue Mar 4, 2024 · 4 comments

Comments

@redlion99
Copy link

在serpapi的配置项中能否增加搜索引擎的配置,以支持更多的搜索引擎如:bing, baidu 等
image

配置项的效果如下:

search:
api_type: 'serpapi' # serpapi/google/serper/ddg
api_key: 'xxxxx'
params:
engine: 'baidu'
ct: 2

@iorisa
Copy link
Collaborator

iorisa commented Mar 4, 2024

params参数是个dict,可以这么配的:

class SerpAPIWrapper(BaseModel):
    model_config = ConfigDict(arbitrary_types_allowed=True)

    search_engine: Any = None  #: :meta private:
    params: dict = Field(
        default_factory=lambda: {
            "engine": "google",
            "google_domain": "google.com",
            "gl": "us",
            "hl": "en",
        }
    )
    # should add `validate_default=True` to check with default value
    serpapi_api_key: Optional[str] = Field(default=None, validate_default=True)
    aiosession: Optional[aiohttp.ClientSession] = None

配置上去会自动生效的,源代码在metagpt/tools/search_engine_serpapi.py里:

    async def results(self, query: str, max_results: int) -> dict:
        """Use aiohttp to run query through SerpAPI and return the results async."""

        def construct_url_and_params() -> Tuple[str, Dict[str, str]]:
            params = self.get_params(query)
            params["source"] = "python"
            params["num"] = max_results
            params["output"] = "json"
            url = "https://serpapi.com/search"
            return url, params

        url, params = construct_url_and_params()
        if not self.aiosession:
            async with aiohttp.ClientSession() as session:
                async with session.get(url, params=params) as response:
                    response.raise_for_status()
                    res = await response.json()
        else:
            async with self.aiosession.get(url, params=params) as response:
                response.raise_for_status()
                res = await response.json()

        return res

    def get_params(self, query: str) -> Dict[str, str]:
        """Get parameters for SerpAPI."""
        _params = {
            "api_key": self.serpapi_api_key,
            "q": query,
        }
        params = {**self.params, **_params}
        return params

@redlion99
Copy link
Author

我可能没有表达清楚,我的意思是可以通过配置文件去修改这些参数。

@geekan
Copy link
Owner

geekan commented Mar 21, 2024

@redlion99 I have added the corresponding configuration. Can you take a look at the relevant PR and test whether it solves your problem?

@better629
Copy link
Collaborator

Due to the lack of updates or replies by the user for a long time, we will close it. Please reopen it if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants