Spaces:
Runtime error
Runtime error
Commit ·
46a1571
1
Parent(s): 694c2b1
Update app.py
Browse files- Modification du prompt caché
- Modification des paramètres de chunk
-Ajout de l'argument similarity_to_k dans le query engine
app.py
CHANGED
|
@@ -41,22 +41,19 @@ def construct_index(doc):
|
|
| 41 |
|
| 42 |
## Define the prompt helper
|
| 43 |
# Set maximum input size
|
| 44 |
-
max_input_size =
|
| 45 |
|
| 46 |
# Set number of output tokens
|
| 47 |
-
num_output =
|
| 48 |
|
| 49 |
#Set the chunk size limit
|
| 50 |
chunk_size_limit = 600 # About 450 words ~ 1 page
|
| 51 |
|
| 52 |
-
# Set maximum chunk overlap
|
| 53 |
-
max_chunk_overlap = 1
|
| 54 |
-
|
| 55 |
# Set chunk overlap ratio
|
| 56 |
chunk_overlap_ratio = 0.2
|
| 57 |
|
| 58 |
# Define prompt helper
|
| 59 |
-
prompt_helper = PromptHelper(max_input_size, num_output,
|
| 60 |
|
| 61 |
## Define the LLM predictor
|
| 62 |
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.4, model_name="gpt-4-32k", max_tokens=num_output))
|
|
@@ -114,15 +111,17 @@ def extract_name(file):
|
|
| 114 |
|
| 115 |
def ask_ai_upload(doc, question):
|
| 116 |
file_name = extract_name(doc)
|
|
|
|
| 117 |
try:
|
| 118 |
storage_context = StorageContext.from_defaults(persist_dir=f'gpt/storage_demo/{file_name}', fs=fs)
|
| 119 |
# Load index
|
| 120 |
index = load_index_from_storage(storage_context)
|
|
|
|
| 121 |
except:
|
|
|
|
| 122 |
text = extract_text(doc)
|
| 123 |
index = construct_index([Document(text)])
|
| 124 |
|
| 125 |
-
|
| 126 |
# Save index to Azure blob storage
|
| 127 |
index.storage_context.persist(f'gpt/storage_demo/{file_name}', fs=fs)
|
| 128 |
|
|
@@ -133,9 +132,8 @@ def ask_ai_upload(doc, question):
|
|
| 133 |
index = load_index_from_storage(storage_context)
|
| 134 |
|
| 135 |
# Define the query & the querying method
|
| 136 |
-
query_engine = index.as_query_engine(optimizer=SentenceEmbeddingOptimizer(percentile_cutoff=0.8))
|
| 137 |
-
query = '
|
| 138 |
-
response = query_engine.query(query)
|
| 139 |
|
| 140 |
return response.response
|
| 141 |
|
|
@@ -158,9 +156,8 @@ def ask_ai_choose(doc, question):
|
|
| 158 |
index = load_index_from_storage(storage_context)
|
| 159 |
|
| 160 |
# Define the query & the querying method
|
| 161 |
-
query_engine = index.as_query_engine(optimizer=SentenceEmbeddingOptimizer(percentile_cutoff=0.8))
|
| 162 |
-
query = '
|
| 163 |
-
response = query_engine.query(query)
|
| 164 |
|
| 165 |
return response.response
|
| 166 |
|
|
|
|
| 41 |
|
| 42 |
## Define the prompt helper
|
| 43 |
# Set maximum input size
|
| 44 |
+
max_input_size = 32000
|
| 45 |
|
| 46 |
# Set number of output tokens
|
| 47 |
+
num_output = 600 # About 300 words
|
| 48 |
|
| 49 |
#Set the chunk size limit
|
| 50 |
chunk_size_limit = 600 # About 450 words ~ 1 page
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
# Set chunk overlap ratio
|
| 53 |
chunk_overlap_ratio = 0.2
|
| 54 |
|
| 55 |
# Define prompt helper
|
| 56 |
+
prompt_helper = PromptHelper(max_input_size, num_output, chunk_size_limit, chunk_overlap_ratio)
|
| 57 |
|
| 58 |
## Define the LLM predictor
|
| 59 |
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.4, model_name="gpt-4-32k", max_tokens=num_output))
|
|
|
|
| 111 |
|
| 112 |
def ask_ai_upload(doc, question):
|
| 113 |
file_name = extract_name(doc)
|
| 114 |
+
|
| 115 |
try:
|
| 116 |
storage_context = StorageContext.from_defaults(persist_dir=f'gpt/storage_demo/{file_name}', fs=fs)
|
| 117 |
# Load index
|
| 118 |
index = load_index_from_storage(storage_context)
|
| 119 |
+
|
| 120 |
except:
|
| 121 |
+
# Construct index
|
| 122 |
text = extract_text(doc)
|
| 123 |
index = construct_index([Document(text)])
|
| 124 |
|
|
|
|
| 125 |
# Save index to Azure blob storage
|
| 126 |
index.storage_context.persist(f'gpt/storage_demo/{file_name}', fs=fs)
|
| 127 |
|
|
|
|
| 132 |
index = load_index_from_storage(storage_context)
|
| 133 |
|
| 134 |
# Define the query & the querying method
|
| 135 |
+
query_engine = index.as_query_engine(optimizer=SentenceEmbeddingOptimizer(percentile_cutoff=0.8), similarity_top_k=7)
|
| 136 |
+
query = 'Answer the question truthfully based on the text provided. Include verbatim quote and after the quote write a step by step explanation. Use bullet points. Provide an answer as detailed and precise as possible. The task is:' + str(question) response = query_engine.query(query)
|
|
|
|
| 137 |
|
| 138 |
return response.response
|
| 139 |
|
|
|
|
| 156 |
index = load_index_from_storage(storage_context)
|
| 157 |
|
| 158 |
# Define the query & the querying method
|
| 159 |
+
query_engine = index.as_query_engine(optimizer=SentenceEmbeddingOptimizer(percentile_cutoff=0.8), similarity_top_k=7)
|
| 160 |
+
query = 'Answer the question truthfully based on the text provided. Include verbatim quote and after the quote write a step by step explanation. Use bullet points. Provide an answer as detailed and precise as possible. The task is:' + str(question) response = query_engine.query(query)
|
|
|
|
| 161 |
|
| 162 |
return response.response
|
| 163 |
|