Py之tavily:tavily的简介、安装和使用方法、案例应用之详细攻略

Py之tavily:tavily的简介、安装和使用方法、案例应用之详细攻略

目录

tavily的简介

tavily的安装和使用方法

1、安装

2、使用方法

tavily的案例应用


tavily的简介

Python SDK Python库允许您轻松与Tavily API进行交互,直接从您的Python程序中提供基本和高级搜索功能。轻松地将智能搜索功能集成到您的应用程序中,利用Tavily强大的搜索功能。

搜索API有两个搜索深度选项:基本高级。基本搜索针对性能进行了优化,导致响应时间更快。高级搜索可能需要更长时间(大约5-10秒的响应时间),但优化了质量。

请注意响应内容字段。使用“高级”搜索深度将极大地改善检索到的内容,仅基于相关性得分从每个站点中检索到最相关的内容。可以使用以下方式使用主要搜索方法:

官方文档Python SDK | Tavily AI

tavily的安装和使用方法

1、安装

pip install tavily-python

pip install -i https://mirrors.aliyun.com/pypi/simple tavily-python

2、使用方法


from tavily import TavilyClient
tavily = TavilyClient(api_key=tavily_api_key)

# 基本搜索:
response = tavily.search(query="Should I invest in Apple in 2024?")
print('For basic search: \n',response)
print(response['results'])

# 高级搜索:
response = tavily.search(query="Should I invest in Apple in 2024?", search_depth="advanced")
print('For advanced search: \n',response)

# 获取搜索结果作为传递给LLM的上下文:
# context = [{"url": obj["url"], "content": obj["content"]} for obj in response.results]

# 您可以轻松地根据任何最大标记获取搜索结果上下文,直接传递给您的RAG。
# 响应是在最大标记限制内的上下文字符串。
response = tavily.get_search_context(query="What happened in the burning man floods?", search_depth="advanced", max_tokens=1500)
print('get_search_context: \n',response)

# 您还可以通过一个简单的函数调用,获得包含相关来源的简单问题答案:
response = tavily.qna_search(query="Where does Messi play right now?")
print('qna_search: \n',response)


response = tavily.qna_search(query="一个处女座的程序猿是来自哪个社区的博主?请你简要说一下这位博主的成就")
print('qna_search: \n',response)

