Spaces:
Build error
Build error
File size: 15,806 Bytes
ab710db 61ef0a5 ab710db 60e98cb 4749eaf 60e98cb 389dc82 ab710db 60e98cb a58124f 60e98cb 389dc82 60e98cb 70473b4 60e98cb 389dc82 60e98cb 389dc82 60e98cb 19f7ee7 60e98cb 389dc82 60e98cb 389dc82 60e98cb 389dc82 60e98cb 389dc82 60e98cb c0205c2 60e98cb c0205c2 60e98cb c0205c2 60e98cb 19f7ee7 b26d7ff 19f7ee7 60e98cb 19f7ee7 60e98cb ab710db 60e98cb ab710db 70473b4 ab710db 70473b4 60e98cb ab710db 60e98cb 19f7ee7 4749eaf 60e98cb 19f7ee7 ab710db b26d7ff 19f7ee7 ab710db 4749eaf ab710db 60e98cb c0205c2 60e98cb ab710db c0205c2 389dc82 60e98cb 19f7ee7 60e98cb c0205c2 4749eaf c0205c2 60e98cb 70473b4 b26d7ff 70473b4 4749eaf c0205c2 4749eaf 19f7ee7 4749eaf 0886e95 60e98cb ab710db 60e98cb ab710db 61ef0a5 60e98cb ab710db 19f7ee7 ab710db 4749eaf 19f7ee7 60e98cb ab710db 096a1bf 690c3bb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# Combined Gemini Flash and Meta-LLAMA 3 GWDG and Groq Chatbot
# For Gemini Flash rate limit is 15 requests per minute
# For Groq rate 30 RPM , 14400 RPD, 6K TPM and 500K TPD
# For GWDG Llama3 60 /min 3000 /h 75000 /day 2000000 /month
import os
import json
import logging
import re
from typing import List, Tuple, Generator
import gradio as gr
from openai import OpenAI
import google.generativeai as genai
import requests
from functools import lru_cache
from tenacity import retry, stop_after_attempt, wait_exponential
from langchain_community.retrievers import BM25Retriever
from langchain_community.vectorstores import FAISS
from langchain_core.embeddings import Embeddings
from langchain_core.documents import Document
from collections import defaultdict
import hashlib
from tqdm import tqdm
from dotenv import load_dotenv
import pickle
load_dotenv()
# --- Configuration ---
FAISS_INDEX_PATH = "faiss_index"
BM25_INDEX_PATH = "bm25_index.pkl"
CACHE_VERSION = "v1"
embedding_model = "e5-mistral-7b-instruct"
data_file_name = "AskNatureNet_data_enhanced.json"
CHUNK_SIZE = 800
OVERLAP = 200
EMBEDDING_BATCH_SIZE = 32
# Initialize clients
OPENAI_API_CONFIG = {
"api_key": os.getenv("OPENAI_API_KEY"),
"base_url": "https://chat-ai.academiccloud.de/v1"
}
client = OpenAI(**OPENAI_API_CONFIG)
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# --- Helper Functions ---
def get_data_hash(file_path: str) -> str:
"""Generate hash of data file for cache validation"""
with open(file_path, "rb") as f:
return hashlib.md5(f.read()).hexdigest()
# --- Custom Embedding Handler ---
class MistralEmbeddings(Embeddings):
"""E5-Mistral-7B embedding adapter"""
def embed_documents(self, texts: List[str]) -> List[List[float]]:
embeddings = []
try:
for i in tqdm(range(0, len(texts), EMBEDDING_BATCH_SIZE), desc="Embedding Progress"):
batch = texts[i:i + EMBEDDING_BATCH_SIZE]
response = client.embeddings.create(
input=batch,
model=embedding_model,
encoding_format="float"
)
embeddings.extend([e.embedding for e in response.data])
return embeddings
except Exception as e:
logger.error(f"Embedding Error: {str(e)}")
return [[] for _ in texts]
def embed_query(self, text: str) -> List[float]:
return self.embed_documents([text])[0]
# --- Data Processing ---
def load_and_chunk_data(file_path: str) -> List[Document]:
"""Enhanced chunking with metadata preservation"""
current_hash = get_data_hash(file_path)
cache_file = f"documents_{CACHE_VERSION}_{current_hash}.pkl"
if os.path.exists(cache_file):
logger.info("Loading cached documents")
with open(cache_file, "rb") as f:
return pickle.load(f)
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
documents = []
for item in tqdm(data, desc="Chunking Progress"):
base_content = f"""Source: {item['Source']}
Application: {item['Application']}
Functions: {', '.join(filter(None, [item.get('Function1'), item.get('Function2')]))}
Technical Concepts: {', '.join(item['technical_concepts'])}
Biological Mechanisms: {', '.join(item['biological_mechanisms'])}"""
strategy = item['Strategy']
for i in range(0, len(strategy), CHUNK_SIZE - OVERLAP):
chunk = strategy[i:i + CHUNK_SIZE]
documents.append(Document(
page_content=f"{base_content}\nStrategy Excerpt:\n{chunk}",
metadata={
"source": item["Source"],
"application": item["Application"],
"technical_concepts": item["technical_concepts"],
"sustainability_impacts": item["sustainability_impacts"],
"hyperlink": item["Hyperlink"],
"chunk_id": f"{item['Source']}-{len(documents)+1}"
}
))
with open(cache_file, "wb") as f:
pickle.dump(documents, f)
return documents
# --- Optimized Retrieval System ---
class EnhancedRetriever:
"""Hybrid retriever with persistent caching"""
def __init__(self, documents: List[Document]):
self.documents = documents
self.bm25 = self._init_bm25()
self.vector_store = self._init_faiss()
self.vector_retriever = self.vector_store.as_retriever(search_kwargs={"k": 3})
def _init_bm25(self) -> BM25Retriever:
cache_key = f"{BM25_INDEX_PATH}_{get_data_hash(data_file_name)}"
if os.path.exists(cache_key):
logger.info("Loading cached BM25 index")
with open(cache_key, "rb") as f:
return pickle.load(f)
logger.info("Building new BM25 index")
retriever = BM25Retriever.from_documents(self.documents)
retriever.k = 5
with open(cache_key, "wb") as f:
pickle.dump(retriever, f)
return retriever
def _init_faiss(self) -> FAISS:
cache_key = f"{FAISS_INDEX_PATH}_{get_data_hash(data_file_name)}"
if os.path.exists(cache_key):
logger.info("Loading cached FAISS index")
return FAISS.load_local(
cache_key,
MistralEmbeddings(),
allow_dangerous_deserialization=True
)
logger.info("Building new FAISS index")
vector_store = FAISS.from_documents(self.documents, MistralEmbeddings())
vector_store.save_local(cache_key)
return vector_store
@lru_cache(maxsize=500)
def retrieve(self, query: str) -> Tuple[str, List[Document]]:
try:
processed_query = self._preprocess_query(query)
expanded_query = self._hyde_expansion(processed_query)
bm25_results = self.bm25.invoke(processed_query)
vector_results = self.vector_retriever.invoke(processed_query)
expanded_results = self.bm25.invoke(expanded_query)
fused_results = self._fuse_results([bm25_results, vector_results, expanded_results])
top_docs = fused_results[:5]
formatted_context = self._format_context(top_docs)
return formatted_context, top_docs
except Exception as e:
logger.error(f"Retrieval Error: {str(e)}")
return "", []
def _preprocess_query(self, query: str) -> str:
return query.lower().strip()
@lru_cache(maxsize=500)
def _hyde_expansion(self, query: str) -> str:
try:
response = client.chat.completions.create(
model="llama-3.3-70b-instruct",
messages=[{
"role": "user",
"content": f"Generate a technical draft about biomimicry for: {query}\nInclude domain-specific terms."
}],
temperature=0.5,
max_tokens=200
)
return response.choices[0].message.content
except Exception as e:
logger.error(f"HyDE Error: {str(e)}")
return query
def _fuse_results(self, result_sets: List[List[Document]]) -> List[Document]:
fused_scores = defaultdict(float)
for docs in result_sets:
for rank, doc in enumerate(docs, 1):
fused_scores[doc.metadata["chunk_id"]] += 1 / (rank + 60)
seen = set()
return [
doc for doc in sorted(
(doc for docs in result_sets for doc in docs),
key=lambda x: fused_scores[x.metadata["chunk_id"]],
reverse=True
) if not (doc.metadata["chunk_id"] in seen or seen.add(doc.metadata["chunk_id"]))
]
def _format_context(self, docs: List[Document]) -> str:
context = []
for doc in docs:
context_str = f"""**Source**: [{doc.metadata['source']}]({doc.metadata['hyperlink']})
**Application**: {doc.metadata['application']}
**Key Concepts**: {', '.join(doc.metadata['technical_concepts'])}
**Strategy Excerpt**:
{doc.page_content.split('Strategy Excerpt:')[-1].strip()}"""
context.append(context_str)
return "\n\n---\n\n".join(context)
# --- Generation System ---
SYSTEM_PROMPT = """
**Expert Biomimicry Advisor**
- **Objective**: Your role is to provide expert-level insights on biomimicry by using the provided AskNature context. When context is unavailable, rely on general knowledge.
- **Answer Precision**: Always use precise technical language and structure your response logically, emphasizing the relationship between biological concepts and innovation.
- **Content Formatting**: Bold technical terms for emphasis (e.g., **protein synthesis**, **ecosystem mimicry**).
- **Conclusion**: Summarize the sustainability impacts of the discussed technologies or ideas. Highlight innovative aspects and benefits.
Context: {context}
"""
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=2, max=20))
def get_ai_response(query: str, context: str, model: str) -> str:
result = ""
try:
if model == "gemini-2.0-flash":
gemini_model = genai.GenerativeModel(model)
response = gemini_model.generate_content(
f"{SYSTEM_PROMPT.format(context=context)}\nQuestion: {query}\nProvide a detailed technical answer:"
)
logger.info(f"Response from gemini-2.0-flash: {response.text}")
result = _postprocess_response(response.text)
elif model == "llama-3.3-70b-instruct":
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": SYSTEM_PROMPT.format(context=context)},
{"role": "user", "content": f"Question: {query}\nProvide a detailed technical answer:"}
],
temperature=0.4,
max_tokens=2000
)
logger.info(f"Response from meta-llama-3-70b-instruct: {response}")
try:
result = response.choices[0].message.content
except Exception as e:
logger.error(f"Error processing meta-llama-3-70b-instruct response: {str(e)}")
result = "Failed to process response from meta-llama-3-70b-instruct"
elif model == "llama3-70b-8192":
result = get_groq_llama3_response(query)
logger.info(f"Response from llama3-70b-8192: {result}")
if result is None:
result = "Failed to get response from llama3-70b-8192"
# Do not append model info here so that only the answer (and references, if any) are returned.
return result
except Exception as e:
logger.error(f"Generation Error: {str(e)}")
return "I'm unable to generate a response right now. Please try again later or try another model."
def _postprocess_response(response: str) -> str:
response = re.sub(r"\[(.*?)\]", r"[\1](#)", response)
response = re.sub(r"\*\*([\w-]+)\*\*", r"**\1**", response)
return response
def get_groq_llama3_response(query: str) -> str:
"""Get response from Llama 3 on Groq Cloud."""
api_key = os.getenv("GROQ_API_KEY")
url = "https://api.groq.com/openai/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": "llama3-70b-8192",
"messages": [
{
"role": "user",
"content": query
}
]
}
try:
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
result = response.json()
logger.info(f"Groq API Response: {result}")
return result["choices"][0]["message"]["content"]
except requests.exceptions.RequestException as e:
logger.error(f"Groq API Error: {str(e)}")
return "An error occurred while contacting Groq's Llama 3 model."
# --- Pipeline ---
documents = load_and_chunk_data(data_file_name)
retriever = EnhancedRetriever(documents)
def generate_response(question: str, model: str) -> str:
try:
formatted_context, retrieved_docs = retriever.retrieve(question)
if not formatted_context:
return "No relevant information found."
response = get_ai_response(question, formatted_context, model)
# If response is an error message, return it without appending references.
if ("I'm unable to generate a response" in response or
"No relevant information found" in response or
"Failed to process" in response):
return response
# Extract references from retrieved documents whose hyperlinks start with "https://asknature.org"
ref_links = []
for doc in retrieved_docs:
hyperlink = doc.metadata.get("hyperlink", "")
if hyperlink.startswith("https://asknature.org") and hyperlink not in ref_links:
ref_links.append(hyperlink)
if ref_links:
references_md = "\n\n**References:**\n"
for i, link in enumerate(ref_links, 1):
references_md += f"[{i}] {link}\n"
response += references_md
for key, value in model_mapping.items():
if value == model:
model = key
response += f"\n\n**Model:** {model}"
return response
except Exception as e:
logger.error(f"Pipeline Error: {str(e)}")
return "An error occurred processing your request."
# --- Gradio Interface ---
model_mapping = {
"Gemini-2.0-Flash": "gemini-2.0-flash",
"Meta-llama-3-70b-instruct(GWDG)": "llama-3.3-70b-instruct",
"llama3-70b-8192(Groq)": "llama3-70b-8192"
}
# Updated chat_interface as a generator to display the user's question immediately.
def chat_interface(question: str, history: List[Tuple[str, str]], display_model: str) -> Generator[Tuple[str, List[Tuple[str, str]]], None, None]:
model = model_mapping.get(display_model, "gemini-2.0-flash")
# Append the user question with a placeholder for the answer
new_history = history.copy()
new_history.append((question, "Generating response..."))
yield "", new_history # Immediately update the chat history
# Generate the actual response
response = generate_response(question, model)
new_history[-1] = (question, response)
yield "", new_history
with gr.Blocks(title="AskNature BioRAG Expert", theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🌿 AskNature RAG-based Chatbot")
with gr.Row():
chatbot = gr.Chatbot(label="Dialogue History", height=500)
with gr.Row():
question = gr.Textbox(placeholder="Ask about biomimicry (e.g. 'How does Werewool use coral proteins to make fibers?')", label="Inquiry", scale=4)
model_selector = gr.Dropdown(choices=list(model_mapping.keys()), label="Generation Model", value="Meta-llama-3-70b-instruct(GWDG)")
clear_btn = gr.Button("Clear History", variant="secondary")
gr.Markdown("""
<div style="text-align: center; color: #4a7c59;">
<small>Powered by AskNature's Database |
Explore nature's blueprints at <a href="https://asknature.org">asknature.org</a></small>
</div>""")
# Use the generator function for streaming updates.
question.submit(chat_interface, [question, chatbot, model_selector], [question, chatbot])
clear_btn.click(lambda: [], None, chatbot)
if __name__ == "__main__":
demo.launch(show_error=True)
|