Обновил структуру проекта
This commit is contained in:
+68
-1
@@ -1,5 +1,6 @@
|
||||
import os
|
||||
import webbrowser
|
||||
import pandas as pd
|
||||
from pathlib import Path
|
||||
|
||||
from pydantic_ai.models.openai import OpenAIChatModel
|
||||
@@ -12,6 +13,7 @@ from pydantic_ai.providers.openai_codex import (
|
||||
|
||||
from .config import CodexConfig, LLMConfig, OpenAiConfig
|
||||
from .credentials import FileCodexCredentionalSource
|
||||
from .schemas import BaseDatasetRecord, ChatRecord, ChatMessage
|
||||
|
||||
|
||||
class LLMModelProvider:
|
||||
@@ -81,4 +83,69 @@ class LLMModelProvider:
|
||||
base_url=base_url
|
||||
)
|
||||
|
||||
raise ValueError(f"Unsupported LLM config: {type(config)}")
|
||||
raise ValueError(f"Unsupported LLM config: {type(config)}")
|
||||
|
||||
class DatasetWriter:
|
||||
def __init__(self,
|
||||
dataset_record: BaseDatasetRecord,
|
||||
base_path: str | Path):
|
||||
|
||||
|
||||
self._dataset_record = dataset_record
|
||||
self._path = Path(base_path)
|
||||
self._path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def __check_file_exists(self, filename :str)-> bool:
|
||||
file_path = self._path / filename
|
||||
return file_path.exists()
|
||||
|
||||
def to_csv(self,
|
||||
filename: str) -> None:
|
||||
|
||||
exists=self.__check_file_exists(filename)
|
||||
|
||||
df = pd.json_normalize(
|
||||
self._dataset_record.model_dump()
|
||||
)
|
||||
|
||||
df.to_csv(self._path / filename,
|
||||
mode="a" if exists else "w",
|
||||
header=not exists,
|
||||
index=False,
|
||||
encoding="utf-8")
|
||||
|
||||
def to_chat_record(self) -> ChatRecord:
|
||||
response = self._dataset_record.response
|
||||
prompts = self._dataset_record.prompts
|
||||
|
||||
assistant_content = (
|
||||
response.assistant_content
|
||||
or response.model_response
|
||||
)
|
||||
|
||||
return ChatRecord(
|
||||
messages=[
|
||||
ChatMessage(
|
||||
role="system",
|
||||
content=prompts.system_prompt,
|
||||
),
|
||||
ChatMessage(
|
||||
role="user",
|
||||
content=prompts.user_prompt,
|
||||
),
|
||||
ChatMessage(
|
||||
role="assistant",
|
||||
content=assistant_content,
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
def to_jsonl(self,
|
||||
filename: str) -> None:
|
||||
|
||||
chat_record = self.to_chat_record()
|
||||
path = self._path/filename
|
||||
|
||||
with path.open("a", encoding='utf-8') as file:
|
||||
file.write(chat_record.model_dump_json())
|
||||
file.write("\n")
|
||||
Reference in New Issue
Block a user