For basic search: 
 {'query': 'Should I invest in Apple in 2024?', 'follow_up_questions': None, 'answer': None, 'images': None, 'results': [{'title': 'Should You Buy Apple Stock in 2024? | The Motley Fool', 'url': 'https://www.fool.com/investing/2023/12/26/should-you-buy-apple-stock-in-2024/', 'content': '"\nThe massive number of installed devices worldwide helps Apple make more money through its services, such as the App Store, Apple Music, and Apple Pay. He has said that the introduction of the product "marks the beginning of a new era for computing" and that Vision Pro is "unlike anything created before. The Motley Fool reaches millions of people every month through our premium investing solutions, free guidance and market analysis on Fool.com, top-rated podcasts, and non-profit The Motley Fool Foundation.\n The Motley Fool reaches millions of people every month through our premium investing solutions, free guidance and market analysis on Fool.com, top-rated podcasts, and non-profit The Motley Fool Foundation.\n "\nApple\'s downsides\nAs great as Apple is, the company and its stock have some downsides.', 'score': 0.98934, 'raw_content': None}, {'title': "Buying Apple Stock in 2024 and Expecting It to 2X by 2029? Here's What ...", 'url': 'https://www.fool.com/investing/2023/12/20/buying-apple-stock-2024-expecting-double-2029/', 'content': "Here's What Has to Happen\nKey Points\nNASDAQ: AAPL\nApple\nYou’re reading a free article with opinions that may differ from The Motley Fool’s Premium Investing Services. The Motley Fool reaches millions of people every month through our premium investing solutions, free guidance and market analysis on Fool.com, top-rated podcasts, and non-profit The Motley Fool Foundation.\n The Motley Fool reaches millions of people every month through our premium investing solutions, free guidance and market analysis on Fool.com, top-rated podcasts, and non-profit The Motley Fool Foundation.\n Some bullish investors on the sidelines might be looking to buy shares in the new year with the expectation that Apple could double by 2029. About The Motley Fool\nOur Services\nAround the Globe\nFree Tools\nOur Affiliates", 'score': 0.9818, 'raw_content': None}, {'title': 'Apple stock forecast and price prediction for 2024 - USA TODAY', 'url': 'https://www.usatoday.com/money/blueprint/investing/stock-forecast-apple/', 'content': 'Investing\nTony Dong\nGold price today November 15, 2023\nInvesting\nTony Dong\nMicrosoft (MSFT) stock forecast and price prediction\nInvesting\nWayne Duggan\nGold price today November 14, 2023\nInvesting\nTony Dong\nGold price today November 13, 2023\nInvesting\nTony Dong\nDisney (DIS) stock forecast and price prediction\nInvesting\nWayne Duggan\nBest long-term stocks of November 2023\nInvesting\nWayne Duggan\nGold price today November 8, 2023\n Investing\nBob Haegele\nGold price today November 21, 2023\nInvesting\nTony Dong\nGold price today November 20, 2023\nInvesting\nTony Dong\nAverage net worth by age: See how you compare\nInvesting\nCoryanne Hicks\nGold price today November 16, 2023\n Gold price today November 24, 2023\nInvesting\nTony Dong\nGold price today November 23, 2023\nInvesting\nTony Dong\nGold price today November 22, 2023\nInvesting\nTony Dong\nBest stocks under $10 to buy in November 2023\n Personal Loans\nBest Personal Loans\nAuto Insurance\nBest Auto Insurance\nBANKING\nBest High-Yields Savings Accounts\nCREDIT CARDS\nBest Credit Cards\nAdvertiser Disclosure\nAdvertiser Disclosure\nBlueprint is an independent, advertising-supported comparison service focused on helping readers make smarter decisions. Investing\nTony Dong\nHow to buy Bitcoin\nHow to buy stocks\nHow to invest in gold\nWhat is a brokerage account?\n', 'score': 0.97612, 'raw_content': None}, {'title': 'Should You Buy Apple Stock in 2024? - Yahoo Finance', 'url': 'https://finance.yahoo.com/news/buy-apple-stock-2024-105200222.html', 'content': 'S&P Futures\nDow Futures\nNasdaq Futures\nRussell 2000 Futures\nCrude Oil\nGold\nSilver\nEUR/USD\n10-Yr Bond\nVix\nGBP/USD\nUSD/JPY\nBitcoin USD\nCMC Crypto 200\nFTSE 100\nNikkei 225\nShould You Buy Apple Stock in 2024?\n Before you buy stock in Apple, consider this:\nThe Motley Fool Stock Advisor analyst team just identified what they believe are the 10 best stocks for investors to buy now... and Apple wasn\'t one of them. "\nThe massive number of installed devices worldwide helps Apple make more money through its services, such as the App Store, Apple Music, and Apple Pay. "\nApple\'s downsides\nAs great as Apple is, the company and its stock have some downsides. He has said that the introduction of the product "marks the beginning of a new era for computing" and that Vision Pro is "unlike anything created before.', 'score': 0.97362, 'raw_content': None}, {'title': 'Apple Stock: Get Ready for a Strong 2024, Says Dan Ives - Yahoo Finance', 'url': 'https://finance.yahoo.com/news/apple-stock-ready-strong-2024-161835905.html', 'content': 'S&P 500\nDow 30\nNasdaq\nRussell 2000\nCrude Oil\nGold\nSilver\nEUR/USD\n10-Yr Bond\nGBP/USD\nUSD/JPY\nBitcoin USD\nCMC Crypto 200\nFTSE 100\nNikkei 225\nApple Stock: Get Ready for a Strong 2024, Says Dan Ives\nWell, there it is. “In our opinion the new tech bull market has now begun and tech stocks are set up for a strong 2024 with tech stocks we expect to be up 25% over the next year,” the 5-star analyst said.\n Despite what Ives terms the “iPhone China demise narrative,” based on his Asia supply chain checks, that is nothing more than a “great fictional story” cooked up by the bears and does not reflect “underlying mainland China growth” which has remained strong in the December quarter.\n There’s no doubt it has been the year of the tech giant, with the stocks dubbed the Magnificent 7 driving the bullish narrative. “In a nutshell,” Ives summed up, “2024 is the year for Cook & Co. to show iPhone growth again and further monetize its golden installed base that Cupertino has built.”\n', 'score': 0.96852, 'raw_content': None}], 'response_time': 0.83}
