|
| 1 | +// Run an LLM inference returns "OK" response |
| 2 | + |
| 3 | +import com.datadog.api.client.ApiClient; |
| 4 | +import com.datadog.api.client.ApiException; |
| 5 | +import com.datadog.api.client.v2.api.LlmObservabilityApi; |
| 6 | +import com.datadog.api.client.v2.model.LLMObsAnthropicEffort; |
| 7 | +import com.datadog.api.client.v2.model.LLMObsAnthropicMetadata; |
| 8 | +import com.datadog.api.client.v2.model.LLMObsAnthropicThinkingConfig; |
| 9 | +import com.datadog.api.client.v2.model.LLMObsAnthropicThinkingType; |
| 10 | +import com.datadog.api.client.v2.model.LLMObsAzureOpenAIMetadata; |
| 11 | +import com.datadog.api.client.v2.model.LLMObsBedrockMetadata; |
| 12 | +import com.datadog.api.client.v2.model.LLMObsInferenceContent; |
| 13 | +import com.datadog.api.client.v2.model.LLMObsInferenceContentValue; |
| 14 | +import com.datadog.api.client.v2.model.LLMObsInferenceFunction; |
| 15 | +import com.datadog.api.client.v2.model.LLMObsInferenceMessage; |
| 16 | +import com.datadog.api.client.v2.model.LLMObsInferenceTool; |
| 17 | +import com.datadog.api.client.v2.model.LLMObsInferenceToolCall; |
| 18 | +import com.datadog.api.client.v2.model.LLMObsInferenceToolResult; |
| 19 | +import com.datadog.api.client.v2.model.LLMObsIntegrationInferenceRequest; |
| 20 | +import com.datadog.api.client.v2.model.LLMObsIntegrationInferenceResponse; |
| 21 | +import com.datadog.api.client.v2.model.LLMObsIntegrationName; |
| 22 | +import com.datadog.api.client.v2.model.LLMObsOpenAIMetadata; |
| 23 | +import com.datadog.api.client.v2.model.LLMObsOpenAIReasoningEffort; |
| 24 | +import com.datadog.api.client.v2.model.LLMObsOpenAIReasoningSummary; |
| 25 | +import com.datadog.api.client.v2.model.LLMObsVertexAIMetadata; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +public class Example { |
| 30 | + public static void main(String[] args) { |
| 31 | + ApiClient defaultClient = ApiClient.getDefaultApiClient(); |
| 32 | + defaultClient.setUnstableOperationEnabled("v2.createLLMObsIntegrationInference", true); |
| 33 | + LlmObservabilityApi apiInstance = new LlmObservabilityApi(defaultClient); |
| 34 | + |
| 35 | + LLMObsIntegrationInferenceRequest body = |
| 36 | + new LLMObsIntegrationInferenceRequest() |
| 37 | + .anthropicMetadata( |
| 38 | + new LLMObsAnthropicMetadata() |
| 39 | + .effort(LLMObsAnthropicEffort.MEDIUM) |
| 40 | + .thinking( |
| 41 | + new LLMObsAnthropicThinkingConfig() |
| 42 | + .budgetTokens(1024L) |
| 43 | + .type(LLMObsAnthropicThinkingType.ENABLED))) |
| 44 | + .azureOpenaiMetadata( |
| 45 | + new LLMObsAzureOpenAIMetadata() |
| 46 | + .deploymentId("my-gpt4-deployment") |
| 47 | + .modelVersion("0613") |
| 48 | + .resourceName("my-azure-resource")) |
| 49 | + .bedrockMetadata(new LLMObsBedrockMetadata().region("us-east-1")) |
| 50 | + .jsonSchema(""" |
| 51 | +{"type":"object","properties":{"answer":{"type":"string"}}} |
| 52 | +""") |
| 53 | + .maxCompletionTokens(1024L) |
| 54 | + .maxTokens(1024L) |
| 55 | + .messages( |
| 56 | + Collections.singletonList( |
| 57 | + new LLMObsInferenceMessage() |
| 58 | + .content("What is the capital of France?") |
| 59 | + .contents( |
| 60 | + Collections.singletonList( |
| 61 | + new LLMObsInferenceContent() |
| 62 | + .type("text") |
| 63 | + .value( |
| 64 | + new LLMObsInferenceContentValue() |
| 65 | + .text("Hello, how can I help you?") |
| 66 | + .toolCall( |
| 67 | + new LLMObsInferenceToolCall() |
| 68 | + .arguments( |
| 69 | + Map.ofEntries( |
| 70 | + Map.entry("location", "San Francisco"))) |
| 71 | + .name("get_weather") |
| 72 | + .toolId("call_abc123") |
| 73 | + .type("function")) |
| 74 | + .toolCallResult( |
| 75 | + new LLMObsInferenceToolResult() |
| 76 | + .name("get_weather") |
| 77 | + .result( |
| 78 | + "The weather in San Francisco is 68°F and" |
| 79 | + + " sunny.") |
| 80 | + .toolId("call_abc123") |
| 81 | + .type("function"))))) |
| 82 | + .id("msg_001") |
| 83 | + .role("user") |
| 84 | + .toolCalls( |
| 85 | + Collections.singletonList( |
| 86 | + new LLMObsInferenceToolCall() |
| 87 | + .arguments( |
| 88 | + Map.ofEntries(Map.entry("location", "San Francisco"))) |
| 89 | + .name("get_weather") |
| 90 | + .toolId("call_abc123") |
| 91 | + .type("function"))) |
| 92 | + .toolResults( |
| 93 | + Collections.singletonList( |
| 94 | + new LLMObsInferenceToolResult() |
| 95 | + .name("get_weather") |
| 96 | + .result("The weather in San Francisco is 68°F and sunny.") |
| 97 | + .toolId("call_abc123") |
| 98 | + .type("function"))))) |
| 99 | + .modelId("gpt-4o") |
| 100 | + .openaiMetadata( |
| 101 | + new LLMObsOpenAIMetadata() |
| 102 | + .reasoningEffort(LLMObsOpenAIReasoningEffort.MEDIUM) |
| 103 | + .reasoningSummary(LLMObsOpenAIReasoningSummary.AUTO)) |
| 104 | + .temperature(0.7) |
| 105 | + .tools( |
| 106 | + Collections.singletonList( |
| 107 | + new LLMObsInferenceTool() |
| 108 | + .function( |
| 109 | + new LLMObsInferenceFunction() |
| 110 | + .description("Get the current weather for a location.") |
| 111 | + .name("get_weather") |
| 112 | + .parameters( |
| 113 | + Map.ofEntries( |
| 114 | + Map.entry("properties", "{'location': {'type': 'string'}}"), |
| 115 | + Map.entry("type", "object")))) |
| 116 | + .type("function"))) |
| 117 | + .topK(50L) |
| 118 | + .topP(1.0) |
| 119 | + .vertexAiMetadata( |
| 120 | + new LLMObsVertexAIMetadata() |
| 121 | + .location("us-central1") |
| 122 | + .project("my-gcp-project") |
| 123 | + .projectIds(Collections.singletonList("my-gcp-project"))); |
| 124 | + |
| 125 | + try { |
| 126 | + LLMObsIntegrationInferenceResponse result = |
| 127 | + apiInstance.createLLMObsIntegrationInference( |
| 128 | + LLMObsIntegrationName.OPENAI, "account-abc123", body); |
| 129 | + System.out.println(result); |
| 130 | + } catch (ApiException e) { |
| 131 | + System.err.println( |
| 132 | + "Exception when calling LlmObservabilityApi#createLLMObsIntegrationInference"); |
| 133 | + System.err.println("Status code: " + e.getCode()); |
| 134 | + System.err.println("Reason: " + e.getResponseBody()); |
| 135 | + System.err.println("Response headers: " + e.getResponseHeaders()); |
| 136 | + e.printStackTrace(); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments