rag_chatbot / app.py
iclalcetin's picture
Create app.py
9fe34ae verified
raw
history blame
1.36 kB
from dotenv import load_dotenv
load_dotenv()
import streamlit as st
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
import os
def main():
load_dotenv()
st.set_page_config(page_title="Chat Text")
st.header("Chat Text 💬")
# Dosyayı oku
file_path = 'shipping.txt'
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read()
# Metni parçalara ayır
char_text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000,
chunk_overlap=200, length_function=len)
text_chunks = char_text_splitter.split_text(text)
embeddings = OpenAIEmbeddings(
openai_api_key=os.getenv('OPENAI_API_KEY')
)
docsearch = FAISS.from_texts(text_chunks, embeddings)
llm = OpenAI()
chain = load_qa_chain(llm, chain_type="stuff")
# Kullanıcıdan soru al
query = st.text_input("Type your question:")
if query:
docs = docsearch.similarity_search(query)
response = chain.run(input_documents=docs, question=query)
st.write(response)
if __name__ == '__main__':
main()