-
Notifications
You must be signed in to change notification settings - Fork 21
Fix instrumentation support for OpenAI client 0.14+ #531
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
Changes from 7 commits
92029fb
3640ff5
ff54d5f
c9bf001
9a45fa6
95da2ca
16d9ec0
b818c1f
106a90c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
|
|
||
| * Add support for OpenAI client 0.14+ - #531 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| plugins { | ||
| id("elastic-otel.java-conventions") | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly(catalog.openaiClient) | ||
| compileOnly("io.opentelemetry:opentelemetry-sdk") | ||
| compileOnly("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api") | ||
| compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api") | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| package co.elastic.otel.openai.wrappers; | ||
|
|
||
| import com.openai.models.ChatCompletionAssistantMessageParam; | ||
| import com.openai.models.ChatCompletionContentPart; | ||
| import com.openai.models.ChatCompletionCreateParams; | ||
| import com.openai.models.ChatCompletionMessageParam; | ||
| import com.openai.models.ChatCompletionSystemMessageParam; | ||
| import com.openai.models.ChatCompletionToolMessageParam; | ||
| import com.openai.models.ChatCompletionUserMessageParam; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Api Adapter to encapsulate breaking changes across openai-client versions. If e.g. methods are | ||
| * renamed we add a adapter method here, so that we can provide per-version implementations. These | ||
| * implementations have to be added to instrumentations as helpers, which also ensures muzzle works | ||
| * effectively. | ||
| */ | ||
| public abstract class ApiAdapter { | ||
|
|
||
| private static volatile ApiAdapter instance; | ||
|
|
||
| public static ApiAdapter get() { | ||
| return instance; | ||
| } | ||
|
|
||
| protected static void init(Supplier<ApiAdapter> implementation) { | ||
| if (instance == null) { | ||
| synchronized (ApiAdapter.class) { | ||
| if (instance == null) { | ||
| instance = implementation.get(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the concrete message object e.g. ({@link ChatCompletionUserMessageParam}) from the | ||
| * given encapsulating {@link ChatCompletionMessageParam}. | ||
| * | ||
| * @param base the encapsulating param | ||
| * @return the unboxed concrete message param type | ||
| */ | ||
| public abstract Object extractConcreteCompletionMessageParam(ChatCompletionMessageParam base); | ||
|
|
||
| /** | ||
| * @return the contained text, if the content is next. null otherwise. | ||
| */ | ||
| public abstract String asText(ChatCompletionToolMessageParam.Content content); | ||
|
|
||
| /** | ||
| * @return the contained text, if the content is next. null otherwise. | ||
| */ | ||
| public abstract String asText(ChatCompletionAssistantMessageParam.Content content); | ||
|
|
||
| /** | ||
| * @return the contained text, if the content is next. null otherwise. | ||
| */ | ||
| public abstract String asText(ChatCompletionSystemMessageParam.Content content); | ||
|
|
||
| /** | ||
| * @return the contained text, if the content is next. null otherwise. | ||
| */ | ||
| public abstract String asText(ChatCompletionUserMessageParam.Content content); | ||
|
|
||
| /** | ||
| * @return the text or refusal reason if either is available, otherwise null | ||
| */ | ||
| public abstract String extractTextOrRefusal( | ||
| ChatCompletionAssistantMessageParam.Content.ChatCompletionRequestAssistantMessageContentPart | ||
| part); | ||
|
|
||
| /** | ||
| * @return the text if available, otherwise null | ||
| */ | ||
| public abstract String extractText(ChatCompletionContentPart part); | ||
|
|
||
| public abstract String extractType(ChatCompletionCreateParams.ResponseFormat val); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| plugins { | ||
| alias(catalog.plugins.muzzleGeneration) | ||
| alias(catalog.plugins.muzzleCheck) | ||
| id("elastic-otel.instrumentation-conventions") | ||
| } | ||
|
|
||
| val openAiVersion = "0.13.0"; // DO NOT UPGRADE | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it something that renovate/dependabot would attempt to upgrade ? If so I think there are ways like splitting the string in parts to prevent this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. I don't know if renovate is smart enough for this. We can just keep it like this for now and see if renovate detects it and attempts an upgrade |
||
|
|
||
| dependencies { | ||
| compileOnly("com.openai:openai-java:${openAiVersion}") | ||
| implementation(project(":instrumentation:openai-client-instrumentation:common")) | ||
|
|
||
| testImplementation("com.openai:openai-java:${openAiVersion}") | ||
| testImplementation(project(":instrumentation:openai-client-instrumentation:testing-common")) | ||
| } | ||
|
|
||
| muzzle { | ||
| pass { | ||
| group.set("com.openai") | ||
| module.set("openai-java") | ||
| versions.set("(,${openAiVersion}]") | ||
| assertInverse.set(true) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.