Spaces:
Running
Running
Hammaad
commited on
Commit
•
2e4fd26
1
Parent(s):
87b6d59
re configured the prompts
Browse files- CHANGELOG.txt +2 -1
- app.py +16 -11
- reggpt/configs/logger.py +7 -3
- reggpt/controller/agent.py +5 -1
- reggpt/logs/log +0 -0
- reggpt/prompts/general.py +15 -1
- reggpt/prompts/retrieval.py +30 -4
- reggpt/prompts/router.py +38 -1
- reggpt/routers/qa.py +2 -0
CHANGELOG.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
2023-11-30 pipeline with only document retrieval
|
2 |
2024-08-23 azure app serice , open ai 'gpt4o mini'
|
3 |
-
2024-10-16 Code Refactor
|
|
|
|
1 |
2023-11-30 pipeline with only document retrieval
|
2 |
2024-08-23 azure app serice , open ai 'gpt4o mini'
|
3 |
+
2024-10-16 Code Refactor
|
4 |
+
2024-10-21 Reconfogured the prompts from open ai to mistral, fixed agent changes
|
app.py
CHANGED
@@ -25,6 +25,11 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
25 |
from reggpt.configs.api import API_TITLE, API_VERSION, API_DESCRIPTION
|
26 |
from reggpt.api.router import ChatAPI
|
27 |
|
|
|
|
|
|
|
|
|
|
|
28 |
def filer():
|
29 |
return "reggpt/logs/log"
|
30 |
# today = datetime.datetime.today()
|
@@ -32,19 +37,19 @@ def filer():
|
|
32 |
# return log_filename
|
33 |
|
34 |
|
35 |
-
file_handler = logging.FileHandler(filer())
|
36 |
-
# file_handler = logging.handlers.TimedRotatingFileHandler(filer(),when="D")
|
37 |
-
file_handler.setLevel(logging.INFO)
|
38 |
|
39 |
-
logging.basicConfig(
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
)
|
46 |
|
47 |
-
logger = logging.getLogger(__name__)
|
48 |
|
49 |
load_dotenv()
|
50 |
host = os.environ.get("APP_HOST")
|
|
|
25 |
from reggpt.configs.api import API_TITLE, API_VERSION, API_DESCRIPTION
|
26 |
from reggpt.api.router import ChatAPI
|
27 |
|
28 |
+
from dotenv import load_dotenv
|
29 |
+
import os
|
30 |
+
|
31 |
+
load_dotenv()
|
32 |
+
|
33 |
def filer():
|
34 |
return "reggpt/logs/log"
|
35 |
# today = datetime.datetime.today()
|
|
|
37 |
# return log_filename
|
38 |
|
39 |
|
40 |
+
# file_handler = logging.FileHandler(filer())
|
41 |
+
# # file_handler = logging.handlers.TimedRotatingFileHandler(filer(),when="D")
|
42 |
+
# file_handler.setLevel(logging.INFO)
|
43 |
|
44 |
+
# logging.basicConfig(
|
45 |
+
# level=logging.DEBUG,
|
46 |
+
# format="%(asctime)s %(levelname)s (%(name)s) : %(message)s",
|
47 |
+
# datefmt="%Y-%m-%d %H:%M:%S",
|
48 |
+
# handlers=[file_handler],
|
49 |
+
# force=True,
|
50 |
+
# )
|
51 |
|
52 |
+
# logger = logging.getLogger(__name__)
|
53 |
|
54 |
load_dotenv()
|
55 |
host = os.environ.get("APP_HOST")
|
reggpt/configs/logger.py
CHANGED
@@ -1,15 +1,17 @@
|
|
1 |
import logging
|
2 |
import time
|
3 |
-
|
|
|
|
|
|
|
4 |
|
5 |
logger = logging.getLogger(__name__)
|
6 |
|
7 |
stream_handler = logging.StreamHandler()
|
8 |
log_filename = "output.log"
|
9 |
-
file_handler = logging.FileHandler(filename=log_filename)
|
10 |
handlers = [stream_handler, file_handler]
|
11 |
|
12 |
-
|
13 |
class TimeFilter(logging.Filter):
|
14 |
def filter(self, record):
|
15 |
return "Running" in record.getMessage()
|
@@ -24,6 +26,8 @@ logging.basicConfig(
|
|
24 |
handlers=handlers,
|
25 |
)
|
26 |
|
|
|
|
|
27 |
|
28 |
def time_logger(func):
|
29 |
"""Decorator function to log time taken by any function."""
|
|
|
1 |
import logging
|
2 |
import time
|
3 |
+
import sys
|
4 |
+
import codecs
|
5 |
+
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
|
6 |
+
sys.stderr = codecs.getwriter("utf-8")(sys.stderr.detach())
|
7 |
|
8 |
logger = logging.getLogger(__name__)
|
9 |
|
10 |
stream_handler = logging.StreamHandler()
|
11 |
log_filename = "output.log"
|
12 |
+
file_handler = logging.FileHandler(filename=log_filename , encoding='utf-8')
|
13 |
handlers = [stream_handler, file_handler]
|
14 |
|
|
|
15 |
class TimeFilter(logging.Filter):
|
16 |
def filter(self, record):
|
17 |
return "Running" in record.getMessage()
|
|
|
26 |
handlers=handlers,
|
27 |
)
|
28 |
|
29 |
+
import logging
|
30 |
+
|
31 |
|
32 |
def time_logger(func):
|
33 |
"""Decorator function to log time taken by any function."""
|
reggpt/controller/agent.py
CHANGED
@@ -15,10 +15,14 @@ from reggpt.routers.out_of_domain import run_out_of_domain_chain
|
|
15 |
from reggpt.routers.general import run_general_qa_chain
|
16 |
from reggpt.routers.qa import run_qa_chain
|
17 |
from reggpt.agents.qa_agent import run_qa_agent
|
18 |
-
|
|
|
|
|
|
|
19 |
|
20 |
def chain_selector(chain_type, query):
|
21 |
chain_type = chain_type.lower().strip()
|
|
|
22 |
logger.info(f"chain_selector : chain_type: {chain_type} Question: {query}")
|
23 |
|
24 |
if "greeting" in chain_type:
|
|
|
15 |
from reggpt.routers.general import run_general_qa_chain
|
16 |
from reggpt.routers.qa import run_qa_chain
|
17 |
from reggpt.agents.qa_agent import run_qa_agent
|
18 |
+
from langchain.globals import set_verbose
|
19 |
+
from langchain.globals import set_debug
|
20 |
+
set_verbose(True)
|
21 |
+
# set_debug(True)
|
22 |
|
23 |
def chain_selector(chain_type, query):
|
24 |
chain_type = chain_type.lower().strip()
|
25 |
+
print(chain_type)
|
26 |
logger.info(f"chain_selector : chain_type: {chain_type} Question: {query}")
|
27 |
|
28 |
if "greeting" in chain_type:
|
reggpt/logs/log
CHANGED
The diff for this file is too large to render.
See raw diff
|
|
reggpt/prompts/general.py
CHANGED
@@ -21,7 +21,21 @@ Additionally, it's important to note that this AI assistant has access to an int
|
|
21 |
Question: {question}
|
22 |
"""
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
# general_qa_chain_prompt = PromptTemplate(
|
25 |
# input_variables=["question"], template=general_qa_template_Mixtral_V0
|
26 |
# )
|
27 |
-
general_qa_chain_prompt = PromptTemplate.from_template(
|
|
|
21 |
Question: {question}
|
22 |
"""
|
23 |
|
24 |
+
general_qa_template_OPENAI_V0="""
|
25 |
+
You are an AI assistant developed by IronOne Technologies, specializing in providing expert guidance on the rules and regulations issued by the Central Bank of Sri Lanka.
|
26 |
+
You are capable of assisting users with accurate, up-to-date information regarding banking laws, financial regulations, compliance guidelines, and other directives set forth by the Central Bank of Sri lanka.
|
27 |
+
Greetings are used to say 'hello' and 'how are you?' and to say 'goodbye' and 'nice speaking with you.' and 'hi, I'm (user's name).'
|
28 |
+
Greetings are words used when we want to introduce ourselves to others and when we want to find out how someone is feeling.
|
29 |
+
|
30 |
+
You can only reply to the user's greetings.
|
31 |
+
If the question is a greeting, reply accordingly as the AI assistant of company boardpac.
|
32 |
+
If the quesiton is "What are your strengths?", "what can you do?" or "Who are you?". Reply with the capabilities of the AI assistant.
|
33 |
+
If the question is not related to greetings and Trading or invest , say that it is out of your domain.
|
34 |
+
If the question is not clear enough, ask for more details and don't try to make up answers.
|
35 |
+
|
36 |
+
Question: {question}
|
37 |
+
"""
|
38 |
# general_qa_chain_prompt = PromptTemplate(
|
39 |
# input_variables=["question"], template=general_qa_template_Mixtral_V0
|
40 |
# )
|
41 |
+
general_qa_chain_prompt = PromptTemplate.from_template(general_qa_template_OPENAI_V0)
|
reggpt/prompts/retrieval.py
CHANGED
@@ -4,13 +4,11 @@ retrieval_qa_template = (
|
|
4 |
"""<<SYS>>
|
5 |
You are the AI assistant of company 'boardpac' which provide services to company board members related to banking and financial sector.
|
6 |
|
7 |
-
please answer the question based on the chat history provided below. Answer should be short and simple as possible and on to the point.
|
8 |
-
<chat history>: {chat_history}
|
9 |
-
|
10 |
If the question is related to welcomes and greetings answer accordingly.
|
11 |
|
12 |
Else If the question is related to Banking and Financial Services Sector like Banking & Financial regulations, legal framework, governance framework, compliance requirements as per Central Bank regulations.
|
13 |
please answer the question based only on the information provided in following central bank documents published in various years.
|
|
|
14 |
The published year is mentioned as the metadata 'year' of each source document.
|
15 |
Please notice that content of a one document of a past year can updated by a new document from a recent year.
|
16 |
Always try to answer with latest information and mention the year which information extracted.
|
@@ -19,15 +17,43 @@ If you dont know the answer say you dont know, dont try to makeup answers. Dont
|
|
19 |
<</SYS>>
|
20 |
|
21 |
[INST]
|
|
|
|
|
22 |
<DOCUMENTS>
|
|
|
23 |
{context}
|
24 |
</DOCUMENTS>
|
25 |
|
|
|
26 |
Question : {question}[/INST]"""
|
27 |
)
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
retrieval_qa_chain_prompt = PromptTemplate(
|
31 |
input_variables=["question", "context", "chat_history"],
|
32 |
-
template=
|
33 |
)
|
|
|
4 |
"""<<SYS>>
|
5 |
You are the AI assistant of company 'boardpac' which provide services to company board members related to banking and financial sector.
|
6 |
|
|
|
|
|
|
|
7 |
If the question is related to welcomes and greetings answer accordingly.
|
8 |
|
9 |
Else If the question is related to Banking and Financial Services Sector like Banking & Financial regulations, legal framework, governance framework, compliance requirements as per Central Bank regulations.
|
10 |
please answer the question based only on the information provided in following central bank documents published in various years.
|
11 |
+
|
12 |
The published year is mentioned as the metadata 'year' of each source document.
|
13 |
Please notice that content of a one document of a past year can updated by a new document from a recent year.
|
14 |
Always try to answer with latest information and mention the year which information extracted.
|
|
|
17 |
<</SYS>>
|
18 |
|
19 |
[INST]
|
20 |
+
This is the context provided to answer the question.
|
21 |
+
|
22 |
<DOCUMENTS>
|
23 |
+
|
24 |
{context}
|
25 |
</DOCUMENTS>
|
26 |
|
27 |
+
Using the context provided above, please answer the following question.
|
28 |
Question : {question}[/INST]"""
|
29 |
)
|
30 |
|
31 |
+
retrieval_qa_OPENAI_template = (
|
32 |
+
"""
|
33 |
+
You are an AI assistant developed by IronOne Technologies, specializing in providing expert guidance on the rules and regulations issued by the Central Bank of Sri Lanka.
|
34 |
+
|
35 |
+
You assist users with accurate, up-to-date information regarding banking laws, financial regulations, compliance guidelines, and other directives set forth by the Central Bank of Sri Lanka. Please answer the question based only on the information provided in the following Central Bank documents published in various years.
|
36 |
+
|
37 |
+
The published year is mentioned as the metadata 'year' of each source document. Note that a document from a past year may be updated by a more recent document. Always try to answer with the latest information and mention the year the information was extracted. If you don't know the answer, say you don't know; do not fabricate responses or add any extra details not mentioned in the context.
|
38 |
+
|
39 |
+
Rephrasing Requirement: If the question includes the terms 'Sri Lanka', 'Central Bank of Sri Lanka,' 'CBSL,' or 'Central Bank,' first rephrase the question to refer to it simply as 'Central Bank.'
|
40 |
+
|
41 |
+
Answer the following question based only on the provided context in a simple, concise manner:
|
42 |
+
|
43 |
+
Context: {context}
|
44 |
+
Question: {question}
|
45 |
+
|
46 |
+
Important and Urgent Guidelines:
|
47 |
+
- Do not use symbols like #, *, etc., in your answer.
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
Always include the full reference to 'Central Bank of Sri Lanka' in your answer. Only return the answer.
|
52 |
+
"""
|
53 |
+
)
|
54 |
+
|
55 |
|
56 |
retrieval_qa_chain_prompt = PromptTemplate(
|
57 |
input_variables=["question", "context", "chat_history"],
|
58 |
+
template=retrieval_qa_OPENAI_template
|
59 |
)
|
reggpt/prompts/router.py
CHANGED
@@ -12,7 +12,44 @@ If a user asks a question you have to classify it to following 3 types Relevant,
|
|
12 |
|
13 |
Give the correct name of question type. If you are not sure return "Not Sure" instead.
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
Question : {question}
|
16 |
"""
|
17 |
# router_prompt=PromptTemplate(input_variables=["question"],template=router_template_Mixtral_V0)
|
18 |
-
router_prompt=PromptTemplate.from_template(
|
|
|
12 |
|
13 |
Give the correct name of question type. If you are not sure return "Not Sure" instead.
|
14 |
|
15 |
+
Question : {question}
|
16 |
+
"""
|
17 |
+
|
18 |
+
router_template_OPENAI_V0= """
|
19 |
+
You are an AI assistant developed by IronOne Technologies, specializing in providing expert guidance on the rules and regulations issued by the Central Bank of Sri Lanka.
|
20 |
+
|
21 |
+
If a user asks a question you have to classify it to following 3 categories Relevant, Greeting, or Complex.
|
22 |
+
|
23 |
+
"Relevant”: If the question is related to the Banking & Financial regulations, legal framework, governance framework, compliance requirements as per Central Bank regulations
|
24 |
+
"Complex" : If the question involves 2 or more sub questions using words like 'and' that are related to the Banking & Financial regulations, legal framework, governance framework, compliance requirements as per Central Bank regulations. For example 'What are the main macroeconomic objectives of the central bank and is adjusting the inflation rate any of them?'
|
25 |
+
"Greeting”: If the question is about yourself like 'who are you?' or if it is a greeting like good morning, hi my name is., thank you. Or if the question is about your strenghts and capabilities.
|
26 |
+
|
27 |
+
Here are some example questions and its relevant category.
|
28 |
+
Question: "What are the latest Ammenments to the Banking Act and when it was published?"
|
29 |
+
Category: Complex
|
30 |
+
|
31 |
+
Question: "What is the main objective of the Central Bank?"
|
32 |
+
Category: Relevant
|
33 |
+
|
34 |
+
Question: "Hi, I am John. Can you help me with the latest Banking regulations?"
|
35 |
+
Category: Greeting
|
36 |
+
|
37 |
+
Question: "What is the weather today?"
|
38 |
+
Category: Other
|
39 |
+
|
40 |
+
Question: "What is the current intrest rate in Sri lanka and what was the main target for imposing that rate?"
|
41 |
+
Category: Complex
|
42 |
+
|
43 |
+
Question: "What is the support given to Women Entrepreneurs by the CBSL?"
|
44 |
+
Category: Relevant
|
45 |
+
|
46 |
+
Question: "What are your strengths?"
|
47 |
+
Category: Greeting
|
48 |
+
|
49 |
+
Important and Urgent : check the user's question and answer and If the Question is not simillar to its category then give the answer as 'Other'.
|
50 |
+
Give the correct name of question type only. If you are not sure or the question is not clear return "Other" instead.
|
51 |
+
|
52 |
Question : {question}
|
53 |
"""
|
54 |
# router_prompt=PromptTemplate(input_variables=["question"],template=router_template_Mixtral_V0)
|
55 |
+
router_prompt=PromptTemplate.from_template(router_template_OPENAI_V0)
|
reggpt/routers/qa.py
CHANGED
@@ -43,6 +43,8 @@ def run_qa_chain(query):
|
|
43 |
start = time.time()
|
44 |
# res = qa_chain(query)
|
45 |
res = qa_chain.invoke({"question": query, "chat_history":""})
|
|
|
|
|
46 |
# res = response
|
47 |
# answer, docs = res['result'],res['source_documents']
|
48 |
end = time.time()
|
|
|
43 |
start = time.time()
|
44 |
# res = qa_chain(query)
|
45 |
res = qa_chain.invoke({"question": query, "chat_history":""})
|
46 |
+
if 'I dont know' in res["answer"] or "don't know" in res["answer"]:
|
47 |
+
res['answer'] = "I currently do not have the information to answer this question. Please rephrase your question or ask another question."
|
48 |
# res = response
|
49 |
# answer, docs = res['result'],res['source_documents']
|
50 |
end = time.time()
|