forked from langchain-ai/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraceablePipeline.java
More file actions
82 lines (73 loc) · 3.29 KB
/
Copy pathTraceablePipeline.java
File metadata and controls
82 lines (73 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS com.langchain.smith:langsmith-java:0.1.0-alpha.25
//DEPS com.openai:openai-java:4.61.0
// :snippet-start: traceable-pipeline-java
// :codegroup-tab: Java
import com.langchain.smith.tracing.RunType;
import com.langchain.smith.tracing.TraceConfig;
import com.langchain.smith.tracing.Tracing;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.ChatModel;
import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
import com.openai.models.chat.completions.ChatCompletionMessageParam;
import com.openai.models.chat.completions.ChatCompletionSystemMessageParam;
import com.openai.models.chat.completions.ChatCompletionUserMessageParam;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
public class TraceablePipeline {
public static void main(String[] args) {
// :remove-start:
if (System.getenv("LANGSMITH_API_KEY") == null
|| System.getenv("LANGSMITH_API_KEY").isBlank()
|| System.getenv("OPENAI_API_KEY") == null
|| System.getenv("OPENAI_API_KEY").isBlank()) {
System.out.println(
"[traceable-pipeline] Skipping (LANGSMITH_API_KEY and OPENAI_API_KEY required).");
return;
}
// :remove-end:
new TraceablePipelineRunner().run();
}
private static final class TraceablePipelineRunner {
private final OpenAIClient openai = OpenAIOkHttpClient.fromEnv();
private final Function<String, List<ChatCompletionMessageParam>> formatPrompt =
Tracing.traceFunction(
subject ->
Arrays.asList(
ChatCompletionMessageParam.ofSystem(
ChatCompletionSystemMessageParam.builder()
.content("You are a helpful assistant.")
.build()),
ChatCompletionMessageParam.ofUser(
ChatCompletionUserMessageParam.builder()
.content("What's a good name for a store that sells " + subject + "?")
.build())),
TraceConfig.builder().name("format_prompt").build());
private final Function<List<ChatCompletionMessageParam>, ChatCompletion> invokeLlm =
Tracing.traceFunction(
messages ->
openai.chat()
.completions()
.create(
ChatCompletionCreateParams.builder()
.model(ChatModel.GPT_5_5)
.messages(messages)
.build()),
TraceConfig.builder().name("invoke_llm").runType(RunType.LLM).build());
private final Function<ChatCompletion, String> parseOutput =
Tracing.traceFunction(
response -> response.choices().get(0).message().content().orElse(""),
TraceConfig.builder().name("parse_output").build());
private final Function<String, String> runPipeline =
Tracing.traceFunction(
subject -> parseOutput.apply(invokeLlm.apply(formatPrompt.apply(subject))),
TraceConfig.builder().name("run_pipeline").build());
void run() {
runPipeline.apply("colorful socks");
}
}
}
// :snippet-end: