import datetime import os import gradio as gr import langchain import pickle from langchain.vectorstores import Weaviate from langchain import OpenAI from chain import get_new_chain1 def get_faiss_store(): with open("docs.pkl", 'rb') as f: faiss_store = pickle.load(f) return faiss_store def set_openai_api_key(api_key, agent): if api_key: os.environ["OPENAI_API_KEY"] = api_key vectorstore = get_faiss_store() rephraser_llm = OpenAI(model_name="text-davinci-003", temperature=0) final_output_llm = OpenAI(model_name="text-davinci-003", temperature=0, max_tokens=-1) qa_chain = get_new_chain1(vectorstore, rephraser_llm, final_output_llm) os.environ["OPENAI_API_KEY"] = "" return qa_chain def chat(inp, history, agent): history = history or [] if agent is None: history.append((inp, "Please paste your OpenAI key to use")) return history, history print("\n==== date/time: " + str(datetime.datetime.now()) + " ====") print("inp: " + inp) history = history or [] output = agent({"question": inp, "chat_history": history}) answer = output["answer"] history.append((inp, answer)) print(history) return history, history block = gr.Blocks(css=".gradio-container {background-color: lightgray}") with block: with gr.Row(): gr.Markdown("