[{'title': 'Should You Buy Apple Stock in 2024? | The Motley Fool', 'url': 'https://www.fool.com/investing/2023/12/26/should-you-buy-apple-stock-in-2024/', 'content': '"\nThe massive number of installed devices worldwide helps Apple make more money through its services, such as the App Store, Apple Music, and Apple Pay. He has said that the introduction of the product "marks the beginning of a new era for computing" and that Vision Pro is "unlike anything created before. The Motley Fool reaches millions of people every month through our premium investing solutions, free guidance and market analysis on Fool.com, top-rated podcasts, and non-profit The Motley Fool Foundation.\n The Motley Fool reaches millions of people every month through our premium investing solutions, free guidance and market analysis on Fool.com, top-rated podcasts, and non-profit The Motley Fool Foundation.\n "\nApple\'s downsides\nAs great as Apple is, the company and its stock have some downsides.', 'score': 0.98934, 'raw_content': None}, {'title': "Buying Apple Stock in 2024 and Expecting It to 2X by 2029? Here's What ...", 'url': 'https://www.fool.com/investing/2023/12/20/buying-apple-stock-2024-expecting-double-2029/', 'content': "Here's What Has to Happen\nKey Points\nNASDAQ: AAPL\nApple\nYou’re reading a free article with opinions that may differ from The Motley Fool’s Premium Investing Services. The Motley Fool reaches millions of people every month through our premium investing solutions, free guidance and market analysis on Fool.com, top-rated podcasts, and non-profit The Motley Fool Foundation.\n The Motley Fool reaches millions of people every month through our premium investing solutions, free guidance and market analysis on Fool.com, top-rated podcasts, and non-profit The Motley Fool Foundation.\n Some bullish investors on the sidelines might be looking to buy shares in the new year with the expectation that Apple could double by 2029. About The Motley Fool\nOur Services\nAround the Globe\nFree Tools\nOur Affiliates", 'score': 0.9818, 'raw_content': None}, {'title': 'Apple stock forecast and price prediction for 2024 - USA TODAY', 'url': 'https://www.usatoday.com/money/blueprint/investing/stock-forecast-apple/', 'content': 'Investing\nTony Dong\nGold price today November 15, 2023\nInvesting\nTony Dong\nMicrosoft (MSFT) stock forecast and price prediction\nInvesting\nWayne Duggan\nGold price today November 14, 2023\nInvesting\nTony Dong\nGold price today November 13, 2023\nInvesting\nTony Dong\nDisney (DIS) stock forecast and price prediction\nInvesting\nWayne Duggan\nBest long-term stocks of November 2023\nInvesting\nWayne Duggan\nGold price today November 8, 2023\n Investing\nBob Haegele\nGold price today November 21, 2023\nInvesting\nTony Dong\nGold price today November 20, 2023\nInvesting\nTony Dong\nAverage net worth by age: See how you compare\nInvesting\nCoryanne Hicks\nGold price today November 16, 2023\n Gold price today November 24, 2023\nInvesting\nTony Dong\nGold price today November 23, 2023\nInvesting\nTony Dong\nGold price today November 22, 2023\nInvesting\nTony Dong\nBest stocks under $10 to buy in November 2023\n Personal Loans\nBest Personal Loans\nAuto Insurance\nBest Auto Insurance\nBANKING\nBest High-Yields Savings Accounts\nCREDIT CARDS\nBest Credit Cards\nAdvertiser Disclosure\nAdvertiser Disclosure\nBlueprint is an independent, advertising-supported comparison service focused on helping readers make smarter decisions. Investing\nTony Dong\nHow to buy Bitcoin\nHow to buy stocks\nHow to invest in gold\nWhat is a brokerage account?\n', 'score': 0.97612, 'raw_content': None}, {'title': 'Should You Buy Apple Stock in 2024? - Yahoo Finance', 'url': 'https://finance.yahoo.com/news/buy-apple-stock-2024-105200222.html', 'content': 'S&P Futures\nDow Futures\nNasdaq Futures\nRussell 2000 Futures\nCrude Oil\nGold\nSilver\nEUR/USD\n10-Yr Bond\nVix\nGBP/USD\nUSD/JPY\nBitcoin USD\nCMC Crypto 200\nFTSE 100\nNikkei 225\nShould You Buy Apple Stock in 2024?\n Before you buy stock in Apple, consider this:\nThe Motley Fool Stock Advisor analyst team just identified what they believe are the 10 best stocks for investors to buy now... and Apple wasn\'t one of them. "\nThe massive number of installed devices worldwide helps Apple make more money through its services, such as the App Store, Apple Music, and Apple Pay. "\nApple\'s downsides\nAs great as Apple is, the company and its stock have some downsides. He has said that the introduction of the product "marks the beginning of a new era for computing" and that Vision Pro is "unlike anything created before.', 'score': 0.97362, 'raw_content': None}, {'title': 'Apple Stock: Get Ready for a Strong 2024, Says Dan Ives - Yahoo Finance', 'url': 'https://finance.yahoo.com/news/apple-stock-ready-strong-2024-161835905.html', 'content': 'S&P 500\nDow 30\nNasdaq\nRussell 2000\nCrude Oil\nGold\nSilver\nEUR/USD\n10-Yr Bond\nGBP/USD\nUSD/JPY\nBitcoin USD\nCMC Crypto 200\nFTSE 100\nNikkei 225\nApple Stock: Get Ready for a Strong 2024, Says Dan Ives\nWell, there it is. “In our opinion the new tech bull market has now begun and tech stocks are set up for a strong 2024 with tech stocks we expect to be up 25% over the next year,” the 5-star analyst said.\n Despite what Ives terms the “iPhone China demise narrative,” based on his Asia supply chain checks, that is nothing more than a “great fictional story” cooked up by the bears and does not reflect “underlying mainland China growth” which has remained strong in the December quarter.\n There’s no doubt it has been the year of the tech giant, with the stocks dubbed the Magnificent 7 driving the bullish narrative. “In a nutshell,” Ives summed up, “2024 is the year for Cook & Co. to show iPhone growth again and further monetize its golden installed base that Cupertino has built.”\n', 'score': 0.96852, 'raw_content': None}]
For advanced search: 
 {'query': 'Should I invest in Apple in 2024?', 'follow_up_questions': None, 'answer': None, 'images': None, 'results': [{'title': 'Should You Buy Apple Stock in 2024? | The Motley Fool', 'url': 'https://www.fool.com/investing/2023/12/26/should-you-buy-apple-stock-in-2024/', 'content': '"\nThe massive number of installed devices worldwide helps Apple make more money through its services, such as the App Store, Apple Music, and Apple Pay. He has said that the introduction of the product "marks the beginning of a new era for computing" and that Vision Pro is "unlike anything created before. The Motley Fool reaches millions of people every month through our premium investing solutions, free guidance and market analysis on Fool.com, top-rated podcasts, and non-profit The Motley Fool Foundation.\n The Motley Fool reaches millions of people every month through our premium investing solutions, free guidance and market analysis on Fool.com, top-rated podcasts, and non-profit The Motley Fool Foundation.\n "\nApple\'s downsides\nAs great as Apple is, the company and its stock have some downsides.', 'score': 0.98687, 'raw_content': None}, {'title': "Buying Apple Stock in 2024 and Expecting It to 2X by 2029? Here's What ...", 'url': 'https://www.fool.com/investing/2023/12/20/buying-apple-stock-2024-expecting-double-2029/', 'content': "Here's What Has to Happen\nKey Points\nNASDAQ: AAPL\nApple\nYou’re reading a free article with opinions that may differ from The Motley Fool’s Premium Investing Services. The Motley Fool reaches millions of people every month through our premium investing solutions, free guidance and market analysis on Fool.com, top-rated podcasts, and non-profit The Motley Fool Foundation.\n The Motley Fool reaches millions of people every month through our premium investing solutions, free guidance and market analysis on Fool.com, top-rated podcasts, and non-profit The Motley Fool Foundation.\n Some bullish investors on the sidelines might be looking to buy shares in the new year with the expectation that Apple could double by 2029. About The Motley Fool\nOur Services\nAround the Globe\nFree Tools\nOur Affiliates", 'score': 0.98131, 'raw_content': None}, {'title': 'Is Apple Stock Going to $240? 1 Wall Street Analyst Thinks So.', 'url': 'https://www.fool.com/investing/2024/06/22/is-apple-stock-going-to-240-1-wall-street-analyst/', 'content': 'The stock is up roughly 9% already in response to the news, and according to wealth management firm Bernstein, Apple could be worth even more. On Friday, Bernstein raised its price target on Apple ...', 'score': 0.97937, 'raw_content': None}, {'title': 'Apple stock forecast and price prediction for 2024 - USA TODAY', 'url': 'https://www.usatoday.com/money/blueprint/investing/stock-forecast-apple/', 'content': 'Investing\nTony Dong\nGold price today November 15, 2023\nInvesting\nTony Dong\nMicrosoft (MSFT) stock forecast and price prediction\nInvesting\nWayne Duggan\nGold price today November 14, 2023\nInvesting\nTony Dong\nGold price today November 13, 2023\nInvesting\nTony Dong\nDisney (DIS) stock forecast and price prediction\nInvesting\nWayne Duggan\nBest long-term stocks of November 2023\nInvesting\nWayne Duggan\nGold price today November 8, 2023\n Investing\nBob Haegele\nGold price today November 21, 2023\nInvesting\nTony Dong\nGold price today November 20, 2023\nInvesting\nTony Dong\nAverage net worth by age: See how you compare\nInvesting\nCoryanne Hicks\nGold price today November 16, 2023\n Gold price today November 24, 2023\nInvesting\nTony Dong\nGold price today November 23, 2023\nInvesting\nTony Dong\nGold price today November 22, 2023\nInvesting\nTony Dong\nBest stocks under $10 to buy in November 2023\n Personal Loans\nBest Personal Loans\nAuto Insurance\nBest Auto Insurance\nBANKING\nBest High-Yields Savings Accounts\nCREDIT CARDS\nBest Credit Cards\nAdvertiser Disclosure\nAdvertiser Disclosure\nBlueprint is an independent, advertising-supported comparison service focused on helping readers make smarter decisions. Investing\nTony Dong\nHow to buy Bitcoin\nHow to buy stocks\nHow to invest in gold\nWhat is a brokerage account?\n', 'score': 0.97385, 'raw_content': None}, {'title': 'Should You Buy Apple Stock in 2024? - Yahoo Finance', 'url': 'https://finance.yahoo.com/news/buy-apple-stock-2024-105200222.html', 'content': 'S&P Futures\nDow Futures\nNasdaq Futures\nRussell 2000 Futures\nCrude Oil\nGold\nSilver\nEUR/USD\n10-Yr Bond\nVix\nGBP/USD\nUSD/JPY\nBitcoin USD\nCMC Crypto 200\nFTSE 100\nNikkei 225\nShould You Buy Apple Stock in 2024?\n Before you buy stock in Apple, consider this:\nThe Motley Fool Stock Advisor analyst team just identified what they believe are the 10 best stocks for investors to buy now... and Apple wasn\'t one of them. "\nThe massive number of installed devices worldwide helps Apple make more money through its services, such as the App Store, Apple Music, and Apple Pay. "\nApple\'s downsides\nAs great as Apple is, the company and its stock have some downsides. He has said that the introduction of the product "marks the beginning of a new era for computing" and that Vision Pro is "unlike anything created before.', 'score': 0.96881, 'raw_content': None}], 'response_time': 0.95}
