Elffuss BraidHub

kikocis/GraphRAG-SDK public

main · 3.8 KiBRawPermalink
1import os
2from .model import (
3 GenerativeModel,
4 GenerativeModelConfig,
5 GenerationResponse,
6 FinishReason,
7 GenerativeModelChatSession,
8)
9
10from google.generativeai import (
11 GenerativeModel as GoogleGenerativeModel,
12 GenerationConfig as GoogleGenerationConfig,
13 configure,
14 protos,
15 types,)
16
17
18class GeminiGenerativeModel(GenerativeModel):
19
20 _model: GoogleGenerativeModel = None
21
22 def __init__(
23 self,
24 model_name: str,
25 generation_config: GoogleGenerationConfig | None = None,
26 system_instruction: str | None = None,
27 ):
28 self._model_name = model_name
29 self._generation_config = generation_config
30 self._system_instruction = system_instruction
31 configure(api_key=os.environ["GOOGLE_API_KEY"])
32
33
34 def _get_model(self) -> GoogleGenerativeModel:
35 if self._model is None:
36 self._model = GoogleGenerativeModel(
37 self._model_name,
38 generation_config=(
39 GoogleGenerationConfig(
40 temperature=self._generation_config.temperature,
41 top_p=self._generation_config.top_p,
42 top_k=self._generation_config.top_k,
43 max_output_tokens=self._generation_config.max_output_tokens,
44 stop_sequences=self._generation_config.stop_sequences,
45 )
46 if self._generation_config is not None
47 else None
48 ),
49 system_instruction=self._system_instruction,
50 )
51
52 return self._model
53
54 def with_system_instruction(self, system_instruction: str) -> "GenerativeModel":
55 self._system_instruction = system_instruction
56 self._model = None
57 self._get_model()
58
59 return self
60
61 def start_chat(self, args: dict | None = None) -> GenerativeModelChatSession:
62 return GeminiChatSession(self, args)
63
64 def ask(self, message: str) -> GenerationResponse:
65 response = self._model.generate_content(message)
66 return self.parse_generate_content_response(response)
67
68 def parse_generate_content_response(
69 self, response: types.generation_types.GenerateContentResponse
70 ) -> GenerationResponse:
71 return GenerationResponse(
72 text=response.text,
73 finish_reason=(
74 FinishReason.MAX_TOKENS
75 if response.candidates[0].finish_reason
76 == protos.Candidate.FinishReason.MAX_TOKENS
77 else (
78 FinishReason.STOP
79 if response.candidates[0].finish_reason == protos.Candidate.FinishReason.STOP
80 else FinishReason.OTHER
81 )
82 ),
83 )
84
85 def to_json(self) -> dict:
86 return {
87 "model_name": self._model_name,
88 "generation_config": self._generation_config.to_json(),
89 "system_instruction": self._system_instruction,
90 }
91
92 @staticmethod
93 def from_json(json: dict) -> "GenerativeModel":
94 return GeminiGenerativeModel(
95 model_name=json["model_name"],
96 generation_config=GenerativeModelConfig.from_json(
97 json["generation_config"]
98 ),
99 system_instruction=json["system_instruction"],
100 )
101
102
103class GeminiChatSession(GenerativeModelChatSession):
104
105 def __init__(self, model: GeminiGenerativeModel, args: dict | None = None):
106 self._model = model
107 self._chat_session = self._model._model.start_chat(
108 history=args.get("history", []) if args is not None else [],
109 )
110
111 def send_message(self, message: str) -> GenerationResponse:
112 response = self._chat_session.send_message(message)
113 return self._model.parse_generate_content_response(response)