avatarKamal Dhungana


Leveraging LLM Technology: How Tavily Simplifies Online Search

Image created by the author using DELL.E

Tavily is a versatile tool designed to enhance and simplify the research process. It serves as a platform that specializes in identifying accurate sources and organizing findings effectively. The goal of Tavily is to make research a smoother and more enjoyable task by transforming the complex process of navigating through vast amounts of information on the internet into a straightforward and efficient experience

The core feature of Tavily is its Search API, which is specifically crafted for language learning models (LLMs) and AI applications. This search engine connects AI with the web, enabling the retrieval of real-time, accurate, and factual results. This capability is crucial for reducing common issues such as hallucinations and biases in AI, leading to more informed decision-making. To use Tavily, one simply makes an API call, and Tavily then scans through numerous authoritative sources to compile relevant information into organized results. This process is designed to be time-saving, accurate, and easy to use, making it accessible to users of all skill levels.

The advantages of using Tavily include significant time savings by conducting thorough research quickly, ensuring accuracy by verifying sources for credibility, and providing an intuitive platform that is easy to navigate. However, potential drawbacks include the possibility of developing an overreliance on technology, which may limit learning and critical thinking, and the inherent internet bias, as the API’s effectiveness is subject to the availability and prominence of information on the web. Despite these considerations, Tavily stands out as a valuable tool for anyone looking to streamline their research process and access meaningful insights with minimal effort.

How to use it?

We can find the official document for Tavily here. The initial step involves creating an account. Once the account is created, an API key will become available, which can be integrated into our application for usage. For testing purposes, we can utilize the web interface (playground). The interface will resemble the following. It brings multiple contents with their relevent scores.

The service is not entirely free of charge. Initially, users are granted 1000 API calls at no cost. Beyond this introductory offer, a subscription fee is required, as outlined below.

LangChain Implementation

Now, the Tavily API can be seamlessly integrated into our Langchain application, providing real-time online information optimized for RAG. Additionally, we have the option to develop a search application directly using the Python SDK (tavily-python), as detailed in this documentation. Below are the steps for LangChain integration.

!pip install -U langchain-community tavily-python

import os
from langchain.retrievers.tavily_search_api import TavilySearchAPIRetriever

from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI

# First Part: 
# provide api keys 
os.environ["TAVILY_API_KEY"] = "tvly-hig3jGeujMrsYzxxx9Kpbxxg2zxxy"
os.environ["OPENAI_API_KEY"] = "sk-G7FmmndGowXOWegxxxx3BlbkFJj7AmmmF6AKKaSVTGQw"

We initiate a retriever using the tavily_search_api function, enabling us to retrieve online results relevant to the query. Setting k=3 indicates our preference to obtain only the top three documents.

retriever = TavilySearchAPIRetriever(k=3)

retriever.invoke("who won 2024 super bowl?")

# here are the documents searched

