Kotta commited on
Commit ·
ed4b12b
1
Parent(s): a8b45b8
feature(#8): LLM Strategy 1 - Build our falcon model
Browse files- src/common/utils.py +1 -1
- src/rising_plugin/guardrails-config/actions/actions.py +12 -8
- src/rising_plugin/guardrails-config/actions/phone.csv +10 -10
- src/rising_plugin/guardrails-config/actions/phone.json +0 -0
- src/rising_plugin/guardrails-config/general.co +1 -1
- src/rising_plugin/llm/falcon_llm.py +6 -0
- src/rising_plugin/risingplugin.py +36 -16
- src/service/llm/chat_service.py +2 -2
src/common/utils.py
CHANGED
|
@@ -23,7 +23,7 @@ PINECONE_NAMESPACE = "risinglangchain-namespace"
|
|
| 23 |
PINECONE_INDEX_NAME = "risinglangchain-index"
|
| 24 |
|
| 25 |
# open ai
|
| 26 |
-
|
| 27 |
|
| 28 |
# AI Agent name
|
| 29 |
AGENT_NAME = "RisingBrain Assistant"
|
|
|
|
| 23 |
PINECONE_INDEX_NAME = "risinglangchain-index"
|
| 24 |
|
| 25 |
# open ai
|
| 26 |
+
DEFAULT_GPT_MODEL = "gpt-4"
|
| 27 |
|
| 28 |
# AI Agent name
|
| 29 |
AGENT_NAME = "RisingBrain Assistant"
|
src/rising_plugin/guardrails-config/actions/actions.py
CHANGED
|
@@ -46,6 +46,7 @@ from src.rising_plugin.llm.llms import (
|
|
| 46 |
|
| 47 |
@action()
|
| 48 |
async def general_question(query, model, uuid, image_search):
|
|
|
|
| 49 |
file_path = os.path.dirname(os.path.abspath(__file__))
|
| 50 |
|
| 51 |
with open(f"{file_path}/phone.json", "r") as infile:
|
|
@@ -68,13 +69,13 @@ async def general_question(query, model, uuid, image_search):
|
|
| 68 |
|
| 69 |
chain_data = get_llm_chain(model=model).run(input_documents=docs, question=query)
|
| 70 |
# test
|
| 71 |
-
if model == GPT_3_5_TURBO or model == GPT_4 or model == GPT_4_32K:
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
elif model == FALCON_7B:
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
try:
|
| 79 |
result = json.loads(chain_data)
|
| 80 |
# check image query with only its text
|
|
@@ -86,6 +87,9 @@ async def general_question(query, model, uuid, image_search):
|
|
| 86 |
|
| 87 |
# else:
|
| 88 |
# return result
|
|
|
|
|
|
|
|
|
|
| 89 |
return str(result)
|
| 90 |
except ValueError as e:
|
| 91 |
# Check sms and browser query
|
|
@@ -93,4 +97,4 @@ async def general_question(query, model, uuid, image_search):
|
|
| 93 |
return str({"program": "sms", "content": chain_data})
|
| 94 |
elif doc_list[0] in COMMAND_BROWSER_OPEN:
|
| 95 |
return str({"program": "browser", "content": "https://google.com"})
|
| 96 |
-
return str({"program": "message", "content":
|
|
|
|
| 46 |
|
| 47 |
@action()
|
| 48 |
async def general_question(query, model, uuid, image_search):
|
| 49 |
+
"""step1: handle with gpt-4"""
|
| 50 |
file_path = os.path.dirname(os.path.abspath(__file__))
|
| 51 |
|
| 52 |
with open(f"{file_path}/phone.json", "r") as infile:
|
|
|
|
| 69 |
|
| 70 |
chain_data = get_llm_chain(model=model).run(input_documents=docs, question=query)
|
| 71 |
# test
|
| 72 |
+
# if model == GPT_3_5_TURBO or model == GPT_4 or model == GPT_4_32K:
|
| 73 |
+
# gpt_llm = GptLLM(model=model)
|
| 74 |
+
# chain_data = gpt_llm.get_chain().run(input_documents=docs, question=query)
|
| 75 |
+
# elif model == FALCON_7B:
|
| 76 |
+
# falcon_llm = FalconLLM()
|
| 77 |
+
# chain_data = falcon_llm.get_chain().run(question=query)
|
| 78 |
+
falcon_llm = FalconLLM()
|
| 79 |
try:
|
| 80 |
result = json.loads(chain_data)
|
| 81 |
# check image query with only its text
|
|
|
|
| 87 |
|
| 88 |
# else:
|
| 89 |
# return result
|
| 90 |
+
"""check program is message to handle it with falcon llm"""
|
| 91 |
+
if result["program"] == "message":
|
| 92 |
+
result["content"] = falcon_llm.query(question=query)
|
| 93 |
return str(result)
|
| 94 |
except ValueError as e:
|
| 95 |
# Check sms and browser query
|
|
|
|
| 97 |
return str({"program": "sms", "content": chain_data})
|
| 98 |
elif doc_list[0] in COMMAND_BROWSER_OPEN:
|
| 99 |
return str({"program": "browser", "content": "https://google.com"})
|
| 100 |
+
return str({"program": "message", "content": falcon_llm.query(question=query)})
|
src/rising_plugin/guardrails-config/actions/phone.csv
CHANGED
|
@@ -6,15 +6,15 @@
|
|
| 6 |
"If user said that send sms or text, please answer belowing json format. {""program"": ""sms"", ""content"": ""ask who""}"
|
| 7 |
"If user said that compose, write, or create an sms message, please answer belowing json format. {""program"": ""sms"", ""content"": ""ask who""}"
|
| 8 |
"If user said that search contact with its description such as display name or phone number, please answer belowing json format. {""program"": ""contact"", ""content"": ""description of the contact that user is going to search""}"
|
| 9 |
-
"If user said that open a
|
| 10 |
-
"If user said that open a
|
| 11 |
-
"If user said that close a tab, please answer belowing json format
|
| 12 |
-
"If user said that launch a browser or open a browser
|
| 13 |
-
"If user said that go to a previous page, or open a previous page
|
| 14 |
-
"If user said that go to a next page, or open a next page
|
| 15 |
-
"If user said that scroll up, scroll up page, or page scroll up
|
| 16 |
-
"If user said that scroll down, scroll down page, page scroll down
|
| 17 |
-
"If user said that scroll top, scroll top page, or scroll top of page
|
| 18 |
-
"If user said that scroll bottom, scroll bottom page, or scroll bottom of page
|
| 19 |
"If user is going to select an item, an article or a website with its description in a web browser, please answer belowing json format. {""program"": ""select_item_detail_info"", ""content"": ""the description of an item, an article or a website in a browser""}"
|
| 20 |
"If all of above is not correct, please give the most appropriate answer to the user's question. Please answer belowing json format. {""program"":""message"", ""content"":""your answer""}"
|
|
|
|
| 6 |
"If user said that send sms or text, please answer belowing json format. {""program"": ""sms"", ""content"": ""ask who""}"
|
| 7 |
"If user said that compose, write, or create an sms message, please answer belowing json format. {""program"": ""sms"", ""content"": ""ask who""}"
|
| 8 |
"If user said that search contact with its description such as display name or phone number, please answer belowing json format. {""program"": ""contact"", ""content"": ""description of the contact that user is going to search""}"
|
| 9 |
+
"If user said that open a tab, go to a tab, or open a page, please answer belowing json format. {""program"": ""open_tab"", ""content"": """"}"
|
| 10 |
+
"If user said that open a tab and search, go to a tab and search, or open a page and search, please answer belowing json format. {""program"": ""open_tab_search"", ""content"": ""keyword that user is going to search""}"
|
| 11 |
+
"If user said that close a tab, please answer belowing json format. {""program"": ""close_tab"", ""content"": """"}"
|
| 12 |
+
"If user said that launch a browser or open a browser, please answer belowing json format. {""program"": ""browser"", ""content"": ""https://google.com""}"
|
| 13 |
+
"If user said that go to a previous page, or open a previous page, please answer belowing json format. {""program"": ""previous_page"", ""content"": """"}"
|
| 14 |
+
"If user said that go to a next page, or open a next page, please answer belowing json format. {""program"": ""next_page"", ""content"": """"}"
|
| 15 |
+
"If user said that scroll up, scroll up page, or page scroll up, please answer belowing json format. {""program"": ""scroll_up"", ""content"": """"}"
|
| 16 |
+
"If user said that scroll down, scroll down page, page scroll down, please answer belowing json format. {""program"": ""scroll_down"", ""content"": """"}"
|
| 17 |
+
"If user said that scroll top, scroll top page, or scroll top of page, please answer belowing json format. {""program"": ""scroll_top"", ""content"": """"}"
|
| 18 |
+
"If user said that scroll bottom, scroll bottom page, or scroll bottom of page, please answer belowing json format. {""program"": ""scroll_bottom"", ""content"": """"}"
|
| 19 |
"If user is going to select an item, an article or a website with its description in a web browser, please answer belowing json format. {""program"": ""select_item_detail_info"", ""content"": ""the description of an item, an article or a website in a browser""}"
|
| 20 |
"If all of above is not correct, please give the most appropriate answer to the user's question. Please answer belowing json format. {""program"":""message"", ""content"":""your answer""}"
|
src/rising_plugin/guardrails-config/actions/phone.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
src/rising_plugin/guardrails-config/general.co
CHANGED
|
@@ -15,5 +15,5 @@ define bot inform capabilities
|
|
| 15 |
define flow
|
| 16 |
priority 0.9
|
| 17 |
user ...
|
| 18 |
-
$result = execute general_question(query=$last_user_message, model="gpt-
|
| 19 |
bot $result
|
|
|
|
| 15 |
define flow
|
| 16 |
priority 0.9
|
| 17 |
user ...
|
| 18 |
+
$result = execute general_question(query=$last_user_message, model="gpt-4", uuid="", image_search=True)
|
| 19 |
bot $result
|
src/rising_plugin/llm/falcon_llm.py
CHANGED
|
@@ -27,3 +27,9 @@ class FalconLLM:
|
|
| 27 |
prompt = PromptTemplate(template=template, input_variables=["question"])
|
| 28 |
llm_chain = LLMChain(prompt=prompt, llm=self.llm, verbose=True)
|
| 29 |
return llm_chain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
prompt = PromptTemplate(template=template, input_variables=["question"])
|
| 28 |
llm_chain = LLMChain(prompt=prompt, llm=self.llm, verbose=True)
|
| 29 |
return llm_chain
|
| 30 |
+
|
| 31 |
+
"""getting the output in query with falcon llm"""
|
| 32 |
+
|
| 33 |
+
def query(self, question: str) -> str:
|
| 34 |
+
chain = self.get_chain()
|
| 35 |
+
return chain.run(question=question)
|
src/rising_plugin/risingplugin.py
CHANGED
|
@@ -7,7 +7,6 @@ import textwrap
|
|
| 7 |
|
| 8 |
from typing import Any
|
| 9 |
|
| 10 |
-
from langchain.chains.question_answering import load_qa_chain
|
| 11 |
from nemoguardrails.rails import LLMRails, RailsConfig
|
| 12 |
|
| 13 |
from langchain.chat_models import ChatOpenAI
|
|
@@ -18,7 +17,6 @@ from .llm.llms import get_llm, GPT_4, FALCON_7B
|
|
| 18 |
from ..common.utils import (
|
| 19 |
OPENAI_API_KEY,
|
| 20 |
FIREBASE_STORAGE_ROOT,
|
| 21 |
-
parseJsonFromCompletion,
|
| 22 |
)
|
| 23 |
from .image_embedding import (
|
| 24 |
query_image_text,
|
|
@@ -50,8 +48,24 @@ def processLargeText(app: any, chunks: any):
|
|
| 50 |
]
|
| 51 |
)
|
| 52 |
result = json.dumps(message["content"])
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
else:
|
| 56 |
first_query = "The total length of the content that I want to send you is too large to send in only one piece.\nFor sending you that content, I will follow this rule:\n[START PART 1/10]\nThis is the content of the part 1 out of 10 in total\n[END PART 1/10]\nThen you just answer: 'Received part 1/10'\nAnd when I tell you 'ALL PART SENT', then you can continue processing the data and answering my requests."
|
| 57 |
app.generate(messages=[{"role": "user", "content": first_query}])
|
|
@@ -103,7 +117,24 @@ def processLargeText(app: any, chunks: any):
|
|
| 103 |
messages=[{"role": "user", "content": last_query}]
|
| 104 |
)
|
| 105 |
result = json.dumps(message["content"])
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
# out of for-loop
|
| 108 |
|
| 109 |
|
|
@@ -122,17 +153,6 @@ def getCompletion(
|
|
| 122 |
return processLargeText(app, chunks)
|
| 123 |
|
| 124 |
|
| 125 |
-
def getCompletionOnly(
|
| 126 |
-
query: str,
|
| 127 |
-
model: str = "gpt-4",
|
| 128 |
-
) -> str:
|
| 129 |
-
llm = ChatOpenAI(model_name=model, temperature=1.7, openai_api_key=OPENAI_API_KEY)
|
| 130 |
-
chain = load_qa_chain(llm, chain_type="stuff")
|
| 131 |
-
test_question = """Please return the link of best relatedness of item which the title is "Android Studio in browser" from the below data. [{"title": "Android Studio", "link": "https://android.com"} , {"title": "What's this?", "link": "https://test.com"} , {"title": "How are you?", "link": "https://d.com"}]"""
|
| 132 |
-
chain_data = chain.run(input_documents=[], question=test_question)
|
| 133 |
-
return chain_data
|
| 134 |
-
|
| 135 |
-
|
| 136 |
def query_image_ask(image_content, message, uuid):
|
| 137 |
prompt_template = get_prompt_image_with_message(image_content, message)
|
| 138 |
try:
|
|
|
|
| 7 |
|
| 8 |
from typing import Any
|
| 9 |
|
|
|
|
| 10 |
from nemoguardrails.rails import LLMRails, RailsConfig
|
| 11 |
|
| 12 |
from langchain.chat_models import ChatOpenAI
|
|
|
|
| 17 |
from ..common.utils import (
|
| 18 |
OPENAI_API_KEY,
|
| 19 |
FIREBASE_STORAGE_ROOT,
|
|
|
|
| 20 |
)
|
| 21 |
from .image_embedding import (
|
| 22 |
query_image_text,
|
|
|
|
| 48 |
]
|
| 49 |
)
|
| 50 |
result = json.dumps(message["content"])
|
| 51 |
+
result = result[1:-1]
|
| 52 |
+
# fmt: off
|
| 53 |
+
result = result.replace("{'", '{"')
|
| 54 |
+
result = result.replace("'}", '"}')
|
| 55 |
+
result = result.replace("': '", '": "')
|
| 56 |
+
result = result.replace("': \\\"", '": \"')
|
| 57 |
+
result = result.replace("', '", '", "')
|
| 58 |
+
|
| 59 |
+
substring = '\\"}'
|
| 60 |
+
replacement = '\"}'
|
| 61 |
+
|
| 62 |
+
index = result.rfind(substring)
|
| 63 |
+
|
| 64 |
+
if index == len(result) - 3:
|
| 65 |
+
result = result[:index] + replacement + result[index + len(substring):]
|
| 66 |
+
# fmt: on
|
| 67 |
+
result = json.loads(result)
|
| 68 |
+
return result
|
| 69 |
else:
|
| 70 |
first_query = "The total length of the content that I want to send you is too large to send in only one piece.\nFor sending you that content, I will follow this rule:\n[START PART 1/10]\nThis is the content of the part 1 out of 10 in total\n[END PART 1/10]\nThen you just answer: 'Received part 1/10'\nAnd when I tell you 'ALL PART SENT', then you can continue processing the data and answering my requests."
|
| 71 |
app.generate(messages=[{"role": "user", "content": first_query}])
|
|
|
|
| 117 |
messages=[{"role": "user", "content": last_query}]
|
| 118 |
)
|
| 119 |
result = json.dumps(message["content"])
|
| 120 |
+
result = result[1:-1]
|
| 121 |
+
# fmt: off
|
| 122 |
+
result = result.replace("{'", '{"')
|
| 123 |
+
result = result.replace("'}", '"}')
|
| 124 |
+
result = result.replace("': '", '": "')
|
| 125 |
+
result = result.replace("': \\\"", '": \"')
|
| 126 |
+
result = result.replace("', '", '", "')
|
| 127 |
+
|
| 128 |
+
substring = '\\"}'
|
| 129 |
+
replacement = '\"}'
|
| 130 |
+
|
| 131 |
+
index = result.rfind(substring)
|
| 132 |
+
|
| 133 |
+
if index == len(result) - 3:
|
| 134 |
+
result = result[:index] + replacement + result[index + len(substring):]
|
| 135 |
+
# fmt: on
|
| 136 |
+
result = json.loads(result)
|
| 137 |
+
return result
|
| 138 |
# out of for-loop
|
| 139 |
|
| 140 |
|
|
|
|
| 153 |
return processLargeText(app, chunks)
|
| 154 |
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
def query_image_ask(image_content, message, uuid):
|
| 157 |
prompt_template = get_prompt_image_with_message(image_content, message)
|
| 158 |
try:
|
src/service/llm/chat_service.py
CHANGED
|
@@ -3,7 +3,7 @@ import time
|
|
| 3 |
|
| 4 |
from openai.error import RateLimitError
|
| 5 |
|
| 6 |
-
from src.common.utils import AGENT_NAME,
|
| 7 |
from src.rising_plugin.risingplugin import handle_chat_completion
|
| 8 |
from src.logs import logger
|
| 9 |
from src.model.chat_response_model import ChatResponseModel
|
|
@@ -11,7 +11,7 @@ from src.model.message_model import MessageModel
|
|
| 11 |
|
| 12 |
|
| 13 |
class ChatService:
|
| 14 |
-
def __init__(self, ai_name=AGENT_NAME, llm_model=
|
| 15 |
self.ai_name = ai_name
|
| 16 |
self.llm_model = llm_model
|
| 17 |
|
|
|
|
| 3 |
|
| 4 |
from openai.error import RateLimitError
|
| 5 |
|
| 6 |
+
from src.common.utils import AGENT_NAME, DEFAULT_GPT_MODEL
|
| 7 |
from src.rising_plugin.risingplugin import handle_chat_completion
|
| 8 |
from src.logs import logger
|
| 9 |
from src.model.chat_response_model import ChatResponseModel
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
class ChatService:
|
| 14 |
+
def __init__(self, ai_name=AGENT_NAME, llm_model=DEFAULT_GPT_MODEL):
|
| 15 |
self.ai_name = ai_name
|
| 16 |
self.llm_model = llm_model
|
| 17 |
|