Kotta commited on
Commit ·
727ac01
1
Parent(s): c840615
bugfix(#136): babyagi service and interface with langchain babyagi.
Browse files- .gitignore +1 -1
- Brain/logs/error.log +0 -3
- Brain/src/rising_plugin/llm/babyagi_llm.py +1 -5
- Brain/src/router/api.py +24 -3
- Brain/src/service/BabyAGIService.py +44 -0
.gitignore
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
/Brain/firebase_cred.json
|
| 2 |
/.idea
|
| 3 |
Brain/firebase_cred.json
|
| 4 |
-
Brain/logs
|
| 5 |
|
|
|
|
| 1 |
/Brain/firebase_cred.json
|
| 2 |
/.idea
|
| 3 |
Brain/firebase_cred.json
|
| 4 |
+
Brain/logs/*
|
| 5 |
|
Brain/logs/error.log
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
2023-06-11 01:39:05,597 ERROR logs:_log:139 sendNotification
|
| 2 |
-
2023-06-16 14:22:55,555 ERROR logs:_log:139 sendNotification json parsing or get completion error
|
| 3 |
-
2023-06-16 14:23:59,508 ERROR logs:_log:139 sendNotification json parsing or get completion error
|
|
|
|
|
|
|
|
|
|
|
|
Brain/src/rising_plugin/llm/babyagi_llm.py
CHANGED
|
@@ -4,12 +4,8 @@ from firebase_admin import db
|
|
| 4 |
from collections import deque
|
| 5 |
from typing import Dict, List, Optional, Any
|
| 6 |
|
| 7 |
-
from langchain import
|
| 8 |
from langchain.embeddings import OpenAIEmbeddings
|
| 9 |
-
from langchain.llms import BaseLLM
|
| 10 |
-
from langchain.vectorstores.base import VectorStore
|
| 11 |
-
from pydantic import BaseModel, Field
|
| 12 |
-
from langchain.chains.base import Chain
|
| 13 |
from langchain.experimental import BabyAGI
|
| 14 |
|
| 15 |
from langchain.vectorstores import FAISS
|
|
|
|
| 4 |
from collections import deque
|
| 5 |
from typing import Dict, List, Optional, Any
|
| 6 |
|
| 7 |
+
from langchain import OpenAI
|
| 8 |
from langchain.embeddings import OpenAIEmbeddings
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
from langchain.experimental import BabyAGI
|
| 10 |
|
| 11 |
from langchain.vectorstores import FAISS
|
Brain/src/router/api.py
CHANGED
|
@@ -5,6 +5,7 @@ from Brain.src.common.brain_exception import BrainException
|
|
| 5 |
from Brain.src.common.utils import ProgramType, DEFAULT_GPT_MODEL
|
| 6 |
from Brain.src.firebase.firebase import (
|
| 7 |
firebase_admin_with_setting,
|
|
|
|
| 8 |
)
|
| 9 |
from Brain.src.model.image_model import ImageModel
|
| 10 |
from Brain.src.model.requests.request_model import (
|
|
@@ -32,13 +33,13 @@ from Brain.src.rising_plugin.image_embedding import embed_image_text, query_imag
|
|
| 32 |
from Brain.src.logs import logger
|
| 33 |
from Brain.src.model.basic_model import BasicModel
|
| 34 |
from Brain.src.model.feedback_model import FeedbackModel
|
|
|
|
| 35 |
from Brain.src.service.auto_task_service import AutoTaskService
|
| 36 |
from Brain.src.service.command_service import CommandService
|
| 37 |
from Brain.src.service.contact_service import ContactsService
|
| 38 |
from Brain.src.service.feedback_service import FeedbackService
|
| 39 |
from Brain.src.service.llm.chat_service import ChatService
|
| 40 |
from Brain.src.service.twilio_service import TwilioService
|
| 41 |
-
from Brain.src.service.auto_task_service import delete_db_data
|
| 42 |
|
| 43 |
from fastapi import APIRouter, Depends
|
| 44 |
|
|
@@ -96,7 +97,7 @@ def construct_blueprint_api() -> APIRouter:
|
|
| 96 |
)
|
| 97 |
if result["program"] == ProgramType.AUTO_TASK:
|
| 98 |
auto_task_service = AutoTaskService()
|
| 99 |
-
result["content"] = auto_task_service.
|
| 100 |
query=query, firebase_app=firebase_app, setting=setting
|
| 101 |
)
|
| 102 |
return assembler.to_response(200, "", result)
|
|
@@ -490,7 +491,7 @@ def construct_blueprint_api() -> APIRouter:
|
|
| 490 |
|
| 491 |
# parsing contacts
|
| 492 |
# train contact
|
| 493 |
-
|
| 494 |
except Exception as e:
|
| 495 |
if isinstance(e, BrainException):
|
| 496 |
return e.get_response_exp()
|
|
@@ -499,6 +500,26 @@ def construct_blueprint_api() -> APIRouter:
|
|
| 499 |
200, "Deleted data from real-time database of firebase", ""
|
| 500 |
)
|
| 501 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 502 |
"""@generator.request_body(
|
| 503 |
{
|
| 504 |
"token": "String",
|
|
|
|
| 5 |
from Brain.src.common.utils import ProgramType, DEFAULT_GPT_MODEL
|
| 6 |
from Brain.src.firebase.firebase import (
|
| 7 |
firebase_admin_with_setting,
|
| 8 |
+
delete_data_from_realtime,
|
| 9 |
)
|
| 10 |
from Brain.src.model.image_model import ImageModel
|
| 11 |
from Brain.src.model.requests.request_model import (
|
|
|
|
| 33 |
from Brain.src.logs import logger
|
| 34 |
from Brain.src.model.basic_model import BasicModel
|
| 35 |
from Brain.src.model.feedback_model import FeedbackModel
|
| 36 |
+
from Brain.src.service.BabyAGIService import BabyAGIService
|
| 37 |
from Brain.src.service.auto_task_service import AutoTaskService
|
| 38 |
from Brain.src.service.command_service import CommandService
|
| 39 |
from Brain.src.service.contact_service import ContactsService
|
| 40 |
from Brain.src.service.feedback_service import FeedbackService
|
| 41 |
from Brain.src.service.llm.chat_service import ChatService
|
| 42 |
from Brain.src.service.twilio_service import TwilioService
|
|
|
|
| 43 |
|
| 44 |
from fastapi import APIRouter, Depends
|
| 45 |
|
|
|
|
| 97 |
)
|
| 98 |
if result["program"] == ProgramType.AUTO_TASK:
|
| 99 |
auto_task_service = AutoTaskService()
|
| 100 |
+
result["content"] = auto_task_service.ask_task_with_llm(
|
| 101 |
query=query, firebase_app=firebase_app, setting=setting
|
| 102 |
)
|
| 103 |
return assembler.to_response(200, "", result)
|
|
|
|
| 491 |
|
| 492 |
# parsing contacts
|
| 493 |
# train contact
|
| 494 |
+
delete_data_from_realtime(data.data.reference_link, firebase_app)
|
| 495 |
except Exception as e:
|
| 496 |
if isinstance(e, BrainException):
|
| 497 |
return e.get_response_exp()
|
|
|
|
| 500 |
200, "Deleted data from real-time database of firebase", ""
|
| 501 |
)
|
| 502 |
|
| 503 |
+
@router.post("/auto_task/babyagi")
|
| 504 |
+
def autotask_babyagi(
|
| 505 |
+
data: Notification, client_info: ClientInfo = Depends(get_client_info)
|
| 506 |
+
):
|
| 507 |
+
# firebase admin init
|
| 508 |
+
try:
|
| 509 |
+
setting, firebase_app = firebase_admin_with_setting(data)
|
| 510 |
+
except BrainException as ex:
|
| 511 |
+
return assembler.to_response(ex.code, ex.message, "")
|
| 512 |
+
try:
|
| 513 |
+
babyagi_service = BabyAGIService()
|
| 514 |
+
reference_link = babyagi_service.ask_task_with_llm(
|
| 515 |
+
query=data.message, firebase_app=firebase_app, setting=setting
|
| 516 |
+
)
|
| 517 |
+
return assembler.to_response(200, "", reference_link)
|
| 518 |
+
except Exception as e:
|
| 519 |
+
if isinstance(e, BrainException):
|
| 520 |
+
return e.get_response_exp()
|
| 521 |
+
return assembler.to_response(400, "Failed to handle it with BabyAGI", "")
|
| 522 |
+
|
| 523 |
"""@generator.request_body(
|
| 524 |
{
|
| 525 |
"token": "String",
|
Brain/src/service/BabyAGIService.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""BabyAGI Service Interface"""
|
| 2 |
+
|
| 3 |
+
import firebase_admin
|
| 4 |
+
|
| 5 |
+
from Brain.src.model.req_model import ReqModel
|
| 6 |
+
from Brain.src.rising_plugin.llm.babyagi_llm import BabyAGILLM
|
| 7 |
+
import time
|
| 8 |
+
import threading
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class BabyAGIService:
|
| 12 |
+
|
| 13 |
+
"""
|
| 14 |
+
self task achievement with babyagi based on langchain
|
| 15 |
+
response -> reference_link :str
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
def ask_task_with_llm(
|
| 19 |
+
self, query: str, firebase_app: firebase_admin.App, setting: ReqModel
|
| 20 |
+
) -> str:
|
| 21 |
+
# init autogpt llm
|
| 22 |
+
babyagi_llm = BabyAGILLM()
|
| 23 |
+
|
| 24 |
+
# generate reference link
|
| 25 |
+
reference_link = self.generate_reference_link(
|
| 26 |
+
llm_name="babyagi", uuid=setting.uuid
|
| 27 |
+
)
|
| 28 |
+
# call autogpt
|
| 29 |
+
thread = threading.Thread(
|
| 30 |
+
target=babyagi_llm.ask_task, args=(query, firebase_app, reference_link)
|
| 31 |
+
)
|
| 32 |
+
thread.start()
|
| 33 |
+
|
| 34 |
+
return reference_link
|
| 35 |
+
|
| 36 |
+
"""
|
| 37 |
+
generate reference link for autoTask
|
| 38 |
+
response type:
|
| 39 |
+
/auto/{llm_name}_{uuid}_{timestamp}
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
def generate_reference_link(self, llm_name: str, uuid: str) -> str:
|
| 43 |
+
milliseconds = int(time.time() * 1000)
|
| 44 |
+
return f"/babyagi/{llm_name}_{uuid}_{milliseconds}"
|