package gpt.plugin;

import gpt.ChatGPT; import gpt.Session; import gpt.function.FunctionCall; import gpt.plugin.FunctionMethod; import gpt.plugin.FunctionPropertie; import gpt.respond.ChatRespond; import gpt.respond.Choices; import gpt.respond.Message; import util.Response;

import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List;

public class PluginSession extends Session { private PluginManger pluginManger;

public PluginSession(String sessionName) {
    super(sessionName);
    pluginManger = new PluginManger();
    body.functions = new PluginFunctions(pluginManger);

    String prompt = ChatGPT.INSTANCE.defaultConfig.prompt;
    if (prompt != null && !prompt.isEmpty()) {
        body.prompts.append(Message.system(prompt));
    }
}

@Override
public Response<ChatRespond> send(Message message) {
    if (message != null) {
        body.messages.append(message);
    }

    Response<ChatRespond> response = ChatGPT.INSTANCE.send(body);
    if (response.isSuccessful()) {
        ChatRespond cr = response.body;
        Choices choice = cr.choices.get(0);
        if (choice.isFunctionCall()) {
            FunctionCall fc = choice.message.function_call;
            List<Plugin> plugins = pluginManger.getPlugins();
            for (Plugin plugin : plugins) {
                Class<?> pluginType = plugin.getClass();
                PluginInfo pi = pluginType.getAnnotation(PluginInfo.class);
                Method[] methods = pluginType.getMethods();
                for (Method method : methods) {
                    FunctionMethod fm = method.getAnnotation(FunctionMethod.class);
                    if (fm == null) {
                        continue;
                    }
                    FunctionPropertie[] fps = method.getAnnotationsByType(FunctionPropertie.class);
                    if (fc.name.equals(fm.name())) {
                        try {
                            List<Object> args = new ArrayList<>();
                            CallParameter cp = new CallParameter();
                            if (!fc.isJSON()) {
                                cp.text = fc.arguments;
                            }
                            args.add(cp);
                            for (FunctionPropertie fp : fps) {
                                if (fc.isJSONObject()) {
                                    switch (fp.type()) {
                                        case 'string':
                                            args.add(fc.asJSONObject().getString(fp.name()));
                                            break;
                                        case 'integer':
                                            args.add(fc.asJSONObject().getInt(fp.name()));
                                            break;
                                    }
                                }
                            }
                            System.out.println('调用函数 ' + fm.name());
                            method.invoke(plugin, args.toArray(new Object[]{}));
                            if (cp.returnContent == null) {
                                body.messages.append(Message.function(fc.name, '调用函数失败'));
                            } else {
                                body.messages.append(Message.function(fc.name, cp.returnContent));
                            }
                            return send(null);
                        } catch (Throwable e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            System.out.println('没有找到函数:' + fc.name);
        } else {
            body.messages.append(choice.message);
        }
    }
    return response;
}

}

GPT 插件会话管理:PluginSession 类详解

原文地址: https://www.cveoy.top/t/topic/hJhJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录