|
| 1 | +package dev.braintrust.instrumentation.langchain; |
| 2 | + |
| 3 | +import dev.langchain4j.http.client.HttpClientBuilder; |
| 4 | +import dev.langchain4j.http.client.HttpClientBuilderLoader; |
| 5 | +import dev.langchain4j.model.openai.OpenAiChatModel; |
| 6 | +import dev.langchain4j.model.openai.OpenAiStreamingChatModel; |
| 7 | +import io.opentelemetry.api.OpenTelemetry; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | + |
| 10 | +/** Braintrust LangChain4j client instrumentation. */ |
| 11 | +@Slf4j |
| 12 | +public final class BraintrustLangchain { |
| 13 | + /** Instrument langchain openai chat model with braintrust traces */ |
| 14 | + public static OpenAiChatModel wrap( |
| 15 | + OpenTelemetry otel, OpenAiChatModel.OpenAiChatModelBuilder builder) { |
| 16 | + try { |
| 17 | + HttpClientBuilder underlyingHttpClient = getPrivateField(builder, "httpClientBuilder"); |
| 18 | + if (underlyingHttpClient == null) { |
| 19 | + underlyingHttpClient = HttpClientBuilderLoader.loadHttpClientBuilder(); |
| 20 | + } |
| 21 | + HttpClientBuilder wrappedHttpClient = |
| 22 | + wrap(otel, underlyingHttpClient, new Options("openai")); |
| 23 | + return builder.httpClientBuilder(wrappedHttpClient).build(); |
| 24 | + } catch (Exception e) { |
| 25 | + log.warn( |
| 26 | + "Braintrust instrumentation could not be applied to OpenAiChatModel builder", |
| 27 | + e); |
| 28 | + return builder.build(); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /** Instrument langchain openai chat model with braintrust traces */ |
| 33 | + public static OpenAiStreamingChatModel wrap( |
| 34 | + OpenTelemetry otel, OpenAiStreamingChatModel.OpenAiStreamingChatModelBuilder builder) { |
| 35 | + try { |
| 36 | + HttpClientBuilder underlyingHttpClient = getPrivateField(builder, "httpClientBuilder"); |
| 37 | + if (underlyingHttpClient == null) { |
| 38 | + underlyingHttpClient = HttpClientBuilderLoader.loadHttpClientBuilder(); |
| 39 | + } |
| 40 | + HttpClientBuilder wrappedHttpClient = |
| 41 | + wrap(otel, underlyingHttpClient, new Options("openai")); |
| 42 | + return builder.httpClientBuilder(wrappedHttpClient).build(); |
| 43 | + } catch (Exception e) { |
| 44 | + log.warn( |
| 45 | + "Braintrust instrumentation could not be applied to OpenAiStreamingChatModel" |
| 46 | + + " builder", |
| 47 | + e); |
| 48 | + return builder.build(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private static HttpClientBuilder wrap( |
| 53 | + OpenTelemetry otel, HttpClientBuilder builder, Options options) { |
| 54 | + return new WrappedHttpClientBuilder(otel, builder, options); |
| 55 | + } |
| 56 | + |
| 57 | + public record Options(String providerName) {} |
| 58 | + |
| 59 | + @SuppressWarnings("unchecked") |
| 60 | + private static <T> T getPrivateField(Object obj, String fieldName) |
| 61 | + throws ReflectiveOperationException { |
| 62 | + java.lang.reflect.Field field = obj.getClass().getDeclaredField(fieldName); |
| 63 | + field.setAccessible(true); |
| 64 | + return (T) field.get(obj); |
| 65 | + } |
| 66 | +} |
0 commit comments