import os
import logging
import asyncio
import gradio as gr
from huggingface_hub import InferenceClient
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.schema import Document
from duckduckgo_search import DDGS
# Environment variables and configurations
huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
MODELS = [
"mistralai/Mistral-7B-Instruct-v0.3",
"mistralai/Mixtral-8x7B-Instruct-v0.1",
"mistralai/Mistral-Nemo-Instruct-2407",
"meta-llama/Meta-Llama-3.1-8B-Instruct",
"meta-llama/Meta-Llama-3.1-70B-Instruct"
]
def get_embeddings():
return HuggingFaceEmbeddings(model_name="sentence-transformers/stsb-roberta-large")
def duckduckgo_search(query):
with DDGS() as ddgs:
results = ddgs.text(query, max_results=10)
return results
async def rephrase_query(query, history, model):
system_message = """You are an AI assistant tasked with analyzing and rephrasing user queries. Your goal is to determine if a query is unique or related to the previous conversation, and then rephrase it appropriately for web search. Keep the rephrased query succinct and in a web search query format.
If the query is unique, rephrase it to be more specific and searchable.
If the query is related to the previous conversation, incorporate relevant context from the previous response.
Provide your analysis in the following format:
Your reasoning about whether the query is unique or related
The rephrased query"""
user_message = f"""Current query: {query}
Previous conversation history:
{history}
Analyze the query and provide a rephrased version suitable for web search."""
client = InferenceClient(model, token=huggingface_token)
try:
response = await asyncio.to_thread(
client.text_generation,
prompt=f"{system_message}\n\n{user_message}",
max_new_tokens=150,
temperature=0.2,
)
# Extract the rephrased query from the response
analysis_start = response.find("")
analysis_end = response.find("")
rephrased_start = response.find("")
rephrased_end = response.find("")
if analysis_start != -1 and analysis_end != -1 and rephrased_start != -1 and rephrased_end != -1:
analysis = response[analysis_start + 10:analysis_end].strip()
rephrased_query = response[rephrased_start + 17:rephrased_end].strip()
return analysis, rephrased_query
else:
logging.error("Failed to parse the rephrase response")
return None, query
except Exception as e:
logging.error(f"Error in rephrase_query: {str(e)}")
return None, query
def create_web_search_vectors(search_results):
embed = get_embeddings()
documents = []
for result in search_results:
if 'body' in result:
content = f"{result['title']}\n{result['body']}\nSource: {result['href']}"
documents.append(Document(page_content=content, metadata={"source": result['href']}))
return FAISS.from_documents(documents, embed)
async def get_response_with_search(query, model, use_embeddings, num_calls=3, temperature=0.2):
search_results = duckduckgo_search(query)
if not search_results:
yield "No web search results available. Please try again.", ""
return
if use_embeddings:
web_search_database = create_web_search_vectors(search_results)
retriever = web_search_database.as_retriever(search_kwargs={"k": 5})
relevant_docs = retriever.get_relevant_documents(query)
context = "\n".join([doc.page_content for doc in relevant_docs])
else:
context = "\n".join([f"{result['title']}\n{result['body']}\nSource: {result['href']}" for result in search_results])
system_message = """ You are a world-class AI system, capable of complex reasoning and reflection.
Reason through the query inside tags, and then provide your final response inside