Spaces:
Sleeping
Sleeping
Create components/api_manager.py
Browse files- components/api_manager.py +72 -0
components/api_manager.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
class APIManager:
|
| 5 |
+
def __init__(self):
|
| 6 |
+
self.minds_api_key = os.getenv('MINDSDB_API_KEY')
|
| 7 |
+
self.supabase_password = os.getenv('SUPABASE_PASSWORD')
|
| 8 |
+
self.minds_name = 'precision_medicine_mind'
|
| 9 |
+
self.headers = {
|
| 10 |
+
'Authorization': f'Bearer {self.minds_api_key}',
|
| 11 |
+
'Content-Type': 'application/json'
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
def create_mind(self):
|
| 15 |
+
supabase_config = {
|
| 16 |
+
"description": "Your Supabase Database",
|
| 17 |
+
"type": "postgres",
|
| 18 |
+
"connection_args": {
|
| 19 |
+
"user": "postgres.mgvepfrmsojwkoudycez",
|
| 20 |
+
"password": self.supabase_password,
|
| 21 |
+
"host": "aws-0-ap-southeast-1.pooler.supabase.com",
|
| 22 |
+
"port": "6543",
|
| 23 |
+
"database": "postgres"
|
| 24 |
+
},
|
| 25 |
+
"tables": ["Precision_medicine"]
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
payload = {
|
| 29 |
+
"name": self.minds_name,
|
| 30 |
+
"data_source_configs": [supabase_config]
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
response = requests.post(
|
| 35 |
+
url='https://llm.mdb.ai/minds',
|
| 36 |
+
json=payload,
|
| 37 |
+
headers=self.headers
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if response.status_code == 201:
|
| 41 |
+
return f"Mind '{self.minds_name}' created successfully!"
|
| 42 |
+
else:
|
| 43 |
+
return "Mind already exists"
|
| 44 |
+
|
| 45 |
+
except requests.exceptions.HTTPError as http_err:
|
| 46 |
+
return f"HTTP error occurred: {http_err}"
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"An error occurred: {e}"
|
| 49 |
+
|
| 50 |
+
def query_mind(self, query):
|
| 51 |
+
query_payload = {
|
| 52 |
+
"model": self.minds_name,
|
| 53 |
+
"messages": [
|
| 54 |
+
{"role": "user", "content": query}
|
| 55 |
+
]
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
query_response = requests.post(
|
| 60 |
+
url='https://llm.mdb.ai/chat/completions',
|
| 61 |
+
json=query_payload,
|
| 62 |
+
headers=self.headers
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
if query_response.status_code == 200:
|
| 66 |
+
response_data = query_response.json()
|
| 67 |
+
return response_data['choices'][0]['message']['content']
|
| 68 |
+
else:
|
| 69 |
+
return f"Error querying the Mind: {query_response.status_code} - {query_response.text}"
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return f"Error querying the Mind: {e}"
|