Open CNSeniorious000 opened 12 months ago
1bfb7247a1
)Here are the sandbox execution logs prior to making any changes:
117c9ef
Checking src/pages/api/generate.ts for syntax errors... ✅ src/pages/api/generate.ts has no syntax errors!
1/1 ✓Checking src/pages/api/generate.ts for syntax errors... ✅ src/pages/api/generate.ts has no syntax errors!
Sandbox passed on the latest endless
, so sandbox checks will be enabled for this issue.
I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.
[X] Create src/utils/tiktoken-server.ts
✓ https://github.com/CNSeniorious000/free-chat/commit/09d72442ebe25ea72693afd406fe601d703d1b27
Create src/utils/tiktoken-server.ts with contents:
• Create a new utility file named `tiktoken-server.ts` in the `src/utils` directory for the server-side token counting logic.
• Use `tiktoken-js` instead of `tiktoken` as the server-side equivalent library.
• Define and export a function `countTokensServer` that implements the same logic as `countTokens` from `src/utils/tiktoken.ts`.
• Ensure the function interface matches that of the `countTokens` presently on the client side, taking an encoder and a list of messages as arguments and returning an object with the total token count.
• Make sure to wrap any initializations that are not available on the server, such as fetching base configurations or initializing WebAssembly modules, in a server-compatible manner.
[X] Running GitHub Actions for src/utils/tiktoken-server.ts
✓
Check src/utils/tiktoken-server.ts with contents:
Ran GitHub Actions for 09d72442ebe25ea72693afd406fe601d703d1b27:
• Vercel Preview Comments: ✓
[X] Modify src/pages/api/generate.ts
✓ https://github.com/CNSeniorious000/free-chat/commit/30a5ea4d0bdc06e092563c96327c3e11eeb3cff2
Modify src/pages/api/generate.ts with contents:
• In the `post` method of the API route, import the `countTokensServer` function from `src/utils/tiktoken-server.ts`.
• After retrieving the request body, apply the token counting logic to trim the `messages` array, ensuring it remains under a defined token limit.
• Use the constants defined in `src/components/Generator.tsx` like `minMessages` and `maxTokens` to set the lower message limit and token count limit. These may need to be moved to a shared constants file if they are not already.
• Ensure that after implementing the logic, the trimmed `messages` are then passed on for the rest of the processing where the generation payload is created.
[X] Running GitHub Actions for src/pages/api/generate.ts
✓
Check src/pages/api/generate.ts with contents:
Ran GitHub Actions for 30a5ea4d0bdc06e092563c96327c3e11eeb3cff2:
• Vercel Preview Comments: ✓
I have finished reviewing the code for completeness. I did not find errors for sweep/server-side-token-counting_1
.
rope
library to refactor Python! Check out Large Language Models are Bad at Refactoring Code. To have Sweep refactor your code, try sweep: Refactor <your_file>.py
!💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request. Join Our Discord
Details
在 src/pages/api/generate.ts 中加上和 src/components/Generator.tsx 中一样的裁剪 messages 的逻辑: 但是注意:服务端用不了 tiktoken 库,只能用 tiktoken-js 库,他们应该有类似的 interface
Checklist
- [X] Create `src/utils/tiktoken-server.ts` ✓ https://github.com/CNSeniorious000/free-chat/commit/09d72442ebe25ea72693afd406fe601d703d1b27 - [X] Running GitHub Actions for `src/utils/tiktoken-server.ts` ✓ - [X] Modify `src/pages/api/generate.ts` ✓ https://github.com/CNSeniorious000/free-chat/commit/30a5ea4d0bdc06e092563c96327c3e11eeb3cff2 - [X] Running GitHub Actions for `src/pages/api/generate.ts` ✓ ![Flowchart](https://raw.githubusercontent.com/CNSeniorious000/free-chat/sweep/assets/af2844339ca9a1866f4f16217294aea5a3ca95949e1b67b7c2151d232b872674_17_flowchart.svg)