Kotta commited on
Commit
28af5bc
·
1 Parent(s): 38d2586

feature(#9): updated all the end points with firebase & pinecone configuration

Browse files
Brain/src/common/brain_exception.py CHANGED
@@ -7,10 +7,11 @@ from Brain.src.common.http_response_codes import responses
7
  class BrainException(Exception):
8
  JSON_PARSING_ISSUE_MSG = "Exception occurred in json paring."
9
 
10
- def __init__(self, message: str = "Exception occurred in brain"):
11
  self.message = message
 
12
  super().__init__(self.message)
13
 
14
  def get_response_exp(self) -> Any:
15
- responses[506] = ("Brain Exception", self.message)
16
- return {"message": responses[506], "result": "", "status_code": 506}
 
7
  class BrainException(Exception):
8
  JSON_PARSING_ISSUE_MSG = "Exception occurred in json paring."
9
 
10
+ def __init__(self, message: str = "Exception occurred in brain", code: int = 506):
11
  self.message = message
12
+ self.code = code
13
  super().__init__(self.message)
14
 
15
  def get_response_exp(self) -> Any:
16
+ responses[self.code] = ("Brain Exception", self.message)
17
+ return {"message": responses[self.code], "result": "", "status_code": self.code}
Brain/src/common/http_response_codes.py CHANGED
@@ -49,4 +49,6 @@ responses = {
49
  504: ("Gateway Timeout", "The gateway server did not receive a timely response"),
50
  505: ("HTTP Version Not Supported", "Cannot fulfill request."),
51
  506: ("Brain Exception"),
 
 
52
  }
 
49
  504: ("Gateway Timeout", "The gateway server did not receive a timely response"),
50
  505: ("HTTP Version Not Supported", "Cannot fulfill request."),
51
  506: ("Brain Exception"),
52
+ 507: ("Firebase Connection Error", "Your key is not correct."),
53
+ 508: ("Pinecone Connection Error", "Your key or env is not correct."),
54
  }
Brain/src/common/utils.py CHANGED
@@ -2,11 +2,8 @@ import json
2
  import os
3
  import re
4
 
5
- from firebase_admin import credentials
6
- from Brain.src.model.req_model import ReqModel
7
 
8
  # env variables
9
- DEFAULT_HOST_NAME = "test3-83ffc.appspot.com"
10
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
11
  PINECONE_KEY = os.getenv("PINECONE_KEY")
12
  PINECONE_ENV = os.getenv("PINECONE_ENV")
@@ -19,7 +16,7 @@ API_URL = "http://localhost:5000/file/swagger.json"
19
 
20
  # firebase
21
  FIREBASE_STORAGE_ROOT = "images/"
22
- FIREBASE_STORAGE_BUCKET = DEFAULT_HOST_NAME
23
 
24
  # pinecone
25
  PINECONE_NAMESPACE = "risinglangchain-namespace"
@@ -42,17 +39,6 @@ AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
42
  HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
43
 
44
 
45
- def get_firebase_cred(setting: ReqModel):
46
- if os.path.exists("Brain/firebase_cred.json"):
47
- file = open("Brain/firebase_cred.json")
48
- cred = json.load(file)
49
- file.close()
50
- return credentials.Certificate(cred)
51
- else:
52
- cred = json.loads(setting.firebase_key)
53
- return credentials.Certificate(cred)
54
-
55
-
56
  class ProgramType:
57
  BROWSER = "browser"
58
  ALERT = "alert"
 
2
  import os
3
  import re
4
 
 
 
5
 
6
  # env variables
 
7
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
8
  PINECONE_KEY = os.getenv("PINECONE_KEY")
9
  PINECONE_ENV = os.getenv("PINECONE_ENV")
 
16
 
17
  # firebase
18
  FIREBASE_STORAGE_ROOT = "images/"
19
+ FIREBASE_STORAGE_BUCKET = "test3-83ffc.appspot.com"
20
 
21
  # pinecone
22
  PINECONE_NAMESPACE = "risinglangchain-namespace"
 
39
  HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
40
 
41
 
 
 
 
 
 
 
 
 
 
 
 
42
  class ProgramType:
43
  BROWSER = "browser"
44
  ALERT = "alert"
Brain/src/firebase/firebase.py CHANGED
@@ -1,9 +1,14 @@
 
 
1
  from typing import Any
2
 
3
  import firebase_admin
 
4
 
5
  from Brain.src.common.assembler import Assembler
6
- from Brain.src.common.utils import get_firebase_cred, FIREBASE_STORAGE_BUCKET
 
 
7
  from Brain.src.logs import logger
8
  from Brain.src.model.req_model import ReqModel
9
  from Brain.src.model.requests.request_model import BasicReq
@@ -37,6 +42,19 @@ def firebase_admin_with_setting(data: BasicReq):
37
  # firebase admin init
38
  assembler = Assembler()
39
  setting = assembler.to_req_model(data)
40
-
41
- firebase_app = initialize_app(setting)
 
 
42
  return setting, firebase_app
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ 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
9
+ from Brain.src.common.brain_exception import BrainException
10
+ from Brain.src.common.http_response_codes import responses
11
+ from Brain.src.common.utils import FIREBASE_STORAGE_BUCKET
12
  from Brain.src.logs import logger
13
  from Brain.src.model.req_model import ReqModel
14
  from Brain.src.model.requests.request_model import BasicReq
 
42
  # firebase admin init
43
  assembler = Assembler()
44
  setting = assembler.to_req_model(data)
45
+ try:
46
+ firebase_app = initialize_app(setting)
47
+ except Exception as ex:
48
+ raise BrainException(code=507, message=responses[507])
49
  return setting, firebase_app
50
+
51
+
52
+ def get_firebase_cred(setting: ReqModel):
53
+ if os.path.exists("Brain/firebase_cred.json"):
54
+ file = open("Brain/firebase_cred.json")
55
+ cred = json.load(file)
56
+ file.close()
57
+ return credentials.Certificate(cred)
58
+ else:
59
+ cred = json.loads(setting.firebase_key)
60
+ return credentials.Certificate(cred)
Brain/src/model/req_model.py CHANGED
@@ -1,6 +1,11 @@
1
  from typing import Any
2
 
3
- DEFAULT_HOST_NAME = "test3-83ffc.appspot.com"
 
 
 
 
 
4
 
5
 
6
  class ReqModel:
@@ -8,7 +13,6 @@ class ReqModel:
8
  def __init__(self):
9
  self.temperature: float = 0.6
10
 
11
- host_name: str
12
  openai_key: str
13
  pinecone_key: str
14
  pinecone_env: str
@@ -18,20 +22,17 @@ class ReqModel:
18
  settings: Settings
19
 
20
  def __init__(self, data: dict):
21
- self.host_name = (
22
- DEFAULT_HOST_NAME if data["host_name"] == "" else data["host_name"]
23
- )
24
  self.openai_key = (
25
- DEFAULT_HOST_NAME if data["openai_key"] == "" else data["openai_key"]
26
  )
27
  self.pinecone_key = (
28
- DEFAULT_HOST_NAME if data["pinecone_key"] == "" else data["pinecone_key"]
29
  )
30
  self.pinecone_env = (
31
- DEFAULT_HOST_NAME if data["pinecone_env"] == "" else data["pinecone_env"]
32
  )
33
  self.firebase_key = (
34
- DEFAULT_HOST_NAME if data["firebase_key"] == "" else data["firebase_key"]
35
  )
36
  self.token = data["token"]
37
  self.uuid = data["uuid"]
@@ -40,7 +41,6 @@ class ReqModel:
40
 
41
  def to_json(self):
