chatbot_for_files_langchain / improved_python_code.py
eliujl
Correct hist_fn
3607afa
# Import required libraries
import json
import os
import shutil
import langchain
import pinecone
import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationalRetrievalChain, LLMChain, load_qa_chain
from langchain.document_loaders import PyMuPDFLoader, UnstructuredFileLoader, UnstructuredWordDocumentLoader
from langchain.embeddings import HuggingFaceEmbeddings, OpenAIEmbeddings
from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Chroma, Pinecone
# API keys and model names
OPENAI_API_KEY = ''
PINECONE_API_KEY = ''
PINECONE_API_ENV = ''
gpt3p5 = 'gpt-3.5-turbo-1106'
gpt4 = 'gpt-4-1106-preview'
local_model_tuples = [
# Local model tuples as in the original code
]
local_model_names = [t[1] for t in local_model_tuples]
langchain.verbose = False
# Initialization function
@st.cache_data()
def init():
pinecone_index_name = ''
chroma_collection_name = ''
persist_directory = ''
docsearch_ready = False
directory_name = 'tmp_docs'
return pinecone_index_name, chroma_collection_name, persist_directory, docsearch_ready, directory_name
# File saving function
@st.cache_data()
def save_file(files, directory_name):
# Original save_file function logic
# File loading function
def load_files(directory_name):
# Original load_files function logic
# Ingestion function
@st.cache_resource()
def ingest(_all_texts, use_pinecone, _embeddings, pinecone_index_name, chroma_collection_name, persist_directory):
# Original ingest function logic
# Retriever setup function
def setup_retriever(docsearch, k):
# Original setup_retriever function logic
# Docsearch setup function
def setup_docsearch(use_pinecone, pinecone_index_name, embeddings, chroma_collection_name, persist_directory):
# Original setup_docsearch function logic
# Response generation function
def get_response(query, chat_history, CRqa):
# Original get_response function logic
# Local LLM usage function
@st.cache_resource()
def use_local_llm(r_llm, local_llm_path, temperature):
# Original use_local_llm function logic
# Prompt setup function
def setup_prompt(r_llm, usage):
# Original setup_prompt function logic
# Embeddings and LLM setup function
def setup_em_llm(OPENAI_API_KEY, temperature, r_llm, local_llm_path, usage):
# Original setup_em_llm function logic
# Chat history loading function
def load_chat_history(CHAT_HISTORY_FILENAME):
# Original load_chat_history function logic
# Chat history saving function
def save_chat_history(chat_history, CHAT_HISTORY_FILENAME):
# Original save_chat_history function logic
# Initialization
pinecone_index_name, chroma_collection_name, persist_directory, docsearch_ready, directory_name = init()
# Main function
def main(pinecone_index_name, chroma_collection_name, persist_directory, docsearch_ready, directory_name):
# Original main function logic
# Entry point
if __name__ == '__main__':
main(pinecone_index_name, chroma_collection_name, persist_directory, docsearch_ready, directory_name)