-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathBetaMessagesToolsRawExample.java
More file actions
122 lines (108 loc) · 5.17 KB
/
BetaMessagesToolsRawExample.java
File metadata and controls
122 lines (108 loc) · 5.17 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
package com.anthropic.example;
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.core.JsonValue;
import com.anthropic.models.beta.messages.*;
import com.anthropic.models.beta.messages.BetaTool.InputSchema;
import java.util.List;
import java.util.Map;
public final class BetaMessagesToolsRawExample {
private BetaMessagesToolsRawExample() {}
enum Unit {
CELSIUS,
FAHRENHEIT;
public String toString() {
switch (this) {
case CELSIUS:
return "°C";
case FAHRENHEIT:
default:
return "°F";
}
}
/** Converts a temperature in Kelvin to a temperature in degrees Fahrenheit or Celsius. */
public double fromKelvin(double temperatureK) {
switch (this) {
case CELSIUS:
return temperatureK - 273.15;
case FAHRENHEIT:
default:
return (temperatureK - 273.15) * 1.8 + 32.0;
}
}
}
public static void main(String[] args) {
// Configure using the `ANTHROPIC_API_KEY` environment variable
AnthropicClient client = AnthropicOkHttpClient.fromEnv();
InputSchema schema = InputSchema.builder()
.properties(JsonValue.from(Map.of(
"location",
Map.of(
"type", "string",
"description", "The city and state, e.g. San Francisco, CA"),
"unit",
Map.of(
"type", "string",
"description", "The unit of temperature",
"enum", List.of(Unit.CELSIUS.name(), Unit.FAHRENHEIT.name())))))
.putAdditionalProperty("required", JsonValue.from(List.of("location", "unit")))
.putAdditionalProperty("additionalProperties", JsonValue.from(false))
.build();
MessageCreateParams.Builder createParamsBuilder = MessageCreateParams.builder()
.model("claude-sonnet-4-5")
.putAdditionalHeader("anthropic-beta", "structured-outputs-2025-11-13")
.maxTokens(2048)
.addTool(BetaTool.builder()
.name("get_weather")
.description("Gets the weather in a given location")
.inputSchema(schema)
// `strict` mode ensures that the output will conform to the schema.
.strict(true)
.build())
.addUserMessage("What's the temperature in Dallas in Celsius?");
client.beta().messages().create(createParamsBuilder.build()).content().stream()
.flatMap(contentBlock -> contentBlock.toolUse().stream())
.forEach(toolUseBlock -> createParamsBuilder
// Add a message indicating that the tool use was requested.
.addAssistantMessageOfBetaContentBlockParams(
List.of(BetaContentBlockParam.ofToolUse(BetaToolUseBlockParam.builder()
.name(toolUseBlock.name())
.id(toolUseBlock.id())
.input(toolUseBlock._input())
.build())))
// Add a message with the result of the requested tool use.
.addUserMessageOfBetaContentBlockParams(
List.of(BetaContentBlockParam.ofToolResult(BetaToolResultBlockParam.builder()
.toolUseId(toolUseBlock.id())
.content(callTool(toolUseBlock))
.build()))));
client.beta().messages().create(createParamsBuilder.build()).content().stream()
.flatMap(contentBlock -> contentBlock.text().stream())
.forEach(textBlock -> System.out.println(textBlock.text()));
}
private static String callTool(BetaToolUseBlock toolUseBlock) {
if (!"get_weather".equals(toolUseBlock.name())) {
throw new IllegalArgumentException("Unknown tool: " + toolUseBlock.name());
}
Map<String, JsonValue> tool =
(Map<String, JsonValue>) toolUseBlock._input().asObject().get();
String location = tool.get("location").asStringOrThrow();
Unit unit = Unit.valueOf(tool.get("unit").asStringOrThrow());
double temperatureK;
switch (location) {
case "San Francisco, CA":
temperatureK = 300.0;
break;
case "New York, NY":
temperatureK = 310.0;
break;
case "Dallas, TX":
temperatureK = 305.0;
break;
default:
temperatureK = 295;
break;
}
return String.format("{\"temperature\":\"%.0f%s\"}", unit.fromKelvin(temperatureK), unit);
}
}