gabykim commited on
Commit
ea31570
·
1 Parent(s): 8d8675f

[BugFix] json encoder for datetime

Browse files
src/know_lang_bot/evaluation/chatbot_evaluation.py CHANGED
@@ -3,11 +3,13 @@ from enum import Enum
3
  from pydantic import BaseModel, Field, computed_field
4
  from pydantic_ai import Agent
5
  from know_lang_bot.config import AppConfig
 
6
  from know_lang_bot.utils.model_provider import create_pydantic_model
7
  from know_lang_bot.chat_bot.chat_graph import ChatResult, process_chat
8
  import asyncio
9
  import datetime
10
  from pathlib import Path
 
11
 
12
  class EvalMetric(str, Enum):
13
  CHUNK_RELEVANCE = "chunk_relevance"
@@ -141,6 +143,10 @@ Format your response as JSON:
141
  )
142
 
143
  for round_id in range(num_rounds):
 
 
 
 
144
  # Get evaluation from the model
145
  result = await self.eval_agent.run(
146
  eval_context.model_dump_json(),
@@ -372,11 +378,15 @@ TRANSFORMER_TEST_CASES : List[EvalCase] = [
372
  *TRANSFORMER_TRAINER_TEST_CASES,
373
  ]
374
 
 
 
 
 
 
375
 
376
  async def main():
377
  from rich.console import Console
378
  from rich.pretty import Pretty
379
- import json
380
  import chromadb
381
  console = Console()
382
  config = AppConfig()
@@ -388,7 +398,7 @@ async def main():
388
  for case in TRANSFORMER_TEST_CASES:
389
  try:
390
  chat_result : ChatResult = await process_chat(question=case.question, collection=collection, config=config)
391
- result : EvalResult = await evaluator.evaluate_single(case, chat_result)
392
 
393
  eval_summary = EvalSummary(
394
  **chat_result.model_dump(),
@@ -397,18 +407,17 @@ async def main():
397
  summary_list.append(eval_summary)
398
 
399
  import time
400
- time.sleep(3) # Sleep for 5 seconds to avoid rate limiting
401
 
402
  except Exception:
403
  console.print_exception()
404
 
405
  # Write the final JSON array to a file
406
-
407
  current_date = datetime.datetime.now().strftime("%Y%m%d")
408
  file_name = Path("evaluations", f"transformers_{config.evaluator.model_provider}_evaluation_results_{current_date}.json")
409
  with open(file_name, "w") as f:
410
  json_list = [summary.model_dump() for summary in summary_list]
411
- json.dump(json_list, f, indent=2)
412
 
413
 
414
  console.print(Pretty(summary_list))
 
3
  from pydantic import BaseModel, Field, computed_field
4
  from pydantic_ai import Agent
5
  from know_lang_bot.config import AppConfig
6
+ from know_lang_bot.utils.chunking_util import truncate_chunk
7
  from know_lang_bot.utils.model_provider import create_pydantic_model
8
  from know_lang_bot.chat_bot.chat_graph import ChatResult, process_chat
9
  import asyncio
10
  import datetime
11
  from pathlib import Path
12
+ import json
13
 
14
  class EvalMetric(str, Enum):
15
  CHUNK_RELEVANCE = "chunk_relevance"
 
143
  )
144
 
145
  for round_id in range(num_rounds):
146
+ # truncate chunks to avoid long text
147
+ for chunk in eval_context.retrieved_context.chunks:
148
+ chunk = truncate_chunk(chunk)
149
+
150
  # Get evaluation from the model
151
  result = await self.eval_agent.run(
152
  eval_context.model_dump_json(),
 
378
  *TRANSFORMER_TRAINER_TEST_CASES,
379
  ]
380
 
381
+ class DateTimeEncoder(json.JSONEncoder):
382
+ def default(self, obj):
383
+ if isinstance(obj, datetime.datetime):
384
+ return obj.isoformat()
385
+ return super().default(obj)
386
 
387
  async def main():
388
  from rich.console import Console
389
  from rich.pretty import Pretty
 
390
  import chromadb
391
  console = Console()
392
  config = AppConfig()
 
398
  for case in TRANSFORMER_TEST_CASES:
399
  try:
400
  chat_result : ChatResult = await process_chat(question=case.question, collection=collection, config=config)
401
+ result : EvalResult = await evaluator.evaluate_single(case, chat_result, config.evaluator.evaluation_rounds)
402
 
403
  eval_summary = EvalSummary(
404
  **chat_result.model_dump(),
 
407
  summary_list.append(eval_summary)
408
 
409
  import time
410
+ time.sleep(3) # Sleep to avoid rate limiting
411
 
412
  except Exception:
413
  console.print_exception()
414
 
415
  # Write the final JSON array to a file
 
416
  current_date = datetime.datetime.now().strftime("%Y%m%d")
417
  file_name = Path("evaluations", f"transformers_{config.evaluator.model_provider}_evaluation_results_{current_date}.json")
418
  with open(file_name, "w") as f:
419
  json_list = [summary.model_dump() for summary in summary_list]
420
+ json.dump(json_list, f, indent=2, cls=DateTimeEncoder)
421
 
422
 
423
  console.print(Pretty(summary_list))