@lobehub/chat 0.69.0 → 0.69.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/.eslintrc.js CHANGED
@@ -12,5 +12,6 @@ config.rules['unicorn/prefer-code-point'] = 0;
12
12
  config.rules['no-extra-boolean-cast'] = 0;
13
13
  config.rules['unicorn/no-useless-undefined'] = 0;
14
14
  config.rules['react/no-unknown-property'] = 0;
15
+ config.rules['unicorn/prefer-ternary'] = 0;
15
16
 
16
17
  module.exports = config;
package/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
 
3
3
  # Changelog
4
4
 
5
+ ### [Version 0.69.1](https://github.com/lobehub/lobe-chat/compare/v0.69.0...v0.69.1)
6
+
7
+ <sup>Released on **2023-09-06**</sup>
8
+
9
+ #### ♻ Code Refactoring
10
+
11
+ - **misc**: Mirgrate openai-edge to openai.
12
+
13
+ <br/>
14
+
15
+ <details>
16
+ <summary><kbd>Improvements and Fixes</kbd></summary>
17
+
18
+ #### Code refactoring
19
+
20
+ - **misc**: Mirgrate openai-edge to openai, closes [#145](https://github.com/lobehub/lobe-chat/issues/145) ([75ee574](https://github.com/lobehub/lobe-chat/commit/75ee574))
21
+
22
+ </details>
23
+
24
+ <div align="right">
25
+
26
+ [![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)
27
+
28
+ </div>
29
+
5
30
  ## [Version 0.69.0](https://github.com/lobehub/lobe-chat/compare/v0.68.1...v0.69.0)
6
31
 
7
32
  <sup>Released on **2023-09-06**</sup>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/chat",
3
- "version": "0.69.0",
3
+ "version": "0.69.1",
4
4
  "description": "Lobe Chat is an open-source chatbot client using LangChain, Typescript and Next.js",
5
5
  "keywords": [
6
6
  "chatbot",
@@ -66,7 +66,7 @@
66
66
  "@emoji-mart/data": "^1",
67
67
  "@emoji-mart/react": "^1",
68
68
  "@icons-pack/react-simple-icons": "^9",
69
- "@lobehub/chat-plugin-sdk": "^1.15.1",
69
+ "@lobehub/chat-plugin-sdk": "^1.17.0",
70
70
  "@lobehub/chat-plugins-gateway": "^1.5.0",
71
71
  "@lobehub/ui": "latest",
72
72
  "@vercel/analytics": "^1",
@@ -85,7 +85,7 @@
85
85
  "lucide-react": "latest",
86
86
  "nanoid": "^4",
87
87
  "next": "13.4.7",
88
- "openai-edge": "^1",
88
+ "openai": "^4.4.0",
89
89
  "polished": "^4",
90
90
  "react": "^18",
91
91
  "react-dom": "^18",
@@ -1,5 +1,5 @@
1
- import { OpenAIStream, OpenAIStreamCallbacks, StreamingTextResponse } from 'ai';
2
- import { Configuration, OpenAIApi } from 'openai-edge';
1
+ import { OpenAIStream, StreamingTextResponse } from 'ai';
2
+ import OpenAI, { ClientOptions } from 'openai';
3
3
 
4
4
  import { getServerConfig } from '@/config/server';
5
5
  import { createErrorResponse } from '@/pages/api/error';
@@ -10,25 +10,29 @@ import { OpenAIStreamPayload } from '@/types/openai';
10
10
  export const createOpenAI = (userApiKey: string | null, endpoint?: string | null) => {
11
11
  const { OPENAI_API_KEY, OPENAI_PROXY_URL } = getServerConfig();
12
12
 
13
- const config = new Configuration({
13
+ const baseURL = endpoint ? endpoint : OPENAI_PROXY_URL ? OPENAI_PROXY_URL : undefined;
14
+
15
+ const config: ClientOptions = {
14
16
  apiKey: !userApiKey ? OPENAI_API_KEY : userApiKey,
15
- });
17
+ };
16
18
 
17
- const basePath = endpoint ? endpoint : OPENAI_PROXY_URL ? OPENAI_PROXY_URL : undefined;
19
+ // a bug with openai: https://github.com/openai/openai-node/issues/283
20
+ // TODO: should refactor when openai fix the bug
21
+ if (baseURL) {
22
+ config.baseURL = baseURL;
23
+ }
18
24
 
19
- return new OpenAIApi(config, basePath);
25
+ return new OpenAI(config);
20
26
  };
21
27
 
22
28
  interface CreateChatCompletionOptions {
23
29
  OPENAI_API_KEY: string | null;
24
- callbacks?: (payload: OpenAIStreamPayload) => OpenAIStreamCallbacks;
25
30
  endpoint?: string | null;
26
31
  payload: OpenAIStreamPayload;
27
32
  }
28
33
 
29
34
  export const createChatCompletion = async ({
30
35
  payload,
31
- callbacks,
32
36
  OPENAI_API_KEY,
33
37
  endpoint,
34
38
  }: CreateChatCompletionOptions) => {
@@ -47,37 +51,30 @@ export const createChatCompletion = async ({
47
51
 
48
52
  // ============ 2. 发送请求 ============ //
49
53
 
50
- const requestParams = { messages: formatMessages, stream: true, ...params };
51
-
52
- let response: Response;
53
-
54
54
  try {
55
- response = await openai.createChatCompletion(requestParams);
55
+ const response = await openai.chat.completions.create({
56
+ messages: formatMessages,
57
+ ...params,
58
+ stream: true,
59
+ });
60
+ const stream = OpenAIStream(response);
61
+ return new StreamingTextResponse(stream);
56
62
  } catch (error) {
57
- // 如果 await 超时报错,说明是 OpenAI 服务端的问题
58
- return createErrorResponse(ChatErrorType.GatewayTimeout, { message: error });
59
- }
60
-
61
- // ============ 4. 处理异常响应 ============ //
62
- if (!response.ok) {
63
- let error;
64
-
65
- try {
66
- // 正常情况下应该是 OpenAI 的 JSON
67
- error = await response.clone().json();
68
- } catch {
69
- // 如果不是 JSON,那么可能是其他接口端的响应,读 text 为结果
70
- const result = await response.text();
71
-
72
- error = { message: result };
63
+ // Check if the error is an OpenAI APIError
64
+ if (error instanceof OpenAI.APIError) {
65
+ return createErrorResponse(ChatErrorType.OpenAIBizError, {
66
+ endpoint: !!endpoint ? endpoint : undefined,
67
+ error: error.error ?? error.cause,
68
+ });
73
69
  }
74
70
 
75
- return createErrorResponse(ChatErrorType.OpenAIBizError, { ...error, endpoint });
76
- }
77
-
78
- // ============ 5. 发送正常相应 ============ //
79
-
80
- const stream = OpenAIStream(response, callbacks?.(requestParams));
71
+ // track the error that not an OpenAI APIError
72
+ console.error(error);
81
73
 
82
- return new StreamingTextResponse(stream);
74
+ // return as a GatewayTimeout error
75
+ return createErrorResponse(ChatErrorType.InternalServerError, {
76
+ endpoint,
77
+ error: JSON.stringify(error),
78
+ });
79
+ }
83
80
  };
@@ -1,4 +1,4 @@
1
- import { PluginRenderProps } from '@lobehub/chat-plugin-sdk';
1
+ import { PluginRenderProps } from '@lobehub/chat-plugin-sdk/client';
2
2
  import { Skeleton } from 'antd';
3
3
  import { memo, useEffect, useRef, useState } from 'react';
4
4
 
@@ -1,4 +1,4 @@
1
- import { PluginChannel } from '@lobehub/chat-plugin-sdk';
1
+ import { PluginChannel } from '@lobehub/chat-plugin-sdk/client';
2
2
 
3
3
  export const onPluginReady = (e: MessageEvent, onReady: () => void) => {
4
4
  if (e.data.type === PluginChannel.pluginReadyForRender) {
@@ -1,4 +1,4 @@
1
- import { PluginRender, PluginRenderProps } from '@lobehub/chat-plugin-sdk';
1
+ import { PluginRender, PluginRenderProps } from '@lobehub/chat-plugin-sdk/client';
2
2
  import { Skeleton } from 'antd';
3
3
  import { memo, useEffect, useState } from 'react';
4
4
 
@@ -1,11 +1,10 @@
1
1
  import { merge } from 'lodash-es';
2
- import { ChatCompletionFunctions } from 'openai-edge/types/api';
3
2
 
4
3
  import { LOBE_CHAT_ACCESS_CODE, OPENAI_API_KEY_HEADER_KEY, OPENAI_END_POINT } from '@/const/fetch';
5
4
  import { useGlobalStore } from '@/store/global';
6
5
  import { pluginSelectors, usePluginStore } from '@/store/plugin';
7
6
  import { initialLobeAgentConfig } from '@/store/session/initialState';
8
- import type { OpenAIStreamPayload } from '@/types/openai';
7
+ import type { ChatCompletionFunctions, OpenAIStreamPayload } from '@/types/openai';
9
8
 
10
9
  import { URLS } from './url';
11
10
 
@@ -1,8 +1,8 @@
1
1
  import { uniqBy } from 'lodash-es';
2
- import { ChatCompletionFunctions } from 'openai-edge/types/api';
3
2
 
4
3
  import { PLUGIN_SCHEMA_SEPARATOR } from '@/const/plugin';
5
4
  import { pluginHelpers } from '@/store/plugin/helpers';
5
+ import { ChatCompletionFunctions } from '@/types/openai';
6
6
 
7
7
  import { PluginStoreState } from './initialState';
8
8
 
@@ -68,3 +68,26 @@ export interface OpenAIStreamPayload {
68
68
  */
69
69
  top_p?: number;
70
70
  }
71
+
72
+ export interface ChatCompletionFunctions {
73
+ /**
74
+ * The description of what the function does.
75
+ * @type {string}
76
+ * @memberof ChatCompletionFunctions
77
+ */
78
+ description?: string;
79
+ /**
80
+ * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
81
+ * @type {string}
82
+ * @memberof ChatCompletionFunctions
83
+ */
84
+ name: string;
85
+ /**
86
+ * The parameters the functions accepts, described as a JSON Schema object. See the [guide](/docs/guides/gpt/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format.
87
+ * @type {{ [key: string]: any }}
88
+ * @memberof ChatCompletionFunctions
89
+ */
90
+ parameters?: {
91
+ [key: string]: any;
92
+ };
93
+ }