Skip to content

Commit 62a03a0

Browse files
committed
Add function test main class
1 parent 2544f73 commit 62a03a0

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,32 @@
175175
</argLine>
176176
</configuration>
177177
</plugin>
178+
<plugin>
179+
<groupId>org.apache.maven.plugins</groupId>
180+
<artifactId>maven-assembly-plugin</artifactId>
181+
<version>3.6.0</version>
182+
<configuration>
183+
<archive>
184+
<manifest>
185+
<mainClass>
186+
io.github.alexcheng1982.springai.dashscope.FunctionExampleMain
187+
</mainClass>
188+
</manifest>
189+
</archive>
190+
<descriptorRefs>
191+
<descriptorRef>jar-with-dependencies</descriptorRef>
192+
</descriptorRefs>
193+
</configuration>
194+
<executions>
195+
<execution>
196+
<id>fat-jar</id>
197+
<phase>package</phase>
198+
<goals>
199+
<goal>single</goal>
200+
</goals>
201+
</execution>
202+
</executions>
203+
</plugin>
178204
</plugins>
179205
</build>
180206
</profile>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package io.github.alexcheng1982.springai.dashscope;
2+
3+
import com.alibaba.dashscope.tools.FunctionDefinition;
4+
import com.alibaba.dashscope.tools.ToolFunction;
5+
import com.alibaba.dashscope.utils.JsonUtils;
6+
import com.fasterxml.jackson.databind.node.ObjectNode;
7+
import com.github.victools.jsonschema.generator.Option;
8+
import com.github.victools.jsonschema.generator.OptionPreset;
9+
import com.github.victools.jsonschema.generator.SchemaGenerator;
10+
import com.github.victools.jsonschema.generator.SchemaGeneratorConfig;
11+
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
12+
import com.github.victools.jsonschema.generator.SchemaVersion;
13+
import io.github.alexcheng1982.springai.dashscope.api.DashscopeApi;
14+
import io.github.alexcheng1982.springai.dashscope.api.DashscopeModelName;
15+
import java.util.List;
16+
import java.util.function.Function;
17+
import org.springframework.ai.chat.prompt.Prompt;
18+
import org.springframework.ai.model.function.FunctionCallback;
19+
import org.springframework.ai.model.function.FunctionCallbackContext;
20+
import org.springframework.ai.model.function.FunctionCallbackWrapper;
21+
22+
public class FunctionExampleMain {
23+
24+
record AddRequest(int left, int right) {
25+
26+
}
27+
28+
record AddResponse(int result) {
29+
30+
}
31+
32+
static class AddFunctionTool implements Function<AddRequest, AddResponse> {
33+
34+
@Override
35+
public AddResponse apply(AddRequest addRequest) {
36+
return new AddResponse(addRequest.left + addRequest.right);
37+
}
38+
}
39+
40+
static class SimpleFunctionCallbackContext extends FunctionCallbackContext {
41+
42+
private final FunctionCallback functionCallback;
43+
44+
SimpleFunctionCallbackContext(FunctionCallback functionCallback) {
45+
this.functionCallback = functionCallback;
46+
}
47+
48+
@Override
49+
public FunctionCallback getFunctionCallback(String beanName,
50+
String defaultDescription) {
51+
return functionCallback;
52+
}
53+
}
54+
55+
void runFunction() {
56+
SchemaGeneratorConfigBuilder configBuilder =
57+
new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12,
58+
OptionPreset.PLAIN_JSON);
59+
SchemaGeneratorConfig config = configBuilder.with(
60+
Option.EXTRA_OPEN_API_FORMAT_VALUES)
61+
.without(Option.FLATTENED_ENUMS_FROM_TOSTRING).build();
62+
SchemaGenerator generator = new SchemaGenerator(config);
63+
ObjectNode jsonSchema = generator.generateSchema(AddFunctionTool.class);
64+
65+
FunctionDefinition fd = FunctionDefinition.builder().name("add")
66+
.description("add two number")
67+
.parameters(
68+
JsonUtils.parseString(jsonSchema.toString()).getAsJsonObject())
69+
.build();
70+
var options = DashscopeChatOptions.builder()
71+
.withModel(DashscopeModelName.QWEN_MAX)
72+
.withTemperature(0.2f)
73+
.withTools(List.of(
74+
ToolFunction.builder()
75+
.type("function")
76+
.function(fd)
77+
.build()
78+
))
79+
.withFunction("add")
80+
.build();
81+
var tool = new AddFunctionTool();
82+
var context = new SimpleFunctionCallbackContext(
83+
FunctionCallbackWrapper.builder(tool)
84+
.withName("add")
85+
.withDescription("add two numbers")
86+
.build());
87+
var client = new DashscopeChatClient(new DashscopeApi(), context);
88+
var response = client.call(new Prompt("add 100 to 200", options));
89+
System.out.println(response.getResult().getOutput().getContent());
90+
}
91+
92+
public static void main(String[] args) {
93+
new FunctionExampleMain().runFunction();
94+
}
95+
}

0 commit comments

Comments
 (0)