helloparthshah commited on
Commit
1c31898
·
1 Parent(s): eb1e30c

Tracking budget for manager too

Browse files
Files changed (2) hide show
  1. src/manager/manager.py +18 -0
  2. src/models/models.json +8 -8
src/manager/manager.py CHANGED
@@ -47,6 +47,8 @@ class GeminiManager:
47
  def __init__(self, system_prompt_file="./src/models/system6.prompt",
48
  gemini_model="gemini-2.5-pro-exp-03-25",
49
  modes: List[Mode] = []):
 
 
50
  load_dotenv()
51
  self.budget_manager = BudgetManager()
52
 
@@ -110,6 +112,14 @@ class GeminiManager:
110
  jitter=None)
111
  def generate_response(self, messages):
112
  tools = self.toolsLoader.getTools()
 
 
 
 
 
 
 
 
113
  return self.client.models.generate_content_stream(
114
  model=self.model_name,
115
  contents=messages,
@@ -141,6 +151,7 @@ class GeminiManager:
141
  }
142
  }
143
  try:
 
144
  toolResponse = self.toolsLoader.runTool(
145
  function_call.name, function_call.args)
146
  except Exception as e:
@@ -187,6 +198,7 @@ class GeminiManager:
187
  response={"result": f"{function_call.name} with {function_call.args} doesn't follow the required format, please read the other tool implementations for reference." + str(e)})
188
  parts.append(tool_content)
189
  i += 1
 
190
  yield {
191
  "role": "tool",
192
  "content": repr(types.Content(
@@ -305,6 +317,8 @@ class GeminiManager:
305
  except Exception as e:
306
  pass
307
  yield from self.invoke_manager(messages)
 
 
308
 
309
  def invoke_manager(self, messages):
310
  chat_history = self.format_chat_history(messages)
@@ -342,6 +356,10 @@ class GeminiManager:
342
  "role": "assistant",
343
  "content": full_text,
344
  })
 
 
 
 
345
  if function_call_requests:
346
  messages = messages + function_call_requests
347
  yield messages
 
47
  def __init__(self, system_prompt_file="./src/models/system6.prompt",
48
  gemini_model="gemini-2.5-pro-exp-03-25",
49
  modes: List[Mode] = []):
50
+ self.input_tokens = 0
51
+ self.output_tokens = 0
52
  load_dotenv()
53
  self.budget_manager = BudgetManager()
54
 
 
112
  jitter=None)
113
  def generate_response(self, messages):
114
  tools = self.toolsLoader.getTools()
115
+ response = self.client.models.count_tokens(
116
+ model=self.model_name,
117
+ contents=messages,
118
+ )
119
+ self.budget_manager.add_to_expense_budget(
120
+ response.total_tokens * 0.10/1000000 # Assuming $0.10 per million tokens
121
+ )
122
+ self.input_tokens += response.total_tokens
123
  return self.client.models.generate_content_stream(
124
  model=self.model_name,
125
  contents=messages,
 
151
  }
152
  }
153
  try:
154
+ self.input_tokens += len(repr(function_call).split())
155
  toolResponse = self.toolsLoader.runTool(
156
  function_call.name, function_call.args)
157
  except Exception as e:
 
198
  response={"result": f"{function_call.name} with {function_call.args} doesn't follow the required format, please read the other tool implementations for reference." + str(e)})
199
  parts.append(tool_content)
200
  i += 1
201
+ self.output_tokens += len(repr(parts).split())
202
  yield {
203
  "role": "tool",
204
  "content": repr(types.Content(
 
317
  except Exception as e:
318
  pass
319
  yield from self.invoke_manager(messages)
320
+ print("Tokens used: Input: {}, Output: {}".format(
321
+ self.input_tokens, self.output_tokens))
322
 
323
  def invoke_manager(self, messages):
324
  chat_history = self.format_chat_history(messages)
 
356
  "role": "assistant",
357
  "content": full_text,
358
  })
359
+ self.output_tokens += len(full_text.split())
360
+ self.budget_manager.add_to_expense_budget(
361
+ len(full_text.split()) * 0.40/1000000 # Assuming $0.40 per million tokens
362
+ )
363
  if function_call_requests:
364
  messages = messages + function_call_requests
365
  yield messages
src/models/models.json CHANGED
@@ -1,12 +1,12 @@
1
  {
2
- "Poet": {
3
- "base_model": "gemini-1.5-flash",
4
- "description": "Agent that writes poems",
5
- "system_prompt": "You are an agent that writes poems. When asked to write a poem, you should write a poem on the topic provided.",
6
- "create_resource_cost": 0,
7
- "invoke_resource_cost": 0,
8
  "create_expense_cost": 0,
9
- "invoke_expense_cost": 0.075,
10
- "output_expense_cost": 0.3
11
  }
12
  }
 
1
  {
2
+ "KunalPaiResearcher": {
3
+ "base_model": "mistral",
4
+ "description": "Agent to research and summarize information about Kunal Pai",
5
+ "system_prompt": "You are an AI agent that specializes in researching individuals based on publicly available information and creating concise summaries of their background, education, work experience, and research interests.",
6
+ "create_resource_cost": 20,
7
+ "invoke_resource_cost": 100,
8
  "create_expense_cost": 0,
9
+ "invoke_expense_cost": 0,
10
+ "output_expense_cost": 0
11
  }
12
  }