Nicolás Larenas
commited on
Create ai_model.py
Browse filesThis file configures and interacts with the AI model (Gemini Pro) through the Google Generative AI API.
- ai_model.py +18 -0
ai_model.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import google.generativeai as genai
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# Load Google API key from environment
|
| 5 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 6 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 7 |
+
model = genai.GenerativeModel('gemini-pro')
|
| 8 |
+
|
| 9 |
+
# Query AI model
|
| 10 |
+
async def query_ai_model(prompt):
|
| 11 |
+
try:
|
| 12 |
+
response = model.generate_content([{'role': 'user', 'parts': [prompt]}], stream=True)
|
| 13 |
+
result = ''
|
| 14 |
+
for chunk in response:
|
| 15 |
+
result += chunk.text
|
| 16 |
+
return result
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return f"An error occurred: {str(e)}"
|