forked from TransformerOptimus/SuperAGI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
433 lines (366 loc) · 18.4 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import os
import pickle
from datetime import datetime, timedelta
import requests
from fastapi import FastAPI, HTTPException, Depends, Request, status, Query
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.responses import RedirectResponse
from fastapi_jwt_auth import AuthJWT
from fastapi_jwt_auth.exceptions import AuthJWTException
from fastapi_sqlalchemy import DBSessionMiddleware, db
from pydantic import BaseModel
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import superagi
from superagi.agent.agent_prompt_builder import AgentPromptBuilder
from superagi.config.config import get_config
from superagi.controllers.agent import router as agent_router
from superagi.controllers.agent_config import router as agent_config_router
from superagi.controllers.agent_execution import router as agent_execution_router
from superagi.controllers.agent_execution_feed import router as agent_execution_feed_router
from superagi.controllers.agent_execution_permission import router as agent_execution_permission_router
from superagi.controllers.agent_template import router as agent_template_router
from superagi.controllers.agent_workflow import router as agent_workflow_router
from superagi.controllers.budget import router as budget_router
from superagi.controllers.config import router as config_router
from superagi.controllers.organisation import router as organisation_router
from superagi.controllers.project import router as project_router
from superagi.controllers.resources import router as resources_router
from superagi.controllers.tool import router as tool_router
from superagi.controllers.tool_config import router as tool_config_router
from superagi.controllers.toolkit import router as toolkit_router
from superagi.controllers.user import router as user_router
from superagi.helper.tool_helper import register_toolkits
from superagi.lib.logger import logger
from superagi.llms.openai import OpenAi
from superagi.models.agent_workflow import AgentWorkflow
from superagi.models.agent_workflow_step import AgentWorkflowStep
from superagi.models.organisation import Organisation
from superagi.models.tool_config import ToolConfig
from superagi.models.toolkit import Toolkit
from superagi.models.types.login_request import LoginRequest
from superagi.models.user import User
app = FastAPI()
database_url = get_config('POSTGRES_URL')
db_username = get_config('DB_USERNAME')
db_password = get_config('DB_PASSWORD')
db_name = get_config('DB_NAME')
if db_username is None:
db_url = f'postgresql://{database_url}/{db_name}'
else:
db_url = f'postgresql://{db_username}:{db_password}@{database_url}/{db_name}'
engine = create_engine(db_url)
# SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# app.add_middleware(DBSessionMiddleware, db_url=f'postgresql://{db_username}:{db_password}@localhost/{db_name}')
app.add_middleware(DBSessionMiddleware, db_url=db_url)
# Configure CORS middleware
origins = [
# Add more origins if needed
"*", # Allow all origins
]
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# Creating requrired tables -- Now handled using migrations
# DBBaseModel.metadata.create_all(bind=engine, checkfirst=True)
# DBBaseModel.metadata.drop_all(bind=engine,checkfirst=True)
app.include_router(user_router, prefix="/users")
app.include_router(tool_router, prefix="/tools")
app.include_router(organisation_router, prefix="/organisations")
app.include_router(project_router, prefix="/projects")
app.include_router(budget_router, prefix="/budgets")
app.include_router(agent_router, prefix="/agents")
app.include_router(agent_config_router, prefix="/agentconfigs")
app.include_router(agent_execution_router, prefix="/agentexecutions")
app.include_router(agent_execution_feed_router, prefix="/agentexecutionfeeds")
app.include_router(agent_execution_permission_router, prefix="/agentexecutionpermissions")
app.include_router(resources_router, prefix="/resources")
app.include_router(config_router, prefix="/configs")
app.include_router(toolkit_router, prefix="/toolkits")
app.include_router(tool_config_router, prefix="/tool_configs")
app.include_router(config_router, prefix="/configs")
app.include_router(agent_template_router, prefix="/agent_templates")
app.include_router(agent_workflow_router, prefix="/agent_workflows")
# in production you can use Settings management
# from pydantic to get secret key from .env
class Settings(BaseModel):
# jwt_secret = get_config("JWT_SECRET_KEY")
authjwt_secret_key: str = superagi.config.config.get_config("JWT_SECRET_KEY")
def create_access_token(email, Authorize: AuthJWT = Depends()):
# expiry_time_hours = get_config("JWT_EXPIRY")
expiry_time_hours = 1
expires = timedelta(hours=expiry_time_hours)
access_token = Authorize.create_access_token(subject=email, expires_time=expires)
return access_token
# callback to get your configuration
@AuthJWT.load_config
def get_config():
return Settings()
# exception handler for authjwt
# in production, you can tweak performance using orjson response
@app.exception_handler(AuthJWTException)
def authjwt_exception_handler(request: Request, exc: AuthJWTException):
return JSONResponse(
status_code=exc.status_code,
content={"detail": exc.message}
)
@app.on_event("startup")
async def startup_event():
# Perform startup tasks here
logger.info("Running Startup tasks")
Session = sessionmaker(bind=engine)
session = Session()
default_user = session.query(User).filter(User.email == "super6@agi.com").first()
logger.info(default_user)
if default_user is not None:
organisation = session.query(Organisation).filter_by(id=default_user.organisation_id).first()
logger.info(organisation)
register_toolkits(session, organisation)
def build_single_step_agent():
agent_workflow = session.query(AgentWorkflow).filter(AgentWorkflow.name == "Goal Based Agent").first()
if agent_workflow is None:
agent_workflow = AgentWorkflow(name="Goal Based Agent", description="Goal based agent")
session.add(agent_workflow)
session.commit()
# step will have a prompt
# output of step is either tasks or set commands
first_step = session.query(AgentWorkflowStep).filter(AgentWorkflowStep.unique_id == "gb1").first()
output = AgentPromptBuilder.get_super_agi_single_prompt()
if first_step is None:
first_step = AgentWorkflowStep(unique_id="gb1",
prompt=output["prompt"], variables=str(output["variables"]),
agent_workflow_id=agent_workflow.id, output_type="tools",
step_type="TRIGGER",
history_enabled=True,
completion_prompt="Determine which next tool to use, and respond using the format specified above:")
session.add(first_step)
session.commit()
else:
first_step.prompt = output["prompt"]
first_step.variables = str(output["variables"])
first_step.output_type = "tools"
first_step.completion_prompt = "Determine which next tool to use, and respond using the format specified above:"
session.commit()
first_step.next_step_id = first_step.id
session.commit()
def build_task_based_agents():
agent_workflow = session.query(AgentWorkflow).filter(AgentWorkflow.name == "Task Queue Agent With Seed").first()
if agent_workflow is None:
agent_workflow = AgentWorkflow(name="Task Queue Agent With Seed", description="Task queue based agent")
session.add(agent_workflow)
session.commit()
output = AgentPromptBuilder.start_task_based()
workflow_step1 = session.query(AgentWorkflowStep).filter(AgentWorkflowStep.unique_id == "tb1").first()
if workflow_step1 is None:
workflow_step1 = AgentWorkflowStep(unique_id="tb1",
prompt=output["prompt"], variables=str(output["variables"]),
step_type="TRIGGER",
agent_workflow_id=agent_workflow.id, next_step_id=-1,
output_type="tasks")
session.add(workflow_step1)
else:
workflow_step1.prompt = output["prompt"]
workflow_step1.variables = str(output["variables"])
workflow_step1.output_type = "tasks"
session.commit()
workflow_step2 = session.query(AgentWorkflowStep).filter(AgentWorkflowStep.unique_id == "tb2").first()
output = AgentPromptBuilder.create_tasks()
if workflow_step2 is None:
workflow_step2 = AgentWorkflowStep(unique_id="tb2",
prompt=output["prompt"], variables=str(output["variables"]),
step_type="NORMAL",
agent_workflow_id=agent_workflow.id, next_step_id=-1,
output_type="tasks")
session.add(workflow_step2)
else:
workflow_step2.prompt = output["prompt"]
workflow_step2.variables = str(output["variables"])
workflow_step2.output_type = "tasks"
session.commit()
workflow_step3 = session.query(AgentWorkflowStep).filter(AgentWorkflowStep.unique_id == "tb3").first()
output = AgentPromptBuilder.analyse_task()
if workflow_step3 is None:
workflow_step3 = AgentWorkflowStep(unique_id="tb3",
prompt=output["prompt"], variables=str(output["variables"]),
step_type="NORMAL",
agent_workflow_id=agent_workflow.id, next_step_id=-1,
output_type="tools")
session.add(workflow_step3)
else:
workflow_step3.prompt = output["prompt"]
workflow_step3.variables = str(output["variables"])
workflow_step3.output_type = "tools"
session.commit()
workflow_step4 = session.query(AgentWorkflowStep).filter(AgentWorkflowStep.unique_id == "tb4").first()
output = AgentPromptBuilder.prioritize_tasks()
if workflow_step4 is None:
workflow_step4 = AgentWorkflowStep(unique_id="tb4",
prompt=output["prompt"], variables=str(output["variables"]),
step_type="NORMAL",
agent_workflow_id=agent_workflow.id, next_step_id=-1,
output_type="replace_tasks")
session.add(workflow_step4)
else:
workflow_step4.prompt = output["prompt"]
workflow_step4.variables = str(output["variables"])
workflow_step4.output_type = "replace_tasks"
session.commit()
session.commit()
workflow_step1.next_step_id = workflow_step3.id
workflow_step3.next_step_id = workflow_step2.id
workflow_step2.next_step_id = workflow_step4.id
workflow_step4.next_step_id = workflow_step3.id
session.commit()
def check_toolkit_registration():
organizations = session.query(Organisation).all()
for organization in organizations:
register_toolkits(session, organization)
logger.info("Successfully registered local toolkits for all Organisations!")
build_single_step_agent()
build_task_based_agents()
check_toolkit_registration()
session.close()
@app.post('/login')
def login(request: LoginRequest, Authorize: AuthJWT = Depends()):
"""Login API for email and password based login"""
email_to_find = request.email
user: User = db.session.query(User).filter(User.email == email_to_find).first()
if user == None or request.email != user.email or request.password != user.password:
raise HTTPException(status_code=401, detail="Bad username or password")
# subject identifier for who this token is for example id or username from database
access_token = create_access_token(user.email, Authorize)
return {"access_token": access_token}
# def get_jwt_from_payload(user_email: str,Authorize: AuthJWT = Depends()):
# access_token = Authorize.create_access_token(subject=user_email)
# return access_token
@app.get('/oauth-calendar')
async def google_auth_calendar(code: str = Query(...), Authorize: AuthJWT = Depends()):
client_id = db.session.query(ToolConfig).filter(ToolConfig.key == "GOOGLE_CLIENT_ID").first()
client_id = client_id.value
client_secret = db.session.query(ToolConfig).filter(ToolConfig.key == "GOOGLE_CLIENT_SECRET").first()
client_secret = client_secret.value
token_uri = 'https://oauth2.googleapis.com/token'
scope = 'https://www.googleapis.com/auth/calendar'
params = {
'client_id': client_id,
'client_secret': client_secret,
'redirect_uri': "http://localhost:3000/api/oauth-calendar",
'scope': scope,
'grant_type': 'authorization_code',
'code': code,
'access_type': 'offline'
}
response = requests.post(token_uri, data=params)
response = response.json()
expire_time = datetime.utcnow() + timedelta(seconds=response['expires_in'])
expire_time = expire_time - timedelta(minutes=5)
response['expiry'] = expire_time.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
root_dir = superagi.config.config.get_config('RESOURCES_OUTPUT_ROOT_DIR')
file_name = "credential_token.pickle"
final_path = file_name
if root_dir is not None:
root_dir = root_dir if root_dir.startswith("/") else os.getcwd() + "/" + root_dir
root_dir = root_dir if root_dir.endswith("/") else root_dir + "/"
final_path = root_dir + file_name
else:
final_path = os.getcwd() + "/" + file_name
try:
with open(final_path, mode="wb") as file:
pickle.dump(response, file)
except Exception as err:
return f"Error: {err}"
frontend_url = superagi.config.config.get_config("FRONTEND_URL", "http://localhost:3000")
return RedirectResponse(frontend_url)
@app.get('/github-login')
def github_login():
"""GitHub login"""
github_client_id = ""
return RedirectResponse(f'https://github.com/login/oauth/authorize?scope=user:email&client_id={github_client_id}')
@app.get('/github-auth')
def github_auth_handler(code: str = Query(...), Authorize: AuthJWT = Depends()):
"""GitHub login callback"""
github_token_url = 'https://github.com/login/oauth/access_token'
github_client_id = superagi.config.config.get_config("GITHUB_CLIENT_ID")
github_client_secret = superagi.config.config.get_config("GITHUB_CLIENT_SECRET")
frontend_url = superagi.config.config.get_config("FRONTEND_URL", "http://localhost:3000")
params = {
'client_id': github_client_id,
'client_secret': github_client_secret,
'code': code
}
headers = {
'Accept': 'application/json'
}
response = requests.post(github_token_url, params=params, headers=headers)
if response.ok:
data = response.json()
access_token = data.get('access_token')
github_api_url = 'https://api.github.com/user'
headers = {
'Authorization': f'Bearer {access_token}'
}
response = requests.get(github_api_url, headers=headers)
if response.ok:
user_data = response.json()
user_email = user_data["email"]
if user_email is None:
user_email = user_data["login"] + "@github.com"
db_user: User = db.session.query(User).filter(User.email == user_email).first()
if db_user is not None:
jwt_token = create_access_token(user_email, Authorize)
redirect_url_success = f"{frontend_url}?access_token={jwt_token}"
return RedirectResponse(url=redirect_url_success)
user = User(name=user_data["name"], email=user_email)
db.session.add(user)
db.session.commit()
jwt_token = create_access_token(user_email, Authorize)
redirect_url_success = f"{frontend_url}?access_token={jwt_token}"
return RedirectResponse(url=redirect_url_success)
else:
redirect_url_failure = "https://superagi.com/"
return RedirectResponse(url=redirect_url_failure)
else:
redirect_url_failure = "https://superagi.com/"
return RedirectResponse(url=redirect_url_failure)
@app.get('/user')
def user(Authorize: AuthJWT = Depends()):
"""API to get current logged in User"""
Authorize.jwt_required()
current_user = Authorize.get_jwt_subject()
return {"user": current_user}
@app.get("/validate-access-token")
async def root(Authorize: AuthJWT = Depends()):
"""API to validate access token"""
try:
Authorize.jwt_required()
current_user_email = Authorize.get_jwt_subject()
current_user = db.session.query(User).filter(User.email == current_user_email).first()
return current_user
except:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")
@app.get("/google/get_google_creds/toolkit_id/{toolkit_id}")
def get_google_calendar_tool_configs(toolkit_id: int):
google_calendar_config = db.session.query(ToolConfig).filter(ToolConfig.toolkit_id == toolkit_id,
ToolConfig.key == "GOOGLE_CLIENT_ID").first()
return {
"client_id": google_calendar_config.value
}
@app.get("/validate-open-ai-key/{open_ai_key}")
async def root(open_ai_key: str, Authorize: AuthJWT = Depends()):
"""API to validate Open AI Key"""
try:
llm = OpenAi(api_key=open_ai_key)
response = llm.chat_completion([{"role": "system", "content": "Hey!"}])
except:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid API Key")
# #Unprotected route
@app.get("/hello/{name}")
async def say_hello(name: str, Authorize: AuthJWT = Depends()):
Authorize.jwt_required()
return {"message": f"Hello {name}"}
# # __________________TO RUN____________________________
# # uvicorn main:app --host 0.0.0.0 --port 8001 --reload