-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgaiservices.py
More file actions
63 lines (51 loc) · 2.1 KB
/
gaiservices.py
File metadata and controls
63 lines (51 loc) · 2.1 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
import json
import asyncio
import os
from typing import List, Dict
from dotenv import load_dotenv
from ai_prompt import SYSTEM_PROMPT_TERMINAL, get_system_prompt
from google import genai
from google.genai import types
import ai_cache
# ---- Mocked AI Service (for learning) ---- #
load_dotenv()
# Later we’ll load OPENAI_API_KEY and MODEL here
model = os.getenv("GOOGLE_API_KEY")
# self.api_key = os.getenv("OPENAI_API_KEY", "mocked-key")
system_prompt = "You are a AI assistant to help terminal"
client = genai.Client()
async def get_ai_response( sys_prompt: str,user_input: str):
"""
Includes FS snapshot in the system prompt so AI behaves consistently.
Checks cache first, then sends prompt to AI if needed.
"""
# 1. CHECK CACHE FIRST (Module 4)
cached = ai_cache.get_cached_response(user_input)
if cached:
print(f"[AIService] Cache HIT for: '{user_input}'")
print(f"[AIService] Cached Response: '{cached}'\n")
return cached
# 2. If not in cache, proceed with API call
try:
print(f"[AIService] Cache MISS for: '{user_input}'")
print(f"system_prompt: '{sys_prompt}'\n")
print(f"user_input: '{user_input}'\n")
# response = await self.client.responses.create(
# model="gpt-4.1-mini",
config = types.GenerateContentConfig(
system_instruction=sys_prompt,
)
response = client.models.generate_content(
model="gemini-2.5-pro",
contents= user_input,
config=config
)
answer = response.text
ai_cache.set_cached_response(user_input, answer)
print(f"[AIService] AI Response: '{answer}'\n")
if answer is None:
answer = ""
return answer
except Exception as e:
print(f"[AIService] Error: {e}")
return None # Return empty string on error so honeypot doesn't crash