42
  return {
43
- "host_name": self.host_name,
44
  "openai_key": self.openai_key,
45
  "pinecone_key": self.pinecone_key,
46
  "pinecone_env": self.pinecone_env,
 
1
  from typing import Any
2
 
3
+ from Brain.src.common.utils import (
4
+ OPENAI_API_KEY,
5
+ PINECONE_KEY,
6
+ PINECONE_ENV,
7
+ FIREBASE_ENV,
8
+ )
9
 
10
 
11
  class ReqModel:
 
13
  def __init__(self):
14
  self.temperature: float = 0.6
15
 
 
16
  openai_key: str
17
  pinecone_key: str
18
  pinecone_env: str
 
22
  settings: Settings
23
 
24
  def __init__(self, data: dict):
 
 
 
25
  self.openai_key = (
26
+ OPENAI_API_KEY if data["openai_key"] == "" else data["openai_key"]
27
  )
28
  self.pinecone_key = (
29
+ PINECONE_KEY if data["pinecone_key"] == "" else data["pinecone_key"]
30
  )
31
  self.pinecone_env = (
32
+ PINECONE_ENV if data["pinecone_env"] == "" else data["pinecone_env"]
33
  )
34
  self.firebase_key = (
35
+ FIREBASE_ENV if data["firebase_key"] == "" else data["firebase_key"]
36
  )
37
  self.token = data["token"]
38
  self.uuid = data["uuid"]
 
41
 
42
  def to_json(self):
43
  return {
 
44
  "openai_key": self.openai_key,
45
  "pinecone_key": self.pinecone_key,
46
  "pinecone_env": self.pinecone_env,
Brain/src/model/requests/request_model.py CHANGED
@@ -70,7 +70,6 @@ class BasicReq(BaseModel):
70
  class Settings(BaseModel):
71
  temperature: float = 0.6
72
 
73
- host_name: str
74
  openai_key: str
75
  pinecone_key: str
76
  pinecone_env: str
@@ -81,7 +80,6 @@ class BasicReq(BaseModel):
81
 
82
  def to_json(self):
83
  return {
84
- "host_name": self.host_name,
85
  "openai_key": self.openai_key,
86
  "pinecone_key": self.pinecone_key,
87
  "pinecone_env": self.pinecone_env,
@@ -138,7 +136,6 @@ class ChatRising(BasicReq):
138
 
139
  history: list[Format]
140
  user_input: str
141
- model: str
142
 
143
 
144
  """endpoint: /send_sms"""
 
70
  class Settings(BaseModel):
71
  temperature: float = 0.6
72
 
 
73
  openai_key: str
74
  pinecone_key: str
75
  pinecone_env: str
 
80
 
81
  def to_json(self):
82
  return {
 
83
  "openai_key": self.openai_key,
84
  "pinecone_key": self.pinecone_key,
85
  "pinecone_env": self.pinecone_env,
 
136
 
137
  history: list[Format]
138
  user_input: str
 
139
 
140
 
141
  """endpoint: /send_sms"""
Brain/src/rising_plugin/guardrails-config/actions/actions.py CHANGED
@@ -104,7 +104,7 @@ async def general_question(query):
104
  if result["program"] == "image":
105
  if image_search:
106
  result["content"] = {
107
- "image_name": query_image_text(result["content"], "", setting.uuid)
108
  }
109
  """ 2. check program is message to handle it with falcon llm """
110
  if result["program"] == "message":
 
104
  if result["program"] == "image":
105
  if image_search:
106
  result["content"] = {
107
+ "image_name": query_image_text(result["content"], "", setting)
108
  }
109
  """ 2. check program is message to handle it with falcon llm """
110
  if result["program"] == "message":
Brain/src/rising_plugin/image_embedding.py CHANGED
@@ -9,6 +9,7 @@ from .pinecone_engine import (
9
  )
10
  from ..model.basic_model import DataStatus
11
  from ..model.image_model import ImageModel
 
12
 
13
 
14
  def get_embeddings():
@@ -16,7 +17,7 @@ def get_embeddings():
16
 
17
 
18
  # def embed_image_text(image_text: str, image_name: str, uuid: str) -> str:
19
- def embed_image_text(image: ImageModel) -> str:
20
  prompt_template = f"""
21
  This is the text about the image.
22
  ###
@@ -24,7 +25,7 @@ def embed_image_text(image: ImageModel) -> str:
24
  """
25
 
26
  embed_image = get_embeddings().embed_query(prompt_template)
27
- index = init_pinecone(PINECONE_INDEX_NAME)
28
 
29
  """create | update | delete in pinecone"""
30
  pinecone_namespace = get_pinecone_index_namespace(image.uuid)
@@ -46,16 +47,16 @@ def embed_image_text(image: ImageModel) -> str:
46
  return "success to embed image text"
47
 
48
 
49
- def query_image_text(image_content, message, uuid):
50
  embed_image = get_embeddings().embed_query(
51
  get_prompt_image_with_message(image_content, message)
52
  )
53
- index = init_pinecone(PINECONE_INDEX_NAME)
54
  relatedness_data = index.query(
55
  vector=embed_image,
56
  top_k=3,
57
  include_values=False,
58
- namespace=get_pinecone_index_namespace(uuid),
59
  )
60
  if len(relatedness_data["matches"]) > 0:
61
  return relatedness_data["matches"][0]["id"]
 
9
  )
10
  from ..model.basic_model import DataStatus
11
  from ..model.image_model import ImageModel
12
+ from ..model.req_model import ReqModel
13
 
14
 
15
  def get_embeddings():
 
17
 
18
 
19
  # def embed_image_text(image_text: str, image_name: str, uuid: str) -> str:
20
+ def embed_image_text(image: ImageModel, setting: ReqModel) -> str:
21
  prompt_template = f"""
22
  This is the text about the image.
23
  ###
 
25
  """
26
 
27
  embed_image = get_embeddings().embed_query(prompt_template)
28
+ index = init_pinecone(index_name=PINECONE_INDEX_NAME, setting=setting)
29
 
30
  """create | update | delete in pinecone"""
31
  pinecone_namespace = get_pinecone_index_namespace(image.uuid)
 
47
  return "success to embed image text"
48
 
49
 
50
+ def query_image_text(image_content, message, setting: ReqModel):
51
  embed_image = get_embeddings().embed_query(
52
  get_prompt_image_with_message(image_content, message)
53
  )
54
+ index = init_pinecone(index_name=PINECONE_INDEX_NAME, setting=setting)
55
  relatedness_data = index.query(
56
  vector=embed_image,
57
  top_k=3,
58
  include_values=False,
59
+ namespace=get_pinecone_index_namespace(setting.uuid),
60
  )
61
  if len(relatedness_data["matches"]) > 0:
62
  return relatedness_data["matches"][0]["id"]
Brain/src/rising_plugin/pinecone_engine.py CHANGED
@@ -1,6 +1,9 @@
1
  # initialize pinecone
2
  import pinecone
3
  from typing import Any
 
 
 
4
  from ..common.utils import (
5
  PINECONE_INDEX_NAME,
6
  PINECONE_NAMESPACE,
@@ -14,14 +17,17 @@ POD_TYPE = "p1.x1"
14
 
15
  # get the existing index in pinecone or create a new one
16
  def init_pinecone(index_name, setting: ReqModel, flag=True):
17
- pinecone.init(api_key=setting.pinecone_key, environment=setting.pinecone_env)
18
- if flag:
19
- return pinecone.Index(index_name)
20
- else:
21
- # create a new index in pinecone
22
- return pinecone.create_index(
23
- index_name, dimension=DIMENSION, metric=METRIC, pod_type=POD_TYPE
24
- )
 
 
 
25
 
26
 
27
  """add item in pinecone"""
 
1
  # initialize pinecone
2
  import pinecone
3
  from typing import Any
4
+
5
+ from ..common.brain_exception import BrainException
6
+ from ..common.http_response_codes import responses
7
  from ..common.utils import (
8
  PINECONE_INDEX_NAME,
9
  PINECONE_NAMESPACE,
 
17
 
18
  # get the existing index in pinecone or create a new one
19
  def init_pinecone(index_name, setting: ReqModel, flag=True):
20
+ try:
21
+ pinecone.init(api_key=setting.pinecone_key, environment=setting.pinecone_env)
22
+ if flag:
23
+ return pinecone.Index(index_name)
24
+ else:
25
+ # create a new index in pinecone
26
+ return pinecone.create_index(
27
+ index_name, dimension=DIMENSION, metric=METRIC, pod_type=POD_TYPE
28
+ )
29
+ except Exception as ex:
30
+ raise BrainException(code=508, message=responses[508])
31
 
32
 
33
  """add item in pinecone"""
Brain/src/rising_plugin/risingplugin.py CHANGED
@@ -21,6 +21,7 @@ from .csv_embed import get_embed
21
  from .llm.llms import get_llm, GPT_4, FALCON_7B
22
  from .pinecone_engine import init_pinecone
23
  from ..common.brain_exception import BrainException
 
24
  from ..common.utils import (
25
  OPENAI_API_KEY,
26
  FIREBASE_STORAGE_ROOT,
@@ -64,13 +65,15 @@ def llm_rails(
64
  """step 1: handle with gpt-4"""
65
 
66
  query_result = get_embed(query)
67
- relatedness_data = index.query(
68
- vector=query_result,
69
- top_k=1,
70
- include_values=False,
71
- namespace=train_service.get_pinecone_index_train_namespace(),
72
- )
73
-
 
 
74
  if len(relatedness_data["matches"]) == 0:
75
  return str({"program": "message", "content": ""})
76
  document_id = relatedness_data["matches"][0]["id"]
@@ -215,9 +218,9 @@ def query_image_ask(image_content, message, setting: ReqModel):
215
  return False
216
 
217
 
218
- def getTextFromImage(filename):
219
  # Create a reference to the image file you want to download
220
- bucket = storage.bucket()
221
  blob = bucket.blob(FIREBASE_STORAGE_ROOT.__add__(filename))
222
  download_url = ""
223
 
 
21
  from .llm.llms import get_llm, GPT_4, FALCON_7B
22
  from .pinecone_engine import init_pinecone
23
  from ..common.brain_exception import BrainException
24
+ from ..common.http_response_codes import responses
25
  from ..common.utils import (
26
  OPENAI_API_KEY,
27
  FIREBASE_STORAGE_ROOT,
 
65
  """step 1: handle with gpt-4"""
66
 
67
  query_result = get_embed(query)
68
+ try:
69
+ relatedness_data = index.query(
70
+ vector=query_result,
71
+ top_k=1,
72
+ include_values=False,
73
+ namespace=train_service.get_pinecone_index_train_namespace(),
74
+ )
75
+ except Exception as ex:
76
+ raise BrainException(code=508, message=responses[508])
77
  if len(relatedness_data["matches"]) == 0:
78
  return str({"program": "message", "content": ""})
79
  document_id = relatedness_data["matches"][0]["id"]
 
218
  return False
219
 
220
 
221
+ def getTextFromImage(filename: str, firebase_app: firebase_admin.App) -> str:
222
  # Create a reference to the image file you want to download
223
+ bucket = storage.bucket(app=firebase_app)
224
  blob = bucket.blob(FIREBASE_STORAGE_ROOT.__add__(filename))
225
  download_url = ""
226
 
Brain/src/router/api.py CHANGED
@@ -48,7 +48,6 @@ def construct_blueprint_api() -> APIRouter:
48
  # Service
49
 
50
  command_service = CommandService()
51
- contacts_service = ContactsService()
52
 
53
  """@generator.response(
54
  status_code=200, schema={"message": "message", "result": "test_result"}
@@ -62,7 +61,10 @@ def construct_blueprint_api() -> APIRouter:
62
  data: Notification, client_info: ClientInfo = Depends(get_client_info)
63
  ):
64
  # firebase admin init
65
- setting, firebase_app = firebase_admin_with_setting(data)
 
 
 
66
 
67
  # cloud message
68
  cloud_message = CloudMessage(firebase_app=firebase_app)
@@ -81,6 +83,7 @@ def construct_blueprint_api() -> APIRouter:
81
 
82
  # check contact querying
83
  try:
 
84
  if result["program"] == ProgramType.CONTACT:
85
  # querying contacts to getting its expected results
86
  contacts_results = contacts_service.query_contacts(
@@ -114,25 +117,33 @@ def construct_blueprint_api() -> APIRouter:
114
  @router.post("/uploadImage")
115
  def upload_image(data: UploadImage):
116
  # firebase admin init
117
- setting, firebase_app = firebase_admin_with_setting(data)
 
 
 
118
  # cloud message
119
- cloud_message = CloudMessage(firebase_app=firebase_app)
 
120
 
121
- image_model = ImageModel()
122
- token = data.token
123
 
124
- image_model.image_name = data.image_name
125
- image_model.uuid = data.uuid
126
- image_model.status = data.status
127
 
128
- image_model.image_text = getTextFromImage(image_model.image_name)
 
 
129
 
130
- embed_result = embed_image_text(image_model)
131
 
132
- notification = {"title": "alert", "content": embed_result}
133
 
134
- state, value = cloud_message.send_message(notification, [token])
135
- return assembler.to_response(200, value, image_model.to_json())
 
 
136
 
137
  """@generator.response(
138
  status_code=200, schema={"message": "message", "result": "test_result"}
@@ -149,7 +160,10 @@ def construct_blueprint_api() -> APIRouter:
149
  @router.post("/image_relatedness")
150
  def image_relatedness(data: ImageRelatedness):
151
  # firebase admin init
152
- setting, firebase_app = firebase_admin_with_setting(data)
 
 
 
153
 
154
  # cloud message
155
  cloud_message = CloudMessage(firebase_app=firebase_app)
@@ -159,7 +173,7 @@ def construct_blueprint_api() -> APIRouter:
159
  token = data.token
160
  uuid = data.uuid
161
 
162
- image_content = getTextFromImage(image_name)
163
  # check message type
164
  image_response = {}
165
  try:
@@ -169,11 +183,13 @@ def construct_blueprint_api() -> APIRouter:
169
  ):
170
  image_response["image_desc"] = image_content
171
  else:
172
- relatedness_data = query_image_text(image_content, message, uuid)
173
 
174
  image_response["image_name"] = relatedness_data
175
  except ValueError as e:
176
  print("image_relatedness parsing error for message chain data")
 
 
177
 
178
  notification = {"title": "alert", "content": json.dumps(image_response)}
179
  state, value = cloud_message.send_message(notification, [token])
@@ -222,6 +238,8 @@ def construct_blueprint_api() -> APIRouter:
222
  # add the feedback
223
  feedback_service.add(feedback)
224
  except Exception as e:
 
 
225
  return assembler.to_response(400, "failed to add", "")
226
  return assembler.to_response(200, "added successfully", "")
227
 
@@ -232,7 +250,10 @@ def construct_blueprint_api() -> APIRouter:
232
  @router.post("/feedback/{search}/{rating}")
233
  def get_feedback(search: str, rating: int, data: BasicReq):
234
  # firebase admin init
235
- setting, firebase_app = firebase_admin_with_setting(data)
 
 
 
236
 
237
  # cloud message
238
  cloud_message = CloudMessage(firebase_app=firebase_app)
@@ -274,7 +295,10 @@ def construct_blueprint_api() -> APIRouter:
274
  @router.post("/chat_rising")
275
  def message_agent(data: ChatRising):
276
  # firebase admin init
277
- setting, firebase_app = firebase_admin_with_setting(data)
 
 
 
278
 
279
  # cloud message
280
  cloud_message = CloudMessage(firebase_app=firebase_app)
@@ -323,7 +347,10 @@ def construct_blueprint_api() -> APIRouter:
323
  @router.post("/send_sms")
324
  def send_sms(data: SendSMS):
325
  # firebase admin init
326
- setting, firebase_app = firebase_admin_with_setting(data)
 
 
 
327
  try:
328
  token = data.token
329
  uuid = data.uuid
@@ -358,7 +385,10 @@ def construct_blueprint_api() -> APIRouter:
358
  @router.post("/train/contacts")
359
  def train_contacts(data: TrainContacts):
360
  # firebase admin init
361
- setting, firebase_app = firebase_admin_with_setting(data)
 
 
 
362
  try:
363
  token = data.token
364
  uuid = data.uuid
@@ -368,8 +398,11 @@ def construct_blueprint_api() -> APIRouter:
368
  for contact in data.contacts:
369
  contacts.append(assembler.to_contact_model(contact))
370
  # train contact
 
371
  contacts_service.train(uuid, contacts)
372
  except Exception as e:
 
 
373
  return assembler.to_response(400, "Failed to train contacts", "")
374
  return assembler.to_response(200, "Trained successfully", "")
375
 
@@ -386,15 +419,21 @@ def construct_blueprint_api() -> APIRouter:
386
  @router.post("/train/contacts/delete")
387
  def delete_all_contacts(data: BasicReq):
388
  # firebase admin init
389
- setting, firebase_app = firebase_admin_with_setting(data)
 
 
 
390
  try:
391
  token = data.token
392
  uuid = data.uuid
393
 
394
  # parsing contacts
395
  # train contact
 
396
  contacts_service.delete_all(uuid)
397
  except Exception as e:
 
 
398
  return assembler.to_response(400, "Failed to delete contacts", "")
399
  return assembler.to_response(
400
  200, "Deleted all contacts from pinecone successfully", ""
 
48
  # Service
49
 
50
  command_service = CommandService()
 
51
 
52
  """@generator.response(
53
  status_code=200, schema={"message": "message", "result": "test_result"}
 
61
  data: Notification, client_info: ClientInfo = Depends(get_client_info)
62
  ):
63
  # firebase admin init
64
+ try:
65
+ setting, firebase_app = firebase_admin_with_setting(data)
66
+ except BrainException as ex:
67
+ return ex.get_response_exp()
68
 
69
  # cloud message
70
  cloud_message = CloudMessage(firebase_app=firebase_app)
 
83
 
84
  # check contact querying
85
  try:
86
+ contacts_service = ContactsService(setting=setting)
87
  if result["program"] == ProgramType.CONTACT:
88
  # querying contacts to getting its expected results
89
  contacts_results = contacts_service.query_contacts(
 
117
  @router.post("/uploadImage")
118
  def upload_image(data: UploadImage):
119
  # firebase admin init
120
+ try:
121
+ setting, firebase_app = firebase_admin_with_setting(data)
122
+ except BrainException as ex:
123
+ return ex.get_response_exp()
124
  # cloud message
125
+ try:
126
+ cloud_message = CloudMessage(firebase_app=firebase_app)
127
 
128
+ image_model = ImageModel()
129
+ token = data.token
130
 
131
+ image_model.image_name = data.image_name
132
+ image_model.uuid = data.uuid
133
+ image_model.status = data.status
134
 
135
+ image_model.image_text = getTextFromImage(
136
+ filename=image_model.image_name, firebase_app=firebase_app
137
+ )
138
 
139
+ embed_result = embed_image_text(image=image_model, setting=setting)
140
 
141
+ notification = {"title": "alert", "content": embed_result}
142
 
143
+ state, value = cloud_message.send_message(notification, [token])
144
+ return assembler.to_response(200, value, image_model.to_json())
145
+ except BrainException as ex:
146
+ return ex.get_response_exp()
147
 
148
  """@generator.response(
149
  status_code=200, schema={"message": "message", "result": "test_result"}
 
160
  @router.post("/image_relatedness")
161
  def image_relatedness(data: ImageRelatedness):
162
  # firebase admin init
163
+ try:
164
+ setting, firebase_app = firebase_admin_with_setting(data)
165
+ except BrainException as ex:
166
+ return ex.get_response_exp()
167
 
168
  # cloud message
169
  cloud_message = CloudMessage(firebase_app=firebase_app)
 
173
  token = data.token
174
  uuid = data.uuid
175
 
176
+ image_content = getTextFromImage(filename=image_name, firebase_app=firebase_app)
177
  # check message type
178
  image_response = {}
179
  try:
 
183
  ):
184
  image_response["image_desc"] = image_content
185
  else:
186
+ relatedness_data = query_image_text(image_content, message, setting)
187
 
188
  image_response["image_name"] = relatedness_data
189
  except ValueError as e:
190
  print("image_relatedness parsing error for message chain data")
191
+ if isinstance(e, BrainException):
192
+ return e.get_response_exp()
193
 
194
  notification = {"title": "alert", "content": json.dumps(image_response)}
195
  state, value = cloud_message.send_message(notification, [token])
 
238
  # add the feedback
239
  feedback_service.add(feedback)
240
  except Exception as e:
241
+ if isinstance(e, BrainException):
242
+ return e.get_response_exp()
243
  return assembler.to_response(400, "failed to add", "")
244
  return assembler.to_response(200, "added successfully", "")
245
 
 
250
  @router.post("/feedback/{search}/{rating}")
251
  def get_feedback(search: str, rating: int, data: BasicReq):
252
  # firebase admin init
253
+ try:
254
+ setting, firebase_app = firebase_admin_with_setting(data)
255
+ except BrainException as ex:
256
+ return ex.get_response_exp()
257
 
258
  # cloud message
259
  cloud_message = CloudMessage(firebase_app=firebase_app)
 
295
  @router.post("/chat_rising")
296
  def message_agent(data: ChatRising):
297
  # firebase admin init
298
+ try:
299
+ setting, firebase_app = firebase_admin_with_setting(data)
300
+ except BrainException as ex:
301
+ return ex.get_response_exp()
302
 
303
  # cloud message
304
  cloud_message = CloudMessage(firebase_app=firebase_app)
 
347
  @router.post("/send_sms")
348
  def send_sms(data: SendSMS):
349
  # firebase admin init
350
+ try:
351
+ setting, firebase_app = firebase_admin_with_setting(data)
352
+ except BrainException as ex:
353
+ return assembler.to_response(ex.code, ex.message, "")
354
  try:
355
  token = data.token
356
  uuid = data.uuid
 
385
  @router.post("/train/contacts")
386
  def train_contacts(data: TrainContacts):
387
  # firebase admin init
388
+ try:
389
+ setting, firebase_app = firebase_admin_with_setting(data)
390
+ except BrainException as ex:
391
+ return assembler.to_response(ex.code, ex.message, "")
392
  try:
393
  token = data.token
394
  uuid = data.uuid
 
398
  for contact in data.contacts:
399
  contacts.append(assembler.to_contact_model(contact))
400
  # train contact
401
+ contacts_service = ContactsService(setting=setting)
402
  contacts_service.train(uuid, contacts)
403
  except Exception as e:
404
+ if isinstance(e, BrainException):
405
+ return e.get_response_exp()
406
  return assembler.to_response(400, "Failed to train contacts", "")
407
  return assembler.to_response(200, "Trained successfully", "")
408
 
 
419
  @router.post("/train/contacts/delete")
420
  def delete_all_contacts(data: BasicReq):
421
  # firebase admin init
422
+ try:
423
+ setting, firebase_app = firebase_admin_with_setting(data)
424
+ except BrainException as ex:
425
+ return assembler.to_response(ex.code, ex.message, "")
426
  try:
427
  token = data.token
428
  uuid = data.uuid
429
 
430
  # parsing contacts
431
  # train contact
432
+ contacts_service = ContactsService(setting=setting)
433
  contacts_service.delete_all(uuid)
434
  except Exception as e:
435
+ if isinstance(e, BrainException):
436
+ return e.get_response_exp()
437
  return assembler.to_response(400, "Failed to delete contacts", "")
438
  return assembler.to_response(
439
  200, "Deleted all contacts from pinecone successfully", ""
Brain/src/router/browser_router.py CHANGED
@@ -1,6 +1,7 @@
1
  from fastapi import APIRouter, Request, Depends
2
 
3
  from Brain.src.common.assembler import Assembler
 
4
  from Brain.src.common.program_type import ProgramType
5
  from Brain.src.common.utils import parseUrlFromStr
6
  from Brain.src.firebase.firebase import firebase_admin_with_setting
@@ -30,7 +31,10 @@ def construct_blueprint_browser_api() -> APIRouter:
30
  @router.post("/item")
31
  def get_item(data: BrowserItem):
32
  # firebase admin init
33
- setting, firebase_app = firebase_admin_with_setting(data)
 
 
 
34
 
35
  item_link = ""
36
  try:
@@ -41,6 +45,8 @@ def construct_blueprint_browser_api() -> APIRouter:
41
  # train contact
42
  item_link = browser_service.query_item(items=data.items, query=data.prompt)
43
  except Exception as e:
 
 
44
  return assembler.to_response(400, "Failed to get item in a browser", "")
45
  return assembler.to_response(
46
  200,
 
1
  from fastapi import APIRouter, Request, Depends
2
 
3
  from Brain.src.common.assembler import Assembler
4
+ from Brain.src.common.brain_exception import BrainException
5
  from Brain.src.common.program_type import ProgramType
6
  from Brain.src.common.utils import parseUrlFromStr
7
  from Brain.src.firebase.firebase import firebase_admin_with_setting
 
31
  @router.post("/item")
32
  def get_item(data: BrowserItem):
33
  # firebase admin init
34
+ try:
35
+ setting, firebase_app = firebase_admin_with_setting(data)
36
+ except BrainException as ex:
37
+ return ex.get_response_exp()
38
 
39
  item_link = ""
40
  try:
 
45
  # train contact
46
  item_link = browser_service.query_item(items=data.items, query=data.prompt)
47
  except Exception as e:
48
+ if isinstance(e, BrainException):
49
+ return e.get_response_exp()
50
  return assembler.to_response(400, "Failed to get item in a browser", "")
51
  return assembler.to_response(
52
  200,
Brain/src/router/train_router.py CHANGED
@@ -1,6 +1,7 @@
1
  from fastapi import APIRouter
2
 
3
  from Brain.src.common.assembler import Assembler
 
4
  from Brain.src.firebase.firebase import firebase_admin_with_setting
5
  from Brain.src.model.requests.request_model import (
6
  Document,
@@ -22,9 +23,12 @@ def construct_blueprint_train_api() -> APIRouter:
22
  @router.get("")
23
  def read_all_documents(data: BasicReq):
24
  # parsing params
25
- setting, firebase_app = firebase_admin_with_setting(data)
 
 
 
26
  # Services
27
- train_service = TrainService(firebase_app=firebase_app)
28
  try:
29
  result = train_service.read_all_documents()
30
  except Exception as e:
@@ -37,9 +41,12 @@ def construct_blueprint_train_api() -> APIRouter:
37
  @router.post("/all")
38
  def train_all_documents(data: BasicReq):
39
  # parsing params
40
- setting, firebase_app = firebase_admin_with_setting(data)
 
 
 
41
  # Services
42
- train_service = TrainService(firebase_app=firebase_app)
43
  try:
44
  result = train_service.train_all_documents()
45
  except Exception as e:
@@ -52,9 +59,12 @@ def construct_blueprint_train_api() -> APIRouter:
52
  @router.post("/{document_id}")
53
  def read_one_document(document_id: str, data: BasicReq):
54
  # parsing params
55
- setting, firebase_app = firebase_admin_with_setting(data)
 
 
 
56
  # Services
57
- train_service = TrainService(firebase_app=firebase_app)
58
  if document_id != "all":
59
  try:
60
  result = train_service.read_one_document(document_id)
@@ -75,9 +85,12 @@ def construct_blueprint_train_api() -> APIRouter:
75
  @router.post("")
76
  def create_document_train(data: Document):
77
  # parsing params
78
- setting, firebase_app = firebase_admin_with_setting(data)
 
 
 
79
  # Services
80
- train_service = TrainService(firebase_app=firebase_app)
81
  try:
82
  result = train_service.create_one_document(data.page_content)
83
  except Exception as e:
@@ -100,9 +113,12 @@ def construct_blueprint_train_api() -> APIRouter:
100
  @router.put("")
101
  def update_one_document(data: Document):
102
  # parsing params
103
- setting, firebase_app = firebase_admin_with_setting(data)
 
 
 
104
  # Services
105
- train_service = TrainService(firebase_app=firebase_app)
106
  try:
107
  result = train_service.update_one_document(
108
  data.document_id, data.page_content
@@ -125,12 +141,17 @@ def construct_blueprint_train_api() -> APIRouter:
125
  @router.post("/delete/{document_id}")
126
  def delete_one_document(document_id: str, data: BasicReq):
127
  # parsing params
128
- setting, firebase_app = firebase_admin_with_setting(data)
 
 
 
129
  # Services
130
- train_service = TrainService(firebase_app=firebase_app)
131
  try:
132
  result = train_service.delete_one_document(document_id)
133
  except Exception as e:
 
 
134
  return assembler.to_response(400, "fail to delete one train", "")
135
  return assembler.to_response(
136
  200, "deleted one document and train data successfully", result
 
1
  from fastapi import APIRouter
2
 
3
  from Brain.src.common.assembler import Assembler
4
+ from Brain.src.common.brain_exception import BrainException
5
  from Brain.src.firebase.firebase import firebase_admin_with_setting
6
  from Brain.src.model.requests.request_model import (
7
  Document,
 
23
  @router.get("")
24
  def read_all_documents(data: BasicReq):
25
  # parsing params
26
+ try:
27
+ setting, firebase_app = firebase_admin_with_setting(data)
28
+ except BrainException as ex:
29
+ return ex.get_response_exp()
30
  # Services
31
+ train_service = TrainService(firebase_app=firebase_app, setting=setting)
32
  try:
33
  result = train_service.read_all_documents()
34
  except Exception as e:
 
41
  @router.post("/all")
42
  def train_all_documents(data: BasicReq):
43
  # parsing params
44
+ try:
45
+ setting, firebase_app = firebase_admin_with_setting(data)
46
+ except BrainException as ex:
47
+ return ex.get_response_exp()
48
  # Services
49
+ train_service = TrainService(firebase_app=firebase_app, setting=setting)
50
  try:
51
  result = train_service.train_all_documents()
52
  except Exception as e:
 
59
  @router.post("/{document_id}")
60
  def read_one_document(document_id: str, data: BasicReq):
61
  # parsing params
62
+ try:
63
+ setting, firebase_app = firebase_admin_with_setting(data)
64
+ except BrainException as ex:
65
+ return ex.get_response_exp()
66
  # Services
67
+ train_service = TrainService(firebase_app=firebase_app, setting=setting)
68
  if document_id != "all":
69
  try:
70
  result = train_service.read_one_document(document_id)
 
85
  @router.post("")
86
  def create_document_train(data: Document):
87
  # parsing params
88
+ try:
89
+ setting, firebase_app = firebase_admin_with_setting(data)
90
+ except BrainException as ex:
91
+ return ex.get_response_exp()
92
  # Services
93
+ train_service = TrainService(firebase_app=firebase_app, setting=setting)
94
  try:
95
  result = train_service.create_one_document(data.page_content)
96
  except Exception as e:
 
113
  @router.put("")
114
  def update_one_document(data: Document):
115
  # parsing params
116
+ try:
117
+ setting, firebase_app = firebase_admin_with_setting(data)
118
+ except BrainException as ex:
119
+ return ex.get_response_exp()
120
  # Services
121
+ train_service = TrainService(firebase_app=firebase_app, setting=setting)
122
  try:
123
  result = train_service.update_one_document(
124
  data.document_id, data.page_content
 
141
  @router.post("/delete/{document_id}")
142
  def delete_one_document(document_id: str, data: BasicReq):
143
  # parsing params
144
+ try:
145
+ setting, firebase_app = firebase_admin_with_setting(data)
146
+ except BrainException as ex:
147
+ return ex.get_response_exp()
148
  # Services
149
+ train_service = TrainService(firebase_app=firebase_app, setting=setting)
150
  try:
151
  result = train_service.delete_one_document(document_id)
152
  except Exception as e:
153
+ if isinstance(e, BrainException):
154
+ return e.get_response_exp()
155
  return assembler.to_response(400, "fail to delete one train", "")
156
  return assembler.to_response(
157
  200, "deleted one document and train data successfully", result
Brain/src/service/contact_service.py CHANGED
@@ -1,6 +1,7 @@
1
  """service to manage contacts"""
2
  from typing import List, Any
3
 
 
4
  from Brain.src.rising_plugin.csv_embed import get_embed
5
  from Brain.src.rising_plugin.pinecone_engine import (
6
  get_pinecone_index_namespace,
@@ -16,6 +17,9 @@ from Brain.src.model.contact_model import ContactModel, ContactStatus
16
 
17
 
18
  class ContactsService:
 
 
 
19
  """train contacts (getting embedding) and update pinecone with embeddings by contact_id
20
  train datatype:
21
  key: contactId
@@ -33,13 +37,23 @@ class ContactsService:
33
  # create | update | delete pinecone
34
  if contact.status == ContactStatus.CREATED:
35
  add_pinecone(
36
- namespace=pinecone_namespace, key=key, value=vectoring_values
 
 
 
37
  )
38
  elif contact.status == ContactStatus.DELETED:
39
- delete_pinecone(namespace=pinecone_namespace, key=key)
 
 
 
 
40
  elif contact.status == ContactStatus.UPDATED:
41
  update_pinecone(
42
- namespace=pinecone_namespace, key=key, value=vectoring_values
 
 
 
43
  )
44
 
45
  """"query contact with search text
@@ -47,7 +61,7 @@ class ContactsService:
47
 
48
  def query_contacts(self, uuid: str, search: str) -> List[str]:
49
  vector_data = get_embed(search)
50
- index = init_pinecone(PINECONE_INDEX_NAME)
51
  relatedness_data = index.query(
52
  vector=vector_data,
53
  top_k=5,
@@ -63,7 +77,9 @@ class ContactsService:
63
  """delete all items in the specific nanespace"""
64
 
65
  def delete_all(self, uuid: str) -> Any:
66
- return delete_all_pinecone(self.get_pinecone_index_namespace(uuid))
 
 
67
 
68
  """get pinecone namespace of pinecone"""
69
 
 
1
  """service to manage contacts"""
2
  from typing import List, Any
3
 
4
+ from Brain.src.model.req_model import ReqModel
5
  from Brain.src.rising_plugin.csv_embed import get_embed
6
  from Brain.src.rising_plugin.pinecone_engine import (
7
  get_pinecone_index_namespace,
 
17
 
18
 
19
  class ContactsService:
20
+ def __init__(self, setting: ReqModel):
21
+ self.setting = setting
22
+
23
  """train contacts (getting embedding) and update pinecone with embeddings by contact_id
24
  train datatype:
25
  key: contactId
 
37
  # create | update | delete pinecone
38
  if contact.status == ContactStatus.CREATED:
39
  add_pinecone(
40
+ namespace=pinecone_namespace,
41
+ key=key,
42
+ value=vectoring_values,
43
+ setting=self.setting,
44
  )
45
  elif contact.status == ContactStatus.DELETED:
46
+ delete_pinecone(
47
+ namespace=pinecone_namespace,
48
+ key=key,
49
+ setting=self.setting,
50
+ )
51
  elif contact.status == ContactStatus.UPDATED:
52
  update_pinecone(
53
+ namespace=pinecone_namespace,
54
+ key=key,
55
+ value=vectoring_values,
56
+ setting=self.setting,
57
  )
58
 
59
  """"query contact with search text
 
61
 
62
  def query_contacts(self, uuid: str, search: str) -> List[str]:
63
  vector_data = get_embed(search)
64
+ index = init_pinecone(index_name=PINECONE_INDEX_NAME, setting=self.setting)
65
  relatedness_data = index.query(
66
  vector=vector_data,
67
  top_k=5,
 
77
  """delete all items in the specific nanespace"""
78
 
79
  def delete_all(self, uuid: str) -> Any:
80
+ return delete_all_pinecone(
81
+ namespace=self.get_pinecone_index_namespace(uuid), setting=self.setting
82
+ )
83
 
84
  """get pinecone namespace of pinecone"""
85