import os from typing import Optional, Tuple import gradio as gr from threading import Lock from langchain.vectorstores import Pinecone from langchain.llms import OpenAI from langchain.chains.question_answering import load_qa_chain from langchain.embeddings.openai import OpenAIEmbeddings import pinecone PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY") PINECONE_API_ENV = os.environ.get("PINECONE_API_ENV") OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") PINECONE_INDEX_NAME = os.environ.get("PINECONE_INDEX_NAME") def get_chain_and_vectorstore(): llm = OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY) chain = load_qa_chain(llm) embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY) pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_API_ENV) vectorstore = Pinecone.from_existing_index(PINECONE_INDEX_NAME, embeddings) return chain, vectorstore class ChatWrapper: def __init__(self): self.lock = Lock() def __call__( self, inp: str, history: Optional[Tuple[str, str]], chain, vectorstore=None ): """Execute the chat functionality.""" self.lock.acquire() if not chain or not vectorstore: chain, vectorstore = get_chain_and_vectorstore() try: history = history or [] docs = vectorstore.similarity_search(inp, k=2) output = chain.run(input_documents=docs, question=inp, chat_history=history) history.append((inp, output)) except Exception as e: raise e finally: self.lock.release() return history, history chat = ChatWrapper() block = gr.Blocks(css=".gradio-container {background-color: lightgray}") with block: with gr.Row(): gr.Markdown("