当前位置:   article > 正文

大模型调用搜索接口【游玩大模型 三】_让大模型调用搜索引擎的方法

让大模型调用搜索引擎的方法

大模型调用搜索接口

功能与问题

  • 单纯调用搜索接口

流程

  1. 写代码
import os
from langchain_openai import OpenAI


os.environ["OPENAI_API_BASE"] = ''
os.environ["OPENAI_API_KEY"] = ''



from aiohttp.client import request
from tempfile import template
from langchain.chains import LLMChain
from langchain.chains import LLMRequestsChain
from langchain.prompts import PromptTemplate
# 定义搜索模板
template = '''在>>> 和 <<<直接是来自google的原始搜索结果。
请把对于问题'{query}'的答案从里面提取出来,如果里面没有相关信息的化就说“找不到”
请使用以下格式:
Extracted:<anser or "找不到">
>>> {requests_result} <<<
Extracted:
'''

PROMPT = PromptTemplate(
    input_variables=["query","requests_result"],
    template=template,
)
request_chain = LLMRequestsChain(llm_chain=LLMChain(llm = OpenAI(temperature = 0),prompt=PROMPT))


question="今天北京市天气"
inputs={
    "query":question,
    #"url":"https://www.google.com/search?q="+question.replace(" ","+")
    "url":"https://www.baidu.com/s?wd="+question.replace(" ","+")
    #"url":"https://cn.bing.com/search?q="+question.replace(" ","+")
    # https://cn.bing.com/search?q=今天北京市天气
}
# 运行一下就会通过opeenAI提取搜索结果
result=request_chain(inputs)
print(result)
# {'query': '今天上海天气', 'url': 'https://www.baidu.com/s?wd=今天上海天气', 'output': '今天上海天气,小雨,7~10℃,东北风4~5级,空气质量优'}
print(result['output'])

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/821301
推荐阅读
相关标签
  

闽ICP备14008679号