web-app-temp / web_app /backend /app /question_model.py
leothesouthafrican's picture
hf fix new branch
e54b26e
from langchain.document_loaders.csv_loader import CSVLoader
from langchain.vectorstores import FAISS
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.prompts import PromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain
from dotenv import load_dotenv, find_dotenv
import os
# Load the environment variables
load_dotenv(find_dotenv())
# Load the data
# Get the directory of the current file
current_dir = os.path.dirname(os.path.realpath(__file__))
# Construct the file path by joining the current directory with the data file name
file_path = os.path.join(current_dir, "data/question_data.csv")
# Instantiate the CSVLoader with the file path
loader = CSVLoader(file_path=file_path)
# Load the documents from the CSV file
documents = loader.load()
# Initialize embeddings and database
# Instantiate the OpenAIEmbeddings class
embeddings = OpenAIEmbeddings()
# Create a FAISS database from the documents using the embeddings
db = FAISS.from_documents(documents, embeddings)
def retrieve_info(query):
"""
Given a query, retrieves the most similar document's content from the database.
Parameters:
query (str): The input query.
Returns:
list: A list of page content from the most similar document.
"""
# Perform a similarity search in the database for the given query
similar_question = db.similarity_search(query, k=1)
# Extract the page content from the similar documents
page_contents_arry = [doc.page_content for doc in similar_question]
return page_contents_arry
# Setup LLMChain & Prompts
# Instantiate a language model from OpenAI
llm = ChatOpenAI(temperature=0, model = "gpt-3.5-turbo-16k")
# Define a template for the prompts to the language model
template = """
You are a extremely intelligent online assistant who's goal is to act as a
reccomendation system for the user. The user will ask you questions about the
retrofitting of their home in Ireland and you will provide them with the next two most
logical questions for them to ask. Your suggested questions should be similar to the
best practice examples that I will provide you with. The examples are structured as RootQuestion
being the original question asked by the user and FollowUp1 and FollowUp2 being the next two
questions that the user should ask.
Below is the question that you have received from the user:
{question}
Here is a list of the best practice examples that you should try to emulate:
{examples}
Please return the most logical next two questions for the user to ask.
Ensure that you only send back exactly two follow-up questions.
"""
# Define the template for the prompt with the input variables and template
prompt = PromptTemplate(
input_variables=["question", "examples"],
template=template
)
# Create a language model chain with the model and prompt
chain = LLMChain(llm = llm, prompt = prompt)
def generate_response(message):
"""
Given a message, generates a response using the best practice examples and language model chain.
Parameters:
message (str): The input message.
Returns:
str: The generated response consisting of two follow-up questions.
"""
# Retrieve the best practice examples for the message
best_practice_examples = retrieve_info(message)
# Generate a response using the language model chain and the best practice examples
response = chain.run(question = message, examples = best_practice_examples)
# Parse the AI response and keep only the first two valid questions
lines = response.split('\n')
follow_ups = [line for line in lines if line.strip().endswith('?')]
if len(follow_ups) > 2:
follow_ups = follow_ups[:2]
# Remove the labels (e.g., "FollowUp1:") from the questions
follow_ups = [follow_up.split(': ', 1)[-1] for follow_up in follow_ups]
# Convert follow-up questions into a string response
response = '\n'.join(follow_ups)
return response