|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.flink.agents.integration.test; |
| 19 | + |
| 20 | +import org.apache.flink.agents.api.Agent; |
| 21 | +import org.apache.flink.agents.api.InputEvent; |
| 22 | +import org.apache.flink.agents.api.OutputEvent; |
| 23 | +import org.apache.flink.agents.api.annotation.Action; |
| 24 | +import org.apache.flink.agents.api.annotation.ChatModelConnection; |
| 25 | +import org.apache.flink.agents.api.annotation.ChatModelSetup; |
| 26 | +import org.apache.flink.agents.api.annotation.Tool; |
| 27 | +import org.apache.flink.agents.api.annotation.ToolParam; |
| 28 | +import org.apache.flink.agents.api.chat.messages.ChatMessage; |
| 29 | +import org.apache.flink.agents.api.chat.messages.MessageRole; |
| 30 | +import org.apache.flink.agents.api.context.RunnerContext; |
| 31 | +import org.apache.flink.agents.api.event.ChatRequestEvent; |
| 32 | +import org.apache.flink.agents.api.event.ChatResponseEvent; |
| 33 | +import org.apache.flink.agents.api.resource.ResourceDescriptor; |
| 34 | +import org.apache.flink.agents.api.resource.ResourceType; |
| 35 | +import org.apache.flink.agents.api.resource.python.PythonChatModelConnection; |
| 36 | +import org.apache.flink.agents.api.resource.python.PythonChatModelSetup; |
| 37 | + |
| 38 | +import java.util.Collections; |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +/** |
| 42 | + * Agent example integrating a built-in Python chat model into Flink Agents. |
| 43 | + * |
| 44 | + * <p>This class demonstrates how to: |
| 45 | + * |
| 46 | + * <ul> |
| 47 | + * <li>Declare a chat model connection using {@link ChatModelConnection} metadata pointing to |
| 48 | + * {@link PythonChatModelConnection} |
| 49 | + * <li>Declare a chat model setup using {@link ChatModelSetup} metadata pointing to {@link |
| 50 | + * PythonChatModelSetup} |
| 51 | + * <li>Expose callable tools via {@link Tool} annotated static methods (temperature conversion, |
| 52 | + * BMI, random number) |
| 53 | + * <li>Fetch a chat model from the {@link RunnerContext} and perform a single-turn chat |
| 54 | + * <li>Emit the model response as an {@link OutputEvent} |
| 55 | + * </ul> |
| 56 | + * |
| 57 | + * <p>The {@code pythonChatModel()} method publishes a resource with type {@link |
| 58 | + * ResourceType#CHAT_MODEL} so it can be retrieved at runtime inside the {@code process} action. The |
| 59 | + * resource is configured with the connection name, the model name and the list of tool names that |
| 60 | + * the model is allowed to call. |
| 61 | + */ |
| 62 | +public class AgentWithPythonChatModel extends Agent { |
| 63 | + @ChatModelConnection |
| 64 | + public static ResourceDescriptor pythonChatModelConnection() { |
| 65 | + return ResourceDescriptor.Builder.newBuilder(PythonChatModelConnection.class.getName()) |
| 66 | + .addInitialArgument( |
| 67 | + "module", "flink_agents.integrations.chat_models.tongyi_chat_model") |
| 68 | + .addInitialArgument("clazz", "TongyiChatModelConnection") |
| 69 | + .build(); |
| 70 | + } |
| 71 | + |
| 72 | + @ChatModelSetup |
| 73 | + public static ResourceDescriptor pythonChatModel() { |
| 74 | + return ResourceDescriptor.Builder.newBuilder(PythonChatModelSetup.class.getName()) |
| 75 | + .addInitialArgument("connection", "pythonChatModelConnection") |
| 76 | + .addInitialArgument( |
| 77 | + "module", "flink_agents.integrations.chat_models.tongyi_chat_model") |
| 78 | + .addInitialArgument("clazz", "TongyiChatModelSetup") |
| 79 | + .addInitialArgument("model", "qwen-plus") |
| 80 | + .addInitialArgument( |
| 81 | + "tools", |
| 82 | + List.of("calculateBMI", "convertTemperature", "createRandomNumber")) |
| 83 | + .addInitialArgument("extract_reasoning", "true") |
| 84 | + .build(); |
| 85 | + } |
| 86 | + |
| 87 | + @Tool(description = "Converts temperature between Celsius and Fahrenheit") |
| 88 | + public static double convertTemperature( |
| 89 | + @ToolParam(name = "value", description = "Temperature value to convert") Double value, |
| 90 | + @ToolParam( |
| 91 | + name = "fromUnit", |
| 92 | + description = "Source unit ('C' for Celsius or 'F' for Fahrenheit)") |
| 93 | + String fromUnit, |
| 94 | + @ToolParam( |
| 95 | + name = "toUnit", |
| 96 | + description = "Target unit ('C' for Celsius or 'F' for Fahrenheit)") |
| 97 | + String toUnit) { |
| 98 | + |
| 99 | + fromUnit = fromUnit.toUpperCase(); |
| 100 | + toUnit = toUnit.toUpperCase(); |
| 101 | + |
| 102 | + if (fromUnit.equals(toUnit)) { |
| 103 | + return value; |
| 104 | + } |
| 105 | + |
| 106 | + if (fromUnit.equals("C") && toUnit.equals("F")) { |
| 107 | + return (value * 9 / 5) + 32; |
| 108 | + } else if (fromUnit.equals("F") && toUnit.equals("C")) { |
| 109 | + return (value - 32) * 5 / 9; |
| 110 | + } else { |
| 111 | + throw new IllegalArgumentException("Invalid temperature units. Use 'C' or 'F'"); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Tool(description = "Calculates Body Mass Index (BMI)") |
| 116 | + public static double calculateBMI( |
| 117 | + @ToolParam(name = "weightKg", description = "Weight in kilograms") Double weightKg, |
| 118 | + @ToolParam(name = "heightM", description = "Height in meters") Double heightM) { |
| 119 | + |
| 120 | + if (weightKg <= 0 || heightM <= 0) { |
| 121 | + throw new IllegalArgumentException("Weight and height must be positive values"); |
| 122 | + } |
| 123 | + return weightKg / (heightM * heightM); |
| 124 | + } |
| 125 | + |
| 126 | + @Tool(description = "Create a random number") |
| 127 | + public static double createRandomNumber() { |
| 128 | + return Math.random(); |
| 129 | + } |
| 130 | + |
| 131 | + @Action(listenEvents = {InputEvent.class}) |
| 132 | + public static void process(InputEvent event, RunnerContext ctx) throws Exception { |
| 133 | + ctx.sendEvent( |
| 134 | + new ChatRequestEvent( |
| 135 | + "pythonChatModel", |
| 136 | + Collections.singletonList( |
| 137 | + new ChatMessage(MessageRole.USER, (String) event.getInput())))); |
| 138 | + } |
| 139 | + |
| 140 | + @Action(listenEvents = {ChatResponseEvent.class}) |
| 141 | + public static void processChatResponse(ChatResponseEvent event, RunnerContext ctx) { |
| 142 | + ctx.sendEvent(new OutputEvent(event.getResponse().getContent())); |
| 143 | + } |
| 144 | +} |
0 commit comments