-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemini_handler.py
More file actions
132 lines (108 loc) · 3.46 KB
/
gemini_handler.py
File metadata and controls
132 lines (108 loc) · 3.46 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from google import genai
from google.genai import types
from config import GEMINI_CONFIG
import re
# --------------------
# Client (NEW SDK)
# --------------------
client = genai.Client(
api_key=GEMINI_CONFIG["api_key"]
)
# --------------------
# Tools (Google Search grounding)
# --------------------
grounding_tool = types.Tool(
google_search=types.GoogleSearch()
)
tools_config = types.GenerateContentConfig(
tools=[grounding_tool],
temperature=GEMINI_CONFIG["temperature"],
top_p=GEMINI_CONFIG["top_p"],
top_k=GEMINI_CONFIG["top_k"],
)
# --------------------
# Memory
# --------------------
my_initial_question = ""
my_initial_response = ""
# ====================
# MAIN RESPONSE
# ====================
def get_response(prompt: str) -> tuple[str, str]:
global my_initial_question, my_initial_response
if my_initial_question:
note = (
f"User previously asked: {my_initial_question}. "
f"You previously replied: {my_initial_response}. "
f"Only use this if relevant. "
)
else:
note = ""
system_instruction = (
GEMINI_CONFIG["system_instruction"]
+ GEMINI_CONFIG["ai_system_instruction"]
+ note
)
response = client.models.generate_content(
model=GEMINI_CONFIG["model"],
contents=prompt,
config=types.GenerateContentConfig(
system_instruction=system_instruction,
tools=[grounding_tool],
temperature=GEMINI_CONFIG["temperature"],
top_p=GEMINI_CONFIG["top_p"],
top_k=GEMINI_CONFIG["top_k"],
),
)
text = response.text or ""
bot_reply = text.strip().splitlines()
command = bot_reply[0] if bot_reply else ""
reply_text = ""
# ---- code block handling ----
if "```" in command:
print("Reformatting code blocks...")
code_blocks = re.findall(
r"```(.*?)```",
text.replace("```python", "```").replace("```tool_code", "```"),
re.DOTALL,
)
command = "; ".join(cb.strip() for cb in code_blocks)
reply_text = re.sub(
r"```.*?```",
"",
text.replace("```python", "```").replace("```tool_code", "```"),
flags=re.DOTALL,
).strip()
else:
reply_text = "\n".join(bot_reply[1:]).strip()
my_initial_question = prompt
my_initial_response = reply_text
print("🤖 Little Bot says:", reply_text)
print("Command:", command)
return command, reply_text
# ====================
# AI-INTERNAL CALL
# ====================
def give_get_response(note: str, prompt: str) -> tuple[str, str]:
print("Prompt:", prompt)
system_instruction = GEMINI_CONFIG["system_instruction"] + note
response = client.models.generate_content(
model=GEMINI_CONFIG["model"],
contents=prompt,
config=types.GenerateContentConfig(
system_instruction=system_instruction,
temperature=GEMINI_CONFIG["temperature"],
top_p=GEMINI_CONFIG["top_p"],
top_k=GEMINI_CONFIG["top_k"],
),
)
text = response.text or ""
bot_reply = text.strip().splitlines()
command = bot_reply[0] if bot_reply else ""
reply_text = "\n".join(bot_reply[1:]).strip()
if command.startswith("pass,"):
reply_text = command[5:] + "\n" + reply_text
command = "pass"
print("🤖 Little Bot says:", reply_text)
print("Command:", command)
return command, reply_text