-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
51 lines (39 loc) · 1.57 KB
/
Copy pathmodels.py
File metadata and controls
51 lines (39 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Shared data structures for Perplexify CLI and HTTP mode."""
from __future__ import annotations
from dataclasses import asdict, dataclass, field
from typing import Any
MODEL_CHOICES: tuple[str, ...] = ("sonar", "sonar-pro", "gpt", "claude")
MODE_CHOICES: tuple[str, ...] = ("ask", "search", "chat")
@dataclass
class PerplexifySource:
"""Normalized source shape for terminal, JSON, and HTTP output."""
title: str
url: str
snippet: str = ""
host: str = ""
@classmethod
def from_raw(cls, raw: dict[str, Any]) -> "PerplexifySource":
url = str(raw.get("url") or raw.get("link") or "").strip()
title = str(raw.get("name") or raw.get("title") or raw.get("host") or url).strip()
snippet = str(raw.get("snippet") or raw.get("description") or "").strip()
host = str(raw.get("host") or "").strip()
return cls(title=title or url, url=url, snippet=snippet, host=host)
def to_dict(self) -> dict[str, str]:
return asdict(self)
@dataclass
class PerplexifyResult:
"""Stable public response contract for Perplexify integrations."""
ok: bool
mode: str
query: str
answer: str = ""
sources: list[PerplexifySource] = field(default_factory=list)
model_requested: str | None = None
model_used: str | None = None
status: str = "unknown"
elapsed_ms: int = 0
error: str | None = None
def to_dict(self) -> dict[str, Any]:
data = asdict(self)
data["sources"] = [source.to_dict() for source in self.sources]
return data