Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement token counting on server side (βœ“ Sandbox Passed) #20

Open
wants to merge 7 commits into
base: endless
Choose a base branch
from

Conversation

sweep-ai[bot]
Copy link

@sweep-ai sweep-ai bot commented Dec 2, 2023

PR Feedback: πŸ‘Ž

Description

This pull request implements token counting on the server side.

Summary

  • Updated src/pages/api/generate.ts to include token counting logic.
  • Added src/utils/tiktoken-server.ts to handle token counting and initialization of Tiktoken.

Fixes #17.


πŸŽ‰ Latest improvements to Sweep:

  • Sweep uses OpenAI's latest Assistant API to plan code changes and modify code! This is 3x faster and significantly more reliable as it allows Sweep to edit code and validate the changes in tight iterations, the same way as a human would.
  • Sweep now uses the 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 get Sweep to edit this pull request, you can:

  • Comment below, and Sweep can edit the entire PR
  • Comment on a file, Sweep will only modify the commented file
  • Edit the original issue to get Sweep to recreate the PR from scratch

sweep-ai bot added 3 commits November 30, 2023 18:37
---------

Co-authored-by: sweep-ai[bot] <128439645+sweep-ai[bot]@users.noreply.github.com>
Copy link
Author

sweep-ai bot commented Dec 2, 2023

Sandbox Executions

  • 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: βœ“

  • 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: βœ“

Copy link

vercel bot commented Dec 2, 2023

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
44444444444 ❌ Failed (Inspect) Dec 23, 2023 2:11pm
emphasize ❌ Failed (Inspect) Dec 23, 2023 2:11pm
endless-chat ❌ Failed (Inspect) Dec 23, 2023 2:11pm
free-chat ❌ Failed (Inspect) Dec 23, 2023 2:11pm
free-chat-personal ❌ Failed (Inspect) Dec 23, 2023 2:11pm

Copy link
Author

sweep-ai bot commented Dec 2, 2023

Apply Sweep Rules to your PR?

  • Apply: All new business logic should have corresponding unit tests.
  • Apply: Refactor large functions to be more modular.
  • Apply: Add docstrings to all functions and file headers.

Copy link

senior-dev-bot bot commented Dec 2, 2023

Hi there! πŸ‘‹ Thanks for opening a PR. πŸŽ‰ To get the most out of Senior Dev, please sign up in our Web App, connect your GitHub account, and add/join your organization CNSeniorious000. After that, you will receive code reviews beginning on your next opened PR. πŸš€

Copy link

netlify bot commented Dec 2, 2023

❌ Deploy Preview for endless-chat failed.

Name Link
πŸ”¨ Latest commit 44b8b8a
πŸ” Latest deploy log https://app.netlify.com/sites/endless-chat/deploys/6580a67726c50f00089cd7fb

Copy link

Pull Request Report

Hey there! I've created a report for the pull request based on the commit history. Let's dive in!

Changes

  1. Updated src/pages/api/generate.ts:
    • Added import for countTokensServer from @/utils/tiktoken-server.
    • Added minMessages and maxTokens constants.
    • Implemented token counting and trimming logic for messages.
    • Updated initOptions with trimmed messages.
    • Added error handling for token counting and trimming logic.

Suggestions

  • In src/pages/api/generate.ts, consider extracting the token counting and trimming logic into a separate function for better readability and maintainability. Here's a suggested refactoring:
// Add this function at the top of the file
const trimMessages = (messages: ChatMessage[], maxTokens: number, minMessages: number) => {
  let trimmedMessages = [...messages];
  let tokenCount = countTokensServer(null, trimmedMessages).total;
  while (tokenCount > maxTokens && trimmedMessages.length > minMessages) {
    trimmedMessages.shift();
    tokenCount = countTokensServer(null, trimmedMessages).total;
  }
  return trimmedMessages;
};

// Update the post function
export const post: APIRoute = async ({ request }) => {
  // ...
  const trimmedMessages = trimMessages(messages, maxTokens, minMessages);
  // ...
};

Bugs

  • No bugs found.

Improvements

  • No improvements found.

Rating

I would rate the code a 7 out of 10. The code is generally readable and performs well. However, there are a few areas that could be refactored for better readability, such as extracting the token counting and trimming logic into a separate function. Overall, the code seems to be secure and error handling is in place.

That's it for the pull request report! Let me know if you need any further assistance. Have a great day! πŸ˜„

Copy link

netlify bot commented Dec 2, 2023

❌ Deploy Preview for chat-for-free failed.

Name Link
πŸ”¨ Latest commit 44b8b8a
πŸ” Latest deploy log https://app.netlify.com/sites/chat-for-free/deploys/6580a677ca656500085be314

Copy link
Author

@sweep-ai sweep-ai bot Dec 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ Failed to make changes

Please join our Discord to report this issue.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bugs

ζ­£εˆ™θ‘¨θΎΎεΌζœͺε…³ι—­

Other Suggestions

  • In src/pages/api/generate.ts, consider extracting the token counting and trimming logic into a separate function for better readability and maintainability. Here's a suggested refactoring:
// Add this function at the top of the file
const trimMessages = (messages: ChatMessage[], maxTokens: number, minMessages: number) => {
  let trimmedMessages = [...messages];
  let tokenCount = countTokensServer(null, trimmedMessages).total;
  while (tokenCount > maxTokens && trimmedMessages.length > minMessages) {
    trimmedMessages.shift();
    tokenCount = countTokensServer(null, trimmedMessages).total;
  }
  return trimmedMessages;
};

// Update the post function
export const post: APIRoute = async ({ request }) => {
  // ...
  const trimmedMessages = trimMessages(messages, maxTokens, minMessages);
  // ...
};

Copy link
Author

@sweep-ai sweep-ai bot Dec 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ Failed to make changes

Please join our Discord to report this issue.

Copy link
Author

@sweep-ai sweep-ai bot Dec 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ Failed to make changes

Please join our Discord to report this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sweep(slow): count tokens on the server side
1 participant