get_search_context: 
 "[\"{\\\"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 \\\\u2018escape\\\\u2019 from Black Rock City\\\\nComedian Chris Rock and Diplo, producer and DJ,\\\\u00a0were 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 \\\\u2014 among others \\\\u2014 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\\\"}\", \"{\\\"url\\\": \\\"https://abcnews.go.com/US/burning-man-flooding-happened-stranded-festivalgoers/story?id=102908331\\\", \\\"content\\\": \\\"\\\\\\\"\\\\nTop Stories\\\\nMacy's Thanksgiving Day Parade temporarily halted by pro-Palestinian protesters\\\\nGuns N' Roses singer Axl Rose accused of alleged 1989 sexual assault by former model\\\\nFBI: Rainbow Bridge crash, explosion not connected to terrorism\\\\nToxic chemical spill from Kentucky train derailment forces residents to flee homes\\\\nHezbollah fires rockets at north Israel after an airstrike kills 5 of the group's senior fighters\\\\nABC News Live\\\\n24/7 coverage of breaking news and live events ABC News\\\\nVideo\\\\nLive\\\\nShows\\\\nElection 2024\\\\n538\\\\nStream on\\\\nBurning Man flooding: What happened to stranded festivalgoers?\\\\n In response to the unusual weather, event organizers shut down traffic in or out of what is called Black Rock City -- where the festival is held in the desert -- including the local airport.\\\\n MORE: These US regions will experience scorching temperatures for the remainder of Labor Day weekend\\\\nOn Sunday, mobile cell trailers to boost cell service and charging stations were placed around the festival grounds amid the recovery efforts, according to organizers.\\\\n This is typically the driest time of the year for the desert, and it does not take much rain to make the desert floor a mud bath.\\\\n\\\"}\", \"{\\\"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 \\\\u2013 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\\\\u201cQuite a wet start to September for much of eastern CA-western NV,\\\\u201d the National Weather Service in Reno wrote on X. \\\\u201d \\\\u201cAs soon as the tents started getting water-logged or unlivable, people in RVs started taking in some of the tenters, so everybody was warm,\\\\u201d Kaz Qamruddin, who attended the event, told CNN\\\\u2019s 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.\\\"}\"]"
