code: # langchain Chatbot
  
  from langchain.document_loaders import DataFrameLoader
  import pandas as pd
  from langchain.memory import ConversationSummaryMemory
  import lancedb
  from langchain.vectorstores import LanceDB
  from langchain.embeddings import OpenAIEmbeddings
  from langchain.text_splitter import RecursiveCharacterTextSplitter
  from langchain.chat_models import ChatOpenAI
  from langchain.chains import ConversationalRetrievalChain
  
  
  def lanceDBConnection(dataset):
      db = lancedb.connect("/tmp/lancedb")
      table = db.create_table("tb", data=dataset, mode="overwrite")
      return table
  
  
  def vectorStoreSetup(text, OPENAI_KEY):
      embedding = OpenAIEmbeddings(openai_api_key=OPENAI_KEY)
      emb = embedding.embed_query(text)
      dataset = [{"vector": emb, "text": text}]
  
      table = lanceDBConnection(dataset)
  
      df = pd.DataFrame(dataset)
      loader = DataFrameLoader(df)
      data = loader.load()
  
      text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
      all_splits = text_splitter.split_documents(data)
  
      vectorstore = LanceDB.from_documents(
          documents=all_splits,
          embedding=OpenAIEmbeddings(openai_api_key=OPENAI_KEY),
          connection=table,
      )
      return vectorstore
  
  
  def retrieverSetup(text, OPENAI_KEY):
      vectorstore = vectorStoreSetup(text, OPENAI_KEY)
      llm = ChatOpenAI(openai_api_key=OPENAI_KEY)
      memory = ConversationSummaryMemory(
          llm=llm, memory_key="chat_history", return_messages=True
      )
  
      retriever = vectorstore.as_retriever()
      qa = ConversationalRetrievalChain.from_llm(llm, retriever=retriever, memory=memory)
      return qa
  
  
  def chat(qa, question):
      r = qa(question + "?")
      return r["answer"]
  
apis: lancedb.connect
extract_api: [((472, 503), 'lancedb.connect', 'lancedb.connect', (['"""/tmp/lancedb"""'], {}), "('/tmp/lancedb')\n", (487, 503), False, 'import lancedb\n'), ((645, 688), 'langchain.embeddings.OpenAIEmbeddings', 'OpenAIEmbeddings', ([], {'openai_api_key': 'OPENAI_KEY'}), '(openai_api_key=OPENAI_KEY)\n', (661, 688), False, 'from langchain.embeddings import OpenAIEmbeddings\n'), ((823, 844), 'pandas.DataFrame', 'pd.DataFrame', (['dataset'], {}), '(dataset)\n', (835, 844), True, 'import pandas as pd\n'), ((858, 877), 'langchain.document_loaders.DataFrameLoader', 'DataFrameLoader', (['df'], {}), '(df)\n', (873, 877), False, 'from langchain.document_loaders import DataFrameLoader\n'), ((924, 987), 'langchain.text_splitter.RecursiveCharacterTextSplitter', 'RecursiveCharacterTextSplitter', ([], {'chunk_size': '(500)', 'chunk_overlap': '(0)'}), '(chunk_size=500, chunk_overlap=0)\n', (954, 987), False, 'from langchain.text_splitter import RecursiveCharacterTextSplitter\n'), ((1335, 1372), 'langchain.chat_models.ChatOpenAI', 'ChatOpenAI', ([], {'openai_api_key': 'OPENAI_KEY'}), '(openai_api_key=OPENAI_KEY)\n', (1345, 1372), False, 'from langchain.chat_models import ChatOpenAI\n'), ((1386, 1473), 'langchain.memory.ConversationSummaryMemory', 'ConversationSummaryMemory', ([], {'llm': 'llm', 'memory_key': '"""chat_history"""', 'return_messages': '(True)'}), "(llm=llm, memory_key='chat_history',\n    return_messages=True)\n", (1411, 1473), False, 'from langchain.memory import ConversationSummaryMemory\n'), ((1537, 1615), 'langchain.chains.ConversationalRetrievalChain.from_llm', 'ConversationalRetrievalChain.from_llm', (['llm'], {'retriever': 'retriever', 'memory': 'memory'}), '(llm, retriever=retriever, memory=memory)\n', (1574, 1615), False, 'from langchain.chains import ConversationalRetrievalChain\n'), ((1132, 1175), 'langchain.embeddings.OpenAIEmbeddings', 'OpenAIEmbeddings', ([], {'openai_api_key': 'OPENAI_KEY'}), '(openai_api_key=OPENAI_KEY)\n', (1148, 1175), False, 'from langchain.embeddings import OpenAIEmbeddings\n')]