Spaces:
Sleeping
Sleeping
Zwea Htet
commited on
Commit
·
e058c61
1
Parent(s):
c6e1770
added prompts
Browse files- assets/prompts/__init__.py +0 -0
- assets/prompts/custom_prompts.py +20 -0
- models/llamaCustom.py +11 -1
assets/prompts/__init__.py
ADDED
|
File without changes
|
assets/prompts/custom_prompts.py
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
text_qa_template_str = (
|
| 2 |
+
"You are a helpful assistant that can give users information about california water regulations."
|
| 3 |
+
"Your task is to answer the question based on the context below and using your own knowledge.\n"
|
| 4 |
+
"Context: {context_str}\n"
|
| 5 |
+
"Question: {query_str}\n"
|
| 6 |
+
"Answer: \n"
|
| 7 |
+
"If the question is relevant, provide the name of the chapter, the article and the section."
|
| 8 |
+
"If possible, also provide the page number of the document from which your answer was found.\n"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
refine_template_str = (
|
| 12 |
+
"The original question is as follows: {query_str}\n"
|
| 13 |
+
"We have provided an existing answer: {existing_answer}\n"
|
| 14 |
+
"We have the opportunity to refine the existing answer "
|
| 15 |
+
"(only if needed) with some more context below.\n"
|
| 16 |
+
"------------\n"
|
| 17 |
+
"{context_msg}\n"
|
| 18 |
+
"------------\n"
|
| 19 |
+
"Using both the new context and your own knowledege, update or repeat the existing answer.\n"
|
| 20 |
+
)
|
models/llamaCustom.py
CHANGED
|
@@ -21,8 +21,11 @@ from llama_index import (
|
|
| 21 |
load_index_from_storage,
|
| 22 |
)
|
| 23 |
from llama_index.llms import CompletionResponse, CustomLLM, LLMMetadata
|
|
|
|
| 24 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 25 |
|
|
|
|
|
|
|
| 26 |
load_dotenv()
|
| 27 |
# openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 28 |
fs = HfFileSystem()
|
|
@@ -35,6 +38,10 @@ NUM_OUTPUT = 525
|
|
| 35 |
# set maximum chunk overlap
|
| 36 |
CHUNK_OVERLAP_RATION = 0.2
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
@st.cache_resource
|
| 40 |
def load_model(model_name: str):
|
|
@@ -136,7 +143,10 @@ class LlamaCustom:
|
|
| 136 |
|
| 137 |
def get_response(self, query_str):
|
| 138 |
print("query_str: ", query_str)
|
| 139 |
-
query_engine = self.vector_index.as_query_engine()
|
|
|
|
|
|
|
|
|
|
| 140 |
response = query_engine.query(query_str)
|
| 141 |
print("metadata: ", response.metadata)
|
| 142 |
return str(response)
|
|
|
|
| 21 |
load_index_from_storage,
|
| 22 |
)
|
| 23 |
from llama_index.llms import CompletionResponse, CustomLLM, LLMMetadata
|
| 24 |
+
from llama_index.prompts import Prompt
|
| 25 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 26 |
|
| 27 |
+
from assets.prompts import custom_prompts
|
| 28 |
+
|
| 29 |
load_dotenv()
|
| 30 |
# openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 31 |
fs = HfFileSystem()
|
|
|
|
| 38 |
# set maximum chunk overlap
|
| 39 |
CHUNK_OVERLAP_RATION = 0.2
|
| 40 |
|
| 41 |
+
text_qa_template = Prompt(custom_prompts.text_qa_template_str)
|
| 42 |
+
|
| 43 |
+
refine_template = Prompt(custom_prompts.refine_template_str)
|
| 44 |
+
|
| 45 |
|
| 46 |
@st.cache_resource
|
| 47 |
def load_model(model_name: str):
|
|
|
|
| 143 |
|
| 144 |
def get_response(self, query_str):
|
| 145 |
print("query_str: ", query_str)
|
| 146 |
+
# query_engine = self.vector_index.as_query_engine()
|
| 147 |
+
query_engine = self.vector_index.as_query_engine(
|
| 148 |
+
text_qa_template=text_qa_template, refine_template=refine_template
|
| 149 |
+
)
|
| 150 |
response = query_engine.query(query_str)
|
| 151 |
print("metadata: ", response.metadata)
|
| 152 |
return str(response)
|