Add registry module (local, remote, models)
This commit is contained in:
55
src/promptforge/registry/models.py
Normal file
55
src/promptforge/registry/models.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import uuid
|
||||||
|
from datetime import datetime
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
|
from ..core.prompt import Prompt
|
||||||
|
|
||||||
|
|
||||||
|
class RegistryEntry(BaseModel):
|
||||||
|
id: str = Field(default_factory=lambda: str(uuid.uuid4())[:8])
|
||||||
|
name: str
|
||||||
|
description: Optional[str] = None
|
||||||
|
content: str
|
||||||
|
version: str = "1.0.0"
|
||||||
|
author: Optional[str] = None
|
||||||
|
provider: Optional[str] = None
|
||||||
|
tags: List[str] = Field(default_factory=list)
|
||||||
|
created_at: datetime = Field(default_factory=datetime.utcnow)
|
||||||
|
updated_at: datetime = Field(default_factory=datetime.utcnow)
|
||||||
|
downloads: int = 0
|
||||||
|
rating: float = 0.0
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_prompt(cls, prompt: Prompt, author: Optional[str] = None) -> "RegistryEntry":
|
||||||
|
return cls(
|
||||||
|
name=prompt.name,
|
||||||
|
description=prompt.description,
|
||||||
|
content=prompt.content,
|
||||||
|
version=prompt.version,
|
||||||
|
author=author,
|
||||||
|
provider=prompt.provider,
|
||||||
|
tags=prompt.tags,
|
||||||
|
)
|
||||||
|
|
||||||
|
def to_file(self, registry_dir: Path) -> Path:
|
||||||
|
registry_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
filepath = registry_dir / f"{self.id}.yaml"
|
||||||
|
with open(filepath, 'w') as f:
|
||||||
|
f.write(self.model_dump_json(indent=2))
|
||||||
|
return filepath
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_file(cls, filepath: Path) -> "RegistryEntry":
|
||||||
|
import json
|
||||||
|
with open(filepath, 'r') as f:
|
||||||
|
data = json.load(f)
|
||||||
|
return cls(**data)
|
||||||
|
|
||||||
|
|
||||||
|
class SearchResult:
|
||||||
|
def __init__(self, entry: RegistryEntry, relevance_score: float = 1.0):
|
||||||
|
self.entry = entry
|
||||||
|
self.relevance_score = relevance_score
|
||||||
Reference in New Issue
Block a user