Skip to content

Commit f50ecc2

Browse files
aponcedeleonchlukehinds
authored andcommitted
Add mocked token-usage endpoints (#780)
* Add mocked token-usage endpoints * Make conversations also use TokenUsage
1 parent 966d229 commit f50ecc2

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

src/codegate/api/v1.py

+22
Original file line numberDiff line numberDiff line change
@@ -470,3 +470,25 @@ def version_check():
470470
"is_latest": None,
471471
"error": "An unexpected error occurred",
472472
}
473+
474+
475+
@v1.get(
476+
"/workspaces/{workspace_name}/token-usage",
477+
tags=["Workspaces", "Token Usage"],
478+
generate_unique_id_function=uniq_name,
479+
)
480+
async def get_workspace_token_usage(workspace_name: str) -> v1_models.TokenUsage:
481+
"""Get the token usage of a workspace."""
482+
# TODO: This is a dummy implementation. In the future, we should have a proper
483+
# implementation that fetches the token usage from the database.
484+
return v1_models.TokenUsage(
485+
used_tokens=50,
486+
tokens_by_model=[
487+
v1_models.TokenUsageByModel(
488+
provider_type="openai", model="gpt-4o-mini", used_tokens=20
489+
),
490+
v1_models.TokenUsageByModel(
491+
provider_type="anthropic", model="claude-3-5-sonnet-20241022", used_tokens=30
492+
),
493+
],
494+
)

src/codegate/api/v1_models.py

+31-10
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,36 @@ class PartialQuestionAnswer(pydantic.BaseModel):
114114
answer: Optional[ChatMessage]
115115

116116

117+
class ProviderType(str, Enum):
118+
"""
119+
Represents the different types of providers we support.
120+
"""
121+
122+
openai = "openai"
123+
anthropic = "anthropic"
124+
vllm = "vllm"
125+
126+
127+
class TokenUsageByModel(pydantic.BaseModel):
128+
"""
129+
Represents the tokens used by a model.
130+
"""
131+
132+
provider_type: ProviderType
133+
model: str
134+
used_tokens: int
135+
136+
137+
class TokenUsage(pydantic.BaseModel):
138+
"""
139+
Represents the tokens used. Includes the information of the tokens used by model.
140+
`used_tokens` are the total tokens used in the `tokens_by_model` list.
141+
"""
142+
143+
tokens_by_model: List[TokenUsageByModel]
144+
used_tokens: int
145+
146+
117147
class Conversation(pydantic.BaseModel):
118148
"""
119149
Represents a conversation.
@@ -124,6 +154,7 @@ class Conversation(pydantic.BaseModel):
124154
type: QuestionType
125155
chat_id: str
126156
conversation_timestamp: datetime.datetime
157+
token_usage: Optional[TokenUsage]
127158

128159

129160
class AlertConversation(pydantic.BaseModel):
@@ -140,16 +171,6 @@ class AlertConversation(pydantic.BaseModel):
140171
timestamp: datetime.datetime
141172

142173

143-
class ProviderType(str, Enum):
144-
"""
145-
Represents the different types of providers we support.
146-
"""
147-
148-
openai = "openai"
149-
anthropic = "anthropic"
150-
vllm = "vllm"
151-
152-
153174
class ProviderAuthType(str, Enum):
154175
"""
155176
Represents the different types of auth we support for providers.

src/codegate/api/v1_processing.py

+1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ async def match_conversations(
331331
type=first_partial_qa.partial_questions.type,
332332
chat_id=first_partial_qa.partial_questions.message_id,
333333
conversation_timestamp=first_partial_qa.partial_questions.timestamp,
334+
token_usage=None,
334335
)
335336
for qa in questions_answers:
336337
map_q_id_to_conversation[qa.question.message_id] = conversation

0 commit comments

Comments
 (0)