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

fix: throw error if quota exceeded #717

Merged
merged 4 commits into from
Feb 2, 2025
Merged
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
6 changes: 3 additions & 3 deletions .github/workflows/pr-tests.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PR Tests

on:
pull_request_target:
pull_request:
branches: [ "main" ]
paths:
- .github/workflows/**
Expand All @@ -12,7 +12,7 @@ on:
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
pull-requests: write
pull-requests: write
actions: write

env:
Expand Down Expand Up @@ -54,7 +54,7 @@ jobs:
pip install -r requirements.txt
pip install ruff
pip install pytest pytest-cov

- name: Lint with Ruff
run: |
ruff check --output-format=github .
Expand Down
26 changes: 15 additions & 11 deletions server/agent/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from typing import AsyncGenerator, AsyncIterator, Dict, Callable, Optional
from langchain.agents import AgentExecutor
from openai import APIError
from agent.llm import BaseLLMClient
from petercat_utils.data_class import ChatData, Message
from langchain.agents.format_scratchpad.openai_tools import (
Expand Down Expand Up @@ -152,15 +153,16 @@
"content": content,
}
elif kind == "on_chat_model_end":
content = event["data"]["output"]["generations"][0][0][
"message"
].usage_metadata
if content:
yield {
"id": event["run_id"],
"type": "usage",
**content,
}
output = event.get("data", {}).get("output", {})
generations = output.get("generations", [])
if generations and len(generations) > 0:
content = generations[0][0].get("message", {}).get("usage_metadata")
if content:
yield {

Check warning on line 161 in server/agent/base.py

View check run for this annotation

Codecov / codecov/patch

server/agent/base.py#L156-L161

Added lines #L156 - L161 were not covered by tests
"id": event["run_id"],
"type": "usage",
**content,
}
elif kind == "on_tool_start":
children_value = event["data"].get("input", {})
yield {
Expand Down Expand Up @@ -213,8 +215,10 @@
}

except Exception as e:
res = {"status": "error", "message": str(e)}
yield res
if isinstance(e, APIError):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that APIError is correctly imported and used to handle specific API-related exceptions. This change improves error specificity and clarity in error messages.

yield { "status": "error", "message": e.body }

Check warning on line 219 in server/agent/base.py

View check run for this annotation

Codecov / codecov/patch

server/agent/base.py#L218-L219

Added lines #L218 - L219 were not covered by tests
else:
yield { "status": "error", "message": str(e) }

Check warning on line 221 in server/agent/base.py

View check run for this annotation

Codecov / codecov/patch

server/agent/base.py#L221

Added line #L221 was not covered by tests

async def run_chat(self, input_data: ChatData) -> str:
try:
Expand Down
1 change: 0 additions & 1 deletion server/chat/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def run_qa_chat(
user: Annotated[User | None, Depends(get_user)] = None,
bot: Annotated[Bot | None, Depends(get_bot)] = None,
):
print(f"run_qa_chat: input_data={input_data}, bot={bot}, llm={bot.llm}")

auth_token = (
Auth.Token(user.access_token) if getattr(user, "access_token", None) else None
Expand Down
2 changes: 2 additions & 0 deletions server/core/service/user_token_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
user_token_usage_dao.create(token_usage)
except Exception as e:
print(f"An error occurred: {e}")
case "error":
yield value

Check warning on line 60 in server/core/service/user_token_usage.py

View check run for this annotation

Codecov / codecov/patch

server/core/service/user_token_usage.py#L59-L60

Added lines #L59 - L60 were not covered by tests
case _:
yield value
except Exception as e:
Expand Down
1 change: 0 additions & 1 deletion server/insight/service/activity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import datetime
import requests

Check failure on line 1 in server/insight/service/activity.py

View workflow job for this annotation

GitHub Actions / build

Ruff (F401)

insight/service/activity.py:1:8: F401 `datetime` imported but unused
from typing import List, Dict


Expand Down
5 changes: 2 additions & 3 deletions server/tests/insight/test_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ def test_get_activity_data_empty(self, mock_get):

@patch("insight.service.activity.requests.get")
def test_get_activity_data_invalid_json(self, mock_get):

mock_response = MagicMock()
mock_response.json.side_effect = ValueError("Invalid JSON")
mock_get.return_value = mock_response

repo_name = "petercat-ai/petercat"
with self.assertRaises(ValueError):
get_activity_data(repo_name)
result = get_activity_data(repo_name)
self.assertEqual(result, [])
2 changes: 0 additions & 2 deletions server/tests/utils/test_insight.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import unittest
from unittest.mock import patch, Mock
from collections import defaultdict

from utils.insight import get_data

Check failure on line 3 in server/tests/utils/test_insight.py

View workflow job for this annotation

GitHub Actions / build

Ruff (F401)

tests/utils/test_insight.py:3:25: F401 `collections.defaultdict` imported but unused


class TestGetData(unittest.TestCase):
Expand Down
Loading