Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
data.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
level1.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_experimental.agents import create_csv_agent
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from langchain_openai import AzureChatOpenAI
|
| 4 |
+
import os
|
| 5 |
+
load_dotenv()
|
| 6 |
+
import streamlit as st
|
| 7 |
+
import pandas as pd
|
| 8 |
+
from langchain_community.document_loaders import JSONLoader
|
| 9 |
+
import requests
|
| 10 |
+
from langchain_openai import OpenAIEmbeddings
|
| 11 |
+
from langchain.vectorstores import FAISS
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
llm = AzureChatOpenAI(openai_api_version=os.environ.get("AZURE_OPENAI_VERSION", "2023-07-01-preview"),
|
| 15 |
+
azure_deployment=os.environ.get("AZURE_OPENAI_DEPLOYMENT", "gpt4chat"),
|
| 16 |
+
azure_endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT", "https://gpt-4-trails.openai.azure.com/"),
|
| 17 |
+
api_key=os.environ.get("AZURE_OPENAI_KEY"))
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def metadata_func(record: str, metadata: dict) -> dict:
|
| 21 |
+
lines = record.split('\n')
|
| 22 |
+
locality_line = lines[10]
|
| 23 |
+
price_range_line = lines[12]
|
| 24 |
+
locality = locality_line.split(': ')[1]
|
| 25 |
+
price_range = price_range_line.split(': ')[1]
|
| 26 |
+
metadata["location"] = locality
|
| 27 |
+
metadata["price_range"] = price_range
|
| 28 |
+
|
| 29 |
+
return metadata
|
| 30 |
+
|
| 31 |
+
# Instantiate the JSONLoader with the metadata_func
|
| 32 |
+
jq_schema = '.parser[] | to_entries | map("\(.key): \(.value)") | join("\n")'
|
| 33 |
+
loader = JSONLoader(
|
| 34 |
+
jq_schema=jq_schema,
|
| 35 |
+
file_path='data.json',
|
| 36 |
+
metadata_func=metadata_func,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Load the JSON file and extract metadata
|
| 40 |
+
documents = loader.load()
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def get_vectorstore(text_chunks):
|
| 44 |
+
embeddings = OpenAIEmbeddings()
|
| 45 |
+
# Check if the FAISS index file already exists
|
| 46 |
+
if os.path.exists("faiss_index"):
|
| 47 |
+
# Load the existing FAISS index
|
| 48 |
+
vectorstore = FAISS.load_local("faiss_index", embeddings=embeddings)
|
| 49 |
+
print("Loaded existing FAISS index.")
|
| 50 |
+
else:
|
| 51 |
+
# Create a new FAISS index
|
| 52 |
+
embeddings = OpenAIEmbeddings()
|
| 53 |
+
vectorstore = FAISS.from_documents(documents=text_chunks, embedding=embeddings)
|
| 54 |
+
# Save the new FAISS index locally
|
| 55 |
+
vectorstore.save_local("faiss_index")
|
| 56 |
+
print("Created and saved new FAISS index.")
|
| 57 |
+
return vectorstore
|
| 58 |
+
|
| 59 |
+
#docs = new_db.similarity_search(query)
|
| 60 |
+
|
| 61 |
+
vector = get_vectorstore(documents)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
from langchain.chains import RetrievalQA
|
| 65 |
+
from langchain.prompts import PromptTemplate
|
| 66 |
+
from langchain.memory import ConversationSummaryMemory
|
| 67 |
+
|
| 68 |
+
template = """
|
| 69 |
+
|
| 70 |
+
context:- I have low budget what is the best hotel in Instanbul?
|
| 71 |
+
anser:- The other hotels in instanbul are costly and are not in your budget. so the best hotel in instanbul for you is hotel is xyz."
|
| 72 |
+
|
| 73 |
+
Don’t give information not mentioned in the CONTEXT INFORMATION.
|
| 74 |
+
The system should take into account various factors such as location, amenities, user reviews, and other relevant criteria to
|
| 75 |
+
generate informative and personalized explanations.
|
| 76 |
+
{context}
|
| 77 |
+
Question: {question}
|
| 78 |
+
Answer:"""
|
| 79 |
+
|
| 80 |
+
prompt = PromptTemplate(template=template, input_variables=["context","question"])
|
| 81 |
+
|
| 82 |
+
chain_type_kwargs = {"prompt": prompt}
|
| 83 |
+
chain = RetrievalQA.from_chain_type(
|
| 84 |
+
llm=llm,
|
| 85 |
+
chain_type="stuff",
|
| 86 |
+
retriever=vector.as_retriever(),
|
| 87 |
+
chain_type_kwargs=chain_type_kwargs,
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def main():
|
| 92 |
+
st.title("Hotel Assistant Chatbot")
|
| 93 |
+
st.write("Welcome to the Hotel Assistant Chatbot!")
|
| 94 |
+
user_input = st.text_input("User Input:")
|
| 95 |
+
|
| 96 |
+
if st.button("Submit"):
|
| 97 |
+
response = chain.run(user_input)
|
| 98 |
+
st.text_area("Chatbot Response:", value=response)
|
| 99 |
+
|
| 100 |
+
if st.button("Exit"):
|
| 101 |
+
st.stop()
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
main()
|