forked from YuvDwi/Steve
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFunctionCallingExample.java
More file actions
292 lines (249 loc) · 10.3 KB
/
FunctionCallingExample.java
File metadata and controls
292 lines (249 loc) · 10.3 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package com.minewright.llm;
import com.google.gson.*;
import java.net.URI;
import java.net.http.*;
import java.time.Duration;
import java.util.*;
/**
* Simplified Function Calling Example for Dissertation Appendix.
*
* This demonstrates the core concepts of function calling with minimal complexity,
* suitable for educational purposes and code examples in academic writing.
*/
public class FunctionCallingExample {
public static class MinecraftTool {
private final String name;
private final String description;
private final String jsonSchema;
public MinecraftTool(String name, String description, String jsonSchema) {
this.name = name;
this.description = description;
this.jsonSchema = jsonSchema;
}
public JsonObject toOpenAIFunction() {
JsonObject function = new JsonObject();
function.addProperty("name", name);
function.addProperty("description", description);
JsonObject parameters = JsonParser.parseString(jsonSchema).getAsJsonObject();
function.add("parameters", parameters);
return function;
}
}
/**
* Example: Defining Minecraft actions as function calling tools.
*/
public static List<MinecraftTool> getMinecraftTools() {
List<MinecraftTool> tools = new ArrayList<>();
// Mine Block Tool
tools.add(new MinecraftTool(
"mine_block",
"Mine a specific block type in the Minecraft world",
"""
{
"type": "object",
"properties": {
"block_type": {
"type": "string",
"enum": ["iron_ore", "coal_ore", "diamond_ore", "copper_ore"],
"description": "Type of block to mine"
},
"quantity": {
"type": "integer",
"minimum": 1,
"maximum": 64,
"description": "Number of blocks to mine"
}
},
"required": ["block_type", "quantity"]
}
"""
));
// Place Block Tool
tools.add(new MinecraftTool(
"place_block",
"Place a block at specified world coordinates",
"""
{
"type": "object",
"properties": {
"block_type": {
"type": "string",
"enum": ["oak_planks", "stone_bricks", "cobblestone", "glass"],
"description": "Minecraft block ID"
},
"position": {
"type": "object",
"properties": {
"x": {"type": "integer"},
"y": {"type": "integer"},
"z": {"type": "integer"}
},
"required": ["x", "y", "z"]
}
},
"required": ["block_type", "position"]
}
"""
));
// Pathfind Tool
tools.add(new MinecraftTool(
"pathfind",
"Navigate to specified world coordinates",
"""
{
"type": "object",
"properties": {
"target": {
"type": "object",
"properties": {
"x": {"type": "integer"},
"y": {"type": "integer"},
"z": {"type": "integer"}
},
"required": ["x", "y", "z"]
}
},
"required": ["target"]
}
"""
));
return tools;
}
/**
* Example: Sending a function calling request to OpenAI.
*/
public static String sendFunctionCallRequest(String apiKey, String userMessage) throws Exception {
HttpClient client = HttpClient.newHttpClient();
// Build the request with tool definitions
JsonObject requestBody = new JsonObject();
requestBody.addProperty("model", "gpt-4");
requestBody.addProperty("temperature", 0.7);
// Add messages
JsonArray messages = new JsonArray();
JsonObject systemMessage = new JsonObject();
systemMessage.addProperty("role", "system");
systemMessage.addProperty("content",
"You are a Minecraft AI assistant. Break down player commands into specific actions " +
"using the available function calls.");
messages.add(systemMessage);
JsonObject userMsg = new JsonObject();
userMsg.addProperty("role", "user");
userMsg.addProperty("content", userMessage);
messages.add(userMsg);
requestBody.add("messages", messages);
// Add tool definitions
JsonArray tools = new JsonArray();
for (MinecraftTool tool : getMinecraftTools()) {
JsonObject toolWrapper = new JsonObject();
toolWrapper.addProperty("type", "function");
toolWrapper.add("function", tool.toOpenAIFunction());
tools.add(toolWrapper);
}
requestBody.add("tools", tools);
requestBody.addProperty("tool_choice", "auto");
// Send request
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.openai.com/v1/chat/completions"))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.timeout(Duration.ofSeconds(60))
.POST(HttpRequest.BodyPublishers.ofString(requestBody.toString()))
.build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
return response.body();
}
/**
* Example: Parsing function calls from OpenAI response.
*/
public static List<GameAction> parseFunctionCalls(String responseJson) {
List<GameAction> actions = new ArrayList<>();
JsonObject response = JsonParser.parseString(responseJson).getAsJsonObject();
JsonArray choices = response.getAsJsonArray("choices");
if (choices != null && choices.size() > 0) {
JsonObject message = choices.get(0).getAsJsonObject()
.getAsJsonObject("message");
JsonArray toolCalls = message.getAsJsonArray("tool_calls");
if (toolCalls != null) {
for (JsonElement toolCall : toolCalls) {
JsonObject tc = toolCall.getAsJsonObject();
JsonObject function = tc.getAsJsonObject("function");
String functionName = function.get("name").getAsString();
String argumentsStr = function.get("arguments").getAsString();
JsonObject arguments = JsonParser.parseString(argumentsStr).getAsJsonObject();
actions.add(new GameAction(functionName, arguments));
}
}
}
return actions;
}
/**
* Example: Executing parsed function calls as game actions.
*/
public static void executeGameActions(List<GameAction> actions) {
for (GameAction action : actions) {
System.out.println("Executing action: " + action.name);
switch (action.name) {
case "mine_block" -> {
String blockType = action.arguments.get("block_type").getAsString();
int quantity = action.arguments.get("quantity").getAsInt();
System.out.println(" → Mining " + quantity + "x " + blockType);
// Execute mining logic here
}
case "place_block" -> {
String blockType = action.arguments.get("block_type").getAsString();
JsonObject position = action.arguments.getAsJsonObject("position");
int x = position.get("x").getAsInt();
int y = position.get("y").getAsInt();
int z = position.get("z").getAsInt();
System.out.println(" → Placing " + blockType + " at (" + x + ", " + y + ", " + z + ")");
// Execute placement logic here
}
case "pathfind" -> {
JsonObject target = action.arguments.getAsJsonObject("target");
int x = target.get("x").getAsInt();
int y = target.get("y").getAsInt();
int z = target.get("z").getAsInt();
System.out.println(" → Pathfinding to (" + x + ", " + y + ", " + z + ")");
// Execute pathfinding logic here
}
default -> System.out.println(" → Unknown action: " + action.name);
}
}
}
/**
* Simple data structure for parsed game actions.
*/
public static class GameAction {
public final String name;
public final JsonObject arguments;
public GameAction(String name, JsonObject arguments) {
this.name = name;
this.arguments = arguments;
}
}
/**
* Complete usage example.
*/
public static void main(String[] args) {
try {
// Example: Process a natural language command
String playerCommand = "Mine 10 iron ore blocks";
System.out.println("Player command: " + playerCommand);
System.out.println("Sending to OpenAI with function calling...\n");
// In real usage, use your actual API key
String apiKey = "your-api-key-here";
String response = sendFunctionCallRequest(apiKey, playerCommand);
System.out.println("LLM Response:");
System.out.println(response);
System.out.println();
// Parse function calls
List<GameAction> actions = parseFunctionCalls(response);
// Execute actions
System.out.println("Executing " + actions.size() + " actions:\n");
executeGameActions(actions);
} catch (Exception e) {
e.printStackTrace();
}
}
}