Skip to content

Commit c809ebe

Browse files
Adds OpenAI example similar to Python and Node.js
Signed-off-by: Adrian Cole <[email protected]> Co-authored-by: Jonas Kunz <[email protected]>
1 parent 8009ef6 commit c809ebe

File tree

11 files changed

+581
-0
lines changed

11 files changed

+581
-0
lines changed

examples/openai/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Ignore elastic-otel-javaagent JAR files
2+
elastic-otel-javaagent*.jar
3+
shdotenv
4+
.env

examples/openai/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# OpenAI Zero-Code Instrumentation Examples
2+
3+
This is an example of how to instrument OpenAI calls with zero code changes,
4+
using `instrumentation-openai-java` included in the Elastic Distribution of
5+
OpenTelemetry Java ([EDOT Java][edot-java]).
6+
7+
When OpenAI examples run, they export traces, metrics and logs to an OTLP
8+
compatible endpoint. Traces and metrics include details such as the model used
9+
and the duration of the LLM request. In the case of chat, Logs capture the
10+
request and the generated response. The combination of these provide a
11+
comprehensive view of the performance and behavior of your OpenAI usage.
12+
13+
## Install
14+
15+
Download the EDOT javaagent binary.
16+
17+
```bash
18+
curl -o elastic-otel-javaagent.jar -L 'https://oss.sonatype.org/service/local/artifact/maven/redirect?r=releases&g=co.elastic.otel&a=elastic-otel-javaagent&v=LATEST'
19+
```
20+
21+
Build the example application.
22+
23+
```bash
24+
./gradlew clean assemble
25+
```
26+
27+
Download shdotenv to load `.env` file when running.
28+
29+
```bash
30+
curl -O -L https://github.com/ko1nksm/shdotenv/releases/download/v0.14.0/shdotenv
31+
chmod +x ./shdotenv
32+
```
33+
34+
## Configure
35+
36+
Copy [env.example](env.example) to `.env` and update its `OPENAI_API_KEY`.
37+
38+
An OTLP compatible endpoint should be listening for traces, metrics and logs on
39+
`http://localhost:4317`. If not, update `OTEL_EXPORTER_OTLP_ENDPOINT` as well.
40+
41+
For example, if Elastic APM server is running locally, edit `.env` like this:
42+
```
43+
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:8200
44+
```
45+
46+
## Run
47+
48+
There are two examples, and they run the same way:
49+
50+
### Chat
51+
52+
[Chat](src/main/java/openai/example/Chat.java) asks the LLM a geography question and prints the response.
53+
54+
Run it like this:
55+
```bash
56+
./shdotenv java -javaagent:elastic-otel-javaagent.jar -jar build/libs/openai-example-all.jar
57+
```
58+
59+
You should see something like "Atlantic Ocean" unless your LLM hallucinates!
60+
61+
### Embeddings
62+
63+
64+
[Embeddings](src/main/java/openai/example/Embeddings.java) creates in-memory VectorDB embeddings about
65+
Elastic products. Then, it searches for one similar to a question.
66+
67+
Run it like this:
68+
```bash
69+
./shdotenv java -classpath build/libs/openai-example-all.jar -javaagent:elastic-otel-javaagent.jar openai.example.Embeddings
70+
```
71+
72+
You should see something like "Connectors can help you connect to a database",
73+
unless your LLM hallucinates!
74+
75+
---
76+
77+
[edot-java]: https://github.com/elastic/elastic-otel-java/

examples/openai/build.gradle.kts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
plugins {
2+
application
3+
java
4+
5+
id("com.gradleup.shadow") version "8.3.5"
6+
}
7+
8+
group = "openai.example"
9+
version = "1.0-SNAPSHOT"
10+
11+
repositories {
12+
mavenCentral()
13+
}
14+
15+
dependencies {
16+
implementation(platform("org.slf4j:slf4j-bom:2.0.16"))
17+
implementation("com.openai:openai-java:0.11.8")
18+
implementation("org.slf4j:slf4j-simple")
19+
}
20+
21+
application {
22+
mainClass = "openai.example.Chat"
23+
}
24+
25+
tasks {
26+
compileJava {
27+
options.release.set(21)
28+
}
29+
30+
shadowJar {
31+
archiveFileName.set("openai-example-all.jar")
32+
}
33+
34+
assemble {
35+
dependsOn("shadowJar")
36+
}
37+
}

examples/openai/env.example

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Update this with your real OpenAI API key
2+
OPENAI_API_KEY=sk-YOUR_API_KEY
3+
4+
# OTEL_EXPORTER_* variables are not required. If you would like to change your
5+
# OTLP endpoint to Elastic APM server using HTTP, uncomment the following:
6+
# OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:8200
7+
# OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
8+
9+
OTEL_SERVICE_NAME=openai-example
10+
11+
# Change to 'false' to hide prompt and completion content
12+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true
13+
# Change to 'false' to not export prompt and completion content events
14+
ELASTIC_OTEL_JAVA_INSTRUMENTATION_GENAI_EMIT_EVENTS=true
42.4 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

examples/openai/gradlew

Lines changed: 249 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)