geekan / MetaGPT

🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
https://deepwisdom.ai/
MIT License
43.99k stars 5.24k forks source link

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

Open redlion99 opened 7 months ago

redlion99 commented 7 months ago

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

配置项的效果如下:

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

iorisa commented 7 months ago

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 commented 6 months ago

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

geekan commented 6 months ago

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