Package dev.dokimos.openai
Class OpenAiSupport
java.lang.Object
dev.dokimos.openai.OpenAiSupport
Utilities for integrating with the OpenAI Java SDK.
This class provides factory methods to create JudgeLMs and to capture an OpenAI
agent's tool calls as Dokimos AgentTrace, ToolCall, and ToolDefinition
objects the agent evaluators understand.
Agent Evaluation
ChatCompletion completion = client.chat().completions().create(params);
ChatCompletionMessage message = completion.choices().get(0).message();
AgentTrace trace = OpenAiSupport.toAgentTrace(message, id -> myApp.resultFor(id));
EvalTestCase testCase = trace.toTestCase(userMessage, OpenAiSupport.toToolDefinitions(tools));
-
Method Summary
Modifier and TypeMethodDescriptionstatic JudgeLMasJudge(com.openai.client.OpenAIClient client, com.openai.models.ChatModel model) static AgentTracetoAgentTrace(com.openai.models.chat.completions.ChatCompletionMessage message) Builds anAgentTracefrom an OpenAIChatCompletionMessage.static AgentTracetoAgentTrace(com.openai.models.chat.completions.ChatCompletionMessage message, Function<String, String> resultLookup) Builds anAgentTracefrom an OpenAIChatCompletionMessage, looking up each tool call's result by its id.static ToolCalltoToolCall(com.openai.models.chat.completions.ChatCompletionMessageToolCall toolCall, String result) Converts a single OpenAI functionChatCompletionMessageToolCallto aToolCall.toToolCalls(com.openai.models.chat.completions.ChatCompletionMessage message) Extracts functionToolCalls from an OpenAIChatCompletionMessagein order.toToolCalls(com.openai.models.chat.completions.ChatCompletionMessage message, Function<String, String> resultLookup) Extracts functionToolCalls from an OpenAIChatCompletionMessage, looking up each tool call's result by its id.static ToolDefinitiontoToolDefinition(com.openai.models.chat.completions.ChatCompletionTool tool) Converts a single OpenAI functionChatCompletionToolto aToolDefinition.static List<ToolDefinition> toToolDefinitions(List<com.openai.models.chat.completions.ChatCompletionTool> tools) Converts OpenAIChatCompletionTools toToolDefinitions so tool calls can be evaluated against the tools the agent was given.
-
Method Details
-
asJudge
public static JudgeLM asJudge(com.openai.client.OpenAIClient client, com.openai.models.ChatModel model) Creates aJudgeLMfrom an OpenAIOpenAIClientand a specificChatModel.Use this to create judges for LLM-based evaluators like
LLMJudgeEvaluator,TaskCompletionEvaluator, etc.- Parameters:
client- the OpenAI client to use as judgemodel- the chat model the judge uses- Returns:
- a JudgeLM that delegates to the client
-
toAgentTrace
public static AgentTrace toAgentTrace(com.openai.models.chat.completions.ChatCompletionMessage message) Builds anAgentTracefrom an OpenAIChatCompletionMessage.The message's
content()becomes the final response and its functiontoolCalls()becomeToolCalls carrying the tool name and parsed arguments.- Parameters:
message- the assistant message (may be null)- Returns:
- an agent trace, never null
-
toAgentTrace
public static AgentTrace toAgentTrace(com.openai.models.chat.completions.ChatCompletionMessage message, Function<String, String> resultLookup) Builds anAgentTracefrom an OpenAIChatCompletionMessage, looking up each tool call's result by its id.- Parameters:
message- the assistant message (may be null)resultLookup- maps a tool call id to its execution result (may return null)- Returns:
- an agent trace, never null
-
toToolCalls
public static List<ToolCall> toToolCalls(com.openai.models.chat.completions.ChatCompletionMessage message) Extracts functionToolCalls from an OpenAIChatCompletionMessagein order.- Parameters:
message- the assistant message (may be null)- Returns:
- the tool calls, or an empty list when there are none
-
toToolCalls
public static List<ToolCall> toToolCalls(com.openai.models.chat.completions.ChatCompletionMessage message, Function<String, String> resultLookup) Extracts functionToolCalls from an OpenAIChatCompletionMessage, looking up each tool call's result by its id.- Parameters:
message- the assistant message (may be null)resultLookup- maps a tool call id to its execution result (may return null)- Returns:
- the tool calls, or an empty list when there are none
-
toToolCall
public static ToolCall toToolCall(com.openai.models.chat.completions.ChatCompletionMessageToolCall toolCall, String result) Converts a single OpenAI functionChatCompletionMessageToolCallto aToolCall.- Parameters:
toolCall- the OpenAI tool call (must be a function tool call)result- the result of executing the tool, or null if not executed- Returns:
- the tool call
-
toToolDefinitions
public static List<ToolDefinition> toToolDefinitions(List<com.openai.models.chat.completions.ChatCompletionTool> tools) Converts OpenAIChatCompletionTools toToolDefinitions so tool calls can be evaluated against the tools the agent was given.- Parameters:
tools- the OpenAI tools (may be null)- Returns:
- the tool definitions, or an empty list
-
toToolDefinition
public static ToolDefinition toToolDefinition(com.openai.models.chat.completions.ChatCompletionTool tool) Converts a single OpenAI functionChatCompletionToolto aToolDefinition.- Parameters:
tool- the OpenAI tool (must be a function tool)- Returns:
- the tool definition
-