Обновил структуру проекта

This commit is contained in:
Даниил Грабарь
2026-10-01 17:38:49 +10:00
parent 2e23b63689
commit 9e551a2ca9
5 changed files with 170 additions and 86 deletions
+4 -2
View File
@@ -10,12 +10,12 @@ from pydantic_ai.providers.openai_codex import (
class FileCodexCredentionalSource(OpenAICodexCredentialSource):
def __init__(self,
path: Path,
path: str| Path,
encoding: str="utf-8",
ensure_ascii: bool=False,
indent: int=2):
super().__init__()
self._path = path
self._path = Path(path)
self._encoding=encoding
self._ensure_ascii=ensure_ascii
self._indent=indent
@@ -32,6 +32,8 @@ class FileCodexCredentionalSource(OpenAICodexCredentialSource):
)
async def save(self, credentials: OpenAICodexCredentials) -> None:
self.path.parent.mkdir(parents=True , exist_ok=True)
self._path.write_text(
json.dumps(
asdict(credentials),
+68 -1
View File
@@ -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")
+21 -11
View File
@@ -1,4 +1,4 @@
from typing import Literal
from typing import Literal, Optional
from pydantic import BaseModel, Field
@@ -10,7 +10,7 @@ class RobotPlan(BaseModel):
)
class Message(BaseModel):
class ChatMessage(BaseModel):
role: Literal["system",
"user",
"assistant"]
@@ -18,22 +18,32 @@ class Message(BaseModel):
content: str
class SFTFormat(BaseModel):
message: list[Message]
class DatasetRecord(BaseModel):
task: str
class ChatRecord(BaseModel):
messages: list[ChatMessage]
class Prompts(BaseModel):
system_prompt: str
instructions: str
user_prompt: str
model_response: str
assistant_content: str
class ModelResponse(BaseModel):
model_response: str
assistant_content: Optional[str] = None
class ModelInfo(BaseModel):
provider: str
model: str
class GenerationSettings(BaseModel):
temperature: float
max_tokens: int
max_tokens: int
class BaseDatasetRecord(BaseModel):
prompts: Prompts
response: ModelResponse
model_info: ModelInfo
generation: GenerationSettings
class RobotDatasetRecord(BaseDatasetRecord):
task: str