Running Tavily.ai To Connect Your LLMs To The Internet



From YouTube to Blog: Running Tav AI to Connect Your LLMs to the Internet


 Introduction

Welcome to Unscripted Coding! Today we're exploring Tav AI, a powerful API that bridges the gap between large language models (LLMs) and the internet. This connection allows your AI applications to search the web and access up-to-date information.



The Evolution of AI and Internet Access

To understand why this matters, let's look back at ChatGPT's initial release. While impressive, it had a significant limitation - its knowledge cutoff date (around 2022) meant it couldn't tell you about current events. It excelled at writing emails and stories but lacked real-time information.

Fast forward to ChatGPT Plus with its "Browse with Bing" feature, and suddenly the LLM could connect to the internet. Ask it for a fictional story, and it uses its internal knowledge. Ask about today's news, and it connects to Bing, fetches relevant articles, summarizes them, and delivers an answer.



This capability opens numerous doors:


- Building up-to-date news applications
- Researching current topics
- Accessing niche information
- Directing targeted searches


The Challenge with OpenAI's Assistants API

I was initially disappointed when exploring OpenAI's Assistants API. While it promises the latest ChatGPT features for developers, its tools are limited to:


- Code interpreter
- Knowledge retrieval (for local documents)
- Function calling

Notably absent was something like "Browse with Bing" - a flexible way to search the internet without building your own backend.

Enter Tavily AI

After searching through various forums and finding other disappointed users, I discovered a useful thread on OpenAI featuring code from a user named "insinxn." This solution uses Tavily AI to enable internet searches through the Assistants API.

 The Implementation

Let me walk you through the code that makes this work:


 Key Components:


1. **Required Libraries**: You'll need both the OpenAI and Tavily AI SDKs


2. **Basic Structure**: 

   - Create an assistant with specific function-calling capabilities
   - Set up a thread for ongoing conversation
   - Implement handlers for when the AI needs to search the web



 The Magic Behind It:

```python
# When the AI needs to search the web
if run.status == "requires_action":
    submit_tool_outputs(client, thread, run)
```

The function `submit_tool_outputs` handles connecting to Tavily AI, performing the search, and returning results to the assistant.



 Testing It Out

When asking general questions like "How are you?", the assistant responds immediately without needing internet access.

However, when I asked "What happened today in world news? Please summarize the biggest story you can find," it recognized it needed more information. The system:


1. Triggered the Tavily AI search function
2. Connected to the internet
3. Retrieved information about current world events
4. Processed and summarized the findings

The response included up-to-date information about ongoing global conflicts and recent developments.



 Expanding the Functionality

What makes this solution powerful is its simplicity and flexibility:

- You can modify the Tav client to look at more results or different types of searches
- The assistant's initial prompt can be customized for specific use cases
- You can build multiple steps into your workflow (find information, create a podcast script, prepare slides, etc.)



 Alternatives

It's worth noting that Tavily AI already offers a research assistant with similar functionality built-in. Additionally, you could use GPTs from OpenAI with "Browse with Bing" capabilities.

However, this custom solution offers advantages:


- Greater control over the workflow
- The ability to use different LLMs beyond just OpenAI
- A foundation you can build upon for more complex applications



 Conclusion

This elegant solution bridges a critical gap in the Assistants API, enabling internet searches without having to build extensive custom infrastructure. By combining OpenAI's powerful language models with Tav AI's internet connection capabilities, developers can create applications that stay current with real-world events.




Links:

Comments

Popular posts from this blog

Video From YouTube

GPT Researcher: Deploy POWERFUL Autonomous AI Agents

Building AI Ready Codebase Indexing With CocoIndex