code: # !pip install llama-index faiss-cpu llama-index-vector-stores-faiss
  
  import faiss
  
  from llama_index.core import (
      SimpleDirectoryReader,
      VectorStoreIndex,
      StorageContext,
  )
  
  from llama_index.vector_stores.faiss import FaissVectorStore
  from llama_index.core import get_response_synthesizer
  from llama_index.core.retrievers import VectorIndexRetriever
  from llama_index.core.query_engine import RetrieverQueryEngine
  from llama_index.core.prompts.base import PromptTemplate
  from llama_index.core.prompts.prompt_type import PromptType
  
  if __name__ == "__main__":
      import os
  
      # Instructions:
      # Run the script with the following command: python constrained_rag.py
      # Ensure to have the products directory in the same directory as this script
      # Ensure to have the OPENAI_API_KEY environment variable set
  
      assert os.getenv("OPENAI_API_KEY") is not None, "Please set OPENAI_API_KEY"
  
      # load document vectors
      documents = SimpleDirectoryReader("products/").load_data()
  
      # load faiss index
      d = 1536  # dimension of the vectors
      faiss_index = faiss.IndexFlatL2(d)
  
      # create vector store
      vector_store = FaissVectorStore(faiss_index=faiss_index)
      # initialize storage context
      storage_context = StorageContext.from_defaults(vector_store=vector_store)
      # create index
      index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
  
      # Configure retriever
      retriever = VectorIndexRetriever(index=index, similarity_top_k=1)
  
      QA_PROMPT_TMPL = (
          "Context information is below.\n"
          "---------------------\n"
          "{context_str}\n"
          "---------------------\n"
          "Given only the context information and no prior knowledge, "
          "answer the query.\n"
          "Query: {query_str}\n"
          "Answer: "
          "Otherwise, state: I cannot answer."
      )
      STRICT_QA_PROMPT = PromptTemplate(
          QA_PROMPT_TMPL, prompt_type=PromptType.QUESTION_ANSWER
      )
  
      # Configure response synthesizer
      response_synthesizer = get_response_synthesizer(
          structured_answer_filtering=True,
          response_mode="refine",
          text_qa_template=STRICT_QA_PROMPT,
      )
  
      # Assemble query engine
      safe_query_engine = RetrieverQueryEngine(
          retriever=retriever, response_synthesizer=response_synthesizer
      )
  
      # Execute query and evaluate response
      print(safe_query_engine.query("describe a summer dress with price"))
      print(safe_query_engine.query("describe a horse"))
  
apis: llama_index.core.StorageContext.from_defaults llama_index.core.retrievers.VectorIndexRetriever llama_index.core.prompts.base.PromptTemplate llama_index.core.query_engine.RetrieverQueryEngine llama_index.vector_stores.faiss.FaissVectorStore llama_index.core.VectorStoreIndex.from_documents llama_index.core.SimpleDirectoryReader llama_index.core.get_response_synthesizer
extract_api: [((1083, 1103), 'faiss.IndexFlatL2', 'faiss.IndexFlatL2', (['d'], {}), '(d)\n', (1100, 1103), False, 'import faiss\n'), ((1150, 1191), 'llama_index.vector_stores.faiss.FaissVectorStore', 'FaissVectorStore', ([], {'faiss_index': 'faiss_index'}), '(faiss_index=faiss_index)\n', (1166, 1191), False, 'from llama_index.vector_stores.faiss import FaissVectorStore\n'), ((1247, 1302), 'llama_index.core.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {'vector_store': 'vector_store'}), '(vector_store=vector_store)\n', (1275, 1302), False, 'from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, StorageContext\n'), ((1334, 1409), 'llama_index.core.VectorStoreIndex.from_documents', 'VectorStoreIndex.from_documents', (['documents'], {'storage_context': 'storage_context'}), '(documents, storage_context=storage_context)\n', (1365, 1409), False, 'from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, StorageContext\n'), ((1453, 1506), 'llama_index.core.retrievers.VectorIndexRetriever', 'VectorIndexRetriever', ([], {'index': 'index', 'similarity_top_k': '(1)'}), '(index=index, similarity_top_k=1)\n', (1473, 1506), False, 'from llama_index.core.retrievers import VectorIndexRetriever\n'), ((1891, 1961), 'llama_index.core.prompts.base.PromptTemplate', 'PromptTemplate', (['QA_PROMPT_TMPL'], {'prompt_type': 'PromptType.QUESTION_ANSWER'}), '(QA_PROMPT_TMPL, prompt_type=PromptType.QUESTION_ANSWER)\n', (1905, 1961), False, 'from llama_index.core.prompts.base import PromptTemplate\n'), ((2041, 2163), 'llama_index.core.get_response_synthesizer', 'get_response_synthesizer', ([], {'structured_answer_filtering': '(True)', 'response_mode': '"""refine"""', 'text_qa_template': 'STRICT_QA_PROMPT'}), "(structured_answer_filtering=True, response_mode=\n    'refine', text_qa_template=STRICT_QA_PROMPT)\n", (2065, 2163), False, 'from llama_index.core import get_response_synthesizer\n'), ((2243, 2332), 'llama_index.core.query_engine.RetrieverQueryEngine', 'RetrieverQueryEngine', ([], {'retriever': 'retriever', 'response_synthesizer': 'response_synthesizer'}), '(retriever=retriever, response_synthesizer=\n    response_synthesizer)\n', (2263, 2332), False, 'from llama_index.core.query_engine import RetrieverQueryEngine\n'), ((839, 866), 'os.getenv', 'os.getenv', (['"""OPENAI_API_KEY"""'], {}), "('OPENAI_API_KEY')\n", (848, 866), False, 'import os\n'), ((953, 987), 'llama_index.core.SimpleDirectoryReader', 'SimpleDirectoryReader', (['"""products/"""'], {}), "('products/')\n", (974, 987), False, 'from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, StorageContext\n')]