forked from langchain-ai/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreads-chat-pipeline.java
More file actions
179 lines (156 loc) · 6.6 KB
/
Copy paththreads-chat-pipeline.java
File metadata and controls
179 lines (156 loc) · 6.6 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
///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: threads-chat-pipeline-java
// :codegroup-tab: Java
// :codegroup-fence-mods: expandable wrap
import com.langchain.smith.client.LangsmithClient;
import com.langchain.smith.client.okhttp.LangsmithOkHttpClient;
import com.langchain.smith.tracing.TraceConfig;
import com.langchain.smith.tracing.Tracing;
import com.langchain.smith.wrappers.openai.OpenAITracing;
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.ChatCompletionAssistantMessageParam;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
import com.openai.models.chat.completions.ChatCompletionMessageParam;
import com.openai.models.chat.completions.ChatCompletionUserMessageParam;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
class ThreadsChatPipeline {
private static final String THREAD_ID = "01990f3e-7f97-74c5-a9b6-8d3f7e8e2f11";
private static final class OpenAiResources {
private static final LangsmithClient langsmith = LangsmithOkHttpClient.fromEnv();
private static final ExecutorService executor = Executors.newSingleThreadExecutor();
private static final Map<String, Object> threadMetadata = new HashMap<>();
static {
threadMetadata.put("thread_id", THREAD_ID);
}
private static final OpenAIClient openai =
OpenAITracing.wrapOpenAI(
OpenAIOkHttpClient.fromEnv(),
TraceConfig.builder()
.client(langsmith)
.executor(executor)
.metadata(threadMetadata)
.build());
private static final List<ChatCompletionMessageParam> threadHistory = new ArrayList<>();
static final Function<ChatRequest, Map<String, List<ChatCompletionMessageParam>>> CHAT_PIPELINE =
Tracing.traceFunction(
request -> {
List<ChatCompletionMessageParam> allMessages = new ArrayList<>();
if (request.getChatHistory()) {
allMessages.addAll(threadHistory);
}
allMessages.addAll(request.getMessages());
ChatCompletion chatCompletion =
openai
.chat()
.completions()
.create(
ChatCompletionCreateParams.builder()
.model(ChatModel.GPT_5_5)
.messages(allMessages)
.build());
String content = chatCompletion.choices().get(0).message().content().orElse("");
List<ChatCompletionMessageParam> fullConversation = new ArrayList<>(allMessages);
fullConversation.add(
ChatCompletionMessageParam.ofAssistant(
ChatCompletionAssistantMessageParam.builder().content(content).build()));
threadHistory.clear();
threadHistory.addAll(fullConversation);
return Collections.singletonMap("messages", fullConversation);
},
TraceConfig.builder()
.name("Chat Bot")
.client(langsmith)
.executor(executor)
.metadata(threadMetadata)
.build());
private OpenAiResources() {}
static ExecutorService executor() {
return executor;
}
}
static Function<ChatRequest, Map<String, List<ChatCompletionMessageParam>>> chatPipeline() {
return OpenAiResources.CHAT_PIPELINE;
}
public static void main(String[] args) throws InterruptedException {
// :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(
"[threads-chat-pipeline] Skipping (LANGSMITH_API_KEY and OPENAI_API_KEY required).");
return;
}
// :remove-end:
try {
List<ChatCompletionMessageParam> messages =
Collections.singletonList(
ChatCompletionMessageParam.ofUser(
ChatCompletionUserMessageParam.builder()
.content("Hi, my name is Sally")
.build()));
chatPipeline().apply(new ChatRequest(messages, false));
} finally {
OpenAiResources.executor().shutdown();
if (!OpenAiResources.executor().awaitTermination(10, TimeUnit.SECONDS)) {
throw new IllegalStateException("Timed out waiting for LangSmith traces to submit");
}
}
}
static class ChatRequest {
private final List<ChatCompletionMessageParam> messages;
private final boolean getChatHistory;
ChatRequest(List<ChatCompletionMessageParam> messages, boolean getChatHistory) {
this.messages = messages;
this.getChatHistory = getChatHistory;
}
List<ChatCompletionMessageParam> getMessages() {
return messages;
}
boolean getChatHistory() {
return getChatHistory;
}
}
}
// :snippet-end:
/** Compile-only helpers so continuation fragments stay valid Java and extract as snippets. */
final class ThreadsContinuationHarness {
private ThreadsContinuationHarness() {}
static void continueWhatIsMyName() {
// :snippet-start: threads-continue-name-java
// :codegroup-tab: Java
List<ChatCompletionMessageParam> messages =
Collections.singletonList(
ChatCompletionMessageParam.ofUser(
ChatCompletionUserMessageParam.builder()
.content("What is my name")
.build()));
ThreadsChatPipeline.chatPipeline().apply(new ThreadsChatPipeline.ChatRequest(messages, true));
// :snippet-end:
}
static void continueFirstMessage() {
// :snippet-start: threads-continue-first-message-java
// :codegroup-tab: Java
List<ChatCompletionMessageParam> messages =
Collections.singletonList(
ChatCompletionMessageParam.ofUser(
ChatCompletionUserMessageParam.builder()
.content("What was the first message I sent you?")
.build()));
ThreadsChatPipeline.chatPipeline().apply(new ThreadsChatPipeline.ChatRequest(messages, true));
// :snippet-end:
}
}