@lobehub/chat 0.40.0 → 0.40.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/package.json +1 -1
- package/src/pages/api/error.ts +1 -0
- package/src/pages/api/openai.ts +25 -10
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
# Changelog
|
|
4
4
|
|
|
5
|
+
### [Version 0.40.1](https://github.com/lobehub/lobe-chat/compare/v0.40.0...v0.40.1)
|
|
6
|
+
|
|
7
|
+
<sup>Released on **2023-08-06**</sup>
|
|
8
|
+
|
|
9
|
+
#### 🐛 Bug Fixes
|
|
10
|
+
|
|
11
|
+
- **misc**: 优化 openai 接口的错误处理逻辑.
|
|
12
|
+
|
|
13
|
+
<br/>
|
|
14
|
+
|
|
15
|
+
<details>
|
|
16
|
+
<summary><kbd>Improvements and Fixes</kbd></summary>
|
|
17
|
+
|
|
18
|
+
#### What's fixed
|
|
19
|
+
|
|
20
|
+
- **misc**: 优化 openai 接口的错误处理逻辑 ([eae78fe](https://github.com/lobehub/lobe-chat/commit/eae78fe))
|
|
21
|
+
|
|
22
|
+
</details>
|
|
23
|
+
|
|
24
|
+
<div align="right">
|
|
25
|
+
|
|
26
|
+
[](#readme-top)
|
|
27
|
+
|
|
28
|
+
</div>
|
|
29
|
+
|
|
5
30
|
## [Version 0.40.0](https://github.com/lobehub/lobe-chat/compare/v0.39.4...v0.40.0)
|
|
6
31
|
|
|
7
32
|
<sup>Released on **2023-08-05**</sup>
|
package/package.json
CHANGED
package/src/pages/api/error.ts
CHANGED
package/src/pages/api/openai.ts
CHANGED
|
@@ -65,20 +65,35 @@ export const createChatCompletion = async ({
|
|
|
65
65
|
|
|
66
66
|
const requestParams = { functions, messages: formatMessages, stream: true, ...params };
|
|
67
67
|
|
|
68
|
+
let response: Response;
|
|
69
|
+
|
|
68
70
|
try {
|
|
69
|
-
|
|
71
|
+
response = await openai.createChatCompletion(requestParams);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
// 如果 await 超时报错,说明是 OpenAI 服务端的问题
|
|
74
|
+
return createErrorResponse(ErrorType.GatewayTimeout, { message: error });
|
|
75
|
+
}
|
|
70
76
|
|
|
71
|
-
|
|
72
|
-
|
|
77
|
+
// ============ 4. 处理异常响应 ============ //
|
|
78
|
+
if (!response.ok) {
|
|
79
|
+
let error;
|
|
73
80
|
|
|
74
|
-
|
|
75
|
-
|
|
81
|
+
try {
|
|
82
|
+
// 正常情况下应该是 OpenAI 的 JSON
|
|
83
|
+
error = await response.clone().json();
|
|
84
|
+
} catch {
|
|
85
|
+
// 如果不是 JSON,那么可能是其他接口端的响应,读 text 为结果
|
|
86
|
+
const result = await response.text();
|
|
76
87
|
|
|
77
|
-
|
|
88
|
+
error = { message: result };
|
|
89
|
+
}
|
|
78
90
|
|
|
79
|
-
return
|
|
80
|
-
} catch (error) {
|
|
81
|
-
// 如果 await 超时报错,说明是 OpenAI 服务端的问题
|
|
82
|
-
return createErrorResponse(ErrorType.GatewayTimeout, { message: error });
|
|
91
|
+
return createErrorResponse(ErrorType.OpenAIBizError, { ...error, endpoint });
|
|
83
92
|
}
|
|
93
|
+
|
|
94
|
+
// ============ 5. 发送正常相应 ============ //
|
|
95
|
+
|
|
96
|
+
const stream = OpenAIStream(response, callbacks?.(requestParams));
|
|
97
|
+
|
|
98
|
+
return new StreamingTextResponse(stream);
|
|
84
99
|
};
|