-
Notifications
You must be signed in to change notification settings - Fork 5
openai: adds examples and flattens tests #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6e3bcc8
openai: adds examples and flattens tests
codefromthecrypt 96f7eb2
Add missing license headers
xrmx 8b42a63
Merge branch 'main' into test-examples
xrmx aa65897
Apply suggestions from code review
xrmx a81637a
prune
codefromthecrypt db1d71b
Introduce ollama after using OpenAI
xrmx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
instrumentation/elastic-opentelemetry-instrumentation-openai/examples/chat.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| # or more contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright | ||
| # ownership. Elasticsearch B.V. licenses this file to you under | ||
| # the Apache License, Version 2.0 (the "License"); you may | ||
| # not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
xrmx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import openai | ||
|
|
||
| CHAT_MODEL = os.environ.get("TEST_CHAT_MODEL", "gpt-4o-mini") | ||
|
|
||
|
|
||
| def main(): | ||
| client = openai.Client() | ||
|
|
||
| messages = [ | ||
| { | ||
| "role": "user", | ||
| "content": "Answer in up to 3 words: Which ocean contains Bouvet Island?", | ||
| } | ||
| ] | ||
|
|
||
| chat_completion = client.chat.completions.create(model=CHAT_MODEL, messages=messages) | ||
| print(chat_completion.choices[0].message.content) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
65 changes: 65 additions & 0 deletions
65
instrumentation/elastic-opentelemetry-instrumentation-openai/examples/embeddings.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| # or more contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright | ||
| # ownership. Elasticsearch B.V. licenses this file to you under | ||
| # the Apache License, Version 2.0 (the "License"); you may | ||
| # not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
xrmx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import numpy as np | ||
| import openai | ||
|
|
||
| EMBEDDINGS_MODEL = os.environ.get("TEST_EMBEDDINGS_MODEL", "text-embedding-3-small") | ||
|
|
||
|
|
||
| def main(): | ||
| client = openai.Client() | ||
|
|
||
| products = [ | ||
| "Search: Ingest your data, and explore Elastic's machine learning and retrieval augmented generation (RAG) capabilities." | ||
| "Observability: Unify your logs, metrics, traces, and profiling at scale in a single platform.", | ||
| "Security: Protect, investigate, and respond to cyber threats with AI-driven security analytics." | ||
| "Elasticsearch: Distributed, RESTful search and analytics.", | ||
| "Kibana: Visualize your data. Navigate the Stack.", | ||
| "Beats: Collect, parse, and ship in a lightweight fashion.", | ||
| "Connectors: Connect popular databases, file systems, collaboration tools, and more.", | ||
| "Logstash: Ingest, transform, enrich, and output.", | ||
| ] | ||
|
|
||
| # Generate embeddings for each product. Keep them in an array instead of a vector DB. | ||
| product_embeddings = [] | ||
| for product in products: | ||
| product_embeddings.append(create_embedding(client, product)) | ||
|
|
||
| query_embedding = create_embedding(client, "What can help me connect to a database?") | ||
|
|
||
| # Calculate cosine similarity between the query and document embeddings | ||
| similarities = [] | ||
| for product_embedding in product_embeddings: | ||
| similarity = np.dot(query_embedding, product_embedding) / ( | ||
| np.linalg.norm(query_embedding) * np.linalg.norm(product_embedding) | ||
| ) | ||
| similarities.append(similarity) | ||
|
|
||
| # Get the index of the most similar document | ||
| most_similar_index = np.argmax(similarities) | ||
|
|
||
| print(products[most_similar_index]) | ||
|
|
||
|
|
||
| def create_embedding(client, text): | ||
| return client.embeddings.create(input=[text], model=EMBEDDINGS_MODEL, encoding_format="float").data[0].embedding | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
10 changes: 10 additions & 0 deletions
10
instrumentation/elastic-opentelemetry-instrumentation-openai/ollama.env
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Env to run the integration tests against a local Ollama. | ||
| OPENAI_BASE_URL=http://127.0.0.1:11434/v1 | ||
| OPENAI_API_KEY=notused | ||
|
|
||
| # These models may be substituted in the future with inexpensive to run, newer | ||
| # variants. | ||
| TEST_CHAT_MODEL=qwen2.5:0.5b | ||
| TEST_EMBEDDINGS_MODEL=all-minilm:33m | ||
|
|
||
| OTEL_SERVICE_NAME=elastic-opentelemetry-instrumentation-openai |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.