choizhang commited on
Commit
f856fca
·
1 Parent(s): e136e97

feat(TokenTracker): Add context manager support to simplify token tracking

Browse files
examples/lightrag_gemini_track_token_demo.py CHANGED
@@ -115,38 +115,36 @@ def main():
115
  # Initialize RAG instance
116
  rag = asyncio.run(initialize_rag())
117
 
118
- # Reset tracker before processing queries
119
- token_tracker.reset()
120
-
121
  with open("./book.txt", "r", encoding="utf-8") as f:
122
  rag.insert(f.read())
123
 
124
- print(
125
- rag.query(
126
- "What are the top themes in this story?", param=QueryParam(mode="naive")
 
 
 
127
  )
128
- )
129
 
130
- print(
131
- rag.query(
132
- "What are the top themes in this story?", param=QueryParam(mode="local")
 
133
  )
134
- )
135
 
136
- print(
137
- rag.query(
138
- "What are the top themes in this story?", param=QueryParam(mode="global")
 
 
139
  )
140
- )
141
 
142
- print(
143
- rag.query(
144
- "What are the top themes in this story?", param=QueryParam(mode="hybrid")
 
 
145
  )
146
- )
147
-
148
- # Display final token usage after main query
149
- print("Token usage:", token_tracker.get_usage())
150
 
151
 
152
  if __name__ == "__main__":
 
115
  # Initialize RAG instance
116
  rag = asyncio.run(initialize_rag())
117
 
 
 
 
118
  with open("./book.txt", "r", encoding="utf-8") as f:
119
  rag.insert(f.read())
120
 
121
+ # Context Manager Method
122
+ with token_tracker:
123
+ print(
124
+ rag.query(
125
+ "What are the top themes in this story?", param=QueryParam(mode="naive")
126
+ )
127
  )
 
128
 
129
+ print(
130
+ rag.query(
131
+ "What are the top themes in this story?", param=QueryParam(mode="local")
132
+ )
133
  )
 
134
 
135
+ print(
136
+ rag.query(
137
+ "What are the top themes in this story?",
138
+ param=QueryParam(mode="global"),
139
+ )
140
  )
 
141
 
142
+ print(
143
+ rag.query(
144
+ "What are the top themes in this story?",
145
+ param=QueryParam(mode="hybrid"),
146
+ )
147
  )
 
 
 
 
148
 
149
 
150
  if __name__ == "__main__":
examples/lightrag_siliconcloud_track_token_demo.py CHANGED
@@ -44,14 +44,10 @@ async def embedding_func(texts: list[str]) -> np.ndarray:
44
 
45
  # function test
46
  async def test_funcs():
47
- # Reset tracker before processing queries
48
- token_tracker.reset()
49
-
50
- result = await llm_model_func("How are you?")
51
- print("llm_model_func: ", result)
52
-
53
- # Display final token usage after main query
54
- print("Token usage:", token_tracker.get_usage())
55
 
56
 
57
  asyncio.run(test_funcs())
 
44
 
45
  # function test
46
  async def test_funcs():
47
+ # Context Manager Method
48
+ with token_tracker:
49
+ result = await llm_model_func("How are you?")
50
+ print("llm_model_func: ", result)
 
 
 
 
51
 
52
 
53
  asyncio.run(test_funcs())
lightrag/utils.py CHANGED
@@ -962,6 +962,13 @@ class TokenTracker:
962
  def __init__(self):
963
  self.reset()
964
 
 
 
 
 
 
 
 
965
  def reset(self):
966
  self.prompt_tokens = 0
967
  self.completion_tokens = 0
 
962
  def __init__(self):
963
  self.reset()
964
 
965
+ def __enter__(self):
966
+ self.reset()
967
+ return self
968
+
969
+ def __exit__(self, exc_type, exc_val, exc_tb):
970
+ print(self)
971
+
972
  def reset(self):
973
  self.prompt_tokens = 0
974
  self.completion_tokens = 0