-
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 1 commit
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
23 changes: 23 additions & 0 deletions
23
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,23 @@ | ||
| 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() | ||
49 changes: 49 additions & 0 deletions
49
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,49 @@ | ||
| 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 |
191 changes: 191 additions & 0 deletions
191
instrumentation/elastic-opentelemetry-instrumentation-openai/tests/base.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,191 @@ | ||
| import os | ||
xrmx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from dataclasses import dataclass | ||
|
|
||
| import openai | ||
| from opentelemetry.instrumentation.openai import OpenAIInstrumentor | ||
| from opentelemetry.metrics import Histogram | ||
| from vcr.unittest import VCRMixin | ||
|
|
||
| # Use the same model for tools as for chat completion | ||
| OPENAI_API_KEY = "test_openai_api_key" | ||
| OPENAI_ORG_ID = "test_openai_org_key" | ||
| OPENAI_PROJECT_ID = "test_openai_project_id" | ||
|
|
||
| LOCAL_MODEL = "qwen2.5:0.5b" | ||
|
|
||
|
|
||
| @dataclass | ||
| class OpenAIEnvironment: | ||
| # TODO: add system | ||
| operation_name: str = "chat" | ||
| model: str = "gpt-4o-mini" | ||
| response_model: str = "gpt-4o-mini-2024-07-18" | ||
| server_address: str = "api.openai.com" | ||
| server_port: int = 443 | ||
|
|
||
|
|
||
| class OpenaiMixin(VCRMixin): | ||
| def _get_vcr_kwargs(self, **kwargs): | ||
| """ | ||
| This scrubs sensitive data and gunzips bodies when in recording mode. | ||
| Without this, you would leak cookies and auth tokens in the cassettes. | ||
| Also, depending on the request, some responses would be binary encoded | ||
| while others plain json. This ensures all bodies are human-readable. | ||
| """ | ||
| return { | ||
| "decode_compressed_response": True, | ||
| "filter_headers": [ | ||
| ("authorization", "Bearer " + OPENAI_API_KEY), | ||
| ("openai-organization", OPENAI_ORG_ID), | ||
| ("openai-project", OPENAI_PROJECT_ID), | ||
| ("cookie", None), | ||
| ], | ||
| "before_record_response": self.scrub_response_headers, | ||
| } | ||
|
|
||
| @staticmethod | ||
| def scrub_response_headers(response): | ||
| """ | ||
| This scrubs sensitive response headers. Note they are case-sensitive! | ||
| """ | ||
| response["headers"]["openai-organization"] = OPENAI_ORG_ID | ||
| response["headers"]["Set-Cookie"] = "test_set_cookie" | ||
| return response | ||
|
|
||
| @classmethod | ||
| def setup_client(cls): | ||
| # Control the arguments | ||
| return openai.Client( | ||
| api_key=os.getenv("OPENAI_API_KEY", OPENAI_API_KEY), | ||
| organization=os.getenv("OPENAI_ORG_ID", OPENAI_ORG_ID), | ||
| project=os.getenv("OPENAI_PROJECT_ID", OPENAI_PROJECT_ID), | ||
| max_retries=1, | ||
| ) | ||
|
|
||
| @classmethod | ||
| def setup_environment(cls): | ||
| return OpenAIEnvironment() | ||
|
|
||
| @classmethod | ||
| def setUpClass(cls): | ||
| cls.client = cls.setup_client() | ||
| cls.openai_env = cls.setup_environment() | ||
|
|
||
| def setUp(self): | ||
| super().setUp() | ||
| OpenAIInstrumentor().instrument() | ||
|
|
||
| def tearDown(self): | ||
| super().tearDown() | ||
| OpenAIInstrumentor().uninstrument() | ||
|
|
||
| def assertOperationDurationMetric(self, metric: Histogram): | ||
| self.assertEqual(metric.name, "gen_ai.client.operation.duration") | ||
| self.assert_metric_expected( | ||
| metric, | ||
| [ | ||
| self.create_histogram_data_point( | ||
| count=1, | ||
| sum_data_point=0.006543334107846022, | ||
| max_data_point=0.006543334107846022, | ||
| min_data_point=0.006543334107846022, | ||
| attributes={ | ||
| "gen_ai.operation.name": self.openai_env.operation_name, | ||
| "gen_ai.request.model": self.openai_env.model, | ||
| "gen_ai.response.model": self.openai_env.response_model, | ||
| "gen_ai.system": "openai", | ||
| "server.address": self.openai_env.server_address, | ||
| "server.port": self.openai_env.server_port, | ||
| }, | ||
| ), | ||
| ], | ||
| est_value_delta=0.2, | ||
| ) | ||
|
|
||
| def assertErrorOperationDurationMetric(self, metric: Histogram, attributes: dict, data_point: float = None): | ||
| self.assertEqual(metric.name, "gen_ai.client.operation.duration") | ||
| default_attributes = { | ||
| "gen_ai.operation.name": self.openai_env.operation_name, | ||
| "gen_ai.request.model": self.openai_env.model, | ||
| "gen_ai.system": "openai", | ||
| "error.type": "APIConnectionError", | ||
| "server.address": "localhost", | ||
| "server.port": 9999, | ||
| } | ||
| if data_point is None: | ||
| data_point = 0.8643839359283447 | ||
| self.assert_metric_expected( | ||
| metric, | ||
| [ | ||
| self.create_histogram_data_point( | ||
| count=1, | ||
| sum_data_point=data_point, | ||
| max_data_point=data_point, | ||
| min_data_point=data_point, | ||
| attributes={**default_attributes, **attributes}, | ||
| ), | ||
| ], | ||
| est_value_delta=0.5, | ||
| ) | ||
|
|
||
| def assertTokenUsageInputMetric(self, metric: Histogram, input_data_point=4): | ||
| self.assertEqual(metric.name, "gen_ai.client.token.usage") | ||
| self.assert_metric_expected( | ||
| metric, | ||
| [ | ||
| self.create_histogram_data_point( | ||
| count=1, | ||
| sum_data_point=input_data_point, | ||
| max_data_point=input_data_point, | ||
| min_data_point=input_data_point, | ||
| attributes={ | ||
| "gen_ai.operation.name": self.openai_env.operation_name, | ||
| "gen_ai.request.model": self.openai_env.model, | ||
| "gen_ai.response.model": self.openai_env.response_model, | ||
| "gen_ai.system": "openai", | ||
| "server.address": self.openai_env.server_address, | ||
| "server.port": self.openai_env.server_port, | ||
| "gen_ai.token.type": "input", | ||
| }, | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
| def assertTokenUsageMetric(self, metric: Histogram, input_data_point=24, output_data_point=4): | ||
| self.assertEqual(metric.name, "gen_ai.client.token.usage") | ||
| self.assert_metric_expected( | ||
| metric, | ||
| [ | ||
| self.create_histogram_data_point( | ||
| count=1, | ||
| sum_data_point=input_data_point, | ||
| max_data_point=input_data_point, | ||
| min_data_point=input_data_point, | ||
| attributes={ | ||
| "gen_ai.operation.name": self.openai_env.operation_name, | ||
| "gen_ai.request.model": self.openai_env.model, | ||
| "gen_ai.response.model": self.openai_env.response_model, | ||
| "gen_ai.system": "openai", | ||
| "server.address": self.openai_env.server_address, | ||
| "server.port": self.openai_env.server_port, | ||
| "gen_ai.token.type": "input", | ||
| }, | ||
| ), | ||
| self.create_histogram_data_point( | ||
| count=1, | ||
| sum_data_point=output_data_point, | ||
| max_data_point=output_data_point, | ||
| min_data_point=output_data_point, | ||
| attributes={ | ||
| "gen_ai.operation.name": self.openai_env.operation_name, | ||
| "gen_ai.request.model": self.openai_env.model, | ||
| "gen_ai.response.model": self.openai_env.response_model, | ||
| "gen_ai.system": "openai", | ||
| "server.address": self.openai_env.server_address, | ||
| "server.port": self.openai_env.server_port, | ||
| "gen_ai.token.type": "output", | ||
| }, | ||
| ), | ||
| ], | ||
| ) | ||
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.