def create_index()

in supporting-blog-content/elasticsearch_llm_cache/elasticsearch_llm_cache.py [0:0]


    def create_index(self, dims: Optional[int] = 768) -> Dict:
        """
        Create the index if it does not already exist.

        :return: Dictionary containing information about the index creation.
        """
        if not self.es.indices.exists(index=self.index_name):
            mappings = {
                "mappings": {
                    "properties": {
                        "prompt": {"type": "text"},
                        "response": {"type": "text"},
                        "create_date": {"type": "date"},
                        "last_hit_date": {"type": "date"},
                        "prompt_vector": {
                            "type": "dense_vector",
                            "dims": dims,
                            "index": True,
                            "similarity": "dot_product",
                        },
                    }
                }
            }

            self.es.indices.create(index=self.index_name, body=mappings, ignore=400)
            logger.info(f"Index {self.index_name} created.")

            return {"cache_index": self.index_name, "created_new": True}
        else:
            logger.info(f"Index {self.index_name} already exists.")
            return {"cache_index": self.index_name, "created_new": False}