Spaces:
Running
Running
Update model.py
Browse files
model.py
CHANGED
@@ -935,12 +935,29 @@ def merge_metadata_outputs(metadata_list):
|
|
935 |
return merged
|
936 |
|
937 |
|
938 |
-
def query_document_info(query_word, alternative_query_word, metadata, master_structured_lookup, faiss_index, document_chunks, llm_api_function, chunk=None, all_output=None):
|
939 |
"""
|
940 |
Queries the document using a hybrid approach:
|
941 |
1. Local structured lookup (fast, cheap, accurate for known patterns).
|
942 |
2. RAG with semantic search and LLM (general, flexible, cost-optimized).
|
943 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
944 |
if metadata:
|
945 |
extracted_country, extracted_specific_location, extracted_ethnicity, extracted_type = metadata["country"], metadata["specific_location"], metadata["ethnicity"], metadata["sample_type"]
|
946 |
extracted_col_date, extracted_iso, extracted_title, extracted_features = metadata["collection_date"], metadata["isolate"], metadata["title"], metadata["all_features"]
|
@@ -1097,7 +1114,6 @@ def query_document_info(query_word, alternative_query_word, metadata, master_str
|
|
1097 |
# run_rag = (extracted_country == 'unknown' or extracted_type == 'unknown')# or \
|
1098 |
# #extracted_ethnicity == 'unknown' or extracted_specific_location == 'unknown')
|
1099 |
run_rag = True
|
1100 |
-
global_llm_model_for_counting_tokens = genai.GenerativeModel("gemini-2.5-flash-lite")#('gemini-1.5-flash-latest')
|
1101 |
if run_rag:
|
1102 |
print("try run rag")
|
1103 |
# Determine the phrase for LLM query
|
@@ -1239,6 +1255,23 @@ def query_document_info(query_word, alternative_query_word, metadata, master_str
|
|
1239 |
# f"If the {explain_list} is not 'unknown', write 1 sentence after the output explaining how you inferred it from the text (one sentence for each)."
|
1240 |
# f"\n\nText Snippets:\n{context_for_llm}\n\n"
|
1241 |
# f"Output Format: {output_format_str}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1242 |
# )
|
1243 |
prompt_for_llm = (
|
1244 |
f"{prompt_instruction_prefix}"
|
@@ -1253,12 +1286,24 @@ def query_document_info(query_word, alternative_query_word, metadata, master_str
|
|
1253 |
f"If the text does not specify ancient or archaeological context, assume 'modern'. "
|
1254 |
f"Provide only {output_format_str}. "
|
1255 |
f"If any information is not explicitly present, use the fallback rules above before defaulting to 'unknown'. "
|
1256 |
-
f"For each non-'unknown' field in {explain_list}, write one sentence explaining how it was inferred from the text
|
1257 |
-
f"
|
1258 |
-
f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1259 |
)
|
1260 |
-
|
1261 |
-
|
|
|
|
|
|
|
|
|
|
|
1262 |
print("\n--- DEBUG INFO FOR RAG ---")
|
1263 |
print("Retrieved Context Sent to LLM (first 500 chars):")
|
1264 |
print(context_for_llm[:500] + "..." if len(context_for_llm) > 500 else context_for_llm)
|
|
|
935 |
return merged
|
936 |
|
937 |
|
938 |
+
def query_document_info(query_word, alternative_query_word, metadata, master_structured_lookup, faiss_index, document_chunks, llm_api_function, chunk=None, all_output=None, model_ai=None):
|
939 |
"""
|
940 |
Queries the document using a hybrid approach:
|
941 |
1. Local structured lookup (fast, cheap, accurate for known patterns).
|
942 |
2. RAG with semantic search and LLM (general, flexible, cost-optimized).
|
943 |
"""
|
944 |
+
if model_ai:
|
945 |
+
if model_ai == "gemini-1.5-flash-latest":
|
946 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
947 |
+
PRICE_PER_1K_INPUT_LLM = 0.000075 # $0.075 per 1M tokens
|
948 |
+
PRICE_PER_1K_OUTPUT_LLM = 0.0003 # $0.30 per 1M tokens
|
949 |
+
PRICE_PER_1K_EMBEDDING_INPUT = 0.000025 # $0.025 per 1M tokens
|
950 |
+
global_llm_model_for_counting_tokens = genai.GenerativeModel("gemini-1.5-flash-latest")#('gemini-1.5-flash-latest')
|
951 |
+
else:
|
952 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY_BACKUP"))
|
953 |
+
# Gemini 2.5 Flash-Lite pricing per 1,000 tokens
|
954 |
+
PRICE_PER_1K_INPUT_LLM = 0.00010 # $0.10 per 1M input tokens
|
955 |
+
PRICE_PER_1K_OUTPUT_LLM = 0.00040 # $0.40 per 1M output tokens
|
956 |
+
|
957 |
+
# Embedding-001 pricing per 1,000 input tokens
|
958 |
+
PRICE_PER_1K_EMBEDDING_INPUT = 0.00015 # $0.15 per 1M input tokens
|
959 |
+
global_llm_model_for_counting_tokens = genai.GenerativeModel("gemini-2.5-flash-lite")#('gemini-1.5-flash-latest')
|
960 |
+
|
961 |
if metadata:
|
962 |
extracted_country, extracted_specific_location, extracted_ethnicity, extracted_type = metadata["country"], metadata["specific_location"], metadata["ethnicity"], metadata["sample_type"]
|
963 |
extracted_col_date, extracted_iso, extracted_title, extracted_features = metadata["collection_date"], metadata["isolate"], metadata["title"], metadata["all_features"]
|
|
|
1114 |
# run_rag = (extracted_country == 'unknown' or extracted_type == 'unknown')# or \
|
1115 |
# #extracted_ethnicity == 'unknown' or extracted_specific_location == 'unknown')
|
1116 |
run_rag = True
|
|
|
1117 |
if run_rag:
|
1118 |
print("try run rag")
|
1119 |
# Determine the phrase for LLM query
|
|
|
1255 |
# f"If the {explain_list} is not 'unknown', write 1 sentence after the output explaining how you inferred it from the text (one sentence for each)."
|
1256 |
# f"\n\nText Snippets:\n{context_for_llm}\n\n"
|
1257 |
# f"Output Format: {output_format_str}"
|
1258 |
+
# )
|
1259 |
+
# prompt_for_llm = (
|
1260 |
+
# f"{prompt_instruction_prefix}"
|
1261 |
+
# f"Given the following text snippets, analyze the entity/concept {rag_query_phrase} "
|
1262 |
+
# f"or the mitochondrial DNA sample in {organism} if these identifiers are not explicitly found. "
|
1263 |
+
# f"Identify its **primary associated geographic location**, preferring the most specific available: "
|
1264 |
+
# f"first try to determine the exact country; if no country is explicitly mentioned, then provide "
|
1265 |
+
# f"the next most specific region, continent, island, or other clear geographic area mentioned. "
|
1266 |
+
# f"If no geographic clues at all are present, state 'unknown' for location. "
|
1267 |
+
# f"Also, determine if the genetic sample is from a 'modern' (present-day living individual) "
|
1268 |
+
# f"or 'ancient' (prehistoric/archaeological) source. "
|
1269 |
+
# f"If the text does not specify ancient or archaeological context, assume 'modern'. "
|
1270 |
+
# f"Provide only {output_format_str}. "
|
1271 |
+
# f"If any information is not explicitly present, use the fallback rules above before defaulting to 'unknown'. "
|
1272 |
+
# f"For each non-'unknown' field in {explain_list}, write one sentence explaining how it was inferred from the text (one sentence for each)."
|
1273 |
+
# f"\n\nText Snippets:\n{context_for_llm}\n\n"
|
1274 |
+
# f"Output Format: {output_format_str}"
|
1275 |
# )
|
1276 |
prompt_for_llm = (
|
1277 |
f"{prompt_instruction_prefix}"
|
|
|
1286 |
f"If the text does not specify ancient or archaeological context, assume 'modern'. "
|
1287 |
f"Provide only {output_format_str}. "
|
1288 |
f"If any information is not explicitly present, use the fallback rules above before defaulting to 'unknown'. "
|
1289 |
+
f"For each non-'unknown' field in {explain_list}, write one sentence explaining how it was inferred from the text "
|
1290 |
+
f"(one sentence for each). "
|
1291 |
+
f"Format your answer so that:\n"
|
1292 |
+
f"1. The **first line** contains only the {output_format_str} answer.\n"
|
1293 |
+
f"2. The **second line onward** contains the explanations.\n"
|
1294 |
+
f"\nText Snippets:\n{context_for_llm}\n\n"
|
1295 |
+
f"Output Format Example:\nChina, modern, Daur, Heilongjiang province.\n"
|
1296 |
+
f"The text explicitly states \"chinese Daur ethnic group in Heilongjiang province\", indicating the country, "
|
1297 |
+
f"the ethnicity, and the specific province. The study is published in a journal, implying research on living "
|
1298 |
+
f"individuals, hence modern."
|
1299 |
)
|
1300 |
+
|
1301 |
+
if model_ai:
|
1302 |
+
print("back up to ", model_ai)
|
1303 |
+
llm_response_text, model_instance = call_llm_api(prompt_for_llm, model=model_ai)
|
1304 |
+
else:
|
1305 |
+
print("still 2.5 flash gemini")
|
1306 |
+
llm_response_text, model_instance = call_llm_api(prompt_for_llm)
|
1307 |
print("\n--- DEBUG INFO FOR RAG ---")
|
1308 |
print("Retrieved Context Sent to LLM (first 500 chars):")
|
1309 |
print(context_for_llm[:500] + "..." if len(context_for_llm) > 500 else context_for_llm)
|