refactor: 使用 xsai 替换 openai sdk 以减小包体积#1024
Open
Hoshino-Yumetsuki wants to merge 3 commits into
Open
Conversation
Contributor
Reviewer's guide (collapsed on small PRs)审阅者指南重构 Twikoo 的垃圾信息检查逻辑,改为使用轻量级 xsai SDK 替代 OpenAI SDK,并相应更新客户端创建、LLM 调用、响应处理以及依赖项。 使用 xsai 进行更新后 LLM 垃圾信息检查的序列图sequenceDiagram
participant SpamChecker as checkByLLM
participant XSClient as openai
participant XSGenerate as generateText
SpamChecker->>SpamChecker: buildMessages(comment, lastError)
SpamChecker->>XSClient: chat(config.LLM_MODEL || deepseek-chat)
XSClient-->>SpamChecker: model
SpamChecker->>XSGenerate: generateText({ model, responseFormat, messages })
XSGenerate-->>SpamChecker: chatCompletion.text
SpamChecker->>SpamChecker: extractJson(rawText)
SpamChecker->>SpamChecker: repairJson(extracted)
文件级变更
Tips and commands与 Sourcery 互动
自定义你的体验访问你的控制面板来:
获取帮助Original review guide in EnglishReviewer's guide (collapsed on small PRs)Reviewer's GuideRefactors the Twikoo spam-checking logic to use the lightweight xsai SDK instead of the OpenAI SDK, updating client creation, LLM invocation, response handling, and dependencies accordingly. Sequence diagram for updated LLM spam check using xsaisequenceDiagram
participant SpamChecker as checkByLLM
participant XSClient as openai
participant XSGenerate as generateText
SpamChecker->>SpamChecker: buildMessages(comment, lastError)
SpamChecker->>XSClient: chat(config.LLM_MODEL || deepseek-chat)
XSClient-->>SpamChecker: model
SpamChecker->>XSGenerate: generateText({ model, responseFormat, messages })
XSGenerate-->>SpamChecker: chatCompletion.text
SpamChecker->>SpamChecker: extractJson(rawText)
SpamChecker->>SpamChecker: repairJson(extracted)
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - 我发现了两个问题,并提供了一些整体反馈:
- 在
checkByLLM中,每次调用函数时都会在函数内部require一次generateText;建议将require('xsai')移到模块作用域,避免重复加载模块,并让代码风格与其他导入保持一致。 - 重构把模型标识从
deepseek-v4-pro改成了deepseek-chat,并且把 base URL 改成包含/v1;请仔细确认这些值是否是有意为之,并且与现有的配置键和支持的端点保持一致,以避免出现意外行为。 - 新的
generateText调用使用了responseFormat并期望返回chatCompletion.text,这和之前 OpenAI SDK 的响应结构不同;请确认这是否符合实际的xsai响应结构和可用选项,如果返回格式与预期不符,考虑增加错误/边缘情况处理。
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `checkByLLM`, `generateText` is required inside the function on each call; consider moving the `require('xsai')` to the module scope to avoid repeated module loading and make the code more consistent with other imports.
- The refactor changes the model identifier from `deepseek-v4-pro` to `deepseek-chat` and alters the base URL to include `/v1`; double-check that these values are intentional and aligned with the existing configuration keys and supported endpoints to avoid unexpected behavior.
- The new `generateText` call uses `responseFormat` and expects `chatCompletion.text`, which differs from the previous OpenAI SDK response structure; verify that this matches the actual `xsai` response shape and options, and consider handling error/edge cases if the returned format is not as expected.
## Individual Comments
### Comment 1
<location path="src/server/function/twikoo/utils/spam.js" line_range="146-149" />
<code_context>
- model: config.LLM_MODEL || 'deepseek-v4-pro',
- response_format: { type: 'json_object' },
+ const { generateText } = require('xsai')
+ const chatCompletion = await generateText({
+ model: openai.chat(config.LLM_MODEL || 'deepseek-chat'),
+ responseFormat: { type: 'json_object' },
messages
})
</code_context>
<issue_to_address>
**issue (bug_risk):** `generateText` 的 `model` 参数看起来被错误指定了,可能会导致运行时失败。
这里你传入的是 `model: openai.chat(config.LLM_MODEL || 'deepseek-chat')`。`model` 选项应该是一个模型标识字符串(例如 `config.LLM_MODEL || 'deepseek-chat'`),而不是 `openai.chat(...)` 的结果,后者很可能是一个函数或 Promise。请直接传入模型名称,并依赖通过 `getOpenAIClient` 配置好的客户端来完成调用。
</issue_to_address>
### Comment 2
<location path="src/server/function/twikoo/utils/lib.js" line_range="75-77" />
<code_context>
return xml2js
},
getOpenAIClient (config) {
- const OpenAI = require('openai') // OpenAI 的 SDK,用于反垃圾
- const openaiClient = new OpenAI({
+ const { createXSClient } = require('xsai') // xsai SDK
+ const openaiClient = createXSClient({
apiKey: config.LLM_API_KEY,
- baseURL: config.LLM_API_ENDPOINT || 'https://api.deepseek.com'
</code_context>
<issue_to_address>
**suggestion:** 辅助函数名 `getOpenAIClient` 现在已经与底层的 xsai 客户端不匹配,可能会造成困惑。
这个函数现在通过 `createXSClient` 实例化的是 xsai 客户端,但函数名(`getOpenAIClient`)和变量名(`openaiClient`)仍然引用 OpenAI。请将它们重命名以体现 xsai(或者使用更通用的名字,如 `getLLMClient`),以保持抽象层清晰并减少混淆。
Suggested implementation:
```javascript
getLLMClient (config) {
const { createXSClient } = require('xsai') // xsai SDK
const llmClient = createXSClient({
apiKey: config.LLM_API_KEY,
baseURL: config.LLM_API_ENDPOINT || 'https://api.deepseek.com/v1'
})
return llmClient
}
```
1. 将代码库中所有对 `getOpenAIClient(...)` 的调用更新为 `getLLMClient(...)`。
2. 如果有任何类型定义、文档或注释引用了 `getOpenAIClient` 或 `openaiClient`,也请统一重命名为 `getLLMClient` / `llmClient`,保持命名一致。帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进之后的评审。
Original comment in English
Hey - I've found 2 issues, and left some high level feedback:
- In
checkByLLM,generateTextis required inside the function on each call; consider moving therequire('xsai')to the module scope to avoid repeated module loading and make the code more consistent with other imports. - The refactor changes the model identifier from
deepseek-v4-protodeepseek-chatand alters the base URL to include/v1; double-check that these values are intentional and aligned with the existing configuration keys and supported endpoints to avoid unexpected behavior. - The new
generateTextcall usesresponseFormatand expectschatCompletion.text, which differs from the previous OpenAI SDK response structure; verify that this matches the actualxsairesponse shape and options, and consider handling error/edge cases if the returned format is not as expected.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `checkByLLM`, `generateText` is required inside the function on each call; consider moving the `require('xsai')` to the module scope to avoid repeated module loading and make the code more consistent with other imports.
- The refactor changes the model identifier from `deepseek-v4-pro` to `deepseek-chat` and alters the base URL to include `/v1`; double-check that these values are intentional and aligned with the existing configuration keys and supported endpoints to avoid unexpected behavior.
- The new `generateText` call uses `responseFormat` and expects `chatCompletion.text`, which differs from the previous OpenAI SDK response structure; verify that this matches the actual `xsai` response shape and options, and consider handling error/edge cases if the returned format is not as expected.
## Individual Comments
### Comment 1
<location path="src/server/function/twikoo/utils/spam.js" line_range="146-149" />
<code_context>
- model: config.LLM_MODEL || 'deepseek-v4-pro',
- response_format: { type: 'json_object' },
+ const { generateText } = require('xsai')
+ const chatCompletion = await generateText({
+ model: openai.chat(config.LLM_MODEL || 'deepseek-chat'),
+ responseFormat: { type: 'json_object' },
messages
})
</code_context>
<issue_to_address>
**issue (bug_risk):** The `model` argument to `generateText` looks mis-specified and may cause runtime failures.
Here you’re passing `model: openai.chat(config.LLM_MODEL || 'deepseek-chat')`. The `model` option should be a model identifier string (e.g. `config.LLM_MODEL || 'deepseek-chat'`), not the result of `openai.chat(...)`, which is likely a function or Promise. Please pass the model name directly and rely on the client configured via `getOpenAIClient` instead.
</issue_to_address>
### Comment 2
<location path="src/server/function/twikoo/utils/lib.js" line_range="75-77" />
<code_context>
return xml2js
},
getOpenAIClient (config) {
- const OpenAI = require('openai') // OpenAI 的 SDK,用于反垃圾
- const openaiClient = new OpenAI({
+ const { createXSClient } = require('xsai') // xsai SDK
+ const openaiClient = createXSClient({
apiKey: config.LLM_API_KEY,
- baseURL: config.LLM_API_ENDPOINT || 'https://api.deepseek.com'
</code_context>
<issue_to_address>
**suggestion:** The helper name `getOpenAIClient` no longer matches the underlying xsai client, which could cause confusion.
This function now instantiates an xsai client via `createXSClient`, but both the function (`getOpenAIClient`) and variable (`openaiClient`) names still reference OpenAI. Please rename them to reflect xsai (or use a generic name like `getLLMClient`) to keep the abstraction clear and reduce confusion.
Suggested implementation:
```javascript
getLLMClient (config) {
const { createXSClient } = require('xsai') // xsai SDK
const llmClient = createXSClient({
apiKey: config.LLM_API_KEY,
baseURL: config.LLM_API_ENDPOINT || 'https://api.deepseek.com/v1'
})
return llmClient
}
```
1. Update all call sites of `getOpenAIClient(...)` in the codebase to use `getLLMClient(...)` instead.
2. If there are any type definitions, documentation, or comments referencing `getOpenAIClient` or `openaiClient`, rename them to `getLLMClient` / `llmClient` to keep naming consistent.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary by Sourcery
在 Twikoo 垃圾检测中将 OpenAI SDK 的使用替换为 xsai,以减小依赖体积,并相应调整 LLM 客户端配置。
增强内容:
generateText,并更新响应格式的处理逻辑。createXSClient,并修订 DeepSeek API 的基础 URL。package.json中用 xsai 替代 OpenAI 依赖,以精简整体包体积。Original summary in English
Summary by Sourcery
Replace OpenAI SDK usage in Twikoo spam detection with xsai to reduce dependency size and adjust LLM client configuration accordingly.
Enhancements: