File size: 3,223 Bytes
e0a13cb
 
 
 
 
 
 
 
 
0f5da8c
 
 
e0a13cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f5da8c
e0a13cb
 
 
0f5da8c
e0a13cb
 
 
 
 
0f5da8c
e0a13cb
 
 
 
 
 
0f5da8c
e0a13cb
 
 
0f5da8c
e0a13cb
 
0f5da8c
e0a13cb
 
 
 
 
 
 
 
 
 
 
 
 
0f5da8c
e0a13cb
 
 
 
0f5da8c
e0a13cb
 
 
0f5da8c
e0a13cb
 
 
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


import streamlit as st
from langchain.document_loaders import DirectoryLoader, TextLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI as LangchainOpenAI
from dotenv import load_dotenv
import os

# Load environment variables
load_dotenv()

# Initialize Streamlit
st.set_page_config(page_title="Ask the Docs")
st.header("Ask the Docs πŸ“š")
st.write('Unleash your credit card superpowers')
st.write('Revolutionize the way you track and manage your credit cards.')
st.write('Experience hassle-free financial control with Anek.')

# Initialize chat session state with a welcome message
if "messages" not in st.session_state.keys():
    st.session_state.messages = [{"role": "assistant", "content": "Welcome to CreditCardChat! Your personal credit card advisor πŸ˜‰. Drop in your queries."}]

# Display previous chat messages
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.write(message["content"])


# Load Documents from Directory
text_loader_kwargs = {'encoding': 'utf-8'}
loader = DirectoryLoader(r'C:\Users\iamma\Documents\JupyterHub\MODELS\V1\credit_card_texts17AUG', glob="**/*.txt", loader_cls=TextLoader, loader_kwargs=text_loader_kwargs)
docs = loader.load()

# Embedding Configuration
openai_api_key = os.getenv('OPENAI_API_KEY')
embedding = OpenAIEmbeddings(openai_api_key=openai_api_key)

try:
    embeddings = embedding.embed_documents([doc.page_content for doc in docs])
except Exception as e:
    st.write(f"An error occurred: {e}")
    st.stop()

# FAISS Database Configuration
try:
    db = FAISS.from_documents(docs, embedding)
except Exception as e:
    st.write(f"An error occurred while creating FAISS database: {e}")
    st.stop()

# Initialize Langchain components
llm = LangchainOpenAI(openai_api_key=openai_api_key)
qa_chain = RetrievalQA.from_chain_type(llm, retriever=db.as_retriever())

# User-provided question
# ... (previous imports and initialization)

# User-provided question
if user_question := st.chat_input("Ask a question about the documents: "):
    # Manually append user's question to session state and display immediately
    st.session_state.messages.append({"role": "user", "content": user_question})
    
    # Display chat messages including the new question
    for message in st.session_state.messages:
        with st.chat_message(message["role"]):
            st.write(message["content"])
    
    # Placeholder for assistant's response
    with st.chat_message("assistant"):
        assistant_placeholder = st.empty()

    try:
        # Get the model's response
        result_dict = qa_chain({"query": user_question})
        result = result_dict.get("result", "I don't know.")

        # Update assistant's message
        assistant_placeholder.write(result)
        st.session_state.messages.append({"role": "assistant", "content": result})

    except Exception as e:
        assistant_placeholder.write(f"An error occurred: {e}")
        st.session_state.messages.append({"role": "assistant", "content": f"An error occurred: {e}"})