GPT实战系列-使用LangChain内部工具

GPT实战系列-使用LangChain内部工具

LangChain

GPT实战系列-LangChain如何构建基通义千问的多工具链

GPT实战系列-构建多参数的自定义LangChain工具

GPT实战系列-通过Basetool构建自定义LangChain工具方法

GPT实战系列-一种构建LangChain自定义Tool工具的简单方法

GPT实战系列-搭建LangChain流程简单应用

GPT实战系列-简单聊聊LangChain搭建本地知识库准备

GPT实战系列-LangChain + ChatGLM3构建天气查询助手

GPT实战系列-大模型为我所用之借用ChatGLM3构建查询助手

GPT实战系列-简单聊聊LangChain

大模型查询工具助手之股票免费查询接口

随着OpenAI的GPT-4这样的大型语言模型(LLMs)已经风靡全球,现在让它们自动执行各种任务,如回答问题、翻译语言、分析文本等。LLMs是在交互上真正体验到像"人工智能"。

如何管理这些模块呢?

LangChain在这方面发挥重要作用。LangChain使构建由LLMs驱动的应用程序变得简单,使用LangChain,可以在统一的界面中轻松与不同类型的LLMs进行交互,管理模型版本,管理对话版本,并将LLMs连接在一起。

python 复制代码
from typing import Optional, Type

from langchain.callbacks.manager import (
    AsyncCallbackManagerForToolRun,
    CallbackManagerForToolRun,
)

自带检索工具Tavily

LangChain可以连接到自己定义的工具,也可以连接到内嵌的tool提供商。这里介绍Tavily搜索引擎作为工具。

首先需要获得API key

定义引用需要用的模块:

python 复制代码
export TAVILY_API_KEY="..."

加载相关模块,langchain_community已经定义,可实现真正搜索。

python 复制代码
from langchain_community.tools.tavily_search import TavilySearchResults

后面的事情就变得简单了:

python 复制代码
# 搜索工具 赋值
search = TavilySearchResults()

search.invoke({"query": "What happened in the latest burning man floods"})

实例化,并打印,得到类似这样的输出:

