通过 Web 搜索功能增强您的 RAG 应用程序!

通过 web 搜索功能增强您的 rag 应用程序!

介绍

当使用检索增强生成(rag)应用程序构建有趣的项目时,我们经常面临浏览限制等限制,这使得很难获取最新信息或当前数据,例如天气更新(我希望有更有趣的东西)。为了解决这个问题,我们可以为 rag 应用程序配备搜索互联网的工具。让我们开始吧!

 我们的工具台

  • langchain(使用大型语言模型构建应用程序的框架)
  • searxng(免费元搜索引擎)
  • cpython(c 语言包装器:> )
  • docker(一个拿着凉面包的男人)

设置

首先我们从 searxng 安装开始。

1 -) 获取 searxng-docker

git 克隆 https://github.com/searxng/searxng-docker.git

2 -) 编辑 .env 文件以设置主机名和电子邮件

3 -) 生成密钥

<linux>

sed -i "s|ultrasecretkey|$(openssl rand -hex 32)|g" searxng/settings.yml

<macos>
sed -i"" -e "s|ultrasecretkey|$(openssl rand -hex 32)|g" searxng/settings.yml 

<windows>
$randombytes = new-object byte[] 32
(new-object security.cryptography.rngcryptoserviceprovider).getbytes($randombytes)
$secretkey = -join ($randombytes | foreach-object { "{0:x2}" -f $_ })
(get-content searxng/settings.yml) -replace 'ultrasecretkey', $secretkey | set-content searxng/settings.yml
</windows></macos></linux>

4 -) 更新searxng/settings.yml以启用可用的搜索格式并禁用我们的langchain实例的限制器:

use_default_settings: true
server:
  # base_url is defined in the searxng_base_url environment variable, see .env and docker-compose.yml
  secret_key: "<secret-key>"  # change this!
  limiter: false
  image_proxy: true
ui:
  static_use_hash: true
redis:
  url: redis://redis:6379/0

search:
    formats:
        - html
        - json
</secret-key>

5-) 运行 searxng 实例

docker 组成

检查 docker 中的 searxng 部署。如果一切看起来都不错,您就可以继续了。

 演示应用程序

1 -) 创建虚拟环境并激活

python3 -m venv .venv
source .venv/bin/activate

2 -) 安装 langchain

pip install langchain langchain-community

3 -) 创建 main.py

## Simple Get Results
from langchain_community.utilities import SearxSearchWrapper
import pprint

s = SearxSearchWrapper(searx_host="http://localhost:8080",)
result = s.results("What is RAG?", num_results=10, engines=["google"])
pprint.pprint(result)

## Github Tool

from langchain_community.tools.searx_search.tool import SearxSearchResults

wrapper = SearxSearchWrapper(searx_host="**")
github_tool = SearxSearchResults(name="Github", wrapper=wrapper,
                            kwargs = {
                                "engines": ["github"],
                                })

就是这样!您的 rag 应用程序现在具有搜索功能。本指南没有介绍任何新内容,而是旨在汇总向 rag 应用程序添加 web 搜索功能的步骤。希望对您有帮助!

以上就是通过 Web 搜索功能增强您的 RAG 应用程序!的详细内容,更多请关注www.sxiaw.com其它相关文章!