最近特别火的AI+Search项目,学习并记录下来
开源地址:https://github.com/leptonai/search_with_lepton
项目体验
先进行lepton认证,在https://www.lepton.ai/网站注册账号,下面login时会跳转到该网站给一个认证的Credential字符串,复制过来就可以登录成功(这个字符串似乎生成一次就可以一定时间内多次用
git clone https://github.com/leptonai/search_with_lepton
cd search_with_lepton
pip3 install -U leptonai && lep login
接下来就是获取搜索引擎的密钥,有以下四个方式,看了一下具体情况:
- google0:https://www.searchapi.io/ 免费100条,之后每1000条从4~2 不等,但不同价格也需要交付月费 40 500 不等,但不同价格也需要交付月费40~500 不等,但不同价格也需要交付月费40 500
- google1:https://www.serper.dev/ 免费2500条,之后每1000条从1~0.3 不等,但不同价格也需要交付月费 50 3750 不等,但不同价格也需要交付月费50~3750 不等,但不同价格也需要交付月费50 3750
- google2:https://programmablesearchengine.google.com/ 在自己网站里插入一个是免费的,API收费,
- bing:https://portal.azure.com/ 每个月免费1000条,付费每1000条从15~200$不等,不同价格服务速度和质量有区别
出于简单的目的,我选择使用bing,先开通一个计划(免费,学生,付费)。获取Bing Key流程:在控制台搜索Bing Search V7==>填表开启一个云服务==>转到资源==>密钥和终结点复制密钥
接下来是运行代码。导出密钥,编译前端(node版本至少要大于等于18.17.0),启动后端(对于python3.8 可能需要将search_with_lepton.py第9行替换成from pydantic.typing import)
export BING_SEARCH_V7_SUBSCRIPTION_KEY=xxxxxx
cd web && npm install && npm run build
BACKEND=BING python3 search_with_lepton.py
用自己的服务器部署使用了一下,初始界面输入问题得到的页面会有bug,只会显示出Sources,不会显示Answer和Related,后端看有奇怪的报错。不过再次提问就没有问题了,下面展示几个运行结果,先来个英文问题,谁是特朗普,这个问题没有特别强的时效,答得比较详实
再跟进时事,问一下中国队亚洲杯战绩,准确答对了小组赛的比分,但是被一篇2023U23亚洲杯混淆了(也和我没有指出是卡塔尔亚洲杯有关),说中国队晋级了16强。总的来说时效性还是有的,只是提示词需要更精确一些。
总的来说,响应速度比较快,回答内容也都能跟进最新时事,还有引用,效果真的比较惊艳。
源码解析
1-22行导入必要的包
import concurrent.futures #进程池,线程池,异步操作
import glob #模式字符文件匹配
import json
import os
import re
import threading
import requests
import traceback
from typing import Annotated, List, Generator, Optional #开发过程规定每个数据的类型
from fastapi import HTTPException
from fastapi.responses import HTMLResponse, StreamingResponse, RedirectResponse
import httpx #类似requests的网络请求库,提供同步和异步 API
from loguru import logger #logging的替换品,不需要创建logger对象,开箱即用
import leptonai
from leptonai import Client
from leptonai.kv import KV
from leptonai.photon import Photon, StaticFiles
from leptonai.photon.types import to_bool
from leptonai.api.workspace import WorkspaceInfoLocalRecord
from leptonai.util import tool
24-94行定义一些常量,这里删去了一些注释
#定义搜索的根网址
BING_SEARCH_V7_ENDPOINT = "https://api.bing.microsoft.com/v7.0/search"
BING_MKT = "en-US"
GOOGLE_SEARCH_ENDPOINT = "https://customsearch.googleapis.com/customsearch/v1"
SERPER_SEARCH_ENDPOINT = "https://google.serper.dev/search"
SEARCHAPI_SEARCH_ENDPOINT = "https://www.searchapi.io/api/v1/search"
#定义引用的文章数量,8比较合适
REFERENCE_COUNT = 8
#搜索超时时间5秒
DEFAULT_SEARCH_ENGINE_TIMEOUT = 5
#直接按enter时用的查询
_default_query = "Who said 'live long and prosper'?"
#用户RAG查询的提示词
#第一句话让LLM认清自己的身份以及引用回复的任务要求
#第二句话让LLM回复需要符合准确/长度限制/不重复/不要不懂装懂
#第三句话让LLM引用参考文献
#最后是放置搜索到的文档内容以及用户问题
_rag_query_text = """
You are a large language AI assistant built by Lepton AI. You are given a user question, and please write clean, concise and accurate answer to the question. You will be given a set of related contexts to the question, each starting with a reference number like [[citation:x]], where x is a number. Please use the context and cite the context at the end of each sentence if applicable.
Your answer must be correct, accurate and written by an expert using an unbiased and professional tone. Please limit to 1024 tokens. Do not give any information that is not related to the question, and do not repeat. Say "information is missing on" followed by the related topic, if the given context do not provide sufficient information.
Please cite the contexts with the reference numbers, in the format [citation:x]. If a sentence comes from multiple contexts, please list all applicable citations, like [citation:3][citation:5]. Other than code and specific names and citations, your answer must be written in the same language as the question.
Here are the set of contexts:
{context}
Remember, don't blindly repeat the contexts verbatim. And here is the user question:
"""
#终止词,当遇到以下内容时停止生成
stop_words = [
"<|im_end|>",
"[End]",
"[end]",
"\nReferences:\n",
"\nSources:\n",
"End.",
]
#更多相关问题生成的提示词
#采用回复用户问题与相关问题分开生成的策略,先后发送两个请求给语言模型得到对用户问题的回复和相关的问题
_more_questions_prompt = """
You are a helpful assistant that helps the user to ask related questions, based on user's original question and the related contexts. Please identify worthwhile topics that can be follow-ups, and write questions no longer than 20 words each. Please make sure that specifics, like events, names, locations, are included in follow up questions so they can be asked standalone. For example, if the original question asks about "the Manhattan project", in the follow up question, do not just say "the project", but use the full na