|
24 | 24 | import io.opentelemetry.api.logs.LogRecordBuilder;
|
25 | 25 | import io.opentelemetry.api.logs.Logger;
|
26 | 26 | import io.opentelemetry.context.Context;
|
| 27 | +import java.lang.invoke.MethodHandle; |
| 28 | +import java.lang.invoke.MethodHandles; |
| 29 | +import java.lang.invoke.MethodType; |
27 | 30 | import java.util.HashMap;
|
28 | 31 | import java.util.List;
|
29 | 32 | import java.util.Map;
|
30 | 33 | import java.util.Objects;
|
| 34 | +import java.util.Optional; |
31 | 35 | import java.util.stream.Collectors;
|
| 36 | +import javax.annotation.Nullable; |
32 | 37 |
|
33 | 38 | final class ChatCompletionEventsHelper {
|
34 | 39 |
|
@@ -215,21 +220,232 @@ private static LogRecordBuilder newEvent(Logger eventLogger, String name) {
|
215 | 220 | private static Value<?> buildToolCallEventObject(
|
216 | 221 | ChatCompletionMessageToolCall call, boolean captureMessageContent) {
|
217 | 222 | Map<String, Value<?>> result = new HashMap<>();
|
218 |
| - result.put("id", Value.of(call.id())); |
219 |
| - result.put("type", Value.of("function")); // "function" is the only currently supported type |
220 |
| - result.put("function", buildFunctionEventObject(call.function(), captureMessageContent)); |
| 223 | + FunctionAccess functionAccess = getFunctionAccess(call); |
| 224 | + if (functionAccess != null) { |
| 225 | + result.put("id", Value.of(functionAccess.id())); |
| 226 | + result.put("type", Value.of("function")); // "function" is the only currently supported type |
| 227 | + result.put("function", buildFunctionEventObject(functionAccess, captureMessageContent)); |
| 228 | + } |
221 | 229 | return Value.of(result);
|
222 | 230 | }
|
223 | 231 |
|
224 | 232 | private static Value<?> buildFunctionEventObject(
|
225 |
| - ChatCompletionMessageToolCall.Function function, boolean captureMessageContent) { |
| 233 | + FunctionAccess functionAccess, boolean captureMessageContent) { |
226 | 234 | Map<String, Value<?>> result = new HashMap<>();
|
227 |
| - result.put("name", Value.of(function.name())); |
| 235 | + result.put("name", Value.of(functionAccess.name())); |
228 | 236 | if (captureMessageContent) {
|
229 |
| - result.put("arguments", Value.of(function.arguments())); |
| 237 | + result.put("arguments", Value.of(functionAccess.arguments())); |
230 | 238 | }
|
231 | 239 | return Value.of(result);
|
232 | 240 | }
|
233 | 241 |
|
| 242 | + @Nullable |
| 243 | + private static FunctionAccess getFunctionAccess(ChatCompletionMessageToolCall call) { |
| 244 | + if (V1FunctionAccess.isAvailable()) { |
| 245 | + return V1FunctionAccess.create(call); |
| 246 | + } |
| 247 | + if (V3FunctionAccess.isAvailable()) { |
| 248 | + return V3FunctionAccess.create(call); |
| 249 | + } |
| 250 | + |
| 251 | + return null; |
| 252 | + } |
| 253 | + |
| 254 | + private interface FunctionAccess { |
| 255 | + String id(); |
| 256 | + |
| 257 | + String name(); |
| 258 | + |
| 259 | + String arguments(); |
| 260 | + } |
| 261 | + |
| 262 | + private static String invokeStringHandle(@Nullable MethodHandle methodHandle, Object object) { |
| 263 | + if (methodHandle == null) { |
| 264 | + return ""; |
| 265 | + } |
| 266 | + |
| 267 | + try { |
| 268 | + return (String) methodHandle.invoke(object); |
| 269 | + } catch (Throwable ignore) { |
| 270 | + return ""; |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + private static class V1FunctionAccess implements FunctionAccess { |
| 275 | + @Nullable private static final MethodHandle idHandle; |
| 276 | + @Nullable private static final MethodHandle functionHandle; |
| 277 | + @Nullable private static final MethodHandle nameHandle; |
| 278 | + @Nullable private static final MethodHandle argumentsHandle; |
| 279 | + |
| 280 | + static { |
| 281 | + MethodHandle id; |
| 282 | + MethodHandle function; |
| 283 | + MethodHandle name; |
| 284 | + MethodHandle arguments; |
| 285 | + |
| 286 | + try { |
| 287 | + MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 288 | + id = |
| 289 | + lookup.findVirtual( |
| 290 | + ChatCompletionMessageToolCall.class, "id", MethodType.methodType(String.class)); |
| 291 | + Class<?> functionClass = |
| 292 | + Class.forName( |
| 293 | + "com.openai.models.chat.completions.ChatCompletionMessageToolCall$Function"); |
| 294 | + function = |
| 295 | + lookup.findVirtual( |
| 296 | + ChatCompletionMessageToolCall.class, |
| 297 | + "function", |
| 298 | + MethodType.methodType(functionClass)); |
| 299 | + name = lookup.findVirtual(functionClass, "name", MethodType.methodType(String.class)); |
| 300 | + arguments = |
| 301 | + lookup.findVirtual(functionClass, "arguments", MethodType.methodType(String.class)); |
| 302 | + } catch (Exception exception) { |
| 303 | + id = null; |
| 304 | + function = null; |
| 305 | + name = null; |
| 306 | + arguments = null; |
| 307 | + } |
| 308 | + idHandle = id; |
| 309 | + functionHandle = function; |
| 310 | + nameHandle = name; |
| 311 | + argumentsHandle = arguments; |
| 312 | + } |
| 313 | + |
| 314 | + private final ChatCompletionMessageToolCall toolCall; |
| 315 | + private final Object function; |
| 316 | + |
| 317 | + V1FunctionAccess(ChatCompletionMessageToolCall toolCall, Object function) { |
| 318 | + this.toolCall = toolCall; |
| 319 | + this.function = function; |
| 320 | + } |
| 321 | + |
| 322 | + @Nullable |
| 323 | + static FunctionAccess create(ChatCompletionMessageToolCall toolCall) { |
| 324 | + if (functionHandle == null) { |
| 325 | + return null; |
| 326 | + } |
| 327 | + |
| 328 | + try { |
| 329 | + return new V1FunctionAccess(toolCall, functionHandle.invoke(toolCall)); |
| 330 | + } catch (Throwable ignore) { |
| 331 | + return null; |
| 332 | + } |
| 333 | + } |
| 334 | + |
| 335 | + static boolean isAvailable() { |
| 336 | + return idHandle != null; |
| 337 | + } |
| 338 | + |
| 339 | + @Override |
| 340 | + public String id() { |
| 341 | + return invokeStringHandle(idHandle, toolCall); |
| 342 | + } |
| 343 | + |
| 344 | + @Override |
| 345 | + public String name() { |
| 346 | + return invokeStringHandle(nameHandle, function); |
| 347 | + } |
| 348 | + |
| 349 | + @Override |
| 350 | + public String arguments() { |
| 351 | + return invokeStringHandle(argumentsHandle, function); |
| 352 | + } |
| 353 | + } |
| 354 | + |
| 355 | + static class V3FunctionAccess implements FunctionAccess { |
| 356 | + @Nullable private static final MethodHandle functionToolCallHandle; |
| 357 | + @Nullable private static final MethodHandle idHandle; |
| 358 | + @Nullable private static final MethodHandle functionHandle; |
| 359 | + @Nullable private static final MethodHandle nameHandle; |
| 360 | + @Nullable private static final MethodHandle argumentsHandle; |
| 361 | + |
| 362 | + static { |
| 363 | + MethodHandle functionToolCall; |
| 364 | + MethodHandle id; |
| 365 | + MethodHandle function; |
| 366 | + MethodHandle name; |
| 367 | + MethodHandle arguments; |
| 368 | + |
| 369 | + try { |
| 370 | + MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 371 | + functionToolCall = |
| 372 | + lookup.findVirtual( |
| 373 | + ChatCompletionMessageToolCall.class, |
| 374 | + "function", |
| 375 | + MethodType.methodType(Optional.class)); |
| 376 | + Class<?> functionToolCallClass = |
| 377 | + Class.forName( |
| 378 | + "com.openai.models.chat.completions.ChatCompletionMessageFunctionToolCall"); |
| 379 | + id = lookup.findVirtual(functionToolCallClass, "id", MethodType.methodType(String.class)); |
| 380 | + Class<?> functionClass = |
| 381 | + Class.forName( |
| 382 | + "com.openai.models.chat.completions.ChatCompletionMessageFunctionToolCall$Function"); |
| 383 | + function = |
| 384 | + lookup.findVirtual( |
| 385 | + functionToolCallClass, "function", MethodType.methodType(functionClass)); |
| 386 | + name = lookup.findVirtual(functionClass, "name", MethodType.methodType(String.class)); |
| 387 | + arguments = |
| 388 | + lookup.findVirtual(functionClass, "arguments", MethodType.methodType(String.class)); |
| 389 | + } catch (Exception exception) { |
| 390 | + functionToolCall = null; |
| 391 | + id = null; |
| 392 | + function = null; |
| 393 | + name = null; |
| 394 | + arguments = null; |
| 395 | + } |
| 396 | + functionToolCallHandle = functionToolCall; |
| 397 | + idHandle = id; |
| 398 | + functionHandle = function; |
| 399 | + nameHandle = name; |
| 400 | + argumentsHandle = arguments; |
| 401 | + } |
| 402 | + |
| 403 | + private final Object functionToolCall; |
| 404 | + private final Object function; |
| 405 | + |
| 406 | + V3FunctionAccess(Object functionToolCall, Object function) { |
| 407 | + this.functionToolCall = functionToolCall; |
| 408 | + this.function = function; |
| 409 | + } |
| 410 | + |
| 411 | + @Nullable |
| 412 | + @SuppressWarnings("unchecked") |
| 413 | + static FunctionAccess create(ChatCompletionMessageToolCall toolCall) { |
| 414 | + if (functionToolCallHandle == null || functionHandle == null) { |
| 415 | + return null; |
| 416 | + } |
| 417 | + |
| 418 | + try { |
| 419 | + Optional<Object> optional = (Optional<Object>) functionToolCallHandle.invoke(toolCall); |
| 420 | + if (!optional.isPresent()) { |
| 421 | + return null; |
| 422 | + } |
| 423 | + Object functionToolCall = optional.get(); |
| 424 | + return new V3FunctionAccess(functionToolCall, functionHandle.invoke(functionToolCall)); |
| 425 | + } catch (Throwable ignore) { |
| 426 | + return null; |
| 427 | + } |
| 428 | + } |
| 429 | + |
| 430 | + static boolean isAvailable() { |
| 431 | + return idHandle != null; |
| 432 | + } |
| 433 | + |
| 434 | + @Override |
| 435 | + public String id() { |
| 436 | + return invokeStringHandle(idHandle, functionToolCall); |
| 437 | + } |
| 438 | + |
| 439 | + @Override |
| 440 | + public String name() { |
| 441 | + return invokeStringHandle(nameHandle, function); |
| 442 | + } |
| 443 | + |
| 444 | + @Override |
| 445 | + public String arguments() { |
| 446 | + return invokeStringHandle(argumentsHandle, function); |
| 447 | + } |
| 448 | + } |
| 449 | + |
234 | 450 | private ChatCompletionEventsHelper() {}
|
235 | 451 | }
|
0 commit comments