Thomas Richardson commited on
Commit
077cffe
·
unverified ·
2 Parent(s): 95aeed2727ac01

Merge pull request #157 from ttt246/feature/babyagi_136

Browse files
.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/firebase/firebase.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  from typing import Any
4
 
5
  import firebase_admin
 
6
  from firebase_admin import credentials
7
 
8
  from Brain.src.common.assembler import Assembler
@@ -62,3 +63,15 @@ def get_firebase_cred(setting: ReqModel):
62
  else:
63
  cred = json.loads(setting.firebase_key)
64
  return credentials.Certificate(cred)
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from typing import Any
4
 
5
  import firebase_admin
6
+ from firebase_admin import db
7
  from firebase_admin import credentials
8
 
9
  from Brain.src.common.assembler import Assembler
 
63
  else:
64
  cred = json.loads(setting.firebase_key)
65
  return credentials.Certificate(cred)
66
+
67
+
68
+ """
69
+ delete data from real time database of firebase using reference link
70
+ """
71
+
72
+
73
+ def delete_data_from_realtime(
74
+ reference_link: str, firebase_app: firebase_admin.App
75
+ ) -> None:
76
+ ref = db.reference(reference_link, app=firebase_app)
77
+ ref.delete()
Brain/src/rising_plugin/llm/autogpt_llm.py CHANGED
@@ -27,6 +27,11 @@ from marshmallow import ValidationError
27
 
28
  import faiss
29
 
 
 
 
 
 
30
 
31
  class AutoGPTLLM:
32
  """autogpt run method to get the expected result"""
@@ -52,6 +57,12 @@ class AutoGPTLLM:
52
  # Discontinue if continuous limit is reached
53
  loop_count += 1
54
 
 
 
 
 
 
 
55
  # Send message to AI, get response
