Spaces:
Sleeping
Sleeping
Commit
·
fb363a1
1
Parent(s):
904f39b
pushing version 2
Browse files
helper.py
CHANGED
|
@@ -3,6 +3,8 @@ from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
| 3 |
from pypdf import PdfReader
|
| 4 |
import requests
|
| 5 |
import json
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
def extract_text_from_pdf(pdf_path):
|
|
@@ -30,32 +32,81 @@ def embedding_function(texts):
|
|
| 30 |
def generate_hypothetical_answer(query):
|
| 31 |
import requests
|
| 32 |
import json
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# Prepare the request payload
|
| 41 |
payload = {
|
| 42 |
-
"
|
| 43 |
-
"
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
|
| 47 |
try:
|
| 48 |
-
# Make the API request to
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
response.raise_for_status() # Raise an exception for HTTP errors
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
# Parse the response
|
| 53 |
result = response.json()
|
| 54 |
|
| 55 |
# Extract the generated text
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
return generated_text.strip()
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
print(f"Error generating hypothetical answer: {e}")
|
| 61 |
return "Failed to generate a hypothetical answer."
|
|
@@ -63,49 +114,96 @@ def generate_hypothetical_answer(query):
|
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
-
def query_llm_with_context(query,context,top_n=3):
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# Use only the top N documents
|
| 71 |
-
top_docs =
|
| 72 |
|
| 73 |
# Create a context string by joining the top documents
|
| 74 |
-
|
| 75 |
|
| 76 |
# Create a prompt with the context and query
|
| 77 |
prompt = f"""
|
| 78 |
Context information is below.
|
| 79 |
---------------------
|
| 80 |
-
{
|
| 81 |
---------------------
|
| 82 |
|
| 83 |
Given the context information and not prior knowledge, answer the following query:
|
| 84 |
Query: {query}
|
| 85 |
"""
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
# Prepare the request payload
|
| 91 |
payload = {
|
| 92 |
-
"
|
| 93 |
-
"
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
}
|
| 96 |
|
| 97 |
try:
|
| 98 |
-
# Make the API request to
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
response.raise_for_status() # Raise an exception for HTTP errors
|
| 101 |
|
|
|
|
|
|
|
|
|
|
| 102 |
# Parse the response
|
| 103 |
result = response.json()
|
| 104 |
|
| 105 |
# Extract the generated text
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
return generated_text.strip()
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
except Exception as e:
|
| 110 |
print(f"Error querying LLM with context: {e}")
|
| 111 |
return "Failed to generate an answer with the provided context."
|
|
|
|
| 3 |
from pypdf import PdfReader
|
| 4 |
import requests
|
| 5 |
import json
|
| 6 |
+
import os
|
| 7 |
+
import time
|
| 8 |
|
| 9 |
|
| 10 |
def extract_text_from_pdf(pdf_path):
|
|
|
|
| 32 |
def generate_hypothetical_answer(query):
|
| 33 |
import requests
|
| 34 |
import json
|
| 35 |
+
import os
|
| 36 |
+
import time
|
| 37 |
|
| 38 |
+
# Hugging Face API endpoint
|
| 39 |
+
api_url = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
|
| 40 |
|
| 41 |
+
# Get API token from environment variable
|
| 42 |
+
api_token = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 43 |
+
if not api_token:
|
| 44 |
+
return "Error: HUGGINGFACE_API_TOKEN environment variable not set"
|
| 45 |
+
|
| 46 |
+
# Headers for the API request
|
| 47 |
+
headers = {
|
| 48 |
+
"Authorization": f"Bearer {api_token}",
|
| 49 |
+
"Content-Type": "application/json"
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
# Create a prompt for generating a hypothetical answer
|
| 53 |
+
prompt = f"""
|
| 54 |
+
Given the following query, generate a hypothetical answer that might be found in a document:
|
| 55 |
+
Query: {query}
|
| 56 |
+
|
| 57 |
+
Hypothetical answer:
|
| 58 |
+
"""
|
| 59 |
|
| 60 |
# Prepare the request payload
|
| 61 |
payload = {
|
| 62 |
+
"inputs": prompt,
|
| 63 |
+
"parameters": {
|
| 64 |
+
"max_new_tokens": 256,
|
| 65 |
+
"temperature": 0.7,
|
| 66 |
+
"top_p": 0.95,
|
| 67 |
+
"do_sample": True
|
| 68 |
+
}
|
| 69 |
}
|
| 70 |
|
| 71 |
try:
|
| 72 |
+
# Make the API request to Hugging Face
|
| 73 |
+
print("Sending request to Hugging Face API for hypothetical answer...")
|
| 74 |
+
start_time = time.time()
|
| 75 |
+
|
| 76 |
+
# Set a longer timeout (5 minutes)
|
| 77 |
+
response = requests.post(api_url, headers=headers, json=payload, timeout=300)
|
| 78 |
response.raise_for_status() # Raise an exception for HTTP errors
|
| 79 |
|
| 80 |
+
end_time = time.time()
|
| 81 |
+
print(f"Received hypothetical answer from Hugging Face API in {end_time - start_time:.2f} seconds")
|
| 82 |
+
|
| 83 |
# Parse the response
|
| 84 |
result = response.json()
|
| 85 |
|
| 86 |
# Extract the generated text
|
| 87 |
+
if isinstance(result, list) and len(result) > 0:
|
| 88 |
+
generated_text = result[0].get("generated_text", "")
|
| 89 |
+
else:
|
| 90 |
+
generated_text = result.get("generated_text", "")
|
| 91 |
+
|
| 92 |
return generated_text.strip()
|
| 93 |
|
| 94 |
+
except requests.exceptions.Timeout:
|
| 95 |
+
print("Request to Hugging Face API timed out after 5 minutes")
|
| 96 |
+
return "The request timed out. The model is taking too long to respond. Please try again with a simpler query."
|
| 97 |
+
|
| 98 |
+
except requests.exceptions.ConnectionError:
|
| 99 |
+
print("Could not connect to Hugging Face API")
|
| 100 |
+
return "Could not connect to the Hugging Face API. Please check your internet connection."
|
| 101 |
+
|
| 102 |
+
except requests.exceptions.HTTPError as e:
|
| 103 |
+
print(f"HTTP error occurred: {e}")
|
| 104 |
+
if e.response.status_code == 401:
|
| 105 |
+
return "Authentication error. Please check your Hugging Face API token."
|
| 106 |
+
elif e.response.status_code == 429:
|
| 107 |
+
return "Rate limit exceeded. Please try again later."
|
| 108 |
+
return f"HTTP error occurred: {e}"
|
| 109 |
+
|
| 110 |
except Exception as e:
|
| 111 |
print(f"Error generating hypothetical answer: {e}")
|
| 112 |
return "Failed to generate a hypothetical answer."
|
|
|
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
+
def query_llm_with_context(query, context, top_n=3):
|
| 118 |
+
import requests
|
| 119 |
+
import json
|
| 120 |
+
import os
|
| 121 |
+
import time
|
| 122 |
+
|
| 123 |
+
# Unpack the context tuple
|
| 124 |
+
documents, similarity_scores = context
|
| 125 |
|
| 126 |
# Use only the top N documents
|
| 127 |
+
top_docs = documents[:top_n]
|
| 128 |
|
| 129 |
# Create a context string by joining the top documents
|
| 130 |
+
context_text = "\n\n===Document Boundary===\n\n".join(top_docs)
|
| 131 |
|
| 132 |
# Create a prompt with the context and query
|
| 133 |
prompt = f"""
|
| 134 |
Context information is below.
|
| 135 |
---------------------
|
| 136 |
+
{context_text}
|
| 137 |
---------------------
|
| 138 |
|
| 139 |
Given the context information and not prior knowledge, answer the following query:
|
| 140 |
Query: {query}
|
| 141 |
"""
|
| 142 |
|
| 143 |
+
# Hugging Face API endpoint
|
| 144 |
+
api_url = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
|
| 145 |
+
|
| 146 |
+
# Get API token from environment variable
|
| 147 |
+
api_token = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 148 |
+
if not api_token:
|
| 149 |
+
return "Error: HUGGINGFACE_API_TOKEN environment variable not set"
|
| 150 |
+
|
| 151 |
+
# Headers for the API request
|
| 152 |
+
headers = {
|
| 153 |
+
"Authorization": f"Bearer {api_token}",
|
| 154 |
+
"Content-Type": "application/json"
|
| 155 |
+
}
|
| 156 |
|
| 157 |
# Prepare the request payload
|
| 158 |
payload = {
|
| 159 |
+
"inputs": prompt,
|
| 160 |
+
"parameters": {
|
| 161 |
+
"max_new_tokens": 512,
|
| 162 |
+
"temperature": 0.7,
|
| 163 |
+
"top_p": 0.95,
|
| 164 |
+
"do_sample": True
|
| 165 |
+
}
|
| 166 |
}
|
| 167 |
|
| 168 |
try:
|
| 169 |
+
# Make the API request to Hugging Face
|
| 170 |
+
print("Sending request to Hugging Face API...")
|
| 171 |
+
start_time = time.time()
|
| 172 |
+
|
| 173 |
+
# Set a longer timeout (5 minutes)
|
| 174 |
+
response = requests.post(api_url, headers=headers, json=payload, timeout=300)
|
| 175 |
response.raise_for_status() # Raise an exception for HTTP errors
|
| 176 |
|
| 177 |
+
end_time = time.time()
|
| 178 |
+
print(f"Received response from Hugging Face API in {end_time - start_time:.2f} seconds")
|
| 179 |
+
|
| 180 |
# Parse the response
|
| 181 |
result = response.json()
|
| 182 |
|
| 183 |
# Extract the generated text
|
| 184 |
+
if isinstance(result, list) and len(result) > 0:
|
| 185 |
+
generated_text = result[0].get("generated_text", "")
|
| 186 |
+
else:
|
| 187 |
+
generated_text = result.get("generated_text", "")
|
| 188 |
+
|
| 189 |
return generated_text.strip()
|
| 190 |
|
| 191 |
+
except requests.exceptions.Timeout:
|
| 192 |
+
print("Request to Hugging Face API timed out after 5 minutes")
|
| 193 |
+
return "The request timed out. The model is taking too long to respond. Please try again with a simpler query or fewer context documents."
|
| 194 |
+
|
| 195 |
+
except requests.exceptions.ConnectionError:
|
| 196 |
+
print("Could not connect to Hugging Face API")
|
| 197 |
+
return "Could not connect to the Hugging Face API. Please check your internet connection."
|
| 198 |
+
|
| 199 |
+
except requests.exceptions.HTTPError as e:
|
| 200 |
+
print(f"HTTP error occurred: {e}")
|
| 201 |
+
if e.response.status_code == 401:
|
| 202 |
+
return "Authentication error. Please check your Hugging Face API token."
|
| 203 |
+
elif e.response.status_code == 429:
|
| 204 |
+
return "Rate limit exceeded. Please try again later."
|
| 205 |
+
return f"HTTP error occurred: {e}"
|
| 206 |
+
|
| 207 |
except Exception as e:
|
| 208 |
print(f"Error querying LLM with context: {e}")
|
| 209 |
return "Failed to generate an answer with the provided context."
|