azure openai chat api sometimes response gibberish (azure openai 聊天接口偶尔返回乱码)

songxin li 20 Reputation points
2023-06-28T08:56:21.7966667+00:00

QQ截图20230628110423

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
4,081 questions
{count} votes

1 answer

Sort by: Most helpful
  1. hao languang 0 Reputation points
    2023-07-27T03:23:00.2566667+00:00

    我也遇到了这个问题,当 stream: true 的时候,openai实际上使用了SSE的方式返回了数据。遇到乱码的原因是因为读取SSE的返回结果没有使用 \n 作为结束,而是固定读取一定的长度,如果这个长度正好读取到某个UTF-8字符的一半,就会导致乱码的产生。具体可以参考:https://github.com/haibbo/cf-openai-azure-proxy/issues/6

    如果你跟我一样使用Ktor作为客户端,使用http方式调用api,那么可以按照下面的方法解决这个问题

    val channel: ByteReadChannel = httpResponse.body()
    
    while (!channel.isClosedForRead) {    
        val line = channel.readUTF8Line() ?: return@execute // 这一行保证每次读取一个完整的sse响应    
        log.debug("receive: {}", line)    
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.