File size: 18,962 Bytes
bf41ce7 1fac9b0 38d2586 e8a6b78 727ac01 e8a6b78 1fac9b0 4cda30b 8996d88 191e0af ef5e6f9 4cda30b 1fac9b0 bf41ce7 e8a6b78 1fac9b0 727ac01 51b69ae 1fac9b0 bf41ce7 38d2586 bf41ce7 4cda30b bf41ce7 4cda30b bf41ce7 38d2586 bf41ce7 4cda30b 8996d88 e8a6b78 28af5bc e8a6b78 4cda30b 4e54695 8996d88 0abf952 bf41ce7 286137e 4e54695 0abf952 286137e bf41ce7 ef5e6f9 51b69ae 727ac01 51b69ae 20555bc bf41ce7 20555bc bf41ce7 20555bc 8324b6b bf41ce7 a8b45b8 bf41ce7 c24b9a6 bf41ce7 4cda30b e8a6b78 28af5bc 38d2586 28af5bc e8a6b78 28af5bc 4e54695 bf41ce7 28af5bc 4e54695 28af5bc c24b9a6 28af5bc bf41ce7 28af5bc bf41ce7 28af5bc bf41ce7 28af5bc bf41ce7 4cda30b 38d2586 28af5bc 38d2586 e8a6b78 38d2586 e8a6b78 4cda30b 4e54695 bf41ce7 28af5bc bf41ce7 286137e 38d2586 286137e bf41ce7 28af5bc bf41ce7 28af5bc bf41ce7 e8a6b78 20555bc bf41ce7 4cda30b bf41ce7 38d2586 4e54695 bf41ce7 4cda30b bf41ce7 28af5bc bf41ce7 38d2586 28af5bc 38d2586 bf41ce7 20555bc bf41ce7 38d2586 bf41ce7 20555bc bf41ce7 4cda30b 38d2586 28af5bc 38d2586 bf41ce7 4e54695 4cda30b bf41ce7 38d2586 bf41ce7 20555bc bf41ce7 4cda30b bf41ce7 4cda30b 38d2586 28af5bc bf41ce7 4e54695 bf41ce7 4cda30b bf41ce7 4cda30b 38d2586 28af5bc bf41ce7 4e54695 bf41ce7 4cda30b bf41ce7 ef5e6f9 bf41ce7 28af5bc bf41ce7 4cda30b 38d2586 28af5bc bf41ce7 4e54695 bf41ce7 ef5e6f9 bf41ce7 28af5bc bf41ce7 191e0af 727ac01 191e0af 727ac01 1dabf5e 93e9960 76be69d 93e9960 1dabf5e aeda442 1dabf5e 4b10639 f89223f 1dabf5e ef5e6f9 f89223f 1dabf5e ef5e6f9 1dabf5e 4cda30b | 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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 | import json
from Brain.src.common.assembler import Assembler
from Brain.src.common.brain_exception import BrainException
from Brain.src.common.utils import ProgramType, DEFAULT_GPT_MODEL
from Brain.src.firebase.firebase import (
firebase_admin_with_setting,
delete_data_from_realtime,
)
from Brain.src.model.image_model import ImageModel
from Brain.src.model.requests.request_model import (
Notification,
UploadImage,
ImageRelatedness,
Feedback,
ChatRising,
SendSMS,
TrainContacts,
BasicReq,
ClientInfo,
get_client_info,
AutoTaskDelete,
GetContactsByIds,
)
from Brain.src.rising_plugin.risingplugin import (
getCompletion,
getTextFromImage,
query_image_ask,
)
from Brain.src.firebase.cloudmessage import CloudMessage
from Brain.src.rising_plugin.image_embedding import embed_image_text, query_image_text
from Brain.src.logs import logger
from Brain.src.model.basic_model import BasicModel
from Brain.src.model.feedback_model import FeedbackModel
from Brain.src.service.BabyAGIService import BabyAGIService
from Brain.src.service.auto_task_service import AutoTaskService
from Brain.src.service.command_service import CommandService
from Brain.src.service.contact_service import ContactsService
from Brain.src.service.feedback_service import FeedbackService
from Brain.src.service.llm.chat_service import ChatService
from Brain.src.service.twilio_service import TwilioService
from fastapi import APIRouter, Depends
router = APIRouter()
def construct_blueprint_api() -> APIRouter:
# Assembler
assembler = Assembler()
# Service
command_service = CommandService()
"""@generator.response(
status_code=200, schema={"message": "message", "result": "test_result"}
)
@generator.request_body(
{"message": "this is test message", "token": "test_token", "uuid": "test_uuid"}
)"""
@router.post("/sendNotification")
def send_notification(
data: Notification, client_info: ClientInfo = Depends(get_client_info)
):
# firebase admin init
try:
setting, firebase_app = firebase_admin_with_setting(data)
except BrainException as ex:
return ex.get_response_exp()
# cloud message
cloud_message = CloudMessage(firebase_app=firebase_app)
# parsing params
query = data.message
token = setting.token
uuid = setting.uuid
# check browser endpoint
# if client_info.is_browser():
# query = f"{query} in a web browser"
is_browser = client_info.is_browser()
result = getCompletion(
query=query,
setting=setting,
firebase_app=firebase_app,
is_browser=is_browser,
)
# check contact querying
try:
contacts_service = ContactsService(
firebase_app=firebase_app, setting=setting
)
if result["program"] == ProgramType.AUTO_TASK:
auto_task_service = AutoTaskService()
result["content"] = auto_task_service.ask_task_with_llm(
query=query, firebase_app=firebase_app, setting=setting
)
return assembler.to_response(200, "", result)
if result["program"] == ProgramType.CONTACT:
# querying contacts to getting its expected results
contacts_results = contacts_service.query_contacts(
uuid=uuid, search=result["content"]
)
result["content"] = str(contacts_results)
value = ""
if not client_info.is_browser():
notification = {"title": "alert", "content": json.dumps(result)}
state, value = cloud_message.send_message(notification, [token])
return assembler.to_response(200, value, result)
except Exception as e:
logger.error(
title="sendNotification", message="json parsing or get completion error"
)
if isinstance(e, BrainException):
return e.get_response_exp()
"""@generator.response(
status_code=200, schema={"message": "message", "result": "test_result"}
)
@generator.request_body(
{
"image_name": "this is test image path",
"token": "test_token",
"uuid": "test_uuid",
"status": "created | updated | deleted",
}
)"""
@router.post("/uploadImage")
def upload_image(data: UploadImage):
# firebase admin init
try:
setting, firebase_app = firebase_admin_with_setting(data)
except BrainException as ex:
return ex.get_response_exp()
# cloud message
try:
cloud_message = CloudMessage(firebase_app=firebase_app)
image_model = ImageModel()
token = setting.token
image_model.image_name = data.image_name
image_model.uuid = setting.uuid
image_model.status = data.status
image_model.image_text = getTextFromImage(
filename=image_model.image_name, firebase_app=firebase_app
)
embed_result = embed_image_text(image=image_model, setting=setting)
notification = {"title": "alert", "content": embed_result}
state, value = cloud_message.send_message(notification, [token])
return assembler.to_response(200, value, image_model.to_json())
except BrainException as ex:
return ex.get_response_exp()
"""@generator.response(
status_code=200, schema={"message": "message", "result": "test_result"}
)
@generator.request_body(
{
"image_name": "this is test image path",
"message": "this is a test message",
"token": "test_token",
"uuid": "test_uuid",
}
)"""
@router.post("/image_relatedness")
def image_relatedness(data: ImageRelatedness):
# firebase admin init
try:
setting, firebase_app = firebase_admin_with_setting(data)
except BrainException as ex:
return ex.get_response_exp()
# cloud message
cloud_message = CloudMessage(firebase_app=firebase_app)
# parsing params
image_name = data.image_name
message = data.message
token = setting.token
uuid = setting.uuid
image_content = getTextFromImage(filename=image_name, firebase_app=firebase_app)
# check message type
image_response = {}
try:
# check about asking image description with trained data
if query_image_ask(
image_content=image_content, message=message, setting=setting
):
image_response["image_desc"] = image_content
else:
relatedness_data = query_image_text(image_content, message, setting)
image_response["image_name"] = relatedness_data
except ValueError as e:
print("image_relatedness parsing error for message chain data")
if isinstance(e, BrainException):
return e.get_response_exp()
notification = {"title": "alert", "content": json.dumps(image_response)}
state, value = cloud_message.send_message(notification, [token])
return assembler.to_response(
code=200,
message=value,
result={"program": "image", "content": image_response},
)
"""@generator.request_body(
{
"token": "test_token",
"uuid": "test_uuid",
"prompt": {"image_name": "test_image", "message": "test_message"},
"completion": {"image_name": "test_image", "message": "test_message"},
"rating": 1,
}
)
@generator.response(
status_code=200, schema={"message": "message", "result": "test_result"}
)"""
@router.post("/feedback")
def add_feedback(data: Feedback):
try:
# firebase admin init
setting, firebase_app = firebase_admin_with_setting(data)
# cloud message
cloud_message = CloudMessage(firebase_app=firebase_app)
feedback_service = FeedbackService(firebase_app=firebase_app)
token = setting.token
uuid = setting.uuid
# parsing feedback payload
prompt = BasicModel(
image_name=data.prompt.image_name, message=data.prompt.message
)
completion = BasicModel(
image_name=data.completion.image_name, message=data.completion.message
)
rating = data.rating
feedback = FeedbackModel(uuid, prompt, completion, rating)
# add the feedback
feedback_service.add(feedback)
except Exception as e:
if isinstance(e, BrainException):
return e.get_response_exp()
return assembler.to_response(400, "failed to add", "")
return assembler.to_response(200, "added successfully", "")
"""@generator.response(
status_code=200, schema={"message": "message", "result": "test_result"}
)"""
@router.post("/feedback/{search}/{rating}")
def get_feedback(search: str, rating: int, data: BasicReq):
# firebase admin init
try:
setting, firebase_app = firebase_admin_with_setting(data)
except BrainException as ex:
return ex.get_response_exp()
# cloud message
cloud_message = CloudMessage(firebase_app=firebase_app)
feedback_service = FeedbackService(firebase_app=firebase_app)
result = feedback_service.get(search, rating)
return assembler.to_response(200, "added successfully", result)
"""@generator.response(
status_code=200, schema={"message": "message", "result": "test_result"}
)"""
@router.post("/commands")
def get_commands(data: BasicReq):
result = command_service.get()
return assembler.to_response(
200, "success", {"program": "help_command", "content": result}
)
"""@generator.request_body(
{
"token": "test_token",
"uuid": "test_uuid",
"history": [{"role": "user", "content": "test_message"}],
"user_input": "user_message",
"model": "gpt-3.5-turbo",
}
)
@generator.response(
status_code=200,
schema={
"message": "message",
"result": {
"program": "agent",
"message": {"role": "assistant", "content": "content"},
},
},
)"""
@router.post("/chat_rising")
def message_agent(data: ChatRising):
# firebase admin init
try:
setting, firebase_app = firebase_admin_with_setting(data)
except BrainException as ex:
return ex.get_response_exp()
# cloud message
cloud_message = CloudMessage(firebase_app=firebase_app)
try:
token = setting.token
uuid = setting.uuid
histories = assembler.to_array_message_model(data.history) # json array
user_input = data.user_input
"""init chat service with model"""
chat_service = ChatService(ai_name=uuid, llm_model=DEFAULT_GPT_MODEL)
# getting chat response from rising ai
assistant_reply = chat_service.chat_with_ai(
prompt="",
user_input=user_input,
full_message_history=histories,
permanent_memory=None,
)
except Exception as e:
return assembler.to_response(
400, json.dumps(e.__getattribute__("error")), ""
)
return assembler.to_response(
200,
"added successfully",
{
"program": "agent",
"message": assistant_reply.get_one_message_item().to_json(),
},
)
"""@generator.request_body(
{
"token": "test_token",
"uuid": "test_uuid",
"data": {
"_from": "+15005550006",
"to": "+12173748105",
"body": "All in the game, yo",
},
}
)
@generator.response(
status_code=200, schema={"message": "message", "result": "test_result"}
)"""
@router.post("/send_sms")
def send_sms(data: SendSMS):
# firebase admin init
try:
setting, firebase_app = firebase_admin_with_setting(data)
except BrainException as ex:
return assembler.to_response(ex.code, ex.message, "")
try:
token = setting.token
uuid = setting.uuid
# parsing feedback payload
sms_model = assembler.to_sms_model(data.data)
# send sms via twilio
twilio_service = TwilioService()
twilio_resp = twilio_service.send_sms(sms_model)
except Exception as e:
return assembler.to_response(400, "Failed to send sms", "")
return assembler.to_response(200, "Sent a sms successfully", twilio_resp.sid)
"""@generator.request_body(
{
"token": "String",
"uuid": "String",
"contacts": [
{
"contactId": "String",
"displayName": "String",
"phoneNumbers": ["String"],
"status": "created | updated | deleted",
}
],
}
)
@generator.response(
status_code=200, schema={"message": "message", "result": "test_result"}
)"""
@router.post("/train/contacts")
def train_contacts(data: TrainContacts):
# firebase admin init
try:
setting, firebase_app = firebase_admin_with_setting(data)
except BrainException as ex:
return assembler.to_response(ex.code, ex.message, "")
try:
token = setting.token
uuid = setting.uuid
# parsing contacts
contacts = []
for contact in data.contacts:
contacts.append(assembler.to_contact_model(contact))
# train contact
contacts_service = ContactsService(
firebase_app=firebase_app, setting=setting
)
contacts_service.train(uuid, contacts)
except Exception as e:
if isinstance(e, BrainException):
return e.get_response_exp()
return assembler.to_response(400, "Failed to train contacts", "")
return assembler.to_response(200, "Trained successfully", "")
"""@generator.request_body(
{
"token": "String",
"uuid": "String",
}
)
@generator.response(
status_code=200, schema={"message": "message", "result": "test_result"}
)"""
@router.post("/train/contacts/delete")
def delete_all_contacts(data: BasicReq):
# firebase admin init
try:
setting, firebase_app = firebase_admin_with_setting(data)
except BrainException as ex:
return assembler.to_response(ex.code, ex.message, "")
try:
token = setting.token
uuid = setting.uuid
# parsing contacts
# train contact
contacts_service = ContactsService(
firebase_app=firebase_app, setting=setting
)
contacts_service.delete_all(uuid)
except Exception as e:
if isinstance(e, BrainException):
return e.get_response_exp()
return assembler.to_response(400, "Failed to delete contacts", "")
return assembler.to_response(
200, "Deleted all contacts from pinecone successfully", ""
)
"""@generator.request_body(
{
"token": "String",
"uuid": "String",
"data": {
"reference_link": "test link",
},
}
)
@generator.response(
status_code=200, schema={"message": "message", "result": "test_result"}
)
"""
@router.post("/auto_task/delete")
def delete_data(data: AutoTaskDelete):
# firebase admin init
try:
setting, firebase_app = firebase_admin_with_setting(data)
except BrainException as ex:
return assembler.to_response(ex.code, ex.message, "")
try:
token = setting.token
uuid = setting.uuid
# parsing contacts
# train contact
delete_data_from_realtime(data.data.reference_link, firebase_app)
except Exception as e:
if isinstance(e, BrainException):
return e.get_response_exp()
return assembler.to_response(400, "Failed to delete data", "")
return assembler.to_response(
200, "Deleted data from real-time database of firebase", ""
)
@router.post("/auto_task/babyagi")
def autotask_babyagi(
data: Notification, client_info: ClientInfo = Depends(get_client_info)
):
# firebase admin init
try:
setting, firebase_app = firebase_admin_with_setting(data)
except BrainException as ex:
return assembler.to_response(ex.code, ex.message, "")
try:
babyagi_service = BabyAGIService()
reference_link = babyagi_service.ask_task_with_llm(
query=data.message, firebase_app=firebase_app, setting=setting
)
return assembler.to_response(200, "", reference_link)
except Exception as e:
if isinstance(e, BrainException):
return e.get_response_exp()
return assembler.to_response(400, "Failed to handle it with BabyAGI", "")
"""@generator.request_body(
{
"token": "String",
"uuid": "String",
"contactIds": [
"String"
]
}
)
@generator.response(
status_code=200, schema={"message": "message", "result": "test_result"}
)
"""
@router.post("/contacts/get_by_ids")
def get_contacts_by_ids(data: GetContactsByIds):
try:
setting, firebase_app = firebase_admin_with_setting(data)
except BrainException as ex:
return ex.get_response_exp()
token: str = setting.token
uuid: str = setting.uuid
result = ContactsService(
firebase_app=firebase_app, setting=setting
).get_contacts_by_ids(uuid=uuid, contactIds=data.contactIds)
return assembler.to_response(200, "Success to get contacts by uuid", result)
return router
|