Kotta commited on
Commit ·
74f1630
1
Parent(s): a27c5a5
feature(#9): updated sendNotificaiton with its configuration.
Browse files- Brain/src/common/assembler.py +12 -1
- Brain/src/common/utils.py +2 -1
- Brain/src/model/req_model.py +51 -0
- Brain/src/model/requests/request_model.py +47 -3
- Brain/src/rising_plugin/guardrails-config/actions/actions.py +34 -15
- Brain/src/rising_plugin/llm/gpt_llm.py +4 -3
- Brain/src/rising_plugin/llm/llms.py +15 -5
- Brain/src/rising_plugin/risingplugin.py +35 -13
- Brain/src/router/api.py +6 -2
Brain/src/common/assembler.py
CHANGED
|
@@ -5,7 +5,13 @@ from Brain.src.common.http_response_codes import responses
|
|
| 5 |
from Brain.src.model.basic_model import BasicModel
|
| 6 |
from Brain.src.model.contact_model import ContactModel
|
| 7 |
from Brain.src.model.message_model import MessageModel
|
| 8 |
-
from Brain.src.model.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
from Brain.src.model.sms_model import SMSModel
|
| 10 |
|
| 11 |
|
|
@@ -58,3 +64,8 @@ class Assembler:
|
|
| 58 |
|
| 59 |
def to_result_format(self, program: str, content: str) -> Any:
|
| 60 |
return {"program": program, "content": content}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from Brain.src.model.basic_model import BasicModel
|
| 6 |
from Brain.src.model.contact_model import ContactModel
|
| 7 |
from Brain.src.model.message_model import MessageModel
|
| 8 |
+
from Brain.src.model.req_model import ReqModel
|
| 9 |
+
from Brain.src.model.requests.request_model import (
|
| 10 |
+
ChatRising,
|
| 11 |
+
SendSMS,
|
| 12 |
+
TrainContacts,
|
| 13 |
+
BasicReq,
|
| 14 |
+
)
|
| 15 |
from Brain.src.model.sms_model import SMSModel
|
| 16 |
|
| 17 |
|
|
|
|
| 64 |
|
| 65 |
def to_result_format(self, program: str, content: str) -> Any:
|
| 66 |
return {"program": program, "content": content}
|
| 67 |
+
|
| 68 |
+
"""mapping basic req"""
|
| 69 |
+
|
| 70 |
+
def to_req_model(self, basic_req: BasicReq) -> ReqModel:
|
| 71 |
+
return ReqModel(basic_req.to_json())
|
Brain/src/common/utils.py
CHANGED
|
@@ -5,6 +5,7 @@ import re
|
|
| 5 |
from firebase_admin import credentials
|
| 6 |
|
| 7 |
# env variables
|
|
|
|
| 8 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 9 |
PINECONE_KEY = os.getenv("PINECONE_KEY")
|
| 10 |
PINECONE_ENV = os.getenv("PINECONE_ENV")
|
|
@@ -17,7 +18,7 @@ API_URL = "http://localhost:5000/file/swagger.json"
|
|
| 17 |
|
| 18 |
# firebase
|
| 19 |
FIREBASE_STORAGE_ROOT = "images/"
|
| 20 |
-
FIREBASE_STORAGE_BUCKET =
|
| 21 |
|
| 22 |
# pinecone
|
| 23 |
PINECONE_NAMESPACE = "risinglangchain-namespace"
|
|
|
|
| 5 |
from firebase_admin import credentials
|
| 6 |
|
| 7 |
# env variables
|
| 8 |
+
DEFAULT_HOST_NAME = "test3-83ffc.appspot.com"
|
| 9 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 10 |
PINECONE_KEY = os.getenv("PINECONE_KEY")
|
| 11 |
PINECONE_ENV = os.getenv("PINECONE_ENV")
|
|
|
|
| 18 |
|
| 19 |
# firebase
|
| 20 |
FIREBASE_STORAGE_ROOT = "images/"
|
| 21 |
+
FIREBASE_STORAGE_BUCKET = DEFAULT_HOST_NAME
|
| 22 |
|
| 23 |
# pinecone
|
| 24 |
PINECONE_NAMESPACE = "risinglangchain-namespace"
|
Brain/src/model/req_model.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any
|
| 2 |
+
|
| 3 |
+
from Brain.src.common.utils import DEFAULT_HOST_NAME
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class ReqModel:
|
| 7 |
+
class Settings:
|
| 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
|
| 15 |
+
firebase_key: str
|
| 16 |
+
token: str = ""
|
| 17 |
+
uuid: str = ""
|
| 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"]
|
| 38 |
+
self.settings = self.Settings()
|
| 39 |
+
self.settings.temperature = data["settings"]["temperature"]
|
| 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,
|
| 47 |
+
"firebase_key": self.firebase_key,
|
| 48 |
+
"settings": {"temperature": self.settings.temperature},
|
| 49 |
+
"token": self.token,
|
| 50 |
+
"uuid": self.uuid,
|
| 51 |
+
}
|
Brain/src/model/requests/request_model.py
CHANGED
|
@@ -4,6 +4,14 @@ from pydantic import BaseModel
|
|
| 4 |
from fastapi import Depends, Request, HTTPException
|
| 5 |
from user_agents import parse
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
"""user-agent management"""
|
| 8 |
|
| 9 |
|
|
@@ -50,10 +58,46 @@ def get_client_info(request: Request):
|
|
| 50 |
return parse_user_agent(user_agent)
|
| 51 |
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
class BasicReq(BaseModel):
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
"""endpoint: /sendNotification"""
|
|
|
|
| 4 |
from fastapi import Depends, Request, HTTPException
|
| 5 |
from user_agents import parse
|
| 6 |
|
| 7 |
+
from Brain.src.common.utils import (
|
| 8 |
+
DEFAULT_HOST_NAME,
|
| 9 |
+
OPENAI_API_KEY,
|
| 10 |
+
PINECONE_KEY,
|
| 11 |
+
FIREBASE_ENV,
|
| 12 |
+
PINECONE_ENV,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
"""user-agent management"""
|
| 16 |
|
| 17 |
|
|
|
|
| 58 |
return parse_user_agent(user_agent)
|
| 59 |
|
| 60 |
|
| 61 |
+
"""
|
| 62 |
+
{
|
| 63 |
+
"host_name": string,
|
| 64 |
+
"openai_key": string,
|
| 65 |
+
"pinecone_key": string,
|
| 66 |
+
"pinecone_env": string,
|
| 67 |
+
"firebase_key": string,
|
| 68 |
+
"settings": {
|
| 69 |
+
"temperature": float
|
| 70 |
+
},
|
| 71 |
+
"token": string,
|
| 72 |
+
"uuid": string,
|
| 73 |
+
}
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
|
| 77 |
class BasicReq(BaseModel):
|
| 78 |
+
class Settings(BaseModel):
|
| 79 |
+
temperature: float = 0.6
|
| 80 |
+
|
| 81 |
+
host_name: str
|
| 82 |
+
openai_key: str
|
| 83 |
+
pinecone_key: str
|
| 84 |
+
pinecone_env: str
|
| 85 |
+
firebase_key: str
|
| 86 |
+
token: str = ""
|
| 87 |
+
uuid: str = ""
|
| 88 |
+
settings: Settings
|
| 89 |
+
|
| 90 |
+
def to_json(self):
|
| 91 |
+
return {
|
| 92 |
+
"host_name": self.host_name,
|
| 93 |
+
"openai_key": self.openai_key,
|
| 94 |
+
"pinecone_key": self.pinecone_key,
|
| 95 |
+
"pinecone_env": self.pinecone_env,
|
| 96 |
+
"firebase_key": self.firebase_key,
|
| 97 |
+
"settings": {"temperature": self.settings.temperature},
|
| 98 |
+
"token": self.token,
|
| 99 |
+
"uuid": self.uuid,
|
| 100 |
+
}
|
| 101 |
|
| 102 |
|
| 103 |
"""endpoint: /sendNotification"""
|
Brain/src/rising_plugin/guardrails-config/actions/actions.py
CHANGED
|
@@ -27,7 +27,10 @@ from Brain.src.common.utils import (
|
|
| 27 |
OPENAI_API_KEY,
|
| 28 |
COMMAND_SMS_INDEXS,
|
| 29 |
COMMAND_BROWSER_OPEN,
|
|
|
|
| 30 |
)
|
|
|
|
|
|
|
| 31 |
from Brain.src.rising_plugin.image_embedding import (
|
| 32 |
query_image_text,
|
| 33 |
)
|
|
@@ -42,6 +45,7 @@ from Brain.src.rising_plugin.llm.llms import (
|
|
| 42 |
GPT_4_32K,
|
| 43 |
GPT_4,
|
| 44 |
FALCON_7B,
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
"""
|
|
@@ -51,12 +55,27 @@ query is json string with below format
|
|
| 51 |
"model": string,
|
| 52 |
"uuid": string,
|
| 53 |
"image_search": bool,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
}
|
| 55 |
"""
|
| 56 |
|
| 57 |
|
| 58 |
@action()
|
| 59 |
async def general_question(query):
|
|
|
|
|
|
|
| 60 |
"""step 0: convert string to json"""
|
| 61 |
try:
|
| 62 |
json_query = json.loads(query)
|
|
@@ -67,13 +86,14 @@ async def general_question(query):
|
|
| 67 |
model = json_query["model"]
|
| 68 |
uuid = json_query["uuid"]
|
| 69 |
image_search = json_query["image_search"]
|
|
|
|
| 70 |
|
| 71 |
"""step 1: handle with gpt-4"""
|
| 72 |
file_path = os.path.dirname(os.path.abspath(__file__))
|
| 73 |
|
| 74 |
with open(f"{file_path}/phone.json", "r") as infile:
|
| 75 |
data = json.load(infile)
|
| 76 |
-
embeddings = OpenAIEmbeddings(openai_api_key=
|
| 77 |
|
| 78 |
query_result = embeddings.embed_query(query)
|
| 79 |
doc_list = utils.maximal_marginal_relevance(np.array(query_result), data, k=1)
|
|
@@ -89,15 +109,15 @@ async def general_question(query):
|
|
| 89 |
)
|
| 90 |
)
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
try:
|
| 102 |
result = json.loads(chain_data)
|
| 103 |
# check image query with only its text
|
|
@@ -106,12 +126,10 @@ async def general_question(query):
|
|
| 106 |
result["content"] = {
|
| 107 |
"image_name": query_image_text(result["content"], "", uuid)
|
| 108 |
}
|
| 109 |
-
|
| 110 |
-
# else:
|
| 111 |
-
# return result
|
| 112 |
-
"""check program is message to handle it with falcon llm"""
|
| 113 |
if result["program"] == "message":
|
| 114 |
-
|
|
|
|
| 115 |
return str(result)
|
| 116 |
except ValueError as e:
|
| 117 |
# Check sms and browser query
|
|
@@ -119,4 +137,5 @@ async def general_question(query):
|
|
| 119 |
return str({"program": "sms", "content": chain_data})
|
| 120 |
elif doc_list[0] in COMMAND_BROWSER_OPEN:
|
| 121 |
return str({"program": "browser", "content": "https://google.com"})
|
|
|
|
| 122 |
return str({"program": "message", "content": falcon_llm.query(question=query)})
|
|
|
|
| 27 |
OPENAI_API_KEY,
|
| 28 |
COMMAND_SMS_INDEXS,
|
| 29 |
COMMAND_BROWSER_OPEN,
|
| 30 |
+
DEFAULT_GPT_MODEL,
|
| 31 |
)
|
| 32 |
+
from Brain.src.model.req_model import ReqModel
|
| 33 |
+
from Brain.src.model.requests.request_model import BasicReq
|
| 34 |
from Brain.src.rising_plugin.image_embedding import (
|
| 35 |
query_image_text,
|
| 36 |
)
|
|
|
|
| 45 |
GPT_4_32K,
|
| 46 |
GPT_4,
|
| 47 |
FALCON_7B,
|
| 48 |
+
GPT_LLM_MODELS,
|
| 49 |
)
|
| 50 |
|
| 51 |
"""
|
|
|
|
| 55 |
"model": string,
|
| 56 |
"uuid": string,
|
| 57 |
"image_search": bool,
|
| 58 |
+
"setting": {
|
| 59 |
+
"host_name": string,
|
| 60 |
+
"openai_key": string,
|
| 61 |
+
"pinecone_key": string,
|
| 62 |
+
"pinecone_env": string,
|
| 63 |
+
"firebase_key": string,
|
| 64 |
+
"firebase_env": string
|
| 65 |
+
"settings": {
|
| 66 |
+
"temperature": float
|
| 67 |
+
},
|
| 68 |
+
"token": string,
|
| 69 |
+
"uuid": string,
|
| 70 |
+
}
|
| 71 |
}
|
| 72 |
"""
|
| 73 |
|
| 74 |
|
| 75 |
@action()
|
| 76 |
async def general_question(query):
|
| 77 |
+
"""init falcon model"""
|
| 78 |
+
falcon_llm = FalconLLM()
|
| 79 |
"""step 0: convert string to json"""
|
| 80 |
try:
|
| 81 |
json_query = json.loads(query)
|
|
|
|
| 86 |
model = json_query["model"]
|
| 87 |
uuid = json_query["uuid"]
|
| 88 |
image_search = json_query["image_search"]
|
| 89 |
+
setting = ReqModel(json_query["setting"])
|
| 90 |
|
| 91 |
"""step 1: handle with gpt-4"""
|
| 92 |
file_path = os.path.dirname(os.path.abspath(__file__))
|
| 93 |
|
| 94 |
with open(f"{file_path}/phone.json", "r") as infile:
|
| 95 |
data = json.load(infile)
|
| 96 |
+
embeddings = OpenAIEmbeddings(openai_api_key=setting.openai_key)
|
| 97 |
|
| 98 |
query_result = embeddings.embed_query(query)
|
| 99 |
doc_list = utils.maximal_marginal_relevance(np.array(query_result), data, k=1)
|
|
|
|
| 109 |
)
|
| 110 |
)
|
| 111 |
|
| 112 |
+
""" 1. calling gpt model to categorize for all message"""
|
| 113 |
+
if model in GPT_LLM_MODELS:
|
| 114 |
+
chain_data = get_llm_chain(model=model, setting=setting).run(
|
| 115 |
+
input_documents=docs, question=query
|
| 116 |
+
)
|
| 117 |
+
else:
|
| 118 |
+
chain_data = get_llm_chain(model=DEFAULT_GPT_MODEL, setting=setting).run(
|
| 119 |
+
input_documents=docs, question=query
|
| 120 |
+
)
|
| 121 |
try:
|
| 122 |
result = json.loads(chain_data)
|
| 123 |
# check image query with only its text
|
|
|
|
| 126 |
result["content"] = {
|
| 127 |
"image_name": query_image_text(result["content"], "", uuid)
|
| 128 |
}
|
| 129 |
+
""" 2. check program is message to handle it with falcon llm """
|
|
|
|
|
|
|
|
|
|
| 130 |
if result["program"] == "message":
|
| 131 |
+
if model == FALCON_7B:
|
| 132 |
+
result["content"] = falcon_llm.query(question=query)
|
| 133 |
return str(result)
|
| 134 |
except ValueError as e:
|
| 135 |
# Check sms and browser query
|
|
|
|
| 137 |
return str({"program": "sms", "content": chain_data})
|
| 138 |
elif doc_list[0] in COMMAND_BROWSER_OPEN:
|
| 139 |
return str({"program": "browser", "content": "https://google.com"})
|
| 140 |
+
|
| 141 |
return str({"program": "message", "content": falcon_llm.query(question=query)})
|
Brain/src/rising_plugin/llm/gpt_llm.py
CHANGED
|
@@ -9,12 +9,13 @@ from Brain.src.common.utils import (
|
|
| 9 |
|
| 10 |
|
| 11 |
class GptLLM:
|
| 12 |
-
def __init__(self, model: str = "gpt-
|
|
|
|
| 13 |
self.llm = self.init_llm(model=model, temperature=temperature)
|
| 14 |
|
| 15 |
-
def init_llm(self, model: str = "gpt-
|
| 16 |
self.llm = ChatOpenAI(
|
| 17 |
-
model_name=model, temperature=temperature, openai_api_key=
|
| 18 |
)
|
| 19 |
return self.llm
|
| 20 |
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
class GptLLM:
|
| 12 |
+
def __init__(self, openai_key: str, model: str = "gpt-4", temperature: float = 0.6):
|
| 13 |
+
self.key = openai_key
|
| 14 |
self.llm = self.init_llm(model=model, temperature=temperature)
|
| 15 |
|
| 16 |
+
def init_llm(self, model: str = "gpt-4", temperature: float = 0.6) -> Any:
|
| 17 |
self.llm = ChatOpenAI(
|
| 18 |
+
model_name=model, temperature=temperature, openai_api_key=self.key
|
| 19 |
)
|
| 20 |
return self.llm
|
| 21 |
|
Brain/src/rising_plugin/llm/llms.py
CHANGED
|
@@ -2,6 +2,8 @@
|
|
| 2 |
from typing import Any
|
| 3 |
|
| 4 |
from Brain.src.common.brain_exception import BrainException
|
|
|
|
|
|
|
| 5 |
from Brain.src.rising_plugin.llm.falcon_llm import FalconLLM
|
| 6 |
from Brain.src.rising_plugin.llm.gpt_llm import GptLLM
|
| 7 |
|
|
@@ -12,6 +14,7 @@ FALCON_7B = "falcon-7b"
|
|
| 12 |
|
| 13 |
"""list of available model we offer you"""
|
| 14 |
LLM_MODELS = [GPT_3_5_TURBO, GPT_4, GPT_4_32K, FALCON_7B]
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
"""exception message"""
|
|
@@ -35,23 +38,30 @@ datatype: LLmChain
|
|
| 35 |
|
| 36 |
|
| 37 |
def get_llm_chain(
|
| 38 |
-
model: str, temperature: float = 0.6, max_new_tokens: int = 2000
|
| 39 |
) -> Any:
|
| 40 |
if not validate_model(model):
|
| 41 |
raise BrainException(EXCEPTION_MSG)
|
| 42 |
"""check model"""
|
| 43 |
-
llm = get_llm(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
return llm.get_chain()
|
| 46 |
|
| 47 |
|
| 48 |
-
def get_llm(
|
|
|
|
|
|
|
| 49 |
if not validate_model(model):
|
| 50 |
raise BrainException(EXCEPTION_MSG)
|
| 51 |
"""check model"""
|
| 52 |
-
llm = GptLLM()
|
| 53 |
if model == GPT_3_5_TURBO or model == GPT_4 or model == GPT_4_32K:
|
| 54 |
-
llm = GptLLM(model=model)
|
| 55 |
elif model == FALCON_7B:
|
| 56 |
llm = FalconLLM(temperature=temperature, max_new_tokens=max_new_tokens)
|
| 57 |
return llm
|
|
|
|
| 2 |
from typing import Any
|
| 3 |
|
| 4 |
from Brain.src.common.brain_exception import BrainException
|
| 5 |
+
from Brain.src.model.req_model import ReqModel
|
| 6 |
+
from Brain.src.model.requests.request_model import BasicReq
|
| 7 |
from Brain.src.rising_plugin.llm.falcon_llm import FalconLLM
|
| 8 |
from Brain.src.rising_plugin.llm.gpt_llm import GptLLM
|
| 9 |
|
|
|
|
| 14 |
|
| 15 |
"""list of available model we offer you"""
|
| 16 |
LLM_MODELS = [GPT_3_5_TURBO, GPT_4, GPT_4_32K, FALCON_7B]
|
| 17 |
+
GPT_LLM_MODELS = [GPT_3_5_TURBO, GPT_4, GPT_4_32K]
|
| 18 |
|
| 19 |
|
| 20 |
"""exception message"""
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
def get_llm_chain(
|
| 41 |
+
setting: ReqModel, model: str, temperature: float = 0.6, max_new_tokens: int = 2000
|
| 42 |
) -> Any:
|
| 43 |
if not validate_model(model):
|
| 44 |
raise BrainException(EXCEPTION_MSG)
|
| 45 |
"""check model"""
|
| 46 |
+
llm = get_llm(
|
| 47 |
+
model=model,
|
| 48 |
+
temperature=temperature,
|
| 49 |
+
max_new_tokens=max_new_tokens,
|
| 50 |
+
setting=setting,
|
| 51 |
+
)
|
| 52 |
|
| 53 |
return llm.get_chain()
|
| 54 |
|
| 55 |
|
| 56 |
+
def get_llm(
|
| 57 |
+
setting: ReqModel, model: str, temperature: float = 0.6, max_new_tokens: int = 2000
|
| 58 |
+
) -> Any:
|
| 59 |
if not validate_model(model):
|
| 60 |
raise BrainException(EXCEPTION_MSG)
|
| 61 |
"""check model"""
|
| 62 |
+
llm = GptLLM(openai_key=setting.openai_key)
|
| 63 |
if model == GPT_3_5_TURBO or model == GPT_4 or model == GPT_4_32K:
|
| 64 |
+
llm = GptLLM(model=model, openai_key=setting.openai_key)
|
| 65 |
elif model == FALCON_7B:
|
| 66 |
llm = FalconLLM(temperature=temperature, max_new_tokens=max_new_tokens)
|
| 67 |
return llm
|
Brain/src/rising_plugin/risingplugin.py
CHANGED
|
@@ -25,6 +25,8 @@ from .image_embedding import (
|
|
| 25 |
query_image_text,
|
| 26 |
get_prompt_image_with_message,
|
| 27 |
)
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Give the path to the folder containing the rails
|
| 30 |
file_path = os.path.dirname(os.path.abspath(__file__))
|
|
@@ -41,7 +43,12 @@ def getChunks(query: str):
|
|
| 41 |
|
| 42 |
|
| 43 |
def processLargeText(
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
):
|
| 46 |
if len(chunks) == 1:
|
| 47 |
message = app.generate(
|
|
@@ -49,6 +56,7 @@ def processLargeText(
|
|
| 49 |
{
|
| 50 |
"role": "user",
|
| 51 |
"content": rails_input_with_args(
|
|
|
|
| 52 |
query=chunks[0],
|
| 53 |
model=model,
|
| 54 |
uuid=uuid,
|
|
@@ -89,6 +97,7 @@ def processLargeText(
|
|
| 89 |
{
|
| 90 |
"role": "user",
|
| 91 |
"content": rails_input_with_args(
|
|
|
|
| 92 |
query=chunk_query,
|
| 93 |
model=model,
|
| 94 |
uuid=uuid,
|
|
@@ -116,6 +125,7 @@ def processLargeText(
|
|
| 116 |
{
|
| 117 |
"role": "user",
|
| 118 |
"content": rails_input_with_args(
|
|
|
|
| 119 |
query=last_query,
|
| 120 |
model=model,
|
| 121 |
uuid=uuid,
|
|
@@ -130,19 +140,26 @@ def processLargeText(
|
|
| 130 |
|
| 131 |
|
| 132 |
def getCompletion(
|
| 133 |
-
query,
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
| 137 |
):
|
| 138 |
-
llm = get_llm(model=DEFAULT_GPT_MODEL).get_llm()
|
|
|
|
| 139 |
# Break input text into chunks
|
| 140 |
chunks = getChunks(query)
|
| 141 |
|
| 142 |
app = LLMRails(config, llm)
|
| 143 |
|
| 144 |
return processLargeText(
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
)
|
| 147 |
|
| 148 |
|
|
@@ -156,10 +173,12 @@ def getCompletionOnly(
|
|
| 156 |
return chain_data
|
| 157 |
|
| 158 |
|
| 159 |
-
def query_image_ask(image_content, message, uuid):
|
| 160 |
prompt_template = get_prompt_image_with_message(image_content, message)
|
| 161 |
try:
|
| 162 |
-
data = getCompletion(
|
|
|
|
|
|
|
| 163 |
# chain_data = json.loads(data.replace("'", '"'))
|
| 164 |
# chain_data = json.loads(data)
|
| 165 |
if data["program"] == "image":
|
|
@@ -214,15 +233,15 @@ response:
|
|
| 214 |
|
| 215 |
|
| 216 |
# Define a content filter function
|
| 217 |
-
def filter_guardrails(model: any, query: str):
|
| 218 |
-
llm = ChatOpenAI(model_name=model, temperature=0, openai_api_key=
|
| 219 |
app = LLMRails(config, llm)
|
| 220 |
|
| 221 |
# split query with chunks
|
| 222 |
chunks = getChunks(query)
|
| 223 |
|
| 224 |
# get message from guardrails
|
| 225 |
-
message = processLargeText(app=app, chunks=chunks, model=model)
|
| 226 |
|
| 227 |
if (
|
| 228 |
message
|
|
@@ -240,13 +259,16 @@ compose json_string for rails input with its arguments
|
|
| 240 |
"""
|
| 241 |
|
| 242 |
|
| 243 |
-
def rails_input_with_args(
|
|
|
|
|
|
|
| 244 |
# convert json with params for rails.
|
| 245 |
json_query_with_params = {
|
| 246 |
"query": query,
|
| 247 |
"model": model,
|
| 248 |
"uuid": uuid,
|
| 249 |
"image_search": image_search,
|
|
|
|
| 250 |
}
|
| 251 |
return json.dumps(json_query_with_params)
|
| 252 |
|
|
|
|
| 25 |
query_image_text,
|
| 26 |
get_prompt_image_with_message,
|
| 27 |
)
|
| 28 |
+
from ..model.req_model import ReqModel
|
| 29 |
+
from ..model.requests.request_model import BasicReq
|
| 30 |
|
| 31 |
# Give the path to the folder containing the rails
|
| 32 |
file_path = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
def processLargeText(
|
| 46 |
+
setting: ReqModel,
|
| 47 |
+
app: any,
|
| 48 |
+
chunks: any,
|
| 49 |
+
model: str,
|
| 50 |
+
uuid: str = "",
|
| 51 |
+
image_search: bool = True,
|
| 52 |
):
|
| 53 |
if len(chunks) == 1:
|
| 54 |
message = app.generate(
|
|
|
|
| 56 |
{
|
| 57 |
"role": "user",
|
| 58 |
"content": rails_input_with_args(
|
| 59 |
+
setting=setting,
|
| 60 |
query=chunks[0],
|
| 61 |
model=model,
|
| 62 |
uuid=uuid,
|
|
|
|
| 97 |
{
|
| 98 |
"role": "user",
|
| 99 |
"content": rails_input_with_args(
|
| 100 |
+
setting=setting,
|
| 101 |
query=chunk_query,
|
| 102 |
model=model,
|
| 103 |
uuid=uuid,
|
|
|
|
| 125 |
{
|
| 126 |
"role": "user",
|
| 127 |
"content": rails_input_with_args(
|
| 128 |
+
setting=setting,
|
| 129 |
query=last_query,
|
| 130 |
model=model,
|
| 131 |
uuid=uuid,
|
|
|
|
| 140 |
|
| 141 |
|
| 142 |
def getCompletion(
|
| 143 |
+
query: str,
|
| 144 |
+
setting: ReqModel,
|
| 145 |
+
model: str = DEFAULT_GPT_MODEL,
|
| 146 |
+
uuid: str = "",
|
| 147 |
+
image_search: bool = True,
|
| 148 |
):
|
| 149 |
+
llm = get_llm(model=DEFAULT_GPT_MODEL, setting=setting).get_llm()
|
| 150 |
+
|
| 151 |
# Break input text into chunks
|
| 152 |
chunks = getChunks(query)
|
| 153 |
|
| 154 |
app = LLMRails(config, llm)
|
| 155 |
|
| 156 |
return processLargeText(
|
| 157 |
+
setting=setting,
|
| 158 |
+
app=app,
|
| 159 |
+
chunks=chunks,
|
| 160 |
+
model=model,
|
| 161 |
+
uuid=uuid,
|
| 162 |
+
image_search=image_search,
|
| 163 |
)
|
| 164 |
|
| 165 |
|
|
|
|
| 173 |
return chain_data
|
| 174 |
|
| 175 |
|
| 176 |
+
def query_image_ask(image_content, message, uuid, setting: BasicReq):
|
| 177 |
prompt_template = get_prompt_image_with_message(image_content, message)
|
| 178 |
try:
|
| 179 |
+
data = getCompletion(
|
| 180 |
+
query=prompt_template, uuid=uuid, image_search=False, setting=setting
|
| 181 |
+
)
|
| 182 |
# chain_data = json.loads(data.replace("'", '"'))
|
| 183 |
# chain_data = json.loads(data)
|
| 184 |
if data["program"] == "image":
|
|
|
|
| 233 |
|
| 234 |
|
| 235 |
# Define a content filter function
|
| 236 |
+
def filter_guardrails(setting: BasicReq, model: any, query: str):
|
| 237 |
+
llm = ChatOpenAI(model_name=model, temperature=0, openai_api_key=setting.openai_key)
|
| 238 |
app = LLMRails(config, llm)
|
| 239 |
|
| 240 |
# split query with chunks
|
| 241 |
chunks = getChunks(query)
|
| 242 |
|
| 243 |
# get message from guardrails
|
| 244 |
+
message = processLargeText(app=app, chunks=chunks, model=model, setting=setting)
|
| 245 |
|
| 246 |
if (
|
| 247 |
message
|
|
|
|
| 259 |
"""
|
| 260 |
|
| 261 |
|
| 262 |
+
def rails_input_with_args(
|
| 263 |
+
setting: ReqModel, query: str, model: str, uuid: str, image_search: bool
|
| 264 |
+
) -> str:
|
| 265 |
# convert json with params for rails.
|
| 266 |
json_query_with_params = {
|
| 267 |
"query": query,
|
| 268 |
"model": model,
|
| 269 |
"uuid": uuid,
|
| 270 |
"image_search": image_search,
|
| 271 |
+
"setting": setting.to_json(),
|
| 272 |
}
|
| 273 |
return json.dumps(json_query_with_params)
|
| 274 |
|
Brain/src/router/api.py
CHANGED
|
@@ -68,7 +68,9 @@ def construct_blueprint_api() -> APIRouter:
|
|
| 68 |
if client_info.is_browser():
|
| 69 |
query = f"{query} in a web browser"
|
| 70 |
|
| 71 |
-
result = getCompletion(
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# check contact querying
|
| 74 |
try:
|
|
@@ -144,7 +146,9 @@ def construct_blueprint_api() -> APIRouter:
|
|
| 144 |
image_response = {}
|
| 145 |
try:
|
| 146 |
# check about asking image description with trained data
|
| 147 |
-
if query_image_ask(
|
|
|
|
|
|
|
| 148 |
image_response["image_desc"] = image_content
|
| 149 |
else:
|
| 150 |
relatedness_data = query_image_text(image_content, message, uuid)
|
|
|
|
| 68 |
if client_info.is_browser():
|
| 69 |
query = f"{query} in a web browser"
|
| 70 |
|
| 71 |
+
result = getCompletion(
|
| 72 |
+
query=query, uuid=uuid, setting=assembler.to_req_model(data)
|
| 73 |
+
)
|
| 74 |
|
| 75 |
# check contact querying
|
| 76 |
try:
|
|
|
|
| 146 |
image_response = {}
|
| 147 |
try:
|
| 148 |
# check about asking image description with trained data
|
| 149 |
+
if query_image_ask(
|
| 150 |
+
image_content=image_content, message=message, uuid=uuid, setting=data
|
| 151 |
+
):
|
| 152 |
image_response["image_desc"] = image_content
|
| 153 |
else:
|
| 154 |
relatedness_data = query_image_text(image_content, message, uuid)
|