Elffuss BraidHub

kikocis/GraphRAG-SDK public

main · 3.1 KiBRawPermalink
1from abc import ABC, abstractmethod
2
3
4class FinishReason:
5 MAX_TOKENS = "MAX_TOKENS"
6 STOP = "STOP"
7 OTHER = "OTHER"
8
9
10class GenerativeModelConfig:
11 """
12 Configuration for a generative model
13
14 Args:
15 temperature (float): The temperature to use for sampling.
16 top_p (float): The top-p value to use for sampling.
17 top_k (int): The top-k value to use for sampling.
18 max_output_tokens (int): The maximum number of tokens to generate.
19 stop_sequences (list[str]): The stop sequences to use for sampling.
20
21 Examples:
22
23 >>> config = GenerativeModelConfig(temperature=0.5, top_p=0.9, top_k=50, max_output_tokens=100, stop_sequences=[".", "?", "!"])
24 """
25
26 def __init__(
27 self,
28 temperature: float | None = None,
29 top_p: float | None = None,
30 top_k: int | None = None,
31 max_output_tokens: int | None = None,
32 stop_sequences: list[str] | None = None,
33 ):
34 self.temperature = temperature
35 self.top_p = top_p
36 self.top_k = top_k
37 self.max_output_tokens = max_output_tokens
38 self.stop_sequences = stop_sequences
39
40 def __str__(self) -> str:
41 return f"GenerativeModelConfig(temperature={self.temperature}, top_p={self.top_p}, top_k={self.top_k}, max_output_tokens={self.max_output_tokens}, stop_sequences={self.stop_sequences})"
42
43 def to_json(self) -> dict:
44 return {
45 "temperature": self.temperature,
46 "top_p": self.top_p,
47 "top_k": self.top_k,
48 "max_output_tokens": self.max_output_tokens,
49 "stop_sequences": self.stop_sequences,
50 }
51
52 @staticmethod
53 def from_json(json: dict) -> "GenerativeModelConfig":
54 return GenerativeModelConfig(
55 temperature=json["temperature"],
56 top_p=json["top_p"],
57 top_k=json["top_k"],
58 max_output_tokens=json["max_output_tokens"],
59 stop_sequences=json["stop_sequences"],
60 )
61
62
63class GenerationResponse:
64
65 def __init__(self, text: str, finish_reason: FinishReason):
66 self.text = text
67 self.finish_reason = finish_reason
68
69 def __str__(self) -> str:
70 return (
71 f"GenerationResponse(text={self.text}, finish_reason={self.finish_reason})"
72 )
73
74
75class GenerativeModelChatSession(ABC):
76 """
77 A chat session with a generative model.
78 """
79
80 @abstractmethod
81 def __init__(self, model: "GenerativeModel"):
82 self.model = model
83
84 @abstractmethod
85 def send_message(self, message: str) -> GenerationResponse:
86 pass
87
88
89class GenerativeModel(ABC):
90 """
91 A generative model that can be used to generate text.
92 """
93
94 @abstractmethod
95 def with_system_instruction(self, system_instruction: str) -> "GenerativeModel":
96 pass
97
98 @abstractmethod
99 def start_chat(self, args: dict | None) -> GenerativeModelChatSession:
100 pass
101
102 @abstractmethod
103 def ask(self, message: str) -> GenerationResponse:
104 pass
105
106 @staticmethod
107 @abstractmethod
108 def from_json(json: dict) -> "GenerativeModel":
109 pass
110
111 @abstractmethod
112 def to_json(self) -> dict:
113 pass