56
  assistant_reply = agent.chain.run(
57
  goals=goals,
 
27
 
28
  import faiss
29
 
30
+ from Brain.src.rising_plugin.llm.llms import (
31
+ MAX_AUTO_THINKING,
32
+ get_finish_command_for_auto_task,
33
+ )
34
+
35
 
36
  class AutoGPTLLM:
37
  """autogpt run method to get the expected result"""
 
57
  # Discontinue if continuous limit is reached
58
  loop_count += 1
59
 
60
+ # validation thinking counter
61
+ if loop_count == MAX_AUTO_THINKING:
62
+ # add finish command of the command
63
+ ref.push().set(get_finish_command_for_auto_task())
64
+ break
65
+
66
  # Send message to AI, get response
67
  assistant_reply = agent.chain.run(
68
  goals=goals,
Brain/src/rising_plugin/llm/babyagi_llm.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """BabyAGP Plugin with Langchain"""
2
+ import firebase_admin
3
+ from firebase_admin import db
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
12
+ from langchain.docstore import InMemoryDocstore
13
+ from langchain.callbacks.manager import CallbackManagerForChainRun
14
+ import faiss
15
+
16
+ from Brain.src.rising_plugin.llm.llms import (
17
+ MAX_AUTO_THINKING,
18
+ get_finish_command_for_auto_task,
19
+ )
20
+
21
+
22
+ class BabyAGILLM:
23
+ """BabyAGI run method to get the expected result"""
24
+
25
+ def run(
26
+ self,
27
+ agent: BabyAGI,
28
+ inputs: Dict[str, Any],
29
+ firebase_app: firebase_admin.App,
30
+ reference_link: str,
31
+ run_manager: Optional[CallbackManagerForChainRun] = None,
32
+ ) -> Dict[str, Any]:
33
+ """firebase realtime database init"""
34
+ ref = db.reference(reference_link, app=firebase_app)
35
+
36
+ """Run the agent."""
37
+ objective = inputs["objective"]
38
+ first_task = inputs.get("first_task", "Make a todo list")
39
+ agent.add_task({"task_id": 1, "task_name": first_task})
40
+ num_iters = 0
41
+ while True:
42
+ if agent.task_list:
43
+ agent.print_task_list()
44
+
45
+ # Step 1: Pull the first task
46
+ task = agent.task_list.popleft()
47
+ agent.print_next_task(task)
48
+ # update the result with the task in firebase realtime database
49
+ ref.push().set(task)
50
+
51
+ # Step 2: Execute the task
52
+ result = agent.execute_task(objective, task["task_name"])
53
+ this_task_id = int(task["task_id"])
54
+ agent.print_task_result(result)
55
+ # add result of the command
56
+ ref.push().set({"result": result})
57
+
58
+ # Step 3: Store the result in Pinecone
59
+ result_id = f"result_{task['task_id']}"
60
+ agent.vectorstore.add_texts(
61
+ texts=[result],
62
+ metadatas=[{"task": task["task_name"]}],
63
+ ids=[result_id],
64
+ )
65
+
66
+ # Step 4: Create new tasks and reprioritize task list
67
+ new_tasks = agent.get_next_task(result, task["task_name"], objective)
68
+ for new_task in new_tasks:
69
+ agent.task_id_counter += 1
70
+ new_task.update({"task_id": agent.task_id_counter})
71
+ agent.add_task(new_task)
72
+ agent.task_list = deque(agent.prioritize_tasks(this_task_id, objective))
73
+ num_iters += 1
74
+ if (
75
+ agent.max_iterations is not None and num_iters == agent.max_iterations
76
+ ) or num_iters == MAX_AUTO_THINKING:
77
+ print(
78
+ "\033[91m\033[1m" + "\n*****TASK ENDING*****\n" + "\033[0m\033[0m"
79
+ )
80
+ # add finish command of the command
81
+ ref.push().set(get_finish_command_for_auto_task())
82
+ break
83
+ return {}
84
+
85
+ def ask_task(
86
+ self, query: str, firebase_app: firebase_admin.App, reference_link: str
87
+ ):
88
+ # Define your embedding model
89
+ embeddings_model = OpenAIEmbeddings()
90
+
91
+ # Initialize the vectorstore as empty
92
+ embedding_size = 1536
93
+ index = faiss.IndexFlatL2(embedding_size)
94
+ vectorstore = FAISS(
95
+ embeddings_model.embed_query, index, InMemoryDocstore({}), {}
96
+ )
97
+
98
+ llm = OpenAI(temperature=0)
99
+
100
+ # Logging of LLMChains
101
+ verbose = False
102
+ # If None, will keep on going forever
103
+ max_iterations: Optional[int] = 3
104
+ baby_agi = BabyAGI.from_llm(
105
+ llm=llm,
106
+ vectorstore=vectorstore,
107
+ verbose=verbose,
108
+ max_iterations=max_iterations,
109
+ )
110
+
111
+ # querying
112
+ self.run(
113
+ agent=baby_agi,
114
+ inputs={"objective": query},
115
+ firebase_app=firebase_app,
116
+ reference_link=reference_link,
117
+ )
Brain/src/rising_plugin/llm/llms.py CHANGED
@@ -20,6 +20,8 @@ GPT_LLM_MODELS = [GPT_3_5_TURBO, GPT_4, GPT_4_32K]
20
  """exception message"""
21
  EXCEPTION_MSG = f"The model is not correct. It should be in {LLM_MODELS}"
22
 
 
 
23
 
24
  """prompt"""
25
  CATEGORY_PROMPT = """
@@ -99,3 +101,17 @@ def get_llm(
99
  elif model == FALCON_7B:
100
  llm = FalconLLM(temperature=temperature, max_new_tokens=max_new_tokens)
101
  return llm
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  """exception message"""
21
  EXCEPTION_MSG = f"The model is not correct. It should be in {LLM_MODELS}"
22
 
23
+ """maximum auto achievement counter"""
24
+ MAX_AUTO_THINKING = 10
25
 
26
  """prompt"""
27
  CATEGORY_PROMPT = """
 
101
  elif model == FALCON_7B:
102
  llm = FalconLLM(temperature=temperature, max_new_tokens=max_new_tokens)
103
  return llm
104
+
105
+
106
+ """
107
+ generate finish command and response for auto achievement
108
+ """
109
+
110
+
111
+ def get_finish_command_for_auto_task() -> Any:
112
+ return {
113
+ "command": {
114
+ "args": {"response": "I have finished all my objectives."},
115
+ "name": "finish",
116
+ }
117
+ }
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.ask_task_with_autogpt(
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
- delete_db_data(data.data.reference_link, firebase_app)
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}"
Brain/src/service/auto_task_service.py CHANGED
@@ -1,27 +1,17 @@
1
  """auto task management to get the expected output"""
2
  import firebase_admin
3
- from firebase_admin import db
4
 
5
  from Brain.src.model.req_model import ReqModel
6
  from Brain.src.rising_plugin.llm.autogpt_llm import AutoGPTLLM
7
  import time
8
- import asyncio
9
  import threading
10
 
11
- """delete data from real time database of firebase using reference link
12
- """
13
-
14
-
15
- def delete_db_data(reference_link: str, firebase_app: firebase_admin.App):
16
- ref = db.reference(reference_link, app=firebase_app)
17
- ref.delete()
18
-
19
 
20
  class AutoTaskService:
21
- """self task archivement with autogpt based on langchain
22
  response -> reference_link :str"""
23
 
24
- def ask_task_with_autogpt(
25
  self, query: str, firebase_app: firebase_admin.App, setting: ReqModel
26
  ) -> str:
27
  # init autogpt llm
@@ -39,9 +29,11 @@ class AutoTaskService:
39
 
40
  return reference_link
41
 
42
- """generate reference link for autoTask
 
43
  response type:
44
- /auto/{llm_name}_{uuid}_{timestamp}"""
 
45
 
46
  def generate_reference_link(self, llm_name: str, uuid: str) -> str:
47
  milliseconds = int(time.time() * 1000)
 
1
  """auto task management to get the expected output"""
2
  import firebase_admin
 
3
 
4
  from Brain.src.model.req_model import ReqModel
5
  from Brain.src.rising_plugin.llm.autogpt_llm import AutoGPTLLM
6
  import time
 
7
  import threading
8
 
 
 
 
 
 
 
 
 
9
 
10
  class AutoTaskService:
11
+ """self task achievement with autogpt based on langchain
12
  response -> reference_link :str"""
13
 
14
+ def ask_task_with_llm(
15
  self, query: str, firebase_app: firebase_admin.App, setting: ReqModel
16
  ) -> str:
17
  # init autogpt llm
 
29
 
30
  return reference_link
31
 
32
+ """
33
+ generate reference link for autoTask
34
  response type:
35
+ /auto/{llm_name}_{uuid}_{timestamp}
36
+ """
37
 
38
  def generate_reference_link(self, llm_name: str, uuid: str) -> str:
39
  milliseconds = int(time.time() * 1000)