11import os
2- from abc import ABC , abstractmethod
32from typing import TYPE_CHECKING , List , Optional
43
4+ from peewee import CharField , Model , SqliteDatabase
5+ from playhouse .sqlite_ext import JSONField
6+
57from eval_protocol .dataset_logger .dataset_logger import DatasetLogger
68from eval_protocol .dataset_logger .directory_utils import find_eval_protocol_dir
79
@@ -13,9 +15,36 @@ class SqliteDatasetLoggerAdapter(DatasetLogger):
1315 def __init__ (self , db_path : Optional [str ] = None ):
1416 eval_protocol_dir = find_eval_protocol_dir ()
1517 self .db_path = os .path .join (eval_protocol_dir , "logs.db" )
18+ db = SqliteDatabase (self .db_path )
19+
20+ class BaseModel (Model ):
21+ class Meta :
22+ database = db
23+
24+ class EvaluationRow (BaseModel ):
25+ row_id = CharField (unique = True )
26+ data = JSONField ()
27+
28+ self .EvaluationRow = EvaluationRow
29+
30+ db .connect ()
31+ db .create_tables ([EvaluationRow ])
1632
1733 def log (self , row : "EvaluationRow" ) -> None :
18- pass
34+ row_id = row .input_metadata .row_id
35+ data = row .model_dump (exclude_none = True , mode = "json" )
36+ # if row_id already exists, update the row
37+ if self .EvaluationRow .select ().where (self .EvaluationRow .row_id == row_id ).exists ():
38+ self .EvaluationRow .update (data = data ).where (self .EvaluationRow .row_id == row_id ).execute ()
39+ else :
40+ self .EvaluationRow .create (row_id = row_id , data = data )
1941
2042 def read (self , row_id : Optional [str ] = None ) -> List ["EvaluationRow" ]:
21- return []
43+ from eval_protocol .models import EvaluationRow
44+
45+ if row_id is None :
46+ query = self .EvaluationRow .select ().dicts ()
47+ else :
48+ query = self .EvaluationRow .select ().dicts ().where (self .EvaluationRow .row_id == row_id )
49+ results = list (query )
50+ return [EvaluationRow (** result ["data" ]) for result in results ]
0 commit comments