python 复制代码
[{'url': 'https://www.npr.org/2023/09/03/1197497458/the-latest-on-the-burning-man-flooding', 'content': "There are also reports that at least one person has died at the counterculture festival about a hundred miles north of Reno, Nev. Earlier this afternoon, I caught up with NPR's Claudia Peschiutta, who's at her first burn, and she told me it's muddy where she is, but that she and her camp family have been making the best of things.\n National\nThe latest on the Burning Man flooding\nClaudia Peschiutta\nAuthorities are investigating a death at the Burning Man festival in the Nevada desert after tens of thousands of people are stuck in camps because of rain.\n SCOTT DETROW, HOST:\nKnee-deep mud, warnings to conserve food and water, orders to shelter in place - this is all at Burning Man 2023 after torrential rains turned the Black Rock Desert into miles and miles of mud. I mean, mostly what I've seen from my personal experience is just any sort of need that you have, somebody, whether friend or neighbor or stranger, will jump in to help you out in some way. And I should mention that desert Wi-Fi is doing the best as it can as we talk to you, dropping in and out."}, 
 {'url': 'https://www.cnn.com/2023/09/05/us/burning-man-storms-shelter-exodus-tuesday/index.html', 'content': "CNN values your feedback\nBurning Man attendees make a mass exodus after a dramatic weekend that left thousands stuck in the Nevada desert\nThousands of Burning Man attendees finally made their mass exodus after intense rain over the weekend flooded camp sites and filled them with thick, ankle-deep mud -- stranding more than 70,000 free-spirited revelers as they waited for the Nevada desert city to dry out.\n Burning Man organizers lift driving ban after heavy rains left the event smothered in mud and trapped thousands\nThe area was still muddy and parts were still difficult to navigate, organizers warned, and the wait time to leave the city Monday night was about seven hours. Diplo hitchhiked ride out of rain-drenched Burning Man after walking miles 'through the mud' and actually made it to his DC concert\n"Quite a wet start to September for much of eastern CA-western NV," the National Weather Service in Reno wrote on X. " "As soon as the tents started getting water-logged or unlivable, people in RVs started taking in some of the tenters, so everybody was warm," Kaz Qamruddin, who attended the event, told CNN's Brianna Keilar Monday.\n From wood blocks to 'poop buckets,' how Burning Man organizers told festivalgoers to prepare for heavy rain\nAmong the early departures was music DJ Diplo, who told CNN he walked several miles in the muddy desert Saturday morning along with other celebrities, including Chris Rock, Cindy Crawford, Kaia Gerber and Austin Butler."}, 
 {'url': 'https://www.cnn.com/2023/09/03/us/burning-man-storms-shelter-sunday/index.html', 'content': "The rainy conditions forecast over the area for Sunday afternoon had mostly passed to the east of the festival site, according to a social media post from organizers, though there is still a chance of showers and thunderstorms "for the rest of daylight hours" into the evening.\n Diplo hitchhiked ride out of rain-drenched Burning Man after walking miles 'through the mud' and actually made it to his DC concert\nA death at the festival that was confirmed by authorities on Saturday was "unrelated to the weather," Burning Man organizers said Sunday night.\n The rain "made it virtually impossible for motorized vehicles to traverse the playa," the Pershing County Sheriff's Office said, noting people were advised to shelter in place until the ground has dried enough to drive on safely.\n Andrew Hyde, another attendee stuck at the Burning Man, said despite the muddy conditions making it difficult to walk, the weather has taken the meaning of the event back to its roots.\n "There is super limited bandwidth and a lot of people at the camp (are) trying to cancel flights and arrange for extended time here" due to the weather, Burhorn told CNN via text message from a Wi-Fi camp.\n"}, 
 {'url': 'https://www.nbcnews.com/news/us-news/live-blog/live-updates-burning-man-flooding-keeps-thousands-stranded-nevada-site-rcna103193', 'content': "Profile\nSections\ntv\nFeatured\nMore From NBC\nFollow NBC News\nnews Alerts\nThere are no new alerts at this time\nBurning Man flooding keeps thousands stranded at Nevada site as authorities investigate 1 death\nBurning Man attendees struggling to get home\n70,000+ stuck at Burning Man: When will they be able to get out?\n Thousands still stranded at Burning Man after torrential rain\nBurning Man revelers unfazed by deluge and deep mud\nReuters\nThousands of Burning Man attendees partied hard on Sunday despite downpours that turned the Nevada desert where the annual arts and music festival takes place into a sea of sticky mud and led officials to order the multitudes to shelter in place.\n Neal Katyal warns hiking in the mud\ncan be 'worse than walking on ice'\nDoha Madani\nNeal Katyal, the former acting U.S. solicitor general, is among the Burning Man attendees who decided to take the risk and hike out of the festival grounds.\n Videos posted to his Instagram story show Diplo walking through mud before, he says, he hitchhiked to Gerlach and Reno to make a flight to Washington, D.C.\n"I just got done DJ'ing for three hours, after walking f---ing for four hours out of the desert and taking a flight, mud still on my face," he said in a video posted to his Instagram story last night.\n Burning Man memes are swamping social media\nAngela Yang\nAs heavy rain turns Burning Man 2023 into a muddy mess, a deluge of unsympathetic jokes has swamped the internet outside Black Rock City, the temporary location built annually for the nine-day festival in the remote desert of Nevada.\n"}, 
 {'url': 'https://www.today.com/news/what-is-burning-man-flood-death-rcna103231', 'content': '"\nAn update on BurningMan.org posted the night of Sept. 3 offered those still on site a "Wet Playa Survival Guide" with tips, as well as an update saying that conditions were "improving" and that roads in Black Rock City were expected to open on Sept. 4 for "Exodus."\n"Thank goodness this community knows how to take care of each other," the Instagram page for Burning Man Information Radio wrote on a post predicting more rain.\nNeal Katyal, Chris Rock and others detail their 'escape' from Black Rock City\nComedian Chris Rock and Diplo, producer and DJ,\xa0were among this year\'s Burning Man attendees. Why are people stranded at Burning Man?\nEach year participants in the Burning Man event gather for nine days in an "annual experiment in temporary community dedicated to Radical Self-expression and Radical Self-reliance," according to burningman.org.\nHeld in Nevada since 1991, the festival sees dreamers, artists, tech founders and celebrities --- among others --- converge to create a temporary community in the desert.\n By Sept. 1, organizers tweeted on X that the more than 70,000 attendees of Burning Man should "help each other stay safe" and that the gate and airport in and out of the area was closed due to the impossibility for vehicles to "traverse the playa. In 2014, a 29-year-old woman was hit by a bus outside of Black Rock City, and a motorcycle accident claimed the life of a friend of the Burning Man founders back in 1996, according to an article in the Reno Gazette Journal.\n "A fan offered Chris Rock and I a ride out of burning man in the back of a pick up," Diplo wrote in a text overlay of the video.\n'}]

LangChain是一个Python框架,可以使用LLMs构建应用程序。它与各种模块连接,使与LLM和提示管理,一切变得简单。

觉得有用 收藏 收藏 收藏

点个赞 点个赞 点个赞

End

GPT专栏文章:

GPT实战系列-实战Qwen通义千问在Cuda 12+24G部署方案_通义千问 ptuning-CSDN博客

GPT实战系列-ChatGLM3本地部署CUDA11+1080Ti+显卡24G实战方案

GPT实战系列-Baichuan2本地化部署实战方案

GPT实战系列-让CodeGeeX2帮你写代码和注释_codegeex 中文-CSDN博客

GPT实战系列-ChatGLM3管理工具的API接口_chatglm3 api文档-CSDN博客

GPT实战系列-大话LLM大模型训练-CSDN博客

GPT实战系列-LangChain + ChatGLM3构建天气查询助手

GPT实战系列-大模型为我所用之借用ChatGLM3构建查询助手

GPT实战系列-P-Tuning本地化训练ChatGLM2等LLM模型,到底做了什么?(二)

GPT实战系列-P-Tuning本地化训练ChatGLM2等LLM模型,到底做了什么?(一)

GPT实战系列-ChatGLM2模型的微调训练参数解读

GPT实战系列-如何用自己数据微调ChatGLM2模型训练

GPT实战系列-ChatGLM2部署Ubuntu+Cuda11+显存24G实战方案

GPT实战系列-Baichuan2等大模型的计算精度与量化

GPT实战系列-GPT训练的Pretraining,SFT,Reward Modeling,RLHF

GPT实战系列-探究GPT等大模型的文本生成-CSDN博客

相关推荐
光芒再现dev9 小时前
已解决,部署GPTSoVITS报错‘AsyncRequest‘ object has no attribute ‘_json_response_data‘
运维·python·gpt·语言模型·自然语言处理
知来者逆1 天前
使用 GPT-4V 全面评估泛化情绪识别 (GER)
人工智能·gpt·语言模型·自然语言处理·gpt-4v
github_czy1 天前
使用GPT-SoVITS训练语音模型
人工智能·gpt
Yeats_Liao2 天前
昇思大模型平台打卡体验活动:基于MindSpore实现GPT1影评分类
gpt·分类·数据挖掘
龙的爹23332 天前
论文 | Evaluating the Robustness of Discrete Prompts
人工智能·gpt·自然语言处理·nlp·prompt·agi
waiting不是违停2 天前
LangChain Ollama实战文献检索助手(二)少样本提示FewShotPromptTemplate示例选择器
langchain·llm·ollama
Y24834908912 天前
05LangChain实战课 - 提示工程与FewShotPromptTemplate的应用
人工智能·langchain
大数据面试宝典2 天前
【Ai测评】GPT Search偷偷上线,向Google和微软发起挑战!
人工智能·gpt·ai测评·ai之家
灰哥数据智能2 天前
DB-GPT系列(二):DB-GPT部署(镜像一键部署、源码部署)
python·gpt·语言模型·数据分析
开发者每周简报2 天前
ChatGPT o1与GPT-4o、Claude 3.5 Sonnet和Gemini 1.5 Pro的比较
人工智能·gpt·chatgpt