Merge pull request #53 from ttt246/feature/falcon_8
Browse files- .github/workflows/ci.yml +1 -0
- src/common/assembler.py +2 -1
- src/common/brain_exception.py +14 -0
- src/common/http_response_codes.py +52 -0
- src/common/utils.py +3 -1
- src/model/requests/request_model.py +3 -0
- src/rising_plugin/guardrails-config/actions/actions.py +24 -6
- 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/__init__.py +0 -0
- src/rising_plugin/llm/falcon_llm.py +35 -0
- src/rising_plugin/llm/gpt_llm.py +26 -0
- src/rising_plugin/llm/llms.py +57 -0
- src/rising_plugin/risingplugin.py +5 -4
- src/router/api.py +10 -5
- src/service/llm/chat_service.py +2 -2
.github/workflows/ci.yml
CHANGED
|
@@ -11,6 +11,7 @@ env:
|
|
| 11 |
REPLICATE_API_TOKEN: ${{ secrets.REPLICATE_API_TOKEN }}
|
| 12 |
PINECONE_ENV: ${{ secrets.PINECONE_ENV }}
|
| 13 |
PINECONE_KEY: ${{ secrets.PINECONE_KEY }}
|
|
|
|
| 14 |
GITHUB_TOKEN: ${{ github.token }}
|
| 15 |
|
| 16 |
jobs:
|
|
|
|
| 11 |
REPLICATE_API_TOKEN: ${{ secrets.REPLICATE_API_TOKEN }}
|
| 12 |
PINECONE_ENV: ${{ secrets.PINECONE_ENV }}
|
| 13 |
PINECONE_KEY: ${{ secrets.PINECONE_KEY }}
|
| 14 |
+
HUGGINGFACEHUB_API_TOKEN: ${{ secrets.HUGGINGFACEHUB_API_TOKEN }}
|
| 15 |
GITHUB_TOKEN: ${{ github.token }}
|
| 16 |
|
| 17 |
jobs:
|
src/common/assembler.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# assembler to mapping data into another data type.
|
| 2 |
from typing import Any, List
|
| 3 |
|
|
|
|
| 4 |
from src.model.basic_model import BasicModel
|
| 5 |
from src.model.contact_model import ContactModel
|
| 6 |
from src.model.message_model import MessageModel
|
|
@@ -18,7 +19,7 @@ class Assembler:
|
|
| 18 |
"""mapping to http response"""
|
| 19 |
|
| 20 |
def to_response(self, code, message, result) -> Any:
|
| 21 |
-
response = {"message":
|
| 22 |
return response
|
| 23 |
|
| 24 |
"""mapping data to a collection of MessageModel"""
|
|
|
|
| 1 |
# assembler to mapping data into another data type.
|
| 2 |
from typing import Any, List
|
| 3 |
|
| 4 |
+
from src.common.http_response_codes import responses
|
| 5 |
from src.model.basic_model import BasicModel
|
| 6 |
from src.model.contact_model import ContactModel
|
| 7 |
from src.model.message_model import MessageModel
|
|
|
|
| 19 |
"""mapping to http response"""
|
| 20 |
|
| 21 |
def to_response(self, code, message, result) -> Any:
|
| 22 |
+
response = {"message": responses[code], "result": result, "status_code": code}
|
| 23 |
return response
|
| 24 |
|
| 25 |
"""mapping data to a collection of MessageModel"""
|
src/common/brain_exception.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Basic Exception in Brain"""
|
| 2 |
+
from typing import Any
|
| 3 |
+
|
| 4 |
+
from src.common.http_response_codes import responses
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class BrainException(Exception):
|
| 8 |
+
def __init__(self, message: str = "Exception occurred in brain"):
|
| 9 |
+
self.message = message
|
| 10 |
+
super().__init__(self.message)
|
| 11 |
+
|
| 12 |
+
def get_response_exp(self) -> Any:
|
| 13 |
+
responses[506] = ("Brain Exception", self.message)
|
| 14 |
+
return {"message": responses[506], "result": "", "status_code": 506}
|
src/common/http_response_codes.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
responses = {
|
| 2 |
+
100: ("Continue", "Request received, please continue"),
|
| 3 |
+
101: ("Switching Protocols", "Switching to new protocol; obey Upgrade header"),
|
| 4 |
+
200: ("OK", "Request fulfilled, document follows"),
|
| 5 |
+
201: ("Created", "Document created, URL follows"),
|
| 6 |
+
202: ("Accepted", "Request accepted, processing continues off-line"),
|
| 7 |
+
203: ("Non-Authoritative Information", "Request fulfilled from cache"),
|
| 8 |
+
204: ("No Content", "Request fulfilled, nothing follows"),
|
| 9 |
+
205: ("Reset Content", "Clear input form for further input."),
|
| 10 |
+
206: ("Partial Content", "Partial content follows."),
|
| 11 |
+
300: ("Multiple Choices", "Object has several resources -- see URI list"),
|
| 12 |
+
301: ("Moved Permanently", "Object moved permanently -- see URI list"),
|
| 13 |
+
302: ("Found", "Object moved temporarily -- see URI list"),
|
| 14 |
+
303: ("See Other", "Object moved -- see Method and URL list"),
|
| 15 |
+
304: ("Not Modified", "Document has not changed since given time"),
|
| 16 |
+
305: (
|
| 17 |
+
"Use Proxy",
|
| 18 |
+
"You must use proxy specified in Location to access this " "resource.",
|
| 19 |
+
),
|
| 20 |
+
307: ("Temporary Redirect", "Object moved temporarily -- see URI list"),
|
| 21 |
+
400: ("Bad Request", "Bad request syntax or unsupported method"),
|
| 22 |
+
401: ("Unauthorized", "No permission -- see authorization schemes"),
|
| 23 |
+
402: ("Payment Required", "No payment -- see charging schemes"),
|
| 24 |
+
403: ("Forbidden", "Request forbidden -- authorization will not help"),
|
| 25 |
+
404: ("Not Found", "Nothing matches the given URI"),
|
| 26 |
+
405: ("Method Not Allowed", "Specified method is invalid for this server."),
|
| 27 |
+
406: ("Not Acceptable", "URI not available in preferred format."),
|
| 28 |
+
407: (
|
| 29 |
+
"Proxy Authentication Required",
|
| 30 |
+
"You must authenticate with " "this proxy before proceeding.",
|
| 31 |
+
),
|
| 32 |
+
408: ("Request Timeout", "Request timed out; try again later."),
|
| 33 |
+
409: ("Conflict", "Request conflict."),
|
| 34 |
+
410: ("Gone", "URI no longer exists and has been permanently removed."),
|
| 35 |
+
411: ("Length Required", "Client must specify Content-Length."),
|
| 36 |
+
412: ("Precondition Failed", "Precondition in headers is false."),
|
| 37 |
+
413: ("Request Entity Too Large", "Entity is too large."),
|
| 38 |
+
414: ("Request-URI Too Long", "URI is too long."),
|
| 39 |
+
415: ("Unsupported Media Type", "Entity body in unsupported format."),
|
| 40 |
+
416: ("Requested Range Not Satisfiable", "Cannot satisfy request range."),
|
| 41 |
+
417: ("Expectation Failed", "Expect condition could not be satisfied."),
|
| 42 |
+
500: ("Internal Server Error", "Server got itself in trouble"),
|
| 43 |
+
501: ("Not Implemented", "Server does not support this operation"),
|
| 44 |
+
502: ("Bad Gateway", "Invalid responses from another server/proxy."),
|
| 45 |
+
503: (
|
| 46 |
+
"Service Unavailable",
|
| 47 |
+
"The server cannot process the request due to a high load",
|
| 48 |
+
),
|
| 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 |
+
}
|
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"
|
|
@@ -35,6 +35,8 @@ COMMAND_BROWSER_OPEN = [10]
|
|
| 35 |
# Twilio
|
| 36 |
ACCOUNT_SID = os.getenv("TWILIO_ACCOUNT_SID")
|
| 37 |
AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
|
|
|
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
def get_firebase_cred():
|
|
|
|
| 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"
|
|
|
|
| 35 |
# Twilio
|
| 36 |
ACCOUNT_SID = os.getenv("TWILIO_ACCOUNT_SID")
|
| 37 |
AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
|
| 38 |
+
# HuggingFace
|
| 39 |
+
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 40 |
|
| 41 |
|
| 42 |
def get_firebase_cred():
|
src/model/requests/request_model.py
CHANGED
|
@@ -4,6 +4,8 @@ from pydantic import BaseModel
|
|
| 4 |
from fastapi import Depends, Request, HTTPException
|
| 5 |
from user_agents import parse
|
| 6 |
|
|
|
|
|
|
|
| 7 |
|
| 8 |
class ClientInfo:
|
| 9 |
def __init__(self, browser, os, device_type):
|
|
@@ -51,6 +53,7 @@ def get_client_info(request: Request):
|
|
| 51 |
class BasicReq(BaseModel):
|
| 52 |
token: str
|
| 53 |
uuid: str
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
"""endpoint: /sendNotification"""
|
|
|
|
| 4 |
from fastapi import Depends, Request, HTTPException
|
| 5 |
from user_agents import parse
|
| 6 |
|
| 7 |
+
"""user-agent management"""
|
| 8 |
+
|
| 9 |
|
| 10 |
class ClientInfo:
|
| 11 |
def __init__(self, browser, os, device_type):
|
|
|
|
| 53 |
class BasicReq(BaseModel):
|
| 54 |
token: str
|
| 55 |
uuid: str
|
| 56 |
+
model: str = "gpt-3.5-turbo"
|
| 57 |
|
| 58 |
|
| 59 |
"""endpoint: /sendNotification"""
|
src/rising_plugin/guardrails-config/actions/actions.py
CHANGED
|
@@ -17,11 +17,9 @@ import os
|
|
| 17 |
import json
|
| 18 |
import numpy as np
|
| 19 |
|
| 20 |
-
from langchain.chat_models import ChatOpenAI
|
| 21 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 22 |
from langchain.vectorstores import utils
|
| 23 |
from langchain.document_loaders.csv_loader import CSVLoader
|
| 24 |
-
from langchain.chains.question_answering import load_qa_chain
|
| 25 |
from langchain.docstore.document import Document
|
| 26 |
|
| 27 |
from src.common.utils import (
|
|
@@ -35,11 +33,20 @@ from src.rising_plugin.image_embedding import (
|
|
| 35 |
|
| 36 |
from nemoguardrails.actions import action
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
@action()
|
| 40 |
async def general_question(query, model, uuid, image_search):
|
| 41 |
-
|
| 42 |
-
chain = load_qa_chain(llm, chain_type="stuff")
|
| 43 |
file_path = os.path.dirname(os.path.abspath(__file__))
|
| 44 |
|
| 45 |
with open(f"{file_path}/phone.json", "r") as infile:
|
|
@@ -60,7 +67,15 @@ async def general_question(query, model, uuid, image_search):
|
|
| 60 |
)
|
| 61 |
)
|
| 62 |
|
| 63 |
-
chain_data =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
try:
|
| 65 |
result = json.loads(chain_data)
|
| 66 |
# check image query with only its text
|
|
@@ -72,6 +87,9 @@ async def general_question(query, model, uuid, image_search):
|
|
| 72 |
|
| 73 |
# else:
|
| 74 |
# return result
|
|
|
|
|
|
|
|
|
|
| 75 |
return str(result)
|
| 76 |
except ValueError as e:
|
| 77 |
# Check sms and browser query
|
|
@@ -79,4 +97,4 @@ async def general_question(query, model, uuid, image_search):
|
|
| 79 |
return str({"program": "sms", "content": chain_data})
|
| 80 |
elif doc_list[0] in COMMAND_BROWSER_OPEN:
|
| 81 |
return str({"program": "browser", "content": "https://google.com"})
|
| 82 |
-
return str({"program": "message", "content":
|
|
|
|
| 17 |
import json
|
| 18 |
import numpy as np
|
| 19 |
|
|
|
|
| 20 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 21 |
from langchain.vectorstores import utils
|
| 22 |
from langchain.document_loaders.csv_loader import CSVLoader
|
|
|
|
| 23 |
from langchain.docstore.document import Document
|
| 24 |
|
| 25 |
from src.common.utils import (
|
|
|
|
| 33 |
|
| 34 |
from nemoguardrails.actions import action
|
| 35 |
|
| 36 |
+
from src.rising_plugin.llm.falcon_llm import FalconLLM
|
| 37 |
+
from src.rising_plugin.llm.gpt_llm import GptLLM
|
| 38 |
+
from src.rising_plugin.llm.llms import (
|
| 39 |
+
get_llm_chain,
|
| 40 |
+
GPT_3_5_TURBO,
|
| 41 |
+
GPT_4_32K,
|
| 42 |
+
GPT_4,
|
| 43 |
+
FALCON_7B,
|
| 44 |
+
)
|
| 45 |
+
|
| 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:
|
|
|
|
| 67 |
)
|
| 68 |
)
|
| 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/__init__.py
ADDED
|
File without changes
|
src/rising_plugin/llm/falcon_llm.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""falcon llm"""
|
| 2 |
+
from langchain import HuggingFaceHub, PromptTemplate, LLMChain
|
| 3 |
+
|
| 4 |
+
from src.common.utils import HUGGINGFACEHUB_API_TOKEN
|
| 5 |
+
|
| 6 |
+
repo_id = "tiiuae/falcon-7b-instruct"
|
| 7 |
+
template = """
|
| 8 |
+
You are an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.
|
| 9 |
+
|
| 10 |
+
{question}
|
| 11 |
+
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class FalconLLM:
|
| 16 |
+
def __init__(self, temperature: float = 0.6, max_new_tokens: int = 2000):
|
| 17 |
+
self.llm = HuggingFaceHub(
|
| 18 |
+
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
|
| 19 |
+
repo_id=repo_id,
|
| 20 |
+
model_kwargs={"temperature": temperature, "max_new_tokens": max_new_tokens},
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
def get_llm(self):
|
| 24 |
+
return self.llm
|
| 25 |
+
|
| 26 |
+
def get_chain(self):
|
| 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/llm/gpt_llm.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""gpt-open ai llm"""
|
| 2 |
+
from typing import Any
|
| 3 |
+
|
| 4 |
+
from langchain.chat_models import ChatOpenAI
|
| 5 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 6 |
+
from src.common.utils import (
|
| 7 |
+
OPENAI_API_KEY,
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class GptLLM:
|
| 12 |
+
def __init__(self, model: str = "gpt-3.5-turbo", temperature: float = 0.6):
|
| 13 |
+
self.llm = self.init_llm(model=model, temperature=temperature)
|
| 14 |
+
|
| 15 |
+
def init_llm(self, model: str = "gpt-3.5-turbo", temperature: float = 0.6) -> Any:
|
| 16 |
+
self.llm = ChatOpenAI(
|
| 17 |
+
model_name=model, temperature=temperature, openai_api_key=OPENAI_API_KEY
|
| 18 |
+
)
|
| 19 |
+
return self.llm
|
| 20 |
+
|
| 21 |
+
def get_llm(self):
|
| 22 |
+
return self.llm
|
| 23 |
+
|
| 24 |
+
def get_chain(self):
|
| 25 |
+
chain = load_qa_chain(self.llm, chain_type="stuff")
|
| 26 |
+
return chain
|
src/rising_plugin/llm/llms.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""lLMs which we offer"""
|
| 2 |
+
from typing import Any
|
| 3 |
+
|
| 4 |
+
from src.common.brain_exception import BrainException
|
| 5 |
+
from src.rising_plugin.llm.falcon_llm import FalconLLM
|
| 6 |
+
from src.rising_plugin.llm.gpt_llm import GptLLM
|
| 7 |
+
|
| 8 |
+
GPT_3_5_TURBO = "gpt-3.5-turbo"
|
| 9 |
+
GPT_4 = "gpt-4"
|
| 10 |
+
GPT_4_32K = "gpt-4-32k"
|
| 11 |
+
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"""
|
| 18 |
+
EXCEPTION_MSG = f"The model is not correct. It should be in {LLM_MODELS}"
|
| 19 |
+
|
| 20 |
+
"""validate model"""
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def validate_model(model: str) -> bool:
|
| 24 |
+
if model in LLM_MODELS:
|
| 25 |
+
return True
|
| 26 |
+
return False
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
"""
|
| 30 |
+
Args
|
| 31 |
+
model: model name of LLM such as 'gpt-3.5-turbo' | 'falcon-7b'
|
| 32 |
+
Returns
|
| 33 |
+
datatype: LLmChain
|
| 34 |
+
"""
|
| 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(model=model, temperature=temperature, max_new_tokens=max_new_tokens)
|
| 44 |
+
|
| 45 |
+
return llm.get_chain()
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def get_llm(model: str, temperature: float = 0.6, max_new_tokens: int = 2000) -> Any:
|
| 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
|
src/rising_plugin/risingplugin.py
CHANGED
|
@@ -14,9 +14,11 @@ from langchain.chat_models import ChatOpenAI
|
|
| 14 |
|
| 15 |
from firebase_admin import storage
|
| 16 |
|
|
|
|
| 17 |
from ..common.utils import (
|
| 18 |
OPENAI_API_KEY,
|
| 19 |
FIREBASE_STORAGE_ROOT,
|
|
|
|
| 20 |
parseJsonFromCompletion,
|
| 21 |
)
|
| 22 |
from .image_embedding import (
|
|
@@ -49,7 +51,6 @@ def processLargeText(app: any, chunks: any):
|
|
| 49 |
]
|
| 50 |
)
|
| 51 |
result = json.dumps(message["content"])
|
| 52 |
-
|
| 53 |
return parseJsonFromCompletion(result)
|
| 54 |
else:
|
| 55 |
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."
|
|
@@ -108,16 +109,16 @@ def processLargeText(app: any, chunks: any):
|
|
| 108 |
|
| 109 |
def getCompletion(
|
| 110 |
query,
|
| 111 |
-
model=
|
| 112 |
uuid="",
|
| 113 |
image_search=True,
|
| 114 |
):
|
| 115 |
-
llm =
|
| 116 |
-
|
| 117 |
# Break input text into chunks
|
| 118 |
chunks = getChunks(query)
|
| 119 |
|
| 120 |
app = LLMRails(config, llm)
|
|
|
|
| 121 |
return processLargeText(app, chunks)
|
| 122 |
|
| 123 |
|
|
|
|
| 14 |
|
| 15 |
from firebase_admin import storage
|
| 16 |
|
| 17 |
+
from .llm.llms import get_llm, GPT_4, FALCON_7B
|
| 18 |
from ..common.utils import (
|
| 19 |
OPENAI_API_KEY,
|
| 20 |
FIREBASE_STORAGE_ROOT,
|
| 21 |
+
DEFAULT_GPT_MODEL,
|
| 22 |
parseJsonFromCompletion,
|
| 23 |
)
|
| 24 |
from .image_embedding import (
|
|
|
|
| 51 |
]
|
| 52 |
)
|
| 53 |
result = json.dumps(message["content"])
|
|
|
|
| 54 |
return parseJsonFromCompletion(result)
|
| 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."
|
|
|
|
| 109 |
|
| 110 |
def getCompletion(
|
| 111 |
query,
|
| 112 |
+
model=DEFAULT_GPT_MODEL,
|
| 113 |
uuid="",
|
| 114 |
image_search=True,
|
| 115 |
):
|
| 116 |
+
llm = get_llm(model=DEFAULT_GPT_MODEL).get_llm()
|
|
|
|
| 117 |
# Break input text into chunks
|
| 118 |
chunks = getChunks(query)
|
| 119 |
|
| 120 |
app = LLMRails(config, llm)
|
| 121 |
+
|
| 122 |
return processLargeText(app, chunks)
|
| 123 |
|
| 124 |
|
src/router/api.py
CHANGED
|
@@ -2,6 +2,7 @@ import json
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
from src.common.assembler import Assembler
|
|
|
|
| 5 |
from src.common.utils import ProgramType
|
| 6 |
from src.model.image_model import ImageModel
|
| 7 |
from src.model.requests.request_model import (
|
|
@@ -77,13 +78,17 @@ def construct_blueprint_api() -> APIRouter:
|
|
| 77 |
uuid=uuid, search=result["content"]
|
| 78 |
)
|
| 79 |
result["content"] = str(contacts_results)
|
| 80 |
-
except Exception as e:
|
| 81 |
-
logger.error(title="sendNotification", message=json.dumps(result))
|
| 82 |
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
"""@generator.response(
|
| 89 |
status_code=200, schema={"message": "message", "result": "test_result"}
|
|
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
from src.common.assembler import Assembler
|
| 5 |
+
from src.common.brain_exception import BrainException
|
| 6 |
from src.common.utils import ProgramType
|
| 7 |
from src.model.image_model import ImageModel
|
| 8 |
from src.model.requests.request_model import (
|
|
|
|
| 78 |
uuid=uuid, search=result["content"]
|
| 79 |
)
|
| 80 |
result["content"] = str(contacts_results)
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
notification = {"title": "alert", "content": json.dumps(result)}
|
| 83 |
|
| 84 |
+
state, value = send_message(notification, [token])
|
| 85 |
+
return assembler.to_response(200, value, result)
|
| 86 |
+
except Exception as e:
|
| 87 |
+
logger.error(
|
| 88 |
+
title="sendNotification", message="json parsing or get completion error"
|
| 89 |
+
)
|
| 90 |
+
if isinstance(e, BrainException):
|
| 91 |
+
return e.get_response_exp()
|
| 92 |
|
| 93 |
"""@generator.response(
|
| 94 |
status_code=200, schema={"message": "message", "result": "test_result"}
|
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 |
|