Update app.py
Browse files
app.py
CHANGED
|
@@ -409,6 +409,36 @@ def get_context_for_query(query, selected_docs):
|
|
| 409 |
else:
|
| 410 |
return "No documents available to answer the query."
|
| 411 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2, search_type="pdf"):
|
| 413 |
headers = {
|
| 414 |
"Authorization": f"Bearer {API_TOKEN}",
|
|
@@ -420,15 +450,19 @@ def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperatur
|
|
| 420 |
instruction = f"""Using the following context from the PDF documents:
|
| 421 |
{context}
|
| 422 |
Write a detailed and complete response that answers the following user question: '{query}'"""
|
| 423 |
-
|
| 424 |
instruction = f"""Using the following context:
|
| 425 |
{context}
|
| 426 |
Write a detailed and complete research document that fulfills the following user request: '{query}'
|
| 427 |
After writing the document, please provide a list of sources used in your response."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
|
| 429 |
inputs = [
|
| 430 |
{"role": "system", "content": instruction},
|
| 431 |
-
{"role": "user", "content": query}
|
| 432 |
]
|
| 433 |
|
| 434 |
payload = {
|
|
@@ -492,18 +526,17 @@ def get_response_with_search(query, model, num_calls=3, temperature=0.1):
|
|
| 492 |
{context}
|
| 493 |
Write a detailed and complete research document that fulfills the following user request: '{query}'
|
| 494 |
After writing the document, please provide a list of sources used in your response.
|
| 495 |
-
Importantly, only include information that is directly supported by the provided context. If you're unsure about any information, state that it couldn't be verified from the given context.
|
| 496 |
-
After writing the document, please provide a list of sources used in your response."""
|
| 497 |
|
|
|
|
| 498 |
if model == "@cf/meta/llama-3.1-8b-instruct":
|
| 499 |
# Use Cloudflare API
|
| 500 |
for response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature, search_type="web"):
|
| 501 |
-
|
| 502 |
else:
|
| 503 |
# Use Hugging Face API
|
| 504 |
client = InferenceClient(model, token=huggingface_token)
|
| 505 |
|
| 506 |
-
main_content = ""
|
| 507 |
for i in range(num_calls):
|
| 508 |
for message in client.chat_completion(
|
| 509 |
messages=[{"role": "user", "content": prompt}],
|
|
@@ -513,8 +546,11 @@ After writing the document, please provide a list of sources used in your respon
|
|
| 513 |
):
|
| 514 |
if message.choices and message.choices[0].delta and message.choices[0].delta.content:
|
| 515 |
chunk = message.choices[0].delta.content
|
| 516 |
-
|
| 517 |
-
|
|
|
|
|
|
|
|
|
|
| 518 |
|
| 519 |
|
| 520 |
INSTRUCTION_PROMPTS = {
|
|
|
|
| 409 |
else:
|
| 410 |
return "No documents available to answer the query."
|
| 411 |
|
| 412 |
+
def validate_response(initial_response, context, query, model, temperature=0.1):
|
| 413 |
+
validation_prompt = f"""Given the following context and initial response to the query "{query}":
|
| 414 |
+
|
| 415 |
+
Context:
|
| 416 |
+
{context}
|
| 417 |
+
|
| 418 |
+
Initial Response:
|
| 419 |
+
{initial_response}
|
| 420 |
+
|
| 421 |
+
Please validate the initial response against the provided context. Remove any hallucinations, irrelevant, or factually incorrect information. Generate a revised response that is accurate and directly supported by the context. If any information cannot be verified from the context, state that explicitly.
|
| 422 |
+
|
| 423 |
+
Revised Response:
|
| 424 |
+
"""
|
| 425 |
+
|
| 426 |
+
if model == "@cf/meta/llama-3.1-8b-instruct":
|
| 427 |
+
return get_response_from_cloudflare(prompt=validation_prompt, context="", query="", num_calls=1, temperature=temperature, search_type="validation")
|
| 428 |
+
else:
|
| 429 |
+
client = InferenceClient(model, token=huggingface_token)
|
| 430 |
+
revised_response = ""
|
| 431 |
+
for message in client.chat_completion(
|
| 432 |
+
messages=[{"role": "user", "content": validation_prompt}],
|
| 433 |
+
max_tokens=10000,
|
| 434 |
+
temperature=temperature,
|
| 435 |
+
stream=True,
|
| 436 |
+
):
|
| 437 |
+
if message.choices and message.choices[0].delta and message.choices[0].delta.content:
|
| 438 |
+
chunk = message.choices[0].delta.content
|
| 439 |
+
revised_response += chunk
|
| 440 |
+
yield revised_response
|
| 441 |
+
|
| 442 |
def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2, search_type="pdf"):
|
| 443 |
headers = {
|
| 444 |
"Authorization": f"Bearer {API_TOKEN}",
|
|
|
|
| 450 |
instruction = f"""Using the following context from the PDF documents:
|
| 451 |
{context}
|
| 452 |
Write a detailed and complete response that answers the following user question: '{query}'"""
|
| 453 |
+
elif search_type == "web":
|
| 454 |
instruction = f"""Using the following context:
|
| 455 |
{context}
|
| 456 |
Write a detailed and complete research document that fulfills the following user request: '{query}'
|
| 457 |
After writing the document, please provide a list of sources used in your response."""
|
| 458 |
+
elif search_type == "validation":
|
| 459 |
+
instruction = prompt # For validation, use the provided prompt directly
|
| 460 |
+
else:
|
| 461 |
+
raise ValueError("Invalid search_type")
|
| 462 |
|
| 463 |
inputs = [
|
| 464 |
{"role": "system", "content": instruction},
|
| 465 |
+
{"role": "user", "content": query if search_type != "validation" else ""}
|
| 466 |
]
|
| 467 |
|
| 468 |
payload = {
|
|
|
|
| 526 |
{context}
|
| 527 |
Write a detailed and complete research document that fulfills the following user request: '{query}'
|
| 528 |
After writing the document, please provide a list of sources used in your response.
|
| 529 |
+
Importantly, only include information that is directly supported by the provided context. If you're unsure about any information, state that it couldn't be verified from the given context."""
|
|
|
|
| 530 |
|
| 531 |
+
initial_response = ""
|
| 532 |
if model == "@cf/meta/llama-3.1-8b-instruct":
|
| 533 |
# Use Cloudflare API
|
| 534 |
for response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature, search_type="web"):
|
| 535 |
+
initial_response = response
|
| 536 |
else:
|
| 537 |
# Use Hugging Face API
|
| 538 |
client = InferenceClient(model, token=huggingface_token)
|
| 539 |
|
|
|
|
| 540 |
for i in range(num_calls):
|
| 541 |
for message in client.chat_completion(
|
| 542 |
messages=[{"role": "user", "content": prompt}],
|
|
|
|
| 546 |
):
|
| 547 |
if message.choices and message.choices[0].delta and message.choices[0].delta.content:
|
| 548 |
chunk = message.choices[0].delta.content
|
| 549 |
+
initial_response += chunk
|
| 550 |
+
|
| 551 |
+
# Validation step
|
| 552 |
+
for revised_response in validate_response(initial_response, context, query, model, temperature):
|
| 553 |
+
yield revised_response, "" # Yield streaming revised response without sources
|
| 554 |
|
| 555 |
|
| 556 |
INSTRUCTION_PROMPTS = {
|