function-calling初体验,使用JAVA调用OpenAI的Gpt-3.5—0613模型

810 阅读2分钟

导入pom依赖

<dependency>
    <groupId>com.unfbx</groupId>
    <artifactId>chatgpt-java</artifactId>
    <version>1.0.14-beta1</version>
</dependency>

参考项目

github.com/Grt1228/cha…

此项目只是对chatgpt-java SDK的一个简单示例项目,实现流式输出,仅做参考仅做参考仅做参考。大家最好还是自己基于SDK动手实现

目前本项目支持两种流式输出,支持Tokens计算,基于ChatGPT-Java SDK 。

流式输出实现方式小程序安卓iosH5
SSE参考:OpenAISSEEventSourceListener不支持支持支持支持
WebSocket参考:OpenAIWebSocketEventSourceListener支持支持支持支持

调用Function-calling的一个Demo

public AIResult chat2fun( String prompt) throws InterruptedException {  
    if (!this.enable) {  
        return AIResult.fail("openai is not enable");  
    }  
    StringBuilder result = new StringBuilder();  
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");  
    JSONObject typeLanguage=new JSONObject();  
    typeLanguage.putOpt("type","string");  
    typeLanguage.putOpt("description","返回值的类型,如果是回答一条信息");  
    typeLanguage.putOpt("enum", Arrays.asList("array","object"));  

    JSONObject resultLanguage=new JSONObject();  
    resultLanguage.putOpt("type","array");  
    resultLanguage.putOpt("description","回答的结果都在这个里面");  


    JSONObject properties=new JSONObject();  
    properties.putOpt("infoType", typeLanguage);  
    properties.putOpt("info", resultLanguage);  
    Parameters parameters=Parameters.builder()  
        .type("object")  
        .properties(properties)  
        .required(Arrays.asList("info"))  
        .build();  
        Functions functions=Functions.builder().name("getInfo")  
        .description("根据问题返回结果包装")  
        .parameters(parameters).build();  

    List<Message> messages = new ArrayList<>();  
    Message message = Message.builder().role(Message.Role.USER).content(prompt).build();  
    messages.add(message);  
    List<Functions> functionsList=new ArrayList<>();  
    functionsList.add(functions);  


    ChatCompletion chatCompletion = ChatCompletion.builder()  
        .messages(messages)  
        .functions(functionsList)  
        .functionCall("auto")  
        .model(ChatCompletion.Model.GPT_3_5_TURBO_16K_0613.getName())  
        .build();
    try {  
        ChatCompletionResponse chatCompletionResponse = this.openAiClient.chatCompletion(chatCompletion);  
        chatCompletionResponse.getChoices().forEach(e -> {  
            log.info(e.getMessage().toString());  
            //拼接返回结果  
            result.append(e.getMessage().getContent());  
        });  
        AIResult resultObj = ChatGPTResultUtil.process(result.toString());  
        return resultObj;  
    } catch (Exception e1) {  
        log.error("ChatGPTService.chat error", e1);  
        return AIResult.fail("ChatGPTService.chat error"+e1.getMessage());  
    }  
}

在OpenAI中,更新的模型增加了两个参数functions、functioncall。

image.png

image.png

在chatgpt-java的SDK中,通过Parameters构建对象。 type:object,默认使用object,其他格式暂时未看到文档中有说明。 properties:JSONObject,传入一个JSONObject对象,设置需要的参数,比如示例中写的properties.putOpt("type", Arrays.asList("object","array")); properties.putOpt("result", "object"); ,意思是type从object和array中选择,result返回值是object类型;

Functions functions=Functions.builder().name("getObject")
.description("返回一个JSONObject对象的字符串")
.parameters(parameters).build();

为函数设置名称、说明、参数说明。

构建后传入ChatCompletion中。

functionCall:取值:null,auto或者自定义 ,functions没有值的时候默认为:null,functions存在值得时候默认为:auto,也可以自定义

返回值

{"type":"object","result":{"title":"汽车的结构解析与原理讲述","content":"...."}}