-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·54 lines (40 loc) · 1.63 KB
/
Copy pathmain.py
File metadata and controls
executable file
·54 lines (40 loc) · 1.63 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
#!/usr/bin/env python3
import google.generativeai as genai
import os
import time
# ANSI цвета
RESET = "\033[0m"
GREEN = "\033[92m"
CYAN = "\033[96m"
RED = "\033[91m"
CONFIG_PATH = os.path.expanduser("~/.cli-gemini/api.conf")
if not os.path.exists(CONFIG_PATH):
print(f"{RED}[Ошибка]{RESET} API ключ не найден.")
print(f"Создай файл {CYAN}{CONFIG_PATH}{RESET} и вставь туда свой ключ (одной строкой).")
print("Как получить ключ: https://aistudio.google.com/app/apikey")
exit(1)
with open(CONFIG_PATH, "r") as f:
api_key = f.read().strip()
if not api_key.startswith("AIza"):
print(f"{RED}[Ошибка]{RESET} В конфиге неправильный ключ!")
exit(1)
genai.configure(api_key=api_key)
model = genai.GenerativeModel("gemini-2.5-flash")
chat_history = [
{"role": "user", "parts": ["Ты — дружелюбный собеседник. Отвечай человечно, кратко и понятно."]}
]
print(f"{GREEN}Gemini started! type 'exit' to stop Gemini.{RESET}")
while True:
try:
user_input = input(f"{CYAN}You > {RESET}")
if user_input.lower() in ["exit", "quit"]:
print(f"{RED}Exiting...{RESET}")
break
chat_history.append({"role": "user", "parts": [user_input]})
response = model.generate_content(chat_history)
reply = response.text
print(f"{GREEN}Gemini > {reply}{RESET}")
chat_history.append({"role": "model", "parts": [reply]})
except Exception as e:
print(f"{RED}Ошибка:{RESET}", e)
time.sleep(2)