AutoGen结合GoogleSearch实现查询

本文介绍了一个名为AutoGen-google-search的Python库,它结合GoogleSearch和大模型如gpt-3.5,用于自动搜索、信息整合,解决大模型知识更新问题。库中包含搜索函数和网页抓取功能,可通过调用API进行高效研究和报告生成。
摘要由CSDN通过智能技术生成

太叼了。

解决AutoGen下使用GoogleSearch痛点问题,当然,这个工具可以结合任意langchain或者Agent使用,将工具作为Tools中的一个Function进行调用即可,可以实现全面的自动搜索和信息整合,解决大模型知识陈旧问题!!!

即使使用gpt-3.5也可以实现意外的效果!!!

集帅们!墙推!

链接如下:

https://pypi.org/project/autogen-google-search/0.0.5/

1.源代码

以下为源代码:

import os
from autogen import config_list_from_json
import autogen
import requests
from bs4 import BeautifulSoup
import json

from langchain.chat_models import ChatOpenAI
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.summarize import load_summarize_chain
from langchain import PromptTemplate
import openai
from dotenv import load_dotenv
from google_prompt import REASEARCH_PROMPT
# Get API key
load_dotenv()
config_list3 = autogen.config_list_from_json(
    "OAI_CONFIG_LIST",
    filter_dict={
        "model": ["gpt-3.5-turbo"],
    },
)

# Define research function
def search(query):
    url = "https://google.serper.dev/search"

    payload = json.dumps({
        "q": query
    })
    headers = {
        'X-API-KEY': 'xxxxx',
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)

    return response.json()


def scrape(url: str):
    # scrape website, and also will summarize the content based on objective if the content is too large
    # objective is the original objective & task that user give to the agent, url is the url of the website to be scraped

    print("Scraping website...")
    # Define the headers for the request
    headers = {
        'Cache-Control': 'no-cache',
        'Content-Type': 'application/json',
    }

    # Define the data to be sent in the request
    data = {
        "url": url
    }

    # Convert Python object to JSON string
    data_json = json.dumps(data)

    # Send the POST request
    response = requests.post(
        "https://chrome.browserless.io/content?token=2db344e9-a08a-4179-8f48-195a2f7ea6ee", headers=headers, data=data_json)

    # Check the response status code
    if response.status_code == 200:
        soup = BeautifulSoup(response.content, "html.parser")
        text = soup.get_text()
        print("CONTENTTTTTT:", text)
        if len(text) > 8000:
            output = summary(text)
            return output
        else:
            return text
    else:
        print(f"HTTP request failed with status code {response.status_code}")


def summary(content):
    llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo")
    text_splitter = RecursiveCharacterTextSplitter(
        separators=["\n\n", "\n"], chunk_size=10000, chunk_overlap=500)
    docs = text_splitter.create_documents([content])
    map_prompt = """
    Write a detailed summary of the following text for a research purpose:
    "{text}"
    SUMMARY:
    """
    map_prompt_template = PromptTemplate(
        template=map_prompt, input_variables=["text"])

    summary_chain = load_summarize_chain(
        llm=llm,
        chain_type='map_reduce',
        map_prompt=map_prompt_template,
        combine_prompt=map_prompt_template,
        verbose=True
    )

    output = summary_chain.run(input_documents=docs,)

    return output


def research(query):
    llm_config_researcher = {
        "functions": [
            {
                "name": "search",
                "description": "google search for relevant information",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "query": {
                            "type": "string",
                            "description": "Google search query",
                        }
                    },
                    "required": ["query"],
                },
            },
            {
                "name": "scrape",
                "description": "Scraping website content based on url",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "url": {
                            "type": "string",
                            "description": "Website url to scrape",
                        }
                    },
                    "required": ["url"],
                },
            },
        ],
        "config_list": config_list3}

    researcher = autogen.AssistantAgent(
        name="researcher",
        # system_message="Research about a given query, collect as many information as possible, and generate detailed research results with loads of technique details with all reference links attached;Add TERMINATE to the end of the research report;",
        system_message=REASEARCH_PROMPT,
        llm_config=llm_config_researcher,
    )

    user_proxy = autogen.UserProxyAgent(
        name="User_proxy",
        code_execution_config={"last_n_messages": 2, "work_dir": "coding","use_docker": False,},
        # code_execution_config=False,
        is_termination_msg=lambda x: x.get("content", "") and x.get(
            "content", "").rstrip().endswith("TERMINATE"),
        human_input_mode="NEVER",
        function_map={
            "search": search,
            "scrape": scrape,
        }
    )

    user_proxy.initiate_chat(researcher, message=query,max_round=4)

    # set the receiver to be researcher, and get a summary of the research report
    user_proxy.stop_reply_at_receive(researcher)
    user_proxy.send(
        "Give me the research report that just generated again, return ONLY the report & reference links", researcher)

    # return the last message the expert received
    return user_proxy.last_message()["content"]

 2.依赖内容

2.1 OAI_CONFIG_LIST文件

文件格式如下:

 [
     {
         "model": "gpt-3.5-turbo",
         "api_key": "sk-xxxxx"
     },
     {
        "model": "gpt-4",
        "api_key": "sk-xxxx",
        "base_url": "xxxxx"
     }
 ]

2.2 prompt文件

Agent结构较为简单,可以根据自己实际需求修改Agent的system_message,或者添加更多的Agent完成更为复杂的查询任务,比如最后增加一个写blog的Agent实现根据搜索结果写文章的功能!

COMPLETION_PROMPT = "If everything looks good, respond with APPROVED"

REASEARCH_PROMPT = """
You are a specialist in online resource searching. You can search for resources and summarize them in a reasonable format for users based on various questions they pose. Research a given query, collect as much information as possible, and generate detailed research results with loads of technical details, all reference links attached. If product search is involved, please use the full name rather than abbreviations for related products and companies. Add "TERMINATE" to the end of the research report.
"""

3.使用

执行

pip install autogen-google-search==0.0.1

python环境测试:

一行代码即可完成伟大的功能,当然,他完全可以作为一个外部函数供其他Agent使用。

from autogen_product_withgoogle import google_search

res = google_search.search("帮我MiltiAgent框架最新知识,帮我写一篇500字博客")
print(res)

运行结果:

Autogen multiagent是一种自动生成多智能体系统的方法。 在传统的多智能体系统中,需要手动设计和编写每个智能体的行为规则和决策策略。这样做的问题是,当系统需要扩展或修改时,需要手动调整每个智能体的规则和策略,非常耗时和困难。而autogen multiagent方法则通过自动生成智能体系统的规则和策略,极大地简化了系统的设计和维护过程。 具体实现autogen multiagent的方法有多种。一种常用的方法是基于机器学习和优化算法。系统首先采用机器学习算法对智能体的行为规则进行训练,让系统能够从大量的实例中学习合适的决策策略。然后,使用优化算法对系统中的智能体进行优化,并自动调整它们的规则和策略,以实现更高的性能和效率。 另一种实现autogen multiagent的方法是基于进化算法。系统首先通过随机生成一组智能体的规则和策略作为初始种群,然后使用进化算法对这些智能体进行迭代优化。在每一代中,系统通过评估智能体的性能选择出适应度高的个体,并通过交叉和变异等遗传操作生成新的智能体。通过不断迭代优化,系统可以自动生成合适的智能体规则和策略。 总的来说,autogen multiagent实现是一种通过机器学习、优化算法或进化算法等方法自动生成多智能体系统的规则和策略的方法。它可以大大简化多智能体系统的设计和维护过程,并且能够在系统性能和效率方面取得更好的结果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值