Groovy代码示例

package com.facishare.function.core.template.groovy

import com.fxiaoke.functions.Fx
import com.fxiaoke.functions.http.HttpResult
import com.fxiaoke.functions.http.Request
import com.fxiaoke.functions.http.StringBody
import com.fxiaoke.functions.service.ai.CustomModelManage
import com.fxiaoke.functions.service.ai.model.ChatCompletion

/**
 * AI自定义模型
 * @type classes
 * @namespace ai_custom_model
 * @returntype
 */
class CustomModelManageImpl implements CustomModelManage {

    //此参数为模型的apiKey
    String apiKey = "Bearer xxxx-be91-4adb-8acc-xxxxxx"
    //此参数为模型的Url
    String url = "https://xxx.xxx.com/api/v3/chat/completions"
    //此参数为模型的型号
    String model = "chat-4"

    ChatCompletion.Result chatCompletion(ChatCompletion.Arg arg) {
        //此参数代表总消息列表
        def messageList = arg.getMessages() as List
        //此参数为消息内容
        String content = arg.getMessages().get(0).getContent()
        //此参数为消息角色
        String user = arg.getMessages().get(0).getRole()

        // 构建消息体
        def messages = []
        messageList.each { message ->
            messages << [
                    "content": message['content'],
                    "role"   : message['role']
            ]
        }

        Map bodyMap = [
                "messages": messages,
                "model"   : model,
                "stream"  : false
        ]
        log.info(bodyMap)
        StringBody body = StringBody.builder().content(bodyMap).build()
        Request request = Request.builder()
                .method("POST")
                .url(url)
                .timeout(7000)
                .retryCount(0)
                .header("Content-Type", "application/json")
                .header("Authorization", apiKey)
                .body(body)
                .build()

        //此处为模型调用,请替换成您的模型调用逻辑
        def (Boolean error, HttpResult ret, String message) = Fx.http.execute(request)

        //打印您的模型出参
        log.info(ret)

        //拼装返回纷享设置的参数结果
        ChatCompletion.Result newResult = convertResult(ret)

        //返回结果
        return newResult
    }

    //此方法为自定义模型输出结果后,使用纷享统一封装的结果返回
    private ChatCompletion.Result convertResult(HttpResult result) {
        log.info(result)
        ChatCompletion.Result res = new ChatCompletion.Result()
        if (result.statusCode != 200) {
            return res
        }

        //拿到result.content["choices"]的第一个元素的message字段
        def choices = result.content["choices"] as List
        def content = choices[0]["message"]["content"] as String

        ChatCompletion.Choice newChoice = new ChatCompletion.Choice()
        //finish_reason, 模型推理终止的原因
        newChoice.setFinish_reason("")

        ChatCompletion.Message newMessage = new ChatCompletion.Message()
        newMessage.setRole("user")
        newMessage.setContent(content)
        newChoice.setMessage(newMessage)

        ChatCompletion.Choice[] newChoices = [newChoice] as ChatCompletion.Choice[]
        res.setChoices(newChoices)
        return res
    }

    //debug 时候的入口方法
    static void main(String[] args) {

    }


}

2024-10-22
0 0
Baidu
map