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
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import signal
|
| 4 |
+
import sys
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
from langchain_community.vectorstores import Chroma
|
| 7 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 8 |
+
import logging
|
| 9 |
+
|
| 10 |
+
# Set up logging
|
| 11 |
+
logging.basicConfig(level=logging.INFO)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
# Load API key from environment variables
|
| 15 |
+
load_dotenv()
|
| 16 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 17 |
+
if not GEMINI_API_KEY:
|
| 18 |
+
raise ValueError("GEMINI_API_KEY is not set in the environment variables.")
|
| 19 |
+
|
| 20 |
+
# Graceful shutdown on SIGINT
|
| 21 |
+
def signal_handler(sig, frame):
|
| 22 |
+
print('\nThanks for using Gemini. :)')
|
| 23 |
+
sys.exit(0)
|
| 24 |
+
|
| 25 |
+
signal.signal(signal.SIGINT, signal_handler)
|
| 26 |
+
|
| 27 |
+
# Conversation class to manage chat history
|
| 28 |
+
class Conversation:
|
| 29 |
+
def __init__(self):
|
| 30 |
+
self.history = []
|
| 31 |
+
|
| 32 |
+
def add_user_message(self, message):
|
| 33 |
+
self.history.append({"role": "user", "content": message})
|
| 34 |
+
|
| 35 |
+
def add_bot_message(self, message):
|
| 36 |
+
self.history.append({"role": "assistant", "content": message})
|
| 37 |
+
|
| 38 |
+
def get_history(self):
|
| 39 |
+
return self.history
|
| 40 |
+
|
| 41 |
+
def clear_history(self):
|
| 42 |
+
self.history = []
|
| 43 |
+
|
| 44 |
+
# Initialize a global conversation object
|
| 45 |
+
conversation = Conversation()
|
| 46 |
+
|
| 47 |
+
# Function to refine the query using conversation history
|
| 48 |
+
def refine_query_with_history(query, history):
|
| 49 |
+
"""
|
| 50 |
+
Refines the query by incorporating conversation history.
|
| 51 |
+
"""
|
| 52 |
+
# Format the conversation history
|
| 53 |
+
history_str = "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in history])
|
| 54 |
+
|
| 55 |
+
# Create a prompt to refine the query
|
| 56 |
+
refine_prompt = f"""
|
| 57 |
+
You are a helpful assistant that refines user queries based on conversation history.
|
| 58 |
+
Your task is to make the query more specific and relevant by incorporating context from the conversation history.
|
| 59 |
+
|
| 60 |
+
CONVERSATION HISTORY:
|
| 61 |
+
{history_str}
|
| 62 |
+
|
| 63 |
+
USER QUERY: '{query}'
|
| 64 |
+
|
| 65 |
+
REFINED QUERY:
|
| 66 |
+
"""
|
| 67 |
+
# Use Gemini to generate the refined query
|
| 68 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 69 |
+
model = genai.GenerativeModel(model_name='gemini-pro')
|
| 70 |
+
response = model.generate_content(refine_prompt)
|
| 71 |
+
refined_query = response.text.strip()
|
| 72 |
+
|
| 73 |
+
logger.info(f"Original Query: {query}")
|
| 74 |
+
logger.info(f"Refined Query: {refined_query}")
|
| 75 |
+
|
| 76 |
+
return refined_query
|
| 77 |
+
|
| 78 |
+
# Functions for RAG prompt generation and database query
|
| 79 |
+
def generate_chat_prompt(query, context, history):
|
| 80 |
+
# Format the conversation history
|
| 81 |
+
if not history:
|
| 82 |
+
history_str = "No prior conversation history."
|
| 83 |
+
else:
|
| 84 |
+
# Format the conversation history
|
| 85 |
+
history_str = "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in history])
|
| 86 |
+
|
| 87 |
+
logger.info(f"Conversation History:\n{history_str}") # Log the conversation history
|
| 88 |
+
logger.info(f"Retrieved Context:\n{context}") # Log the retrieved context
|
| 89 |
+
|
| 90 |
+
prompt = f"""
|
| 91 |
+
You are a helpful and informative chatbot that answers questions using text from the reference context included below.
|
| 92 |
+
Be sure to respond in a complete sentence, being comprehensive, including all relevant background information.
|
| 93 |
+
However, you are talking to a non-technical audience, so be sure to break down complicated concepts and
|
| 94 |
+
strike a friendly and conversational tone.
|
| 95 |
+
If the context is irrelevant to the answer, you may ignore it.
|
| 96 |
+
|
| 97 |
+
CONVERSATION HISTORY:
|
| 98 |
+
{history_str}
|
| 99 |
+
|
| 100 |
+
CONTEXT:
|
| 101 |
+
{context}
|
| 102 |
+
|
| 103 |
+
USER QUESTION: '{query}'
|
| 104 |
+
BOT ANSWER:
|
| 105 |
+
"""
|
| 106 |
+
return prompt
|
| 107 |
+
|
| 108 |
+
def get_relevant_context_from_db(query, history):
|
| 109 |
+
"""
|
| 110 |
+
Retrieves relevant context from ChromaDB using a refined query.
|
| 111 |
+
"""
|
| 112 |
+
try:
|
| 113 |
+
# Refine the query using conversation history
|
| 114 |
+
refined_query = refine_query_with_history(query, history)
|
| 115 |
+
|
| 116 |
+
# Retrieve context using the refined query
|
| 117 |
+
embedding_function = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 118 |
+
vector_db = Chroma(persist_directory="./chroma_db_nccn", embedding_function=embedding_function)
|
| 119 |
+
search_results = vector_db.similarity_search(refined_query, k=6)
|
| 120 |
+
context = "\n".join([result.page_content for result in search_results])
|
| 121 |
+
sources = list(set([result.metadata.get("source", "Unknown") for result in search_results])) # Remove duplicates
|
| 122 |
+
|
| 123 |
+
logger.info(f"Refined Query: {refined_query}")
|
| 124 |
+
logger.info(f"Retrieved Context: {context}")
|
| 125 |
+
logger.info(f"Sources: {sources}")
|
| 126 |
+
|
| 127 |
+
return context, sources
|
| 128 |
+
except Exception as e:
|
| 129 |
+
logger.error(f"Error retrieving context from database: {e}", exc_info=True)
|
| 130 |
+
return "", []
|
| 131 |
+
|
| 132 |
+
def generate_chat_answer(query):
|
| 133 |
+
try:
|
| 134 |
+
logger.info(f"Generating answer for query: {query}")
|
| 135 |
+
|
| 136 |
+
# Configure Gemini API
|
| 137 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 138 |
+
model = genai.GenerativeModel(model_name='gemini-pro')
|
| 139 |
+
|
| 140 |
+
# Retrieve context and sources from Chroma DB using refined query
|
| 141 |
+
context, sources = get_relevant_context_from_db(query, conversation.get_history())
|
| 142 |
+
logger.info(f"Retrieved context: {context}")
|
| 143 |
+
logger.info(f"Sources: {sources}")
|
| 144 |
+
|
| 145 |
+
# Generate prompt with conversation history
|
| 146 |
+
prompt = generate_chat_prompt(query, context, conversation.get_history())
|
| 147 |
+
logger.info(f"Generated prompt: {prompt}")
|
| 148 |
+
|
| 149 |
+
# Generate answer using Gemini
|
| 150 |
+
answer = model.generate_content(prompt)
|
| 151 |
+
logger.info(f"Generated answer: {answer.text}")
|
| 152 |
+
|
| 153 |
+
# Add user query and bot response to conversation history
|
| 154 |
+
conversation.add_user_message(query)
|
| 155 |
+
conversation.add_bot_message(answer.text)
|
| 156 |
+
|
| 157 |
+
# Format the answer with cleaned and sorted sources
|
| 158 |
+
formatted_answer = f"{answer.text}\n\nSources:\n" + "\n".join([f'- {source.replace(".pdf", "")}' for source in sorted(sources)])
|
| 159 |
+
|
| 160 |
+
return formatted_answer
|
| 161 |
+
except Exception as e:
|
| 162 |
+
logger.error(f"Error generating answer: {e}", exc_info=True)
|
| 163 |
+
return f"An error occurred while generating the answer. Please try again. Error: {str(e)}"
|