Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand All @@ -37,4 +37,4 @@ jobs:
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
pytest || ( [ $? -eq 5 ] && exit 0 )
128 changes: 67 additions & 61 deletions modules/ai_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,61 @@ def __init__(self, api_key, models, base_url):
self.failed_models = {} # {model: failed_time}
self.cooldown_period = 60 # seconds before retrying failed model

def _make_request(self, model, system_prompt, user_prompt, temperature):
"""Execute the HTTP POST request to the API."""
return requests.post(
self.base_url,
headers={
'Authorization': f'Bearer {self.api_key}',
'Content-Type': 'application/json',
},
json={
'model': model,
'messages': [
{'role': 'system', 'content': system_prompt},
{'role': 'user', 'content': user_prompt}
],
'temperature': temperature
},
timeout=30
)

def _handle_response_errors(self, response, model, consecutive_failures, max_consecutive_failures):
"""
Handle specific HTTP status codes and raise exceptions for others.
Returns a tuple: (should_continue, should_return_none, new_consecutive_failures)
"""
if response.status_code == 429:
logger.warning(f"Rate limit on {model}, rotating...")
self._mark_failed(model)
return True, False, consecutive_failures + 1

if response.status_code == 401:
logger.error(f"401 Unauthorized on {model} - check API key!")
consecutive_failures += 1
if consecutive_failures >= max_consecutive_failures:
logger.error("Multiple consecutive 401 errors - invalid API key!")
return False, True, consecutive_failures
return True, False, consecutive_failures

response.raise_for_status()
return False, False, consecutive_failures

def _parse_response(self, response):
"""Parse JSON response from the API, handling markdown code blocks if present."""
result = response.json()
content = result['choices'][0]['message']['content'].strip()

# Extract JSON from markdown if needed
if content.startswith('```'):
content = content.split('```')[1]
if content.startswith('json'):
content = content[4:]
content = content.strip()

# Parse JSON
return json.loads(content)

def call(self, system_prompt, user_prompt, temperature=0.3, max_retries=3):
"""
Call AI with automatic model rotation
Expand Down Expand Up @@ -65,74 +120,25 @@ def call(self, system_prompt, user_prompt, temperature=0.3, max_retries=3):
try:
logger.debug(f"AI call with model: {model} (attempt {attempt + 1}/{max_total_attempts})")

response = requests.post(
self.base_url,
headers={
'Authorization': f'Bearer {self.api_key}',
'Content-Type': 'application/json',
},
json={
'model': model,
'messages': [
{'role': 'system', 'content': system_prompt},
{'role': 'user', 'content': user_prompt}
],
'temperature': temperature
},
timeout=30
)

# Check for rate limit
if response.status_code == 429:
logger.warning(f"Rate limit on {model}, rotating...")
self._mark_failed(model)
consecutive_failures += 1
continue
response = self._make_request(model, system_prompt, user_prompt, temperature)

# Check for auth errors - don't retry if all models failing
if response.status_code == 401:
logger.error(f"401 Unauthorized on {model} - check API key!")
consecutive_failures += 1
# If we get 5 consecutive 401s, API key is definitely bad
if consecutive_failures >= max_consecutive_failures:
logger.error("Multiple consecutive 401 errors - invalid API key!")
return None
# Handle status codes and errors
should_continue, should_return_none, consecutive_failures = self._handle_response_errors(
response, model, consecutive_failures, max_consecutive_failures
)
if should_return_none:
return None
if should_continue:
continue

response.raise_for_status()

# Parse response
result = response.json()
content = result['choices'][0]['message']['content'].strip()

# Extract JSON from markdown if needed
if content.startswith('```'):
content = content.split('```')[1]
if content.startswith('json'):
content = content[4:]
content = content.strip()

# Parse JSON
data = json.loads(content)

data = self._parse_response(response)
logger.debug(f"AI call successful with {model}")
return data

except requests.exceptions.HTTPError as e:
if e.response.status_code == 429:
logger.warning(f"Rate limit on {model}")
self._mark_failed(model)
consecutive_failures += 1
elif e.response.status_code == 401:
logger.error(f"401 Unauthorized on {model}")
consecutive_failures += 1
if consecutive_failures >= max_consecutive_failures:
logger.error("API key invalid - stopping retries!")
return None
else:
logger.error(f"HTTP error on {model}: {e}")
self._mark_failed(model)
consecutive_failures += 1
logger.error(f"HTTP error on {model}: {e}")
self._mark_failed(model)
consecutive_failures += 1
continue

except json.JSONDecodeError as e:
Expand Down
5 changes: 5 additions & 0 deletions worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
async fetch(request, env, ctx) {
return new Response("LeadScraper is a Python CLI tool, not a Worker API.");
},
};
6 changes: 6 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name = "leadscraper"
compatibility_date = "2024-01-01"
main = "worker.js"

[build]
command = "echo 'No build required'"
Loading