Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement rate limiting for Tavily API requests #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion maestro-lmstudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import json
from tavily import TavilyClient
from openai import OpenAI
import time
from collections import deque

# Set up the LM Studio API client
client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")
Expand Down Expand Up @@ -68,6 +70,12 @@ def opus_orchestrator(objective, file_content=None, previous_results=None, use_s
return response_text, file_content, search_query


# Initialize the Tavily request rate limiting parameters (see https://docs.tavily.com/docs/tavily-api/rest_api#rate-limiting).
TAVILY_REQUEST_LIMIT = 20
TAVILY_REQUEST_WINDOW = 60 # In seconds.
tavily_request_timestamps = deque()


def haiku_sub_agent(prompt, search_query=None, previous_haiku_tasks=None, use_search=False, continuation=False):
if previous_haiku_tasks is None:
previous_haiku_tasks = []
Expand All @@ -79,12 +87,24 @@ def haiku_sub_agent(prompt, search_query=None, previous_haiku_tasks=None, use_se

qna_response = None
if search_query and use_search:
# Rate limiting for Tavily requests.
current_time = time.time()
while tavily_request_timestamps and tavily_request_timestamps[0] < current_time - TAVILY_REQUEST_WINDOW:
tavily_request_timestamps.popleft()

if len(tavily_request_timestamps) >= TAVILY_REQUEST_LIMIT:
sleep_time = tavily_request_timestamps[0] + TAVILY_REQUEST_WINDOW - current_time
console.print(f"[bold yellow]Tavily rate limit exceeded. Waiting for {sleep_time:.2f} seconds.[/bold yellow]")
time.sleep(sleep_time)

# Initialize the Tavily client
tavily = TavilyClient(api_key="YOUR API KEY HERE")
# Perform a QnA search based on the search query
qna_response = tavily.qna_search(query=search_query)
console.print(f"QnA response: {qna_response}", style="yellow")

tavily_request_timestamps.append(time.time())

# Prepare the messages array with only the prompt initially
messages = [
{
Expand Down Expand Up @@ -281,4 +301,4 @@ def read_file(file_path):

with open(filename, 'w') as file:
file.write(exchange_log)
print(f"\nFull exchange log saved to {filename}")
print(f"\nFull exchange log saved to {filename}")
20 changes: 20 additions & 0 deletions maestro.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from datetime import datetime
import json
from tavily import TavilyClient
import time
from collections import deque

# Set up the Anthropic API client
client = Anthropic(api_key="YOUR API KEY")
Expand Down Expand Up @@ -85,6 +87,12 @@ def opus_orchestrator(objective, file_content=None, previous_results=None, use_s
return response_text, file_content, search_query


# Initialize the Tavily request rate limiting parameters (see https://docs.tavily.com/docs/tavily-api/rest_api#rate-limiting).
TAVILY_REQUEST_LIMIT = 20
TAVILY_REQUEST_WINDOW = 60 # In seconds.
tavily_request_timestamps = deque()


def haiku_sub_agent(prompt, search_query=None, previous_haiku_tasks=None, use_search=False, continuation=False):
if previous_haiku_tasks is None:
previous_haiku_tasks = []
Expand All @@ -96,12 +104,24 @@ def haiku_sub_agent(prompt, search_query=None, previous_haiku_tasks=None, use_se

qna_response = None
if search_query and use_search:
# Rate limiting for Tavily requests.
current_time = time.time()
while tavily_request_timestamps and tavily_request_timestamps[0] < current_time - TAVILY_REQUEST_WINDOW:
tavily_request_timestamps.popleft()

if len(tavily_request_timestamps) >= TAVILY_REQUEST_LIMIT:
sleep_time = tavily_request_timestamps[0] + TAVILY_REQUEST_WINDOW - current_time
console.print(f"[bold yellow]Tavily rate limit exceeded. Waiting for {sleep_time:.2f} seconds.[/bold yellow]")
time.sleep(sleep_time)

# Initialize the Tavily client
tavily = TavilyClient(api_key="YOUR API KEY HERE")
# Perform a QnA search based on the search query
qna_response = tavily.qna_search(query=search_query)
console.print(f"QnA response: {qna_response}", style="yellow")

tavily_request_timestamps.append(time.time())

# Prepare the messages array with only the prompt initially
messages = [
{
Expand Down