# # for requirements # pip install chromadb # pip install tiktoken # imports import openai from langchain.document_loaders.csv_loader import CSVLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain_community.embeddings import OpenAIEmbeddings from langchain_community.vectorstores import Chroma import os from huggingface_hub import login, HfApi, hf_hub_download from langchain_community.chat_models import ChatOpenAI from langchain.memory import ConversationSummaryMemory from langchain.chains import ConversationalRetrievalChain import gradio as gr # Redundant imports #from langchain_community.llms import OpenAI #import re #import pandas as pd #from langchain.memory import ConversationSummaryBufferMemory #from langchain.memory import ConversationBufferMemory #from langchain.chains import LLMChain #from langchain.embeddings import OpenAIEmbeddings # deprecated import method #from langchain.vectorstores import Chroma # deprecated import method #from langchain.chat_models import ChatOpenAI # deprecated import method #from langchain.llms import OpenAI # deprecated import method openai.api_key = os.environ.get("OPENAI_API_KEY") HF_TOKEN = os.environ.get("hf_token") destination = "j-m-w/FAQ_for_learners" HF_REPOSITORY = "j-m-w/FAQ_df_repo" # the database repo SUMMARY_FILE = "FAQ_sheet.csv" hf = HfApi() login(HF_TOKEN) def download(): # re-used from app 1 try: hf_hub_download(repo_id=HF_REPOSITORY, filename=SUMMARY_FILE, repo_type="dataset", local_dir=".") # so with local_dir='.' it downloads it into your wd. (And, when running not_the_app.py, that is # '/Users/jw/Desktop/dt/jbs_work/Psychometrician_position/aimsbot2/course_participants_query' except Exception: pass download() # Load and query the person_info_df file loader = CSVLoader(file_path=SUMMARY_FILE, csv_args={ 'delimiter': ',' }) data = loader.load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0) all_splits = text_splitter.split_documents(data) # hopefully n logical splits vectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings()) def learner_query(message, history): conversation = ConversationalRetrievalChain.from_llm(llm = ChatOpenAI(), retriever=vectorstore.as_retriever(), memory=ConversationSummaryMemory(llm=ChatOpenAI(temperature=0), memory_key="chat_history", return_messages=True)) convo = conversation({"question": message}) return convo['answer'] gr.ChatInterface( learner_query, chatbot=gr.Chatbot(height=300), textbox=gr.Textbox(placeholder="Ask FAQ bot a question", container=False, scale=7), title="FAQ bot", description="Type your question and press enter", theme="soft", examples=["What gets covered in the course sessions"], cache_examples=True, retry_btn=None, undo_btn="Delete Previous", clear_btn="Clear", ).launch()