Certainly! If you're looking to implement the Tavily Search API while avoiding OpenAI's API (due to usage limits or costs), you can leverage free and open-source Language Learning Models (LLMs) supported by Langchain. Below, I'll guide you through setting up your environment using a free LLM, such as Hugging Face's GPT-J or GPT-Neo, which can be accessed via Hugging Face's API.
Step 3: Setting Up API Keys with a Free LLM
1. Choose a Free LLM Provider
For this guide, we'll use Hugging Face's inference API, which offers free access to various open-source models like GPT-J and GPT-Neo. You'll need to create an account and obtain an API key.
2. Set Up Environment Variables
You have two options to set up your API keys: exporting them directly in your shell or storing them in a .env file.
Option A: Exporting Directly (Temporary Setup)
For Linux/macOS or Windows (using Git Bash or similar):
# Replace with your actual API keys
export HUGGINGFACE_API_KEY="your_huggingface_api_key_here"
export TAVILY_API_KEY="your_tavily_api_key_here"
For Windows Command Prompt:
set HUGGINGFACE_API_KEY=your_huggingface_api_key_here
set TAVILY_API_KEY=your_tavily_api_key_here
Option B: Using a .env File (Permanent Setup)
Create a .env file in your gpt-researcher directory:
cd path/to/gpt-researcher
touch .env
Open the .env file with your preferred text editor and add the following lines:
HUGGINGFACE_API_KEY=your_huggingface_api_key_here
TAVILY_API_KEY=your_tavily_api_key_here
Ensure that your .env file is added to .gitignore to prevent accidental commits of sensitive information:
echo ".env" >> .gitignore
3. Configure config.py to Use the Free LLM
Modify your config/config.py to use Hugging Face's LLM instead of OpenAI's. Here's how you can adjust the configuration:
# config/config.py
from dotenv import load_dotenv
import os
load_dotenv() # Load environment variables from .env
# Language Model Configuration
LLM_PROVIDER = "huggingface" # Change from 'openai' to 'huggingface'
LLM_MODEL = "gpt-j-6B" # Choose your preferred model: 'gpt-j-6B', 'gpt-neo-2.7B', etc.
HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
# Search Engine Configuration
SEARCH_PROVIDER = "tavily" # Can be 'tavily', 'duckduckgo', 'googleAPI', etc.
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
# Add other search provider API keys as needed
4. Update Your Langchain Adapter Configuration
Ensure that your Langchain Adapter is set up to use Hugging Face's API. Here's an example using Langchain with Hugging Face:
# adapters/langchain_adapter.py
from langchain.llms import HuggingFaceHub
import os
def get_llm():
hf_api_key = os.getenv("HUGGINGFACE_API_KEY")
model_name = os.getenv("LLM_MODEL", "gpt-j-6B")
llm = HuggingFaceHub(
repo_id=model_name,
huggingfacehub_api_token=hf_api_key
)
return llm
5. Install Necessary Dependencies
Ensure you have all required packages installed. You might need to install python-dotenv for loading .env files and langchain if you haven't already.
pip install python-dotenv langchain
6. Final Bash Script for Setup
Here’s a comprehensive Bash script that automates the setup process:
#!/bin/bash
# Step 1: Prompt user for API keys
read -p "Enter your Hugging Face API Key: " HUGGINGFACE_API_KEY
read -p "Enter your Tavily API Key: " TAVILY_API_KEY
# Step 2: Choose setup method
echo "Choose setup method:"
echo "1) Export API keys directly (temporary)"
echo "2) Store API keys in a .env file (permanent)"
read -p "Enter choice [1 or 2]: " CHOICE
if [ "$CHOICE" -eq 1 ]; then
# Exporting directly
export HUGGINGFACE_API_KEY="$HUGGINGFACE_API_KEY"
export TAVILY_API_KEY="$TAVILY_API_KEY"
echo "API keys exported successfully."
elif [ "$CHOICE" -eq 2 ]; then
# Storing in .env file
ENV_PATH="./gpt-researcher/.env"
mkdir -p ./gpt-researcher
echo "HUGGINGFACE_API_KEY=${HUGGINGFACE_API_KEY}" > $ENV_PATH
echo "TAVILY_API_KEY=${TAVILY_API_KEY}" >> $ENV_PATH
echo ".env file created at $ENV_PATH"
echo "Ensure to add .env to your .gitignore to keep your API keys secure."
else
echo "Invalid choice. Please run the script again and select either 1 or 2."
exit 1
fi
# Step 3: Configure config.py
CONFIG_PATH="./gpt-researcher/config/config.py"
mkdir -p ./gpt-researcher/config
cat > $CONFIG_PATH <<EOL
from dotenv import load_dotenv
import os
load_dotenv() # Load environment variables from .env if available
# Language Model Configuration
LLM_PROVIDER = "huggingface" # Using Hugging Face instead of OpenAI
LLM_MODEL = "gpt-j-6B" # You can change this to any supported Hugging Face model
HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
# Search Engine Configuration
SEARCH_PROVIDER = "tavily" # You can change to other providers if needed
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
EOL
echo "config.py has been set up at $CONFIG_PATH."
# Step 4: Inform the user
echo "Setup completed successfully."
echo "Don't forget to install necessary Python packages if you haven't:"
echo "pip install python-dotenv langchain"
Usage:
-
Save the script to a file, e.g., setup.sh.
-
Make it executable:
chmod +x setup.sh
-
Run the script:
./setup.sh
7. Run Your Application
After completing the setup, you can run your application, and it should utilize the Hugging Face LLM alongside the Tavily Search API.
python main.py # Replace with your application's entry point
Additional Considerations
-
Rate Limits: While Hugging Face offers free tiers, be mindful of their rate limits and usage policies. For extensive usage, consider subscribing to a paid plan or deploying the model locally.
-
Local Deployment: If you prefer full control and unlimited usage, consider running the LLM locally. This requires significant computational resources, especially for larger models like GPT-J. Tools like Transformers by Hugging Face can help set this up.
-
Alternative Free LLMs: Explore other free LLM providers or models that fit your project's requirements. Some popular options include:
- ** Cohere **: Offers free tiers for their API.
- ** Aleph Alpha **: Provides access to various models with free limited usage.
- ** Open-Source Models**: Numerous community-driven models available on Hugging Face.
-
Security: Always keep your API keys secure. Avoid hardcoding them in your scripts or committing them to version control systems. Use environment variables or secure secrets management services.
Summary
By following the steps above, you can effectively set up the Tavily Search API with a free, open-source LLM like Hugging Face's GPT-J or GPT-Neo. This approach helps you bypass the limitations and costs associated with OpenAI's API while leveraging powerful language models for your application.
If you encounter any issues or need further customization, feel free to ask!