Skip to content

Commit ca6471f

Browse files
committed
docs: update Code of Conduct and enhance README examples for chat model invocation
1 parent 8d18b6e commit ca6471f

File tree

3 files changed

+531
-3
lines changed

3 files changed

+531
-3
lines changed

CODE_OF_CONDUCT.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ dispute. If you are unable to resolve the matter for any reason, or if the
6969
behavior is threatening or harassing, report it. We are dedicated to providing
7070
an environment where participants feel welcome and safe.
7171

72-
Reports should be directed to *[PROJECT STEWARD NAME(s) AND EMAIL(s)]*, the
73-
Project Steward(s) for *[PROJECT NAME]*. It is the Project Steward’s duty to
72+
Reports should be directed to *Chao Gong ([email protected])*, the
73+
Project Steward(s) for *zhipuai-sdk-java-v4*. It is the Project Steward’s duty to
7474
receive and address reported violations of the code of conduct. They will then
7575
work with a committee consisting of representatives from the Open Source
7676
Programs Office and the Z.ai Open Source Strategy team.

README.md

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,270 @@ private static final ClientV4 client = new ClientV4.Builder(API_SECRET_KEY)
9191

9292
## 💡 Examples
9393

94+
### Chat Model Invocation
95+
96+
#### Streaming Invocation (SSE)
97+
98+
- **Basic Chat**
99+
100+
```java
101+
List<ChatMessage> messages = new ArrayList<>();
102+
ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), "What is the relationship between ZhipuAI and ChatGLM?");
103+
messages.add(chatMessage);
104+
String requestId = String.format("your-request-id-%d", System.currentTimeMillis());
105+
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
106+
.model(Constants.ModelChatGLM4)
107+
.stream(Boolean.TRUE)
108+
.messages(messages)
109+
.requestId(requestId)
110+
.build();
111+
ModelApiResponse sseModelApiResp = client.invokeModelApi(chatCompletionRequest);
112+
if (sseModelApiResp.isSuccess()) {
113+
AtomicBoolean isFirst = new AtomicBoolean(true);
114+
ChatMessageAccumulator chatMessageAccumulator = mapStreamToAccumulator(sseModelApiResp.getFlowable())
115+
.doOnNext(accumulator -> {
116+
// Process streaming results
117+
System.out.println("accumulator: " + accumulator);
118+
})
119+
.doOnComplete(System.out::println)
120+
.lastElement()
121+
.blockingGet();
122+
}
123+
```
124+
125+
- **Function-Calling**
126+
127+
```java
128+
List<ChatMessage> messages = new ArrayList<>();
129+
ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), "How much is a flight ticket from Chengdu to Beijing?");
130+
messages.add(chatMessage);
131+
String requestId = String.format("your-request-id-%d", System.currentTimeMillis());
132+
// Function definition
133+
List<ChatTool> chatToolList = new ArrayList<>();
134+
ChatTool chatTool = new ChatTool();
135+
chatTool.setType(ChatToolType.FUNCTION.value());
136+
ChatFunctionParameters chatFunctionParameters = new ChatFunctionParameters();
137+
chatFunctionParameters.setType("object");
138+
Map<String, Object> properties = new HashMap<>();
139+
properties.put("departure", new HashMap<String, Object>() {{
140+
put("type", "string");
141+
put("description", "Departure city");
142+
}});
143+
properties.put("destination", new HashMap<String, Object>() {{
144+
put("type", "string");
145+
put("description", "Destination city");
146+
}});
147+
chatFunctionParameters.setProperties(properties);
148+
ChatFunction chatFunction = ChatFunction.builder()
149+
.name("query_flight_prices")
150+
.description("Query flight prices")
151+
.parameters(chatFunctionParameters)
152+
.build();
153+
chatTool.setFunction(chatFunction);
154+
chatToolList.add(chatTool);
155+
156+
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
157+
.model(Constants.ModelChatGLM4)
158+
.stream(Boolean.TRUE)
159+
.messages(messages)
160+
.requestId(requestId)
161+
.tools(chatToolList)
162+
.toolChoice("auto")
163+
.build();
164+
ModelApiResponse sseModelApiResp = client.invokeModelApi(chatCompletionRequest);
165+
// Process the returned results
166+
```
167+
168+
#### Synchronous Invocation
169+
170+
- **Basic Chat**
171+
172+
```java
173+
List<ChatMessage> messages = new ArrayList<>();
174+
ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), "What is the relationship between ZhipuAI and ChatGLM?");
175+
messages.add(chatMessage);
176+
String requestId = String.format("your-request-id-%d", System.currentTimeMillis());
177+
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
178+
.model(Constants.ModelChatGLM4)
179+
.stream(Boolean.FALSE)
180+
.invokeMethod(Constants.invokeMethod)
181+
.messages(messages)
182+
.requestId(requestId)
183+
.build();
184+
ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);
185+
System.out.println("model output:" + new ObjectMapper().writeValueAsString(invokeModelApiResp));
186+
```
187+
188+
- **Function-Calling**
189+
190+
```java
191+
List<ChatMessage> messages = new ArrayList<>();
192+
ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), "What can you do?");
193+
messages.add(chatMessage);
194+
String requestId = String.format("your-request-id-%d", System.currentTimeMillis());
195+
// Function definition... (refer to streaming Function-Calling)
196+
List<ChatTool> chatToolList = new ArrayList<>();
197+
// ... Add Function and WebSearch tools
198+
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
199+
.model(Constants.ModelChatGLM4)
200+
.stream(Boolean.FALSE)
201+
.invokeMethod(Constants.invokeMethod)
202+
.messages(messages)
203+
.requestId(requestId)
204+
.tools(chatToolList)
205+
.toolChoice("auto")
206+
.build();
207+
ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);
208+
```
209+
210+
#### Asynchronous Invocation
211+
212+
```java
213+
// 1. Initiate an asynchronous task
214+
List<ChatMessage> messages = new ArrayList<>();
215+
ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), "What is the relationship between ZhipuAI and ChatGLM?");
216+
messages.add(chatMessage);
217+
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
218+
.model(Constants.ModelChatGLM4)
219+
.stream(Boolean.FALSE)
220+
.invokeMethod(Constants.invokeMethodAsync)
221+
.messages(messages)
222+
.build();
223+
ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);
224+
String taskId = invokeModelApiResp.getData().getTaskId();
225+
226+
// 2. Query the result by taskId
227+
QueryModelResultRequest request = new QueryModelResultRequest();
228+
request.setTaskId(taskId);
229+
QueryModelResultResponse queryResultResp = client.queryModelResult(request);
230+
```
231+
232+
### Role Playing
233+
234+
```java
235+
List<ChatMessage> messages = new ArrayList<>();
236+
ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), "How have you been lately?");
237+
messages.add(chatMessage);
238+
239+
ChatMeta meta = new ChatMeta();
240+
meta.setUser_info("I am a film director, specializing in music-themed movies.");
241+
meta.setBot_info("You are a popular female singer and actress in the country, with outstanding musical talent.");
242+
meta.setBot_name("Su Mengyuan");
243+
meta.setUser_name("Lu Xingchen");
244+
245+
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
246+
.model(Constants.ModelCharGLM3)
247+
.stream(Boolean.FALSE)
248+
.invokeMethod(Constants.invokeMethod)
249+
.messages(messages)
250+
.meta(meta)
251+
.build();
252+
ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);
253+
```
254+
255+
### Image Generation
256+
257+
```java
258+
CreateImageRequest createImageRequest = new CreateImageRequest();
259+
createImageRequest.setModel(Constants.ModelCogView);
260+
createImageRequest.setPrompt("A futuristic cloud data center");
261+
ImageApiResponse imageApiResponse = client.createImage(createImageRequest);
262+
```
263+
264+
### Vector Models
265+
266+
```java
267+
EmbeddingRequest embeddingRequest = new EmbeddingRequest();
268+
embeddingRequest.setInput("hello world");
269+
embeddingRequest.setModel(Constants.ModelEmbedding2);
270+
EmbeddingApiResponse apiResponse = client.invokeEmbeddingsApi(embeddingRequest);
271+
```
272+
273+
### Fine-tuning
274+
275+
#### Create Fine-tuning Job
276+
277+
```java
278+
FineTuningJobRequest request = new FineTuningJobRequest();
279+
request.setModel("chatglm3-6b");
280+
request.setTraining_file("your-file-id");
281+
CreateFineTuningJobApiResponse createFineTuningJobApiResponse = client.createFineTuningJob(request);
282+
```
283+
284+
#### Retrieve Fine-tuning Job
285+
286+
```java
287+
QueryFineTuningJobRequest queryFineTuningJobRequest = new QueryFineTuningJobRequest();
288+
queryFineTuningJobRequest.setJobId("your-job-id");
289+
QueryFineTuningJobApiResponse queryFineTuningJobApiResponse = client.retrieveFineTuningJobs(queryFineTuningJobRequest);
290+
```
291+
292+
#### List Fine-tuning Jobs
293+
294+
```java
295+
QueryPersonalFineTuningJobRequest queryPersonalFineTuningJobRequest = new QueryPersonalFineTuningJobRequest();
296+
queryPersonalFineTuningJobRequest.setLimit(10);
297+
QueryPersonalFineTuningJobApiResponse queryPersonalFineTuningJobApiResponse = client.queryPersonalFineTuningJobs(queryPersonalFineTuningJobRequest);
298+
```
299+
300+
#### List Fine-tuning Events
301+
302+
```java
303+
QueryFineTuningJobRequest queryFineTuningJobRequest = new QueryFineTuningJobRequest();
304+
queryFineTuningJobRequest.setJobId("your-job-id");
305+
QueryFineTuningEventApiResponse queryFineTuningEventApiResponse = client.queryFineTuningJobsEvents(queryFineTuningJobRequest);
306+
```
307+
308+
#### Cancel Fine-tuning Job
309+
310+
```java
311+
FineTuningJobIdRequest request = FineTuningJobIdRequest.builder().jobId("your-job-id").build();
312+
QueryFineTuningJobApiResponse queryFineTuningJobApiResponse = client.cancelFineTuningJob(request);
313+
```
314+
315+
#### Delete Fine-tuned Model
316+
317+
```java
318+
FineTuningJobModelRequest request = FineTuningJobModelRequest.builder().fineTunedModel("your-fine-tuned-model").build();
319+
FineTunedModelsStatusResponse fineTunedModelsStatusResponse = client.deleteFineTuningModel(request);
320+
```
321+
322+
### Batch Processing
323+
324+
#### Create Batch Job
325+
326+
```java
327+
BatchCreateParams batchCreateParams = new BatchCreateParams(
328+
"24h",
329+
"/v4/chat/completions",
330+
"your-file-id",
331+
new HashMap<String, String>() {{
332+
put("model", "glm-4");
333+
}}
334+
);
335+
BatchResponse batchResponse = client.batchesCreate(batchCreateParams);
336+
```
337+
338+
#### Retrieve Batch Job
339+
340+
```java
341+
BatchResponse batchResponse = client.batchesRetrieve("your-batch-id");
342+
```
343+
344+
#### List Batch Jobs
345+
346+
```java
347+
QueryBatchRequest queryBatchRequest = new QueryBatchRequest();
348+
queryBatchRequest.setLimit(10);
349+
QueryBatchResponse queryBatchResponse = client.batchesList(queryBatchRequest);
350+
```
351+
352+
#### Cancel Batch Job
353+
354+
```java
355+
BatchResponse batchResponse = client.batchesCancel("your-batch-id");
356+
```
357+
94358
### Spring Boot Integration
95359

96360
```java

0 commit comments

Comments
 (0)