Skip to content

Commit 2ce4cdc

Browse files
authored
Merge pull request #116 from braintrustdata/ark/refactor-examples
refactor examples into separate subprojects
2 parents 92826c9 + 1d68cbc commit 2ce4cdc

33 files changed

Lines changed: 295 additions & 298 deletions

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ To send Braintrust otel data to the local collector:
6767

6868
```
6969
# assumes you have BRAINTRUST_API_KEY and OPENAI_API_KEY exported
70-
export BRAINTRUST_API_URL="http://localhost:4318" ; export BRAINTRUST_TRACES_PATH="/v1/traces"; export BRAINTRUST_LOGS_PATH="/v1/logs" ; ./gradlew :examples:runOpenAIInstrumentation
70+
export BRAINTRUST_API_URL="http://localhost:4318" ; export BRAINTRUST_TRACES_PATH="/v1/traces"; export BRAINTRUST_LOGS_PATH="/v1/logs" ; ./gradlew :examples:openai-instrumentation:run
7171
```

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ A list of supported instrumentation can be found [here](./braintrust-sdk/instrum
105105

106106
## Running Examples
107107

108-
Example source code can be found [here](./examples/src/main/java/dev/braintrust/examples)
108+
Example source code can be found [here](./examples). Each example is its own Gradle subproject under `examples/` with its own `:run` target.
109109

110110
```bash
111111
export BRAINTRUST_API_KEY="your-braintrust-api-key"
@@ -115,9 +115,9 @@ export ANTHROPIC_API_KEY="your-anthropic-api-key" # to run anthropic examples
115115
brew install openjdk@17 # macOS
116116
sudo apt install openjdk-17-jdk # ubuntu
117117
# to run a specific example
118-
./gradlew :examples:runSimpleOpenTelemetry
119-
# to see all examples
120-
./gradlew :examples:tasks --group="Braintrust SDK Examples"
118+
./gradlew :examples:simple-open-telemetry:run
119+
# to list every example subproject
120+
./gradlew projects
121121
```
122122

123123
## Logging
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
application {
2+
mainClass = 'dev.braintrust.examples.AnthropicInstrumentationExample'
3+
}
4+
5+
dependencies {
6+
implementation project(':braintrust-sdk:instrumentation:anthropic_2_2_0')
7+
implementation "com.anthropic:anthropic-java:2.8.1"
8+
}
9+
10+
run {
11+
description = 'Run the Anthropic instrumentation example. NOTE: this requires ANTHROPIC_API_KEY to be exported and will make a small call to anthropic, using your tokens'
12+
debugOptions {
13+
enabled = true
14+
port = 5566
15+
server = true
16+
suspend = false
17+
}
18+
}

examples/src/main/java/dev/braintrust/examples/AnthropicInstrumentationExample.java renamed to examples/anthropic-instrumentation/src/main/java/dev/braintrust/examples/AnthropicInstrumentationExample.java

File renamed without changes.

examples/build.gradle

Lines changed: 33 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -1,201 +1,45 @@
1-
plugins {
2-
id 'java'
3-
id 'application'
4-
}
5-
6-
java {
7-
toolchain {
8-
languageVersion = JavaLanguageVersion.of(17)
9-
vendor = JvmVendorSpec.ADOPTIUM // eclipse JVM
10-
}
11-
withJavadocJar()
12-
withSourcesJar()
13-
}
14-
15-
repositories {
16-
mavenCentral()
17-
}
18-
19-
def braintrustLogLevel = System.getenv('BRAINTRUST_LOG_LEVEL') ?: 'info'
20-
21-
dependencies {
22-
implementation project(':braintrust-sdk')
23-
implementation project(':braintrust-sdk:instrumentation:openai_2_15_0')
24-
implementation project(':braintrust-sdk:instrumentation:anthropic_2_2_0')
25-
implementation project(':braintrust-sdk:instrumentation:genai_1_18_0')
26-
implementation project(':braintrust-sdk:instrumentation:langchain_1_8_0')
27-
implementation project(':braintrust-sdk:instrumentation:springai_1_0_0')
28-
implementation project(':braintrust-sdk:instrumentation:aws_bedrock_2_30_0')
29-
runtimeOnly "org.slf4j:slf4j-simple:${slf4jVersion}"
30-
// To run otel examples
31-
implementation "io.opentelemetry:opentelemetry-exporter-otlp:${otelVersion}"
32-
// to run OAI instrumentation examples
33-
implementation 'com.openai:openai-java:2.8.1'
34-
// to run anthropic examples
35-
implementation "com.anthropic:anthropic-java:2.8.1"
36-
// to run gemini examples
37-
implementation 'com.google.genai:google-genai:1.20.0'
38-
// spring ai examples
39-
implementation 'org.springframework.ai:spring-ai-anthropic:1.1.0'
40-
implementation 'org.springframework.ai:spring-ai-bedrock-converse:1.1.0'
41-
implementation 'org.springframework.ai:spring-ai-google-genai:1.1.0'
42-
implementation 'org.springframework.ai:spring-ai-openai:1.1.0'
43-
// spring-ai-openai requires spring-webflux (WebClient) at runtime
44-
implementation 'org.springframework:spring-webflux:6.2.3'
45-
// spring boot for SpringAIExample (exclude logback, use slf4j-simple like other examples)
46-
implementation('org.springframework.boot:spring-boot-starter:3.4.1') {
47-
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
48-
}
49-
// to run langchain4j examples
50-
implementation 'dev.langchain4j:langchain4j:1.9.1'
51-
implementation 'dev.langchain4j:langchain4j-open-ai:1.9.1'
52-
53-
}
54-
55-
application {
56-
mainClass = 'dev.braintrust.examples.SimpleOpenTelemetryExample'
57-
}
58-
59-
task runSimpleOpenTelemetry(type: JavaExec) {
60-
group = 'Braintrust SDK Examples'
61-
description = 'Run the simple OpenTelemetry example'
62-
classpath = sourceSets.main.runtimeClasspath
63-
mainClass = 'dev.braintrust.examples.SimpleOpenTelemetryExample'
64-
systemProperty 'org.slf4j.simpleLogger.log.dev.braintrust', braintrustLogLevel
65-
}
66-
67-
task runCustomOpenTelemetry(type: JavaExec) {
68-
group = 'Braintrust SDK Examples'
69-
description = 'Run the custom OpenTelemetry example'
70-
classpath = sourceSets.main.runtimeClasspath
71-
mainClass = 'dev.braintrust.examples.CustomOpenTelemetryExample'
72-
systemProperty 'org.slf4j.simpleLogger.log.dev.braintrust', braintrustLogLevel
73-
}
1+
// Shared convention for all example subprojects.
2+
//
3+
// Each example lives in its own subproject under examples/ and applies the
4+
// 'application' plugin so it can be launched with:
5+
//
6+
// ./gradlew :examples:<example-name>:run
7+
//
8+
// List every example via:
9+
//
10+
// ./gradlew projects
11+
//
12+
// Per-example build.gradle files only declare their mainClass, extra
13+
// dependencies, and (optionally) a description / debugOptions block on the
14+
// inherited 'run' task.
7415

75-
task runOpenAIInstrumentation(type: JavaExec) {
76-
group = 'Braintrust SDK Examples'
77-
description = 'Run the OpenAI instrumentation example. NOTE: this requires OPENAI_API_KEY to be exported and will make a small call to openai, using your tokens'
78-
classpath = sourceSets.main.runtimeClasspath
79-
mainClass = 'dev.braintrust.examples.OpenAIInstrumentationExample'
80-
systemProperty 'org.slf4j.simpleLogger.log.dev.braintrust', braintrustLogLevel
81-
debugOptions {
82-
enabled = true
83-
port = 5566
84-
server = true
85-
suspend = false
86-
}
87-
}
16+
subprojects {
17+
apply plugin: 'java'
18+
apply plugin: 'application'
8819

89-
task runExperiment(type: JavaExec) {
90-
group = 'Braintrust SDK Examples'
91-
description = 'Run the experiment example'
92-
classpath = sourceSets.main.runtimeClasspath
93-
mainClass = 'dev.braintrust.examples.ExperimentExample'
94-
systemProperty 'org.slf4j.simpleLogger.log.dev.braintrust', braintrustLogLevel
95-
debugOptions {
96-
enabled = true
97-
port = 5566
98-
server = true
99-
suspend = false
100-
}
101-
}
20+
group = rootProject.group
21+
version = rootProject.version
10222

103-
task runAnthropicInstrumentation(type: JavaExec) {
104-
group = 'Braintrust SDK Examples'
105-
description = 'Run the Anthropic instrumentation example. NOTE: this requires ANTHROPIC_API_KEY to be exported and will make a small call to anthropic, using your tokens'
106-
classpath = sourceSets.main.runtimeClasspath
107-
mainClass = 'dev.braintrust.examples.AnthropicInstrumentationExample'
108-
systemProperty 'org.slf4j.simpleLogger.log.dev.braintrust', braintrustLogLevel
109-
debugOptions {
110-
enabled = true
111-
port = 5566
112-
server = true
113-
suspend = false
23+
java {
24+
toolchain {
25+
languageVersion = JavaLanguageVersion.of(17)
26+
vendor = JvmVendorSpec.ADOPTIUM // eclipse JVM
27+
}
11428
}
115-
}
11629

117-
118-
task runPromptFetching(type: JavaExec) {
119-
group = 'Braintrust SDK Examples'
120-
description = 'Run the prompt fetching example'
121-
classpath = sourceSets.main.runtimeClasspath
122-
mainClass = 'dev.braintrust.examples.PromptFetchingExample'
123-
systemProperty 'org.slf4j.simpleLogger.log.dev.braintrust', braintrustLogLevel
124-
debugOptions {
125-
enabled = true
126-
port = 5566
127-
server = true
128-
suspend = false
30+
repositories {
31+
mavenCentral()
12932
}
130-
}
13133

132-
task runGeminiInstrumentation(type: JavaExec) {
133-
group = 'Braintrust SDK Examples'
134-
description = 'Run the Gemini instrumentation example. NOTE: this requires GOOGLE_API_KEY or GEMINI_API_KEY to be exported and will make a small call to google, using your tokens'
135-
classpath = sourceSets.main.runtimeClasspath
136-
mainClass = 'dev.braintrust.examples.GeminiInstrumentationExample'
137-
systemProperty 'org.slf4j.simpleLogger.log.dev.braintrust', braintrustLogLevel
138-
debugOptions {
139-
enabled = true
140-
port = 5566
141-
server = true
142-
suspend = false
143-
}
144-
}
34+
def braintrustLogLevel = System.getenv('BRAINTRUST_LOG_LEVEL') ?: 'info'
14535

146-
task runSpringAI(type: JavaExec) {
147-
group = 'Braintrust SDK Examples'
148-
description = 'Run the Spring Boot + Spring AI example'
149-
classpath = sourceSets.main.runtimeClasspath
150-
mainClass = 'dev.braintrust.examples.SpringAIExample'
151-
systemProperty 'org.slf4j.simpleLogger.log.dev.braintrust', braintrustLogLevel
152-
153-
debugOptions {
154-
enabled = true
155-
port = 5566
156-
server = true
157-
suspend = false
158-
}
159-
}
160-
161-
task runRemoteEval(type: JavaExec) {
162-
group = 'Braintrust SDK Examples'
163-
description = 'Run the remote eval example'
164-
classpath = sourceSets.main.runtimeClasspath
165-
mainClass = 'dev.braintrust.examples.RemoteEvalExample'
166-
systemProperty 'org.slf4j.simpleLogger.log.dev.braintrust', braintrustLogLevel
167-
debugOptions {
168-
enabled = true
169-
port = 5566
170-
server = true
171-
suspend = false
36+
dependencies {
37+
implementation project(':braintrust-sdk')
38+
runtimeOnly "org.slf4j:slf4j-simple:${slf4jVersion}"
17239
}
173-
}
174-
175-
task runLangchainSimple(type: JavaExec) {
176-
group = 'Braintrust SDK Examples'
177-
description = 'Run the LangChain4j instrumentation example. NOTE: this requires OPENAI_API_KEY to be exported and will make a small call to openai, using your tokens'
178-
classpath = sourceSets.main.runtimeClasspath
179-
mainClass = 'dev.braintrust.examples.LangchainSimpleExample'
180-
systemProperty 'org.slf4j.simpleLogger.log.dev.braintrust', braintrustLogLevel
181-
debugOptions {
182-
enabled = true
183-
port = 5566
184-
server = true
185-
suspend = false
186-
}
187-
}
18840

189-
task runLangchainAIServices(type: JavaExec) {
190-
group = 'Braintrust SDK Examples'
191-
description = 'Run the LangChain4j AI Services example. NOTE: this requires OPENAI_API_KEY to be exported and will make a small call to openai, using your tokens'
192-
classpath = sourceSets.main.runtimeClasspath
193-
mainClass = 'dev.braintrust.examples.LangchainAIServicesExample'
194-
systemProperty 'org.slf4j.simpleLogger.log.dev.braintrust', braintrustLogLevel
195-
debugOptions {
196-
enabled = true
197-
port = 5566
198-
server = true
199-
suspend = false
41+
tasks.named('run') {
42+
group = 'Braintrust SDK Examples'
43+
systemProperty 'org.slf4j.simpleLogger.log.dev.braintrust', braintrustLogLevel
20044
}
20145
}

examples/classifiers/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
application {
2+
mainClass = 'dev.braintrust.examples.ClassifiersExample'
3+
}
4+
5+
run {
6+
description = 'Run the classifiers example'
7+
}

examples/src/main/java/dev/braintrust/examples/ClassifiersExample.java renamed to examples/classifiers/src/main/java/dev/braintrust/examples/ClassifiersExample.java

File renamed without changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
application {
2+
mainClass = 'dev.braintrust.examples.CustomOpenTelemetryExample'
3+
}
4+
5+
dependencies {
6+
implementation "io.opentelemetry:opentelemetry-exporter-otlp:${otelVersion}"
7+
}
8+
9+
run {
10+
description = 'Run the custom OpenTelemetry example'
11+
}

examples/src/main/java/dev/braintrust/examples/CustomOpenTelemetryExample.java renamed to examples/custom-open-telemetry/src/main/java/dev/braintrust/examples/CustomOpenTelemetryExample.java

File renamed without changes.

examples/experiment/build.gradle

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
application {
2+
mainClass = 'dev.braintrust.examples.ExperimentExample'
3+
}
4+
5+
dependencies {
6+
implementation project(':braintrust-sdk:instrumentation:openai_2_15_0')
7+
implementation 'com.openai:openai-java:2.8.1'
8+
}
9+
10+
run {
11+
description = 'Run the experiment example'
12+
debugOptions {
13+
enabled = true
14+
port = 5566
15+
server = true
16+
suspend = false
17+
}
18+
}

0 commit comments

Comments
 (0)