wenjin272 commented on code in PR #556: URL: https://github.com/apache/flink-agents/pull/556#discussion_r2882665987
########## integrations/chat-models/openai/src/main/java/org/apache/flink/agents/integrations/chatmodels/openai/OpenAIResponseModelConnection.java: ########## @@ -0,0 +1,470 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.flink.agents.integrations.chatmodels.openai; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.openai.client.OpenAIClient; +import com.openai.client.okhttp.OpenAIOkHttpClient; +import com.openai.core.JsonValue; +import com.openai.models.ChatModel; +import com.openai.models.Reasoning; +import com.openai.models.ReasoningEffort; +import com.openai.models.responses.*; +import org.apache.flink.agents.api.chat.messages.ChatMessage; +import org.apache.flink.agents.api.chat.messages.MessageRole; +import org.apache.flink.agents.api.chat.model.BaseChatModelConnection; +import org.apache.flink.agents.api.resource.Resource; +import org.apache.flink.agents.api.resource.ResourceDescriptor; +import org.apache.flink.agents.api.resource.ResourceType; +import org.apache.flink.agents.api.tools.ToolMetadata; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.BiFunction; + +/** + * A <b>dedicated</b> OpenAI chat model integration using the Responses API. + * + * <p>Unlike {@link OpenAIChatModelConnection} which uses the Chat Completions API and works with + * any OpenAI-compatible provider (DeepSeek, DashScope, etc.), this implementation uses OpenAI's + * Responses API which is specific to OpenAI and offers additional capabilities: + * + * <ul> + * <li>Built-in tools (web search, file search, code interpreter) + * <li>Native remote MCP server support + * <li>Reasoning summaries for o-series models + * <li>Stateful multi-turn via previous_response_id + * <li>Response storage for later retrieval + * </ul> + * + * <p>For OpenAI-compatible providers that only support the Chat Completions API, use {@link + * OpenAIChatModelConnection} instead. + * + * <p>Supported connection parameters: + * + * <ul> + * <li><b>api_key</b> (required): OpenAI API key + * <li><b>api_base_url</b> (optional): Base URL for OpenAI API (useful for proxies) + * <li><b>timeout</b> (optional): Timeout in seconds for API requests + * <li><b>max_retries</b> (optional): Maximum number of retry attempts (default: 2) + * <li><b>default_headers</b> (optional): Map of default headers to include in all requests + * <li><b>model</b> (optional): Default model to use if not specified in setup + * </ul> + * + * <p>Example usage: + * + * <pre>{@code + * public class MyAgent extends Agent { + * @ChatModelConnection + * public static ResourceDesc openAIResponses() { + * return ResourceDescriptor.Builder.newBuilder(OpenAIResponseModelConnection.class.getName()) + * .addInitialArgument("api_key", System.getenv("OPENAI_API_KEY")) + * .addInitialArgument("timeout", 120) + * .addInitialArgument("max_retries", 3) + * .build(); + * } + * } + * }</pre> + */ +public class OpenAIResponseModelConnection extends BaseChatModelConnection { + + private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<>() {}; + + private final ObjectMapper mapper = new ObjectMapper(); Review Comment: Because ObjectMapper is thread safe, this could be a static field. ########## integrations/chat-models/openai/src/main/java/org/apache/flink/agents/integrations/chatmodels/openai/OpenAIResponseModelConnection.java: ########## @@ -0,0 +1,470 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.flink.agents.integrations.chatmodels.openai; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.openai.client.OpenAIClient; +import com.openai.client.okhttp.OpenAIOkHttpClient; +import com.openai.core.JsonValue; +import com.openai.models.ChatModel; +import com.openai.models.Reasoning; +import com.openai.models.ReasoningEffort; +import com.openai.models.responses.*; +import org.apache.flink.agents.api.chat.messages.ChatMessage; +import org.apache.flink.agents.api.chat.messages.MessageRole; +import org.apache.flink.agents.api.chat.model.BaseChatModelConnection; +import org.apache.flink.agents.api.resource.Resource; +import org.apache.flink.agents.api.resource.ResourceDescriptor; +import org.apache.flink.agents.api.resource.ResourceType; +import org.apache.flink.agents.api.tools.ToolMetadata; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.BiFunction; + +/** + * A <b>dedicated</b> OpenAI chat model integration using the Responses API. + * + * <p>Unlike {@link OpenAIChatModelConnection} which uses the Chat Completions API and works with + * any OpenAI-compatible provider (DeepSeek, DashScope, etc.), this implementation uses OpenAI's + * Responses API which is specific to OpenAI and offers additional capabilities: + * + * <ul> + * <li>Built-in tools (web search, file search, code interpreter) + * <li>Native remote MCP server support + * <li>Reasoning summaries for o-series models + * <li>Stateful multi-turn via previous_response_id + * <li>Response storage for later retrieval + * </ul> + * + * <p>For OpenAI-compatible providers that only support the Chat Completions API, use {@link + * OpenAIChatModelConnection} instead. + * + * <p>Supported connection parameters: + * + * <ul> + * <li><b>api_key</b> (required): OpenAI API key + * <li><b>api_base_url</b> (optional): Base URL for OpenAI API (useful for proxies) + * <li><b>timeout</b> (optional): Timeout in seconds for API requests + * <li><b>max_retries</b> (optional): Maximum number of retry attempts (default: 2) + * <li><b>default_headers</b> (optional): Map of default headers to include in all requests + * <li><b>model</b> (optional): Default model to use if not specified in setup + * </ul> + * + * <p>Example usage: + * + * <pre>{@code + * public class MyAgent extends Agent { + * @ChatModelConnection + * public static ResourceDesc openAIResponses() { + * return ResourceDescriptor.Builder.newBuilder(OpenAIResponseModelConnection.class.getName()) + * .addInitialArgument("api_key", System.getenv("OPENAI_API_KEY")) + * .addInitialArgument("timeout", 120) + * .addInitialArgument("max_retries", 3) + * .build(); + * } + * } + * }</pre> + */ +public class OpenAIResponseModelConnection extends BaseChatModelConnection { + + private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<>() {}; + + private final ObjectMapper mapper = new ObjectMapper(); + private final OpenAIClient client; + private final String defaultModel; Review Comment: This field maybe not needed. The `OpenAIResponseModelSetup` will always pass a model name when calling chat. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
