|
| 1 | +# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +# or more contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright |
| 4 | +# ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +# the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import os |
| 18 | + |
| 19 | +import numpy as np |
| 20 | +import openai |
| 21 | + |
| 22 | +EMBEDDINGS_MODEL = os.environ.get("EMBEDDINGS_MODEL", "text-embedding-3-small") |
| 23 | + |
| 24 | + |
| 25 | +def main(): |
| 26 | + client = openai.Client() |
| 27 | + |
| 28 | + products = [ |
| 29 | + "Search: Ingest your data, and explore Elastic's machine learning and retrieval augmented generation (RAG) capabilities." |
| 30 | + "Observability: Unify your logs, metrics, traces, and profiling at scale in a single platform.", |
| 31 | + "Security: Protect, investigate, and respond to cyber threats with AI-driven security analytics." |
| 32 | + "Elasticsearch: Distributed, RESTful search and analytics.", |
| 33 | + "Kibana: Visualize your data. Navigate the Stack.", |
| 34 | + "Beats: Collect, parse, and ship in a lightweight fashion.", |
| 35 | + "Connectors: Connect popular databases, file systems, collaboration tools, and more.", |
| 36 | + "Logstash: Ingest, transform, enrich, and output.", |
| 37 | + ] |
| 38 | + |
| 39 | + # Generate embeddings for each product. Keep them in an array instead of a vector DB. |
| 40 | + product_embeddings = [] |
| 41 | + for product in products: |
| 42 | + product_embeddings.append(create_embedding(client, product)) |
| 43 | + |
| 44 | + query_embedding = create_embedding(client, "What can help me connect to a database?") |
| 45 | + |
| 46 | + # Calculate cosine similarity between the query and document embeddings |
| 47 | + similarities = [] |
| 48 | + for product_embedding in product_embeddings: |
| 49 | + similarity = np.dot(query_embedding, product_embedding) / ( |
| 50 | + np.linalg.norm(query_embedding) * np.linalg.norm(product_embedding) |
| 51 | + ) |
| 52 | + similarities.append(similarity) |
| 53 | + |
| 54 | + # Get the index of the most similar document |
| 55 | + most_similar_index = np.argmax(similarities) |
| 56 | + |
| 57 | + print(products[most_similar_index]) |
| 58 | + |
| 59 | + |
| 60 | +def create_embedding(client, text): |
| 61 | + return client.embeddings.create(input=[text], model=EMBEDDINGS_MODEL, encoding_format="float").data[0].embedding |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + main() |
0 commit comments