Kotta commited on
Commit ·
51b69ae
1
Parent(s): bc41366
feature(#23): updated prompts to categorify the input and applied for autogpt to get its achievement.
Browse filesFiles changed (7)
56 unmodified lines5758596061626356 unmodified lines"""mapping result type into json{"program": sms | contacts | browser | select_item_detail_info,"content": string}"""56 unmodified lines5758596061626356 unmodified lines"""mapping result type into json{"program": sms | contacts | browser | selectitemdetailinfo,"content": string}"""
9 unmodified lines1011121314151617181920212223249 unmodified linesMESSAGE = "message"class BrowserType:OPEN_TAB = "open_tab"OPEN_TAB_SEARCH = "open_tab_search"CLOSE_TAB = "close_tab"PREVIOUS_PAGE = "previous_page"NEXT_PAGE = "next_page"SCROLL_UP = "scroll_up"SCROLL_DOWN = "scroll_down"SCROLL_TOP = "scroll_top"SCROLL_BOTTOM = "scroll_bottom"SELECT_ITEM_DETAIL_INFO = "select_item_detail_info"SELECT_ITEM = "select_item"MESSAGE = "message"9 unmodified lines1011121314151617181920212223249 unmodified linesMESSAGE = "message"class BrowserType:OPEN_TAB = "opentab"OPEN_TAB_SEARCH = "opentabsearch"CLOSE_TAB = "closetab"PREVIOUS_PAGE = "previouspage"NEXT_PAGE = "nextpage"SCROLL_UP = "scrollup"SCROLL_DOWN = "scrolldown"SCROLL_TOP = "scrolltop"SCROLL_BOTTOM = "scrollbottom"SELECT_ITEM_DETAIL_INFO = "selectitemdetailinfo"SELECT_ITEM = "selectitem"MESSAGE = "message"
39 unmodified lines4041424344451 unmodified line47484950515239 unmodified linesHUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")class ProgramType:BROWSER = "browser"ALERT = "alert"1 unmodified lineSMS = "sms"CONTACT = "contact"MESSAGE = "message"# validate json format39 unmodified lines404142434445464748491 unmodified line5152535455565739 unmodified linesHUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")# actions flag :True -> Directly with Category_promptACTION_FLAG = Trueclass ProgramType:BROWSER = "browser"ALERT = "alert"1 unmodified lineSMS = "sms"CONTACT = "contact"MESSAGE = "message"AUTO_TASK = "autotask"# validate json format
16 unmodified lines1718192021223 unmodified lines26272829303113 unmodified lines45464748495029 unmodified lines8081828384853 unmodified lines89909192939495969798991001011021031045 unmodified lines1101111121131141151161171181193 unmodified lines12312412512612712812916 unmodified linesimport jsonimport numpy as npfrom Brain.src.service.train_service import TrainServicefrom langchain.docstore.document import Document3 unmodified linesCOMMAND_BROWSER_OPEN,PINECONE_INDEX_NAME,DEFAULT_GPT_MODEL,)from Brain.src.model.req_model import ReqModelfrom Brain.src.model.requests.request_model import BasicReq13 unmodified linesGPT_4,FALCON_7B,GPT_LLM_MODELS,)from Brain.src.rising_plugin.pinecone_engine import (29 unmodified linesasync def general_question(query):"""init falcon model"""falcon_llm = FalconLLM()docs = []"""step 0-->: parsing parms from the json query"""3 unmodified linesraise BrainException(BrainException.JSON_PARSING_ISSUE_MSG)query = json_query["query"]image_search = json_query["image_search"]page_content = json_query["page_content"]document_id = json_query["document_id"]setting = ReqModel(json_query["setting"])is_browser = json_query["is_browser"]docs.append(Document(page_content=page_content, metadata=""))""" 1. calling gpt model to categorize for all message"""chain_data = get_llm_chain(model=DEFAULT_GPT_MODEL, setting=setting).run(input_documents=docs, question=query)try:result = json.loads(chain_data)# check image query with only its text5 unmodified lines""" 2. check program is message to handle it with falcon llm """if result["program"] == "message":if is_browser:result["program"] = "ask_website"else:# """FALCON_7B:"""result["content"] = falcon_llm.query(question=query)return json.dumps(result)except ValueError as e:# Check sms and browser query3 unmodified linesreturn json.dumps({"program": "browser", "content": "https://google.com"})if is_browser:return json.dumps({"program": "ask_website", "content": ""})return json.dumps({"program": "message", "content": falcon_llm.query(question=query)})16 unmodified lines171819202122233 unmodified lines2728293031323313 unmodified lines4748495051525329 unmodified lines83848586878889903 unmodified lines9495969798991001011021031041051061071081091101111121131141151165 unmodified lines1221231241251261271283 unmodified lines13213313413513616 unmodified linesimport jsonimport numpy as npfrom Brain.src.service.auto_task_service import AutoTaskServicefrom Brain.src.service.train_service import TrainServicefrom langchain.docstore.document import Document3 unmodified linesCOMMAND_BROWSER_OPEN,PINECONE_INDEX_NAME,DEFAULT_GPT_MODEL,ACTION_FLAG,)from Brain.src.model.req_model import ReqModelfrom Brain.src.model.requests.request_model import BasicReq13 unmodified linesGPT_4,FALCON_7B,GPT_LLM_MODELS,CATEGORY_PROMPT,)from Brain.src.rising_plugin.pinecone_engine import (29 unmodified linesasync def general_question(query):"""init falcon model"""falcon_llm = FalconLLM()autotask_service = AutoTaskService()document_id = ""docs = []"""step 0-->: parsing parms from the json query"""3 unmodified linesraise BrainException(BrainException.JSON_PARSING_ISSUE_MSG)query = json_query["query"]image_search = json_query["image_search"]setting = ReqModel(json_query["setting"])is_browser = json_query["is_browser"]if ACTION_FLAG:docs.append(Document(page_content=CATEGORY_PROMPT, metadata=""))# temperature shouldbe 0.chain_data = get_llm_chain(model=DEFAULT_GPT_MODEL, setting=setting, temperature=0.0).run(input_documents=docs, question=query)else:document_id = json_query["document_id"]page_content = json_query["page_content"]docs.append(Document(page_content=page_content, metadata=""))""" 1. calling gpt model to categorize for all message"""chain_data = get_llm_chain(model=DEFAULT_GPT_MODEL, setting=setting).run(input_documents=docs, question=query)try:result = json.loads(chain_data)# check image query with only its text5 unmodified lines""" 2. check program is message to handle it with falcon llm """if result["program"] == "message":if is_browser:result["program"] = "askwebsite"return json.dumps(result)except ValueError as e:# Check sms and browser query3 unmodified linesreturn json.dumps({"program": "browser", "content": "https://google.com"})if is_browser:return json.dumps({"program": "askwebsite", "content": ""})return json.dumps({"program": "message", "content": chain_data})
19 unmodified lines20212223242535 unmodified lines6162636465666719 unmodified lines"""exception message"""EXCEPTION_MSG = f"The model is not correct. It should be in {LLM_MODELS}""""validate model"""35 unmodified lines"""check model"""llm = GptLLM(openai_key=setting.openai_key)if model == GPT_3_5_TURBO or model == GPT_4 or model == GPT_4_32K:llm = GptLLM(model=model, openai_key=setting.openai_key)elif model == FALCON_7B:llm = FalconLLM(temperature=temperature, max_new_tokens=max_new_tokens)return llm19 unmodified lines202122232425262728293031323334353637383940414243444546474849505152535455565735 unmodified lines9394959697989910010119 unmodified lines"""exception message"""EXCEPTION_MSG = f"The model is not correct. It should be in {LLM_MODELS}""""prompt"""CATEGORY_PROMPT = """If user is going to say about a image with its description to search, please answer belowing json format. {"program": "image", "content": "description of the image that user is going to search"}If user is going to ask about a image, please answer belowing json format. {"program": "image", "content": "description of the image that user is going to search"}If user said that send sms or text, please answer belowing json format. {"program": "sms", "content": "ask who"}If user said that compose, write, or create an sms message, please answer belowing json format. {"program": "sms", "content": "ask who"}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"}If user said that open a tab, go to a tab, or open a page, please answer belowing json format. {"program": "opentab", "content": ""}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": "opentabsearch", "content": "keyword that user is going to search"}If user said that close a tab, please answer belowing json format. {"program": "close_tab", "content": ""}If user said that launch a browser or open a browser, please answer belowing json format. {"program": "browser", "content": "https://google.com"}If user said that go to a previous page, or open a previous page, please answer belowing json format. {"program": "previouspage", "content": ""}If user said that go to a next page, or open a next page, please answer belowing json format. {"program": "nextpage", "content": ""}If user said that scroll up, scroll up page, or page scroll up, please answer belowing json format. {"program": "scrollup", "content": ""}If user said that scroll down, scroll down page, page scroll down, please answer belowing json format. {"program": "scrolldown", "content": ""}If user said that scroll top, scroll top page, or scroll top of page, please answer belowing json format. {"program": "scrolltop", "content": ""}If user said that scroll bottom, scroll bottom page, or scroll bottom of page, please answer belowing json format. {"program": "scrollbottom", "content": ""}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": "selectitemdetailinfo", "content": "the description of an item, an article or a website in a browser"}If user said that ask about the content in website, for example, if users ask something like 'what is #query in this website?' or 'Can you tell me about #query based on this website?', please answer belowing json format. {"program": "askwebsite", "content": ""}If user said that open a website using web browsers, please answer belowing json format. The url user is going to open can exist or not. If user doesn\\'t say exact url and want to open some sites, you have to find the best proper url. If user didn\\'t say any url and you can't find proper url, please set website url to "https://www.google.com". {"program": "browser", "url": "website url that user is going to open"}If users are going to ask something based on the data of website, please answer belowing json format. {"program": "askwebsite", "content": ""}If user is going to set or create alarm with time and label, please answer belowing json format.\n {"program": "alarm", "content": {"type":"create", "time":"please set time as 24-hours format that user is going to set. If user did not provide any alarm time, set "0:0"", "label":"please set label that user is going to set. If user did not provide any label, set "alarm""}}\n This is example data.\n User: Set an alarm.\n AI: {"program":"alarm", "content": {"type":"create", "time":"0:0", "label":"alarm"}}\n User: Set an alarm with label as "wake up".\n AI: {"program":"alarm", "content": {"type":"create", "time":"0:0", "label":"wake up"}}\n User: Set an alarm for 5:00 AM.\n AI: {"program":"alarm", "content": {"type":"create", "time":"5:00", "label":"alarm"}}\n User: Set an alarm for 5:00 PM with label as "wake up".\n AI: {"program":"alarm", "content": {"type":"create", "time":"17:00", "label":"wake up"}}If user is going to read email or message, please answer belowing json format. {"program": "reademail", "content": ""}If user is going to send email or message, please answer belowing json format. {"program": "sendemail", "content": ""}If user is going to compose, write, or create an email, please answer belowing json format. {"program": "writeemail", "content": ""}User wants to help organization in achieving its goals, which bridges the gap between the user is (present) and where he/she wants to go (future), or that is deciding in advance what to do, how to do when to do it and by whom it is to be done. Also the description ensures in thoughts and actions, work is carried on smoothly without any confusion and misunderstanding. \n And it can be done for the future and the future is full of uncertainties. \n So it looks like to make a decision as well. It helps make rational decisions by choosing the best most profitable alternative which may bring lower cost, adaptable to the organization and situations. \n If user is going to say about planning strategy, or something like that , please answer belowing json format. {"program": "autotask", "content": ""}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"}If users are going to know about real-time capabilities such as News, Weather, Stocks, Booking, Planning, or etc, then please answer belowing json format. {"program": "autotask", "content": ""}If your answer is not correct with the program type which mentioned in this rules, please answer belowing json format. {"program": "autotask", "content": ""}""""""validate model"""35 unmodified lines"""check model"""llm = GptLLM(openai_key=setting.openai_key)if model == GPT_3_5_TURBO or model == GPT_4 or model == GPT_4_32K:llm = GptLLM(model=model, openai_key=setting.openai_key, temperature=temperature)elif model == FALCON_7B:llm = FalconLLM(temperature=temperature, max_new_tokens=max_new_tokens)return llm
27 unmodified lines28293031323325 unmodified lines5960616263646566676869707172737475767778798081828384858687881 unmodified line90919293949596979899100101102103205 unmodified lines30931031131231331431531631727 unmodified linesDEFAULT_GPT_MODEL,parseJsonFromCompletion,PINECONE_INDEX_NAME,)from .image_embedding import (query_image_text,25 unmodified linesimage_search: bool = True,is_browser: bool = False,) -> Any:"""step 0: convert string to json"""index = init_pinecone(index_name=PINECONE_INDEX_NAME, setting=setting)train_service = TrainService(firebase_app=firebase_app, setting=setting)"""step 1: handle with gpt-4"""query_result = get_embed(data=query, setting=setting)try:relatedness_data = index.query(vector=query_result,top_k=1,include_values=False,namespace=train_service.get_pinecone_index_train_namespace(),)except Exception as ex:raise BrainException(code=508, message=responses[508])if len(relatedness_data["matches"]) == 0:return str({"program": "message", "content": ""})document_id = relatedness_data["matches"][0]["id"]document = train_service.read_one_document(document_id)page_content = document["page_content"]message = rails_app.generate(messages=[{"role": "user",1 unmodified linesetting=setting,query=query,image_search=image_search,page_content=page_content,document_id=document_id,is_browser=is_browser,),}])return messagedef processLargeText(205 unmodified linessetting: ReqModel,query: str,image_search: bool,page_content: str,document_id: str,is_browser: bool,) -> str:# convert json with params for rails.json_query_with_params = {27 unmodified lines2829303132333425 unmodified lines606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051 unmodified line107108109110111112113114115116117205 unmodified lines32332432532632732832933033127 unmodified linesDEFAULT_GPT_MODEL,parseJsonFromCompletion,PINECONE_INDEX_NAME,ACTION_FLAG,)from .image_embedding import (query_image_text,25 unmodified linesimage_search: bool = True,is_browser: bool = False,) -> Any:if not ACTION_FLAG:"""step 0: convert string to json"""index = init_pinecone(index_name=PINECONE_INDEX_NAME, setting=setting)train_service = TrainService(firebase_app=firebase_app, setting=setting)"""step 1: handle with gpt-4"""query_result = get_embed(data=query, setting=setting)try:relatedness_data = index.query(vector=query_result,top_k=1,include_values=False,namespace=train_service.get_pinecone_index_train_namespace(),)except Exception as ex:raise BrainException(code=508, message=responses[508])if len(relatedness_data["matches"]) == 0:return str({"program": "message", "content": ""})document_id = relatedness_data["matches"][0]["id"]document = train_service.read_one_document(document_id)page_content = document["page_content"]return rails_app.generate(messages=[{"role": "user","content": rails_input_with_args(setting=setting,query=query,image_search=image_search,page_content=page_content,document_id=document_id,is_browser=is_browser,),}])return rails_app.generate(messages=[{"role": "user",1 unmodified linesetting=setting,query=query,image_search=image_search,is_browser=is_browser,),}])def processLargeText(205 unmodified linessetting: ReqModel,query: str,image_search: bool,is_browser: bool,page_content: str = "",document_id: str = "",) -> str:# convert json with params for rails.json_query_with_params = {
29 unmodified lines30313233343552 unmodified lines88899091929329 unmodified linesfrom Brain.src.logs import loggerfrom Brain.src.model.basic_model import BasicModelfrom Brain.src.model.feedback_model import FeedbackModelfrom Brain.src.service.command_service import CommandServicefrom Brain.src.service.contact_service import ContactsServicefrom Brain.src.service.feedback_service import FeedbackService52 unmodified lines# check contact queryingtry:contacts_service = ContactsService(setting=setting)if result["program"] == ProgramType.CONTACT:# querying contacts to getting its expected resultscontacts_results = contacts_service.query_contacts(29 unmodified lines3031323334353652 unmodified lines899091929394959697989910010129 unmodified linesfrom Brain.src.logs import loggerfrom Brain.src.model.basic_model import BasicModelfrom Brain.src.model.feedback_model import FeedbackModelfrom Brain.src.service.auto_task_service import AutoTaskServicefrom Brain.src.service.command_service import CommandServicefrom Brain.src.service.contact_service import ContactsServicefrom Brain.src.service.feedback_service import FeedbackService52 unmodified lines# check contact queryingtry:contacts_service = ContactsService(setting=setting)if result["program"] == ProgramType.AUTO_TASK:auto_task_service = AutoTaskService()result["content"] = auto_task_service.ask_task_with_autogpt(query=query, firebase_app=firebase_app, setting=setting)return assembler.to_response(200, "", result)if result["program"] == ProgramType.CONTACT:# querying contacts to getting its expected resultscontacts_results = contacts_service.query_contacts(
Brain/src/common/assembler.py+1-1
Brain/src/common/program_type.py+11-11
Brain/src/common/utils.py+5
Brain/src/rising_plugin/guardrails-config/actions/actions.py+23-16
Brain/src/rising_plugin/llm/llms.py+35-1
Brain/src/rising_plugin/risingplugin.py+40-26
Brain/src/router/api.py+8