admin
2025-06-06 9cd825aea53fa5ba0cda1485464af027e27f0ce4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package tech.aiflowy.ai.entity;
 
import tech.aiflowy.ai.service.AiKnowledgeService;
import tech.aiflowy.common.domain.Result;
import tech.aiflowy.common.util.SpringContextUtil;
import com.agentsflex.core.llm.functions.BaseFunction;
import com.agentsflex.core.llm.functions.Parameter;
 
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
 
public class AiKnowledgeFunction extends BaseFunction {
 
    private BigInteger knowledgeId;
 
    public AiKnowledgeFunction() {
    }
 
    public AiKnowledgeFunction(AiKnowledge aiKnowledge) {
        this.knowledgeId = aiKnowledge.getId();
        this.name = aiKnowledge.getTitle();
        this.description = aiKnowledge.getDescription();
        this.parameters = getDefaultParameters();
    }
 
 
    public Parameter[] getDefaultParameters() {
        Parameter parameter = new Parameter();
        parameter.setName("input");
        parameter.setDescription("要查询的相关知识");
        parameter.setType("String");
        parameter.setRequired(true);
        return new Parameter[]{parameter};
    }
 
    public BigInteger getKnowledgeId() {
        return knowledgeId;
    }
 
    public void setKnowledgeId(BigInteger knowledgeId) {
        this.knowledgeId = knowledgeId;
    }
 
    @Override
    public Object invoke(Map<String, Object> argsMap) {
 
        AiKnowledgeService knowledgeService = SpringContextUtil.getBean(AiKnowledgeService.class);
        Result result = knowledgeService.search(this.knowledgeId, (String) argsMap.get("input"));
 
        if (result.isSuccess()) {
            StringBuilder sb = new StringBuilder();
            //noinspection unchecked
            List<AiDocumentChunk> chunks = (List<AiDocumentChunk>) result.data();
            if (chunks != null) {
                for (AiDocumentChunk chunk : chunks) {
                    sb.append(chunk.getContent());
                }
            }
            return sb.toString();
        }
 
 
        return result.failMessage();
    }
 
    @Override
    public String toString() {
        return "AiKnowledgeFunction{" +
                "knowledgeId=" + knowledgeId +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                ", parameters=" + Arrays.toString(parameters) +
                '}';
    }
}