-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathai_agent.py
65 lines (55 loc) · 2.26 KB
/
ai_agent.py
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
import requests
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
API_KEY = os.getenv("GEMINI_API_KEY")
def ask_gemini(prompt: str, model="gemini-1.5-flash"):
"""Queries the Gemini AI API and returns the response."""
gemini_api_url = f"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?key={API_KEY}"
headers = {"Content-Type": "application/json"}
data = {"contents": [{"parts": [{"text": prompt}]}]}
response = requests.post(gemini_api_url, json=data, headers=headers)
if response.status_code == 200:
parsed_response = response.json()
try:
return parsed_response["candidates"][0]["content"]["parts"][0]["text"]
except (KeyError, IndexError):
return "Error: AI response structure changed or missing."
else:
return f"Error: {response.status_code}, Message: {response.text}"
# AI Agent Base Class
class AI_Agent:
def __init__(self, name, role, tools, backstory):
self.name = name
self.role = role
self.tools = tools
self.backstory = backstory
def respond(self, query: str):
prompt = f"{self.backstory}\nUser: {query}\n{self.name}:"
return ask_gemini(prompt)
# Define Specialized AI Agents
class MedicalAgent(AI_Agent):
def __init__(self):
super().__init__(
"Medical Assistant",
"Assist astronauts with medical issues",
["First Aid Guide", "Vitals Monitor"],
"You are a trained medical assistant designed to help astronauts with medical emergencies in space."
)
class TechSupportAgent(AI_Agent):
def __init__(self):
super().__init__(
"Tech Support",
"Assist astronauts with technical issues",
["System Diagnostics", "Repair Manuals"],
"You are a tech support specialist designed to help astronauts with technical problems in space."
)
class NavigationAgent(AI_Agent):
def __init__(self):
super().__init__(
"Navigation Assistant",
"Assist astronauts with navigation",
["Star Charts", "Spacecraft Sensors"],
"You are a navigation assistant designed to help astronauts with navigation in space."
)