#[Document(page_content="With a relentless defense and opportune plays by their star quarterback -- including a pair of gutsy overtime scrambles -- the Chiefs won their third Super Bowl in five years in a 25-22 overtime victory against the San Francisco 49ers in only the second overtime game in Super Bowl history.\n Staff\nTaylor Swift supports Travis Kelce, chugs drink at Super Bowl LVIII10hTory Barron\nAfter posting a losing record in December, the Chiefs embraced an underdog, villain mentality throughout the postseason, upsetting three teams en route to their second consecutive Super Bowl title, becoming the ninth team to repeat as Super Bowl champions and first since the 2004 New England Patriots.\n ESPN\nSuper Bowl 2024 - Highlights from Chiefs' win vs. 49ers\nMike Tannenbaum and Tim Hasselbeck react to the Chiefs' thrilling overtime victory over the 49ers in the Super Bowl. The 49ers had the ball with 2:51 to go in a tied game, but a signature Steve Spagnuolo blitz on third down late in the fourth quarter forced a 49ers field goal. Led by their captains, most of the Chiefs arrived to Allegiant Stadium in Las Vegas on Sunday in all black, signaling a steely resolve to upset Brock Purdy and the NFC's best offensive ensemble.\n", metadata={'title': "Super Bowl 2024 - Highlights from Chiefs' win vs. 49ers - ESPN", 'source': 'https://www.espn.com/nfl/story/_/id/39480722/49ers-chiefs-live-super-bowl-lviii-updates-moments-highlights', 'score': 0.97542, 'images': None}),
# Document(page_content="The championship-winning drive, which included a fourth-and-1 scramble from Mahomes and a clutch 7-yard catch from tight end Travis Kelce, was a must-score for K.C. The NFL's new playoff overtime rules -- both teams are guaranteed at least one possession in the extra period -- were in effect for the first time, and the Chiefs needed to answer the Niners' field goal.\n Held out of the end zone until that point, Kansas City grabbed its first lead of the game at 13-10.\nJennings' touchdown receiving (followed by a missed extra point) concluded a 75-yard drive that put the Niners back on top, 16-13, as the wideout joined former Philadelphia Eagles quarterback Nick Foles as the only players to throw and catch a touchdown in a Super Bowl.\n He spread the ball around -- eight pass-catchers had at least two receptions -- slowly but surely overcoming a threatening 49ers defense that knocked him off his spot consistently in the first half.\nMahomes, with his third Super Bowl MVP, now sits alongside Tom Brady (five) and Joe Montana (three) atop the mountain while becoming just the third player to win the award back-to-back, joining Bart Starr (I-II) and Terry Bradshaw (XIII-XIV).\n The muffed punt that bounced off of cornerback Darrell Luter Jr.'s ankle was also the big break that the Chiefs needed as they scored on the very next play to take the lead for the first time in the game. College Pick'em\nA Daily SportsLine Betting Podcast\nNFL Playoff Time!\n2024 Super Bowl, Chiefs vs. 49ers score: Patrick Mahomes leads OT comeback as K.C. wins back-to-back titles\nCall it a dynasty; the Chiefs are the first team to win consecutive Super Bowls since 2003-04\nThe Kansas City Chiefs are Super Bowl champions, again.", metadata={'title': '2024 Super Bowl, Chiefs vs. 49ers score: Patrick Mahomes leads OT ...', 'source': 'https://www.cbssports.com/nfl/news/2024-super-bowl-chiefs-vs-49ers-score-patrick-mahomes-leads-ot-comeback-as-k-c-wins-back-to-back-titles/live/', 'score': 0.9473, 'images': None}),
# Document(page_content='Throw in the fact that Chiefs coach Andy Reid will be in his fifth Super Bowl, the third most in NFL history, and has a chance to win a third ring, and the knowledge on the Kansas City sideline will be an advantage too big for the 49ers to overcome.\n She performed in Japan on Saturday night before a flight across nine time zones and the international date line to reach the U.S.\nRihanna performs during halftime of the NFL Super Bowl 57 football game between the Philadelphia Eagles and the Kansas City Chiefs, Sunday, Feb. 12, 2023, in Glendale, Ariz. (AP Photo/David J. Phillip)\n After the teams take the field, Post Malone will perform “America the Beautiful” and Reba McEntire will sing “The Star-Spangled Banner.”\nSan Francisco 49ers quarterback Brock Purdy (13) warms up before the NFL Super Bowl 58 football game against the Kansas City Chiefs, Sunday, Feb. 11, 2024, in Las Vegas. He was also the referee when the Chiefs beat the 49ers in the Super Bowl four years ago — and when the Rams beat the Saints in the 2019 NFC championship game after an infamous missed call.\n Purdy’s comeback from the injury to his throwing arm suffered in last season’s NFC championship loss to the Philadelphia Eagles has been part of the storybook start to his career that started as Mr. Irrelevant as the 262nd pick in the 2022 draft.\n', metadata={'title': 'Super Bowl 2024 highlights: Kansas City Chiefs win | AP News', 'source': 'https://apnews.com/live/super-bowl-2024-updates', 'score': 0.93697, 'images': None})]

We aim to generate the ultimate result utilizing the retrieved documents. To accomplish this, we establish a chain and activate it with the specified query, as demonstrated below:

rompt = ChatPromptTemplate.from_template(
    """Answer the question based only on the context provided.

Context: {context}

Question: {question}"""
chain = (
    RunnablePassthrough.assign(context=(lambda x: x["question"]) | retriever)
    | prompt
    | ChatOpenAI(model="gpt-4-1106-preview")
    | StrOutputParser()

chain.invoke({'question':'who won 2024 super bowl?'})

# Final Result

'The Kansas City Chiefs won the 2024 Super Bowl.'

In conclusion, Tavily emerges as a versatile tool poised to revolutionize the research landscape by simplifying the process and enhancing efficiency. With its specialized Search API tailored for language learning models and AI applications, Tavily bridges the gap between artificial intelligence and the web, facilitating the retrieval of real-time, factual information crucial for informed decision-making. While Tavily offers significant advantages such as time-saving research, accuracy verification, and user-friendly navigation, potential drawbacks include the risk of overreliance on technology and susceptibility to internet bias. Nonetheless, Tavily remains a valuable asset for those seeking to streamline their research endeavors and access meaningful insights effortlessly. Through its integration into frameworks like LangChain, Tavily further solidifies its position as an indispensable tool in the quest for knowledge.

If you enjoyed this article, consider trying out the AI service I recommend. It provides the same performance and functions to ChatGPT Plus(GPT-4) but more cost-effective, at just $6/month (Special offer for $1/month). Click here to try ZAI.chat.

Langchain Agents
Recommended from ReadMedium