code: import flask
  import lancedb
  import openai
  import langchain
  # import clip
  import torch
  from langchain.embeddings import OpenAIEmbeddings
  from langchain.vectorstores import LanceDB
  from langchain.docstore.document import Document
  from langchain.embeddings.openai import OpenAIEmbeddings
  from langchain.text_splitter import CharacterTextSplitter
  import os
  import datetime
  from flask import Flask, render_template, request, jsonify
  from langchain.chat_models import ChatOpenAI
  from langchain.prompts.chat import (
      ChatPromptTemplate,
      SystemMessagePromptTemplate,
      AIMessagePromptTemplate,
      HumanMessagePromptTemplate,
  )
  from langchain.schema import (
      AIMessage,
      HumanMessage,
      SystemMessage
  )
  
  
  from dotenv import dotenv_values
  env_vars = dotenv_values('.env')
  
  
  OPENAI_API_KEY = env_vars['OPENAI_API_KEY']
  uri = "~/.lancedb"
  db = lancedb.connect(uri)
  embeddings = OpenAIEmbeddings( openai_api_key= OPENAI_API_KEY)
  
  
  if 'text' not in db.table_names():
      table = db.create_table("text", data=[
          {"vector": embeddings.embed_query("Hello World"), "text": "Hello World", "id": "1"}
      ])
  chat = ChatOpenAI(temperature=0, openai_api_key= OPENAI_API_KEY)
  app = Flask(__name__)
  
  
  
  # Route for "/" for a web-based interface to this micro-service:
  @app.route('/')
  def index():
    return "Hello, World"
  
  @app.after_request
  def add_cors_headers(response):
      response.headers.add('Access-Control-Allow-Origin', '*')
      response.headers.add('Access-Control-Allow-Headers', 'Content-Type')
      response.headers.add('Access-Control-Allow-Methods', 'POST, OPTIONS')
  
      return response
  
  # Handle OPTIONS request for CORS preflight
  @app.route('/store', methods=['OPTIONS'])
  def handle_options():
      response = jsonify({'message': 'Preflight request received'})
      # response.headers['Access-Control-Allow-Origin'] = '*'
      # response.headers.add('Access-Control-Allow-Headers', 'Content-Type')
      # response.headers.add('Access-Control-Allow-Methods', 'POST')
      print('Testing')
      return response
  
  
  @app.route('/store', methods = ['POST'])
  def store_embedding():
      json_data = request.get_json()
      print(json_data['raw_text'][:50],'\n\nURL: ', json_data['url'], '\n\n','title', json_data['title'])
      text = json_data['raw_text']
      text_metadata = {"time": datetime.datetime.now().timestamp(), "url": json_data['url']}
      table = db.open_table('text')
      document = Document(page_content=text, metadata=text_metadata)
      # chunks = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0).split_documents([document])
      # print(len(chunks))
      # print(chunks[0])
      arr = text.split(' ')
      text_documents = []
      for i in range(0, len(arr), 1000):
          chunk = ' '.join(arr[i:i+1000])
          text_documents.append(Document(page_content=chunk, metadata=text_metadata))
      print(len(text_documents))
      docsearch = LanceDB.from_documents(text_documents, embeddings, connection=table)
      
      return jsonify({'message': 'Preflight request received'}), 200
  
  # use curl -X POST -H "Content-Type: application/json" -d '{"query": "basketball"}' http://127.0.0.1:5000/retrieve
  # to test this endpoint
  @app.route('/retrieve', methods = ['POST'])
  def retrieve_embedding():
          query = request.get_json()['query']
          table = db.open_table('text')
          docs = LanceDB(embedding=embeddings, connection=table).similarity_search(query, 5)
          print(docs)
          return [str(d) for d in docs]
     
  
  
  
  @app.route('/chat', methods = ['POST'])
  def chat():
     def retrieve_embedding(query):
          table = db.open_table('text')
          docs = LanceDB(embedding=embeddings, connection=table).similarity_search(query, 3)
  
          print(docs)
          return [d.page_content for d in docs]
     
     json = request.get_json()
     query = json['query']
     embeddings = retrieve_embedding(query)
     final_embeddings = ''
     for embedding in embeddings:
         final_embeddings += embedding + '\n'
     final_embeddings -= '\n'
     query = f"{query}\nCONTEXT: {final_embeddings}"
     messages = [
          SystemMessage(content="You are a human assistant that will help users remember about topics from their previous bookmarks"),
          HumanMessage(content=query)
     ]
     chat(messages)
     return 
  
  
  
  # # image embedding
  
  # device = "cude" if torch.cude.is_available() else "cpu"
  # mode1, preprocess, clip.load("ViT-B/32")
  
     
  if __name__ == '__main__':
      app.run(debug=True)
  
  
  
  
  