qna_search: 
 Lionel Messi currently plays for Inter Miami in the Major League Soccer (MLS) based in Fort Lauderdale, Florida.
qna_search: 
 这位处女座的程序猿博主是AI知名大佬。他是人工智能硕博生,目前任职于世界200强公司,是核心技术专家,拥有多项发明专利和软件著作权。该博主在国内外多个平台累计粉丝近100万,全网博客文章浏览量超过3000多万,被称为阿里云乘风者计划专家博主等多个平台顶级博主。


tavily的案例应用

持续更新中……

### LangChain 函数调用与工具调用 #### 函数调用 在LangChain框架内,函数调用指的是直接集成特定功能到LLM(大型语言模型)的工作流程中。当用户发出请求时,系统能够解析该请求并自动执行相应的预定义函数[^1]。例如,在Llama 2环境中设置了一个用于获取当前时间戳的简单Python函数: ```python from datetime import datetime def get_current_timestamp(): """返回当前的时间戳""" now = datetime.now() timestamp = now.strftime("%Y-%m-%d %H:%M:%S") return {"timestamp": timestamp} ``` 这种类型的函数可以直接被聊天机器人调用来响应用户的即时需求。 #### 工具调用 相比之下,工具调用涉及更复杂的过程,通常是为了实现某些高级特性而设计的功能模块集合。这些工具可能包括但不限于搜索引擎、数据库查询接口或其他第三方服务API。通过配置`render_text_description`这样的组件,开发者可以让代理理解如何解释来自不同源的数据描述,并据此采取行动[^2]。 以Tavily搜索为例,这是一个具体的外部资源检索插件,允许代理根据输入的问题去互联网上查找相关信息。为了使这一过程更加智能化,可以通过编写自定义逻辑来指导何时以及怎样触发此类操作[^3]。 #### 实际应用对比 对于简单的计算任务或是内部状态更新来说,采用内置函数可能是最简便的选择。然而,如果目标是增强机器人的交互能力,使其能处理更为广泛的任务,则应该考虑引入更多的专用工具支持。下面是一个展示两者差异的小例子: ##### 内置函数示例 假设有一个场景是要让AI助手报告今天的日期: ```python import langchain as lc # 定义一个简单的函数 def today_date(): from datetime import date return str(date.today()) lc.register_function(today_date, "today_date") response = lc.call_function("what's the date today?", ["today_date"]) print(response) ``` ##### 外部工具示例 再看另一个情况,即利用Wikipedia API来进行知识型问答: ```python import wikipediaapi wiki_wiki = wikipediaapi.Wikipedia('en') def search_wikipedia(query): page_py = wiki_wiki.page(query) if not page_py.exists(): return f"No results found for '{query}'" summary = page_py.summary[0:240] + '...' url = page_py.fullurl result = { "summary": summary, "link": url } return result lc.add_tool(search_wikipedia) question = "Tell me about Python programming language." answer = lc.use_tool(question, tool="search_wikipedia") print(f"{answer['summary']}\nRead more at {answer['link']}") ``` 在这个案例里,不仅实现了信息的获取,还提供了额外的学习材料链接给用户。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个处女座的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值