36 lines
849 B
Python
36 lines
849 B
Python
# gemini.py
|
|
import tomllib
|
|
from pathlib import Path
|
|
from google import genai
|
|
from google.genai import types
|
|
|
|
_client = None
|
|
_chat = None
|
|
|
|
def _load_key() -> str:
|
|
with open("credentials.toml", "rb") as f:
|
|
return tomllib.load(f)["gemini"]["api_key"]
|
|
|
|
def _ensure_client():
|
|
global _client
|
|
if _client is None:
|
|
_client = genai.Client(api_key=_load_key())
|
|
|
|
def _ensure_chat():
|
|
global _chat
|
|
if _chat is None:
|
|
_ensure_client()
|
|
_chat = _client.chats.create(model="gemini-2.0-flash")
|
|
|
|
def send(md_content: str, user_message: str) -> str:
|
|
global _chat
|
|
_ensure_chat()
|
|
full_message = f"<context>\n{md_content}\n</context>\n\n{user_message}"
|
|
response = _chat.send_message(full_message)
|
|
return response.text
|
|
|
|
def reset_session():
|
|
global _client, _chat
|
|
_client = None
|
|
_chat = None
|