|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.openai.v1_1; |
| 7 | + |
| 8 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 9 | +import static io.opentelemetry.semconv.incubating.GenAiIncubatingAttributes.GEN_AI_OPERATION_NAME; |
| 10 | +import static io.opentelemetry.semconv.incubating.GenAiIncubatingAttributes.GEN_AI_REQUEST_MODEL; |
| 11 | +import static io.opentelemetry.semconv.incubating.GenAiIncubatingAttributes.GEN_AI_RESPONSE_MODEL; |
| 12 | +import static io.opentelemetry.semconv.incubating.GenAiIncubatingAttributes.GEN_AI_SYSTEM; |
| 13 | +import static io.opentelemetry.semconv.incubating.GenAiIncubatingAttributes.GEN_AI_TOKEN_TYPE; |
| 14 | +import static io.opentelemetry.semconv.incubating.GenAiIncubatingAttributes.GEN_AI_USAGE_INPUT_TOKENS; |
| 15 | +import static io.opentelemetry.semconv.incubating.GenAiIncubatingAttributes.GenAiOperationNameIncubatingValues.EMBEDDINGS; |
| 16 | +import static io.opentelemetry.semconv.incubating.GenAiIncubatingAttributes.GenAiSystemIncubatingValues.OPENAI; |
| 17 | +import static io.opentelemetry.semconv.incubating.GenAiIncubatingAttributes.GenAiTokenTypeIncubatingValues.INPUT; |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import com.openai.client.OpenAIClient; |
| 21 | +import com.openai.client.OpenAIClientAsync; |
| 22 | +import com.openai.models.embeddings.CreateEmbeddingResponse; |
| 23 | +import com.openai.models.embeddings.EmbeddingCreateParams; |
| 24 | +import io.opentelemetry.api.trace.Span; |
| 25 | +import io.opentelemetry.api.trace.SpanKind; |
| 26 | +import io.opentelemetry.context.Context; |
| 27 | +import io.opentelemetry.instrumentation.testing.recording.RecordingExtension; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.concurrent.CompletionException; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 32 | + |
| 33 | +public abstract class AbstractEmbeddingsTest extends AbstractOpenAiTest { |
| 34 | + protected static final String INSTRUMENTATION_NAME = "io.opentelemetry.openai-java-1.1"; |
| 35 | + |
| 36 | + private static final String API_URL = "https://api.openai.com/v1"; |
| 37 | + |
| 38 | + private static final String MODEL = "text-embedding-3-small"; |
| 39 | + |
| 40 | + @RegisterExtension static final RecordingExtension recording = new RecordingExtension(API_URL); |
| 41 | + |
| 42 | + protected final CreateEmbeddingResponse doEmbeddings(EmbeddingCreateParams request) { |
| 43 | + return doEmbeddings(request, getClient(), getClientAsync()); |
| 44 | + } |
| 45 | + |
| 46 | + protected final CreateEmbeddingResponse doEmbeddings( |
| 47 | + EmbeddingCreateParams request, OpenAIClient client, OpenAIClientAsync clientAsync) { |
| 48 | + switch (testType) { |
| 49 | + case SYNC: |
| 50 | + return client.embeddings().create(request); |
| 51 | + case SYNC_FROM_ASYNC: |
| 52 | + return clientAsync.sync().embeddings().create(request); |
| 53 | + case ASYNC: |
| 54 | + case ASYNC_FROM_SYNC: |
| 55 | + OpenAIClientAsync cl = testType == TestType.ASYNC ? clientAsync : client.async(); |
| 56 | + try { |
| 57 | + return cl.embeddings() |
| 58 | + .create(request) |
| 59 | + .thenApply( |
| 60 | + res -> { |
| 61 | + assertThat(Span.fromContextOrNull(Context.current())).isNull(); |
| 62 | + return res; |
| 63 | + }) |
| 64 | + .join(); |
| 65 | + } catch (CompletionException e) { |
| 66 | + if (e.getCause() instanceof RuntimeException) { |
| 67 | + throw (RuntimeException) e.getCause(); |
| 68 | + } |
| 69 | + throw e; |
| 70 | + } |
| 71 | + } |
| 72 | + throw new AssertionError(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void basic() { |
| 77 | + String text = "South Atlantic Ocean."; |
| 78 | + |
| 79 | + EmbeddingCreateParams request = |
| 80 | + EmbeddingCreateParams.builder() |
| 81 | + .model(MODEL) |
| 82 | + .inputOfArrayOfStrings(Collections.singletonList(text)) |
| 83 | + .build(); |
| 84 | + CreateEmbeddingResponse response = doEmbeddings(request); |
| 85 | + |
| 86 | + assertThat(response.data()).hasSize(1); |
| 87 | + |
| 88 | + getTesting() |
| 89 | + .waitAndAssertTraces( |
| 90 | + trace -> |
| 91 | + maybeWithTransportSpan( |
| 92 | + span -> |
| 93 | + span.hasName("embeddings text-embedding-3-small") |
| 94 | + .hasKind(SpanKind.CLIENT) |
| 95 | + .hasAttributesSatisfyingExactly( |
| 96 | + equalTo(GEN_AI_SYSTEM, OPENAI), |
| 97 | + equalTo(GEN_AI_OPERATION_NAME, EMBEDDINGS), |
| 98 | + equalTo(GEN_AI_REQUEST_MODEL, MODEL), |
| 99 | + equalTo(GEN_AI_RESPONSE_MODEL, MODEL), |
| 100 | + equalTo(GEN_AI_USAGE_INPUT_TOKENS, 4)))); |
| 101 | + |
| 102 | + getTesting() |
| 103 | + .waitAndAssertMetrics( |
| 104 | + INSTRUMENTATION_NAME, |
| 105 | + metric -> |
| 106 | + metric |
| 107 | + .hasName("gen_ai.client.operation.duration") |
| 108 | + .hasHistogramSatisfying( |
| 109 | + histogram -> |
| 110 | + histogram.hasPointsSatisfying( |
| 111 | + point -> |
| 112 | + point |
| 113 | + .hasSumGreaterThan(0.0) |
| 114 | + .hasAttributesSatisfyingExactly( |
| 115 | + equalTo(GEN_AI_SYSTEM, "openai"), |
| 116 | + equalTo(GEN_AI_OPERATION_NAME, "chat"), |
| 117 | + equalTo(GEN_AI_REQUEST_MODEL, MODEL), |
| 118 | + equalTo(GEN_AI_RESPONSE_MODEL, MODEL)))), |
| 119 | + metric -> |
| 120 | + metric |
| 121 | + .hasName("gen_ai.client.token.usage") |
| 122 | + .hasHistogramSatisfying( |
| 123 | + histogram -> |
| 124 | + histogram.hasPointsSatisfying( |
| 125 | + point -> |
| 126 | + point |
| 127 | + .hasSum(4.0) |
| 128 | + .hasAttributesSatisfyingExactly( |
| 129 | + equalTo(GEN_AI_SYSTEM, "openai"), |
| 130 | + equalTo(GEN_AI_OPERATION_NAME, "chat"), |
| 131 | + equalTo(GEN_AI_REQUEST_MODEL, MODEL), |
| 132 | + equalTo(GEN_AI_RESPONSE_MODEL, MODEL), |
| 133 | + equalTo(GEN_AI_TOKEN_TYPE, INPUT))))); |
| 134 | + } |
| 135 | +} |
0 commit comments