Create agent/gemini_agent.py
Browse files- agent/gemini_agent.py +20 -0
agent/gemini_agent.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import google.generativeai as genai
|
3 |
+
|
4 |
+
def get_gemini_api_key():
|
5 |
+
return os.getenv("GEMINI_API_KEY")
|
6 |
+
|
7 |
+
def run_gemini_chat(prompt, context=None):
|
8 |
+
api_key = get_gemini_api_key()
|
9 |
+
if not api_key:
|
10 |
+
return "Error: Gemini API key not set. Please add it to your environment variables."
|
11 |
+
genai.configure(api_key=api_key)
|
12 |
+
model = genai.GenerativeModel("gemini-pro")
|
13 |
+
chat = model.start_chat()
|
14 |
+
if context:
|
15 |
+
prompt = f"{context}\n\n{prompt}"
|
16 |
+
try:
|
17 |
+
response = chat.send_message(prompt)
|
18 |
+
return response.text
|
19 |
+
except Exception as e:
|
20 |
+
return f"Gemini Error: {e}"
|