apis: lancedb.connect
extract_api: [((760, 781), 'dotenv.dotenv_values', 'dotenv_values', (['""".env"""'], {}), "('.env')\n", (773, 781), False, 'from dotenv import dotenv_values\n'), ((852, 872), 'lancedb.connect', 'lancedb.connect', (['uri'], {}), '(uri)\n', (867, 872), False, 'import lancedb\n'), ((886, 933), 'langchain.embeddings.openai.OpenAIEmbeddings', 'OpenAIEmbeddings', ([], {'openai_api_key': 'OPENAI_API_KEY'}), '(openai_api_key=OPENAI_API_KEY)\n', (902, 933), False, 'from langchain.embeddings.openai import OpenAIEmbeddings\n'), ((1122, 1178), 'langchain.chat_models.ChatOpenAI', 'ChatOpenAI', ([], {'temperature': '(0)', 'openai_api_key': 'OPENAI_API_KEY'}), '(temperature=0, openai_api_key=OPENAI_API_KEY)\n', (1132, 1178), False, 'from langchain.chat_models import ChatOpenAI\n'), ((1186, 1201), 'flask.Flask', 'Flask', (['__name__'], {}), '(__name__)\n', (1191, 1201), False, 'from flask import Flask, render_template, request, jsonify\n'), ((1728, 1778), 'flask.jsonify', 'jsonify', (["{'message': 'Preflight request received'}"], {}), "({'message': 'Preflight request received'})\n", (1735, 1778), False, 'from flask import Flask, render_template, request, jsonify\n'), ((2104, 2122), 'flask.request.get_json', 'request.get_json', ([], {}), '()\n', (2120, 2122), False, 'from flask import Flask, render_template, request, jsonify\n'), ((2400, 2451), 'langchain.docstore.document.Document', 'Document', ([], {'page_content': 'text', 'metadata': 'text_metadata'}), '(page_content=text, metadata=text_metadata)\n', (2408, 2451), False, 'from langchain.docstore.document import Document\n'), ((2859, 2927), 'langchain.vectorstores.LanceDB.from_documents', 'LanceDB.from_documents', (['text_documents', 'embeddings'], {'connection': 'table'}), '(text_documents, embeddings, connection=table)\n', (2881, 2927), False, 'from langchain.vectorstores import LanceDB\n'), ((3744, 3762), 'flask.request.get_json', 'request.get_json', ([], {}), '()\n', (3760, 3762), False, 'from flask import Flask, render_template, request, jsonify\n'), ((2944, 2994), 'flask.jsonify', 'jsonify', (["{'message': 'Preflight request received'}"], {}), "({'message': 'Preflight request received'})\n", (2951, 2994), False, 'from flask import Flask, render_template, request, jsonify\n'), ((3226, 3244), 'flask.request.get_json', 'request.get_json', ([], {}), '()\n', (3242, 3244), False, 'from flask import Flask, render_template, request, jsonify\n'), ((4034, 4167), 'langchain.schema.SystemMessage', 'SystemMessage', ([], {'content': '"""You are a human assistant that will help users remember about topics from their previous bookmarks"""'}), "(content=\n    'You are a human assistant that will help users remember about topics from their previous bookmarks'\n    )\n", (4047, 4167), False, 'from langchain.schema import AIMessage, HumanMessage, SystemMessage\n'), ((4167, 4194), 'langchain.schema.HumanMessage', 'HumanMessage', ([], {'content': 'query'}), '(content=query)\n', (4179, 4194), False, 'from langchain.schema import AIMessage, HumanMessage, SystemMessage\n'), ((2758, 2810), 'langchain.docstore.document.Document', 'Document', ([], {'page_content': 'chunk', 'metadata': 'text_metadata'}), '(page_content=chunk, metadata=text_metadata)\n', (2766, 2810), False, 'from langchain.docstore.document import Document\n'), ((3307, 3354), 'langchain.vectorstores.LanceDB', 'LanceDB', ([], {'embedding': 'embeddings', 'connection': 'table'}), '(embedding=embeddings, connection=table)\n', (3314, 3354), False, 'from langchain.vectorstores import LanceDB\n'), ((2289, 2312), 'datetime.datetime.now', 'datetime.datetime.now', ([], {}), '()\n', (2310, 2312), False, 'import datetime\n'), ((3587, 3634), 'langchain.vectorstores.LanceDB', 'LanceDB', ([], {'embedding': 'embeddings', 'connection': 'table'}), '(embedding=embeddings, connection=table)\n', (3594, 3634), False, 'from langchain.vectorstores import LanceDB\n')]