Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

23 Commits

Repository files navigation

🛠️ Function Call Processor (FCP)

Maven CentralLicenseJava VersionGitHub starsGitHub issues

FCP是一个基于注解的Java工具库,只需使用@Function@Property注解,即可轻松将Java方法转换为大模型可识别的FunctionCall格式,并且还支持方法的快速回调。

✨ 功能特性

  • 通过注解将Java方法解析为大模型的函数调用格式
  • 自动执行大模型返回的函数调用

🔧 环境要求

  • JDK 11 或更高版本

📦 引入依赖

Maven

<dependency>
<groupId>io.github.azirzsk</groupId>
<artifactId>function-call-processor</artifactId>
<version>1.0.0</version>
</dependency>

Gradle

implementation 'io.github.azirzsk:function-call-processor:1.0.0'

🚀 快速开始

📤 1. 解析对象方法为FunctionCall

将Java对象中的方法解析为大模型可识别的FunctionCall格式:

publicclassCalculator {
@Function(desc = "将两个数字相加")
publicintadd(@Property(desc = "第一个数字") inta,
@Property(desc = "第二个数字") intb) {
returna + b;
}
@Function(desc = "将两个数字相乘")
publicintmultiply(@Property(desc = "第一个数字") intx,
@Property(desc = "第二个数字") inty) {
returnx * y;
}
}

然后使用FCP解析这个类:

// 创建FCP实例FCPfcp = FCP.create();
// 传入需要解析的对象Calculatorcalculator = newCalculator();
StringfunctionCallJson = fcp.parse(calculator);
解析后得到的JSON格式如下:
[
{
"type": "function",
"function": {
"name": "add",
"description": "将两个数字相加",
"parameters": {
"type": "object",
"properties": {
"a": {
"type": "integer",
"description": "第一个数字"
},
"b": {
"type": "integer",
"description": "第二个数字"
}
},
"required": [
"a",
"b"
]
}
}
},
{
"type": "function",
"function": {
"name": "multiply",
"description": "将两个数字相乘",
"parameters": {
"type": "object",
"properties": {
"x": {
"type": "integer",
"description": "第一个数字"
},
"y": {
"type": "integer",
"description": "第二个数字"
}
},
"required": [
"x",
"y"
]
}
}
}
]

📥 2. 执行FunctionCall回调

接收大模型返回的FunctionCall并执行对应的方法。

// 创建FCP实例(如果已创建可以复用)FCPfcp = FCP.create();
// 传入需要解析的对象(如果已传入可以复用)Calculatorcalculator = newCalculator();
fcp.parse(calculator);
// 大模型返回的tool_calls格式StringllmResponse = """{ "tool_calls": [ { "id": "call_123456", "type": "function", "function": { "name": "add", "arguments": "{\\"a\\": 1, \\"b\\": 2}" } } ]}""";
// 从大模型响应中提取函数名和参数StringfunctionName = "add"; // 从llmResponse中解析function.nameStringargumentsJson = "{\"a\": 1, \"b\": 2}"; // 从llmResponse中解析function.arguments// 执行函数调用Objectresult = fcp.functionCall(functionName, argumentsJson);
System.out.println(result); // 输出: 3

About

使用注解快速开发大模型FunctionCall

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages