大模型调用搜索接口后,将信息解析,将功能封装【游玩大模型 四】

本文介绍了如何将GPT的网络检索功能整合到chain中,通过封装和解析,实现对天气信息的查询,并利用正则表达式处理API返回的搜索结果。
摘要由CSDN通过智能技术生成

封装之前的天气模块

功能与问题

  • 上一篇文章中,我们成果的调用了GPT的网络检索接口,这次我们将这个功能集成到chain中

流程

  1. 解析
  2. 封装
# 做成transformerChain (封装)



import os
from langchain_openai import OpenAI


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



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=今天北京市天气
}


import re
def parse_weather_info(weather_info: str) -> dict:
    print(weather_info)
    # 使用正则表达式提取数据
    temperature_match = re.search(r'(\d+)°F', weather_info)
    precipitation_match = re.search(r'Precipitation: (\d+)%', weather_info)
    humidity_match = re.search(r'Humidity: (\d+)%', weather_info)
    wind_match = re.search(r'Wind: (\d+) mph', weather_info)

    # 存储提取的数据的字典
    weather_data = {}

    # 将提取的数据存储在字典中
    if temperature_match:
        weather_data['Temperature'] = temperature_match.group(1)

    if precipitation_match:
        weather_data['Precipitation'] = precipitation_match.group(1)

    if humidity_match:
        weather_data['Humidity'] = humidity_match.group(1)

    if wind_match:
        weather_data['Wind'] = wind_match.group(1)
    print("!!!!!!!!!!!!!")
    print(weather_data)
    return weather_data

def transform_func(inputs: dict) -> dict:
    text = inputs["output"]
    return {"weather_info":parse_weather_info(text)}

# {'Temperature': '478', 'Precipitation': '0', 'Humidity': '45', 'Wind': '6'}
from langchain.chains import TransformChain,SequentialChain
transform_chain = TransformChain(input_variables = ["output"],
                                 output_variables=["weather_info"],
                                 transform=transform_func)
final_chain = SequentialChain(chains=[request_chain,transform_chain],
                              input_variables=["query","url"],output_variables=["weather_info"])


final_rst = final_chain.run(inputs)
print(final_rst)
# {'Temperature': '478', 'Precipitation': '0', 'Humidity': '45', 'Wind': '6'}

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值