| |
| """ |
| Created on Tue Jul 25 10:49:22 2023 |
| |
| This is python script to create UI through gradio and manage user chat |
| |
| @author: intern.giwon.kim |
| """ |
|
|
|
|
| import gradio as gr |
| import os |
| import PreProcessing |
| from langchain.chains import ConversationalRetrievalChain |
| from langchain.chat_models import ChatOpenAI |
| from langchain.embeddings.openai import OpenAIEmbeddings |
| from langchain.vectorstores import Chroma |
|
|
| |
| os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") |
|
|
| |
| PreProcessing.preProcess() |
|
|
| |
| llm = ChatOpenAI(temperature=0.7,model_name="gpt-4-1106-preview") |
| embeddings = OpenAIEmbeddings() |
| chromaDB = Chroma(persist_directory="./ChromaDB", embedding_function=embeddings) |
| chromaDB.get() |
| qa = ConversationalRetrievalChain.from_llm(llm, chromaDB.as_retriever(), return_source_documents=True) |
|
|
|
|
| |
| with gr.Blocks() as demo: |
| chatbot = gr.Chatbot() |
| msg = gr.Textbox() |
| clear = gr.Button("Clear") |
| chat_history_tuples = [] |
| |
|
|
| def user(user_message, history): |
| |
| response = qa({"question": "Eventhough you are given some context think and response like regular ChatGPT 4 using the knowledge from the world wide web. You are an investment banking analysis, you want to analyze the market and company based on the latest available information. When presented with a question, make sure to acquire latest available in formation before answering the question." + user_message, "chat_history": chat_history_tuples}) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| chat_history = [(user_message, response["answer"])] |
|
|
| |
| |
| for message in chat_history: |
| chat_history_tuples.append((message[0], message[1])) |
| return gr.update(value=""), chat_history_tuples |
| msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False) |
| clear.click(lambda: None, None, chatbot, queue=False) |
|
|
| if __name__ == "__main__": |
| demo.launch() |