Chat对话API 流式异常返回 流式异常分为两种: 1. 如果在流式请求接收处理之前发生了异常,如鉴权、参数校验等问题,与普通的非流式一样返回http code,并带有error结构。 2. 如果在流式请求已经接收,会先对外返回流式请求连接建立的信息,此时http code为200,而在后续模型流式返回过程中发生了异常,会在流失返回的chunk返回error结构,并终止当前的流式请求。 流式请求建立后的异常返回示例 plaintext {"id":"chatcmpl123","object":"chat.completion.chunk","created":1694268190,"model":"xxxchat", "choices":[{"index":0,"delta":{"role":"assistant"},"finishreason":null}]} {"id":"chatcmpl123","object":"chat.completion.chunk","created":1694268190,"model":"xxxchat", "choices":[{"index":0,"delta":{"content":"Hello"},"finishreason":null}]} .... {"error":{"code":"500001","type":"INVOKEMODELERROR","message":"服务接口异常,请联系管理员"}} 请求示例代码 plaintext 假设平台用户组AppKey884c8fc4054548a7b1ca1123592f5b7,模型ID96dcaaaaaaaaaaaa5ff55ea377831a,以此为例进行说明。 curl方式请求 plaintext curl request POST url header 'Accept: /' header 'AcceptEncoding: gzip, deflate, br' header 'Authorization: Bearer 884c8fc4054548a7b1ca1123592f5b7' header 'ContentType: application/json' data '{ "model": "96dcaaaaaaaaaaaa5ff55ea377831a", "messages": [ { "role": "user", "content": "Hello" } ] }' python方式请求 plaintext import json import requests URL " headers { "Authorization": "Bearer 884c8fc4054548a7b1ca1123592f5b7", "ContentType": "application/json" } data { "model": "96dcaaaaaaaaaaaa5ff55ea377831a", "messages": [ {"role": "user", "content": "Hello"} ], "stream": True } try: response requests.post(URL, headersheaders, jsondata, streamTrue) if response.statuscode ! 200: print(response.text) else for line in response.iterlines(chunksize8192, decodeunicodeTrue):