使用OkHttp流式请求OpenAI API(GPT API)接口

前言

因为 GPT 流式请求的出色交互体验,我们打算做一个开源基础应用,方便开发者快速集成项目。
本应用集成 ChatGPT API,使用模型为 gpt-3.5-turbo,项目代码为 Kotlin 语言开发的安卓应用。
人机交互的趋势已经到来,本应用框架也希望能帮助更多开发者快速集成 ChatGPT 体验到人机交互的乐趣!

正文

我们根据流式请求 Chat API 开源一个安卓项目,可方便开发者快速上手使用 开源地址

直接上核心代码,由 kotlin 编写

import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.JsonObject
import okhttp3.*
import org.yameida.asrassistant.config.Config
import org.yameida.asrassistant.model.Message
import org.yameida.asrassistant.model.StreamAiAnswer
import java.io.BufferedReader
import java.io.IOException
import java.lang.Exception

object HttpUtil {

    /**
     * ChatGPT
     */
    fun chat(send: String, callback: CallBack) {
        val url = "https://api.openai.com/v1/chat/completions"
        val apiKey = "Bearer ${Config.apiKey}"
        val jsonObject = JsonObject()
        jsonObject.addProperty("model", "gpt-3.5-turbo")
        val body = RequestBody.create(MediaType.parse("application/json"), "{\n" +
                "  \"model\": \"gpt-3.5-turbo\",\n" +
                "  \"stream\": true,\n" +
                "  \"messages\": [{\"role\": \"user\", \"content\": \"$send!\"}]\n" +
                "}")
        val request: Request = Request.Builder().url(url).method("POST", body)
            .addHeader("Authorization", apiKey)
            .build()
        OkHttpUtil.okHttpClient.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                e.printStackTrace()
                ToastUtils.showLong("网络请求出错 请检查网络")
            }
            override fun onResponse(call: Call, response: Response) {
                try {
                    val responseBody = response.body()
                    if (responseBody != null) {
                        val bufferedReader = BufferedReader(responseBody.charStream())
                        var line = bufferedReader.readLine()
                        var index = 0
                        val sb = StringBuilder()
                        while (line != null) {
                            val msg = convert(line, "1", index++)
                            if (msg != null) {
                                sb.append(msg.content)
                                callback.onCallBack(sb.toString(), false)
                            }
                            line = bufferedReader.readLine()
                        }
                        callback.onCallBack(sb.toString(), true)
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                    ToastUtils.showLong("网络请求出错 请检查配置")
                }
            }
        })
    }

    fun convert(answer: String, questionId: String, index: Int): Message? {
        val msg = Message()
        msg.content = ""
        msg.messageType = "normal"
        msg.id = questionId
        if ("data: [DONE]" != answer) {
            val beanStr = answer.replaceFirst("data: ", "", false)
            val aiAnswer = GsonUtils.fromJson(beanStr, StreamAiAnswer::class.java) ?: return null
            val choices = aiAnswer.choices
            if (choices.isEmpty()) {
                return null
            }
            val stringBuffer = StringBuffer()
            for (choice in choices) {
                if (choice.finish_reason != "stop") {
                    if (choice.delta.content != null) {
                        stringBuffer.append(choice.delta.content)
                    } else {
                        return null
                    }
                }
            }
            msg.content = stringBuffer.toString()
            if (index == 0) {
                if (msg.content == "\n\n") {
                    LogUtils.e("发现开头有两次换行,移除两次换行")
                    return null
                }
            }
        } else {
            msg.type = "stop"
        }
        msg.index = index
        return msg
    }

    interface CallBack {
        fun onCallBack(result: String, isLast: Boolean)
    }
}

// 使用方法
HttpUtil.chat(result, object : HttpUtil.CallBack {
    override fun onCallBack(result: String, isLast: Boolean) {
        runOnUiThread {
        	//更新数据或UI
            updateData()
        }
    }
})

总结

至此,你应该已经完成了Chat机器人智能问答对接,一个智能QA机器人就实现了,后续我会继续进行AI能力的扩展,如多模态等。喜欢本文可以给开源项目一个 star~有问题可以留言或私信我。

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
使用OkHttp发送流式接口请求,可以使用RequestBody来处理请求体,并使用流式的方式进行数据传输。以下是一个示例: ```java import okhttp3.*; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class StreamRequestExample { public static void main(String[] args) { OkHttpClient client = new OkHttpClient(); // 创建请求体 RequestBody requestBody = new RequestBody() { @Override public MediaType contentType() { return MediaType.parse("application/octet-stream"); } @Override public void writeTo(BufferedSink bufferedSink) throws IOException { // 打开文件输入流 InputStream inputStream = new FileInputStream(new File("path/to/file")); try { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { bufferedSink.write(buffer, 0, bytesRead); } } finally { inputStream.close(); } } }; // 创建请求 Request request = new Request.Builder() .url("http://example.com/streaming-endpoint") .post(requestBody) .build(); try (Response response = client.newCall(request).execute()) { // 处理响应... if (response.isSuccessful()) { // 请求成功 System.out.println("请求成功"); } else { // 请求失败 System.out.println("请求失败"); } } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们创建了一个OkHttpClient实例,并使用自定义的RequestBody来处理请求体。在RequestBody的writeTo方法中,我们打开了文件输入流,然后使用BufferedSink将文件内容写入请求体。 然后,我们使用Request.Builder构建请求,并设置请求方法为POST,并将RequestBody设置为请求体。最后,我们使用OkHttpClient发送请求,并获取响应。你可以根据实际情况进行适当的错误处理和异常处理。 请注意,这只是一个简单的示例,实际使用时可能需要根据你的需求进行适当的调整和错误处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gallonyin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值