-
Notifications
You must be signed in to change notification settings - Fork 54
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
Server-side token counting and message trimming #19
Conversation
Sandbox Executions |
Apply Sweep Rules to your PR?
|
❌ Deploy Preview for endless-chat failed.
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
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. 🚀 |
Pull Request ReportHey there! 👋 Here's a report on the changes made in the Changes
Suggestions to Improve Code
Bugs
Improvements
RatingI would rate the code a 7 out of 10 based on the following criteria:
Keep up the good work! If you have any questions or need further assistance, feel free to ask. 😄 Code DiffsDiff-1- import { ProxyAgent, fetch } from 'undici'
+ import { ProxyAgent, fetch } from 'undici' Diff-2- import { generatePayload, parseOpenAIStream } from '@/utils/openAI'
+ import { generatePayload, parseOpenAIStream } from '@/utils/openAI' Diff-3- import { verifySignature } from '@/utils/auth'
+ import { verifySignature } from '@/utils/auth' Diff-4- import type { APIRoute } from 'astro'
+ import type { APIRoute } from 'astro' Diff-5- const apiKey = import.meta.env.OPENAI_API_KEY
+ const apiKey = import.meta.env.OPENAI_API_KEY Diff-6- const httpsProxy = import.meta.env.HTTPS_PROXY
+ const httpsProxy = import.meta.env.HTTPS_PROXY Diff-7- const baseUrl = ((import.meta.env.OPENAI_API_BASE_URL) || 'https://api.openai.com').trim().replace(/\/$/, '')
+ const baseUrl = ((import.meta.env.OPENAI_API_BASE_URL) || 'https://api.openai.com').trim().replace(/\/$/, '') Diff-8- const sitePassword = import.meta.env.SITE_PASSWORD
+ const sitePassword = import.meta.env.SITE_PASSWORD Diff-9+ const ua = import.meta.env.UNDICI_UA Diff-10+ if (!messages) {
+ return new Response(JSON.stringify({
+ error: {
+ message: 'No input text.',
+ },
+ }), { status: 400 })
+ } Diff-11+ if (sitePassword && sitePassword !== pass) {
+ return new Response(JSON.stringify({
+ error: {
+ message: 'Invalid password.',
+ },
+ }), { status: 401 })
+ } Diff-12+ if (import.meta.env.PROD && !await verifySignature({ t: time, m: messages?.[messages.length - 1]?.content || '' }, sign)) {
+ return new Response(JSON.stringify({
+ error: {
+ message: 'Invalid signature.',
+ },
+ }), { status: 401 })
+ } Diff-13+ const encoder = new Encoder('en'); Diff-14+ const minMessages = 5;
+ const maxTokens = 1024; Diff-15+ function countTokens(message) {
+ return encoder.encode(message).length;
+ } Diff-16+ let totalTokens = messages.reduce((sum, msg) => sum + countTokens(msg.content), 0);
+ while (totalTokens > maxTokens && messages.length > minMessages) {
+ totalTokens -= countTokens(messages[0].content);
+ messages.shift();
+ } Diff-17+ const initOptions = generatePayload(request.headers.get('Authorization') ?? `Bearer ${apiKey}`, messages, model); Diff-18+ if (baseUrl) request.headers.forEach((val, key) => (FORWARD_HEADERS.includes(key) || key.startsWith('sec-') || key |
❌ Deploy Preview for chat-for-free failed.
|
PR Feedback (click)
Description
This pull request includes changes to the
generate.ts
file in thesrc/pages/api
directory. The changes involve adding server-side token counting and message trimming logic to the existing code.Summary
ProxyAgent
andfetch
from theundici
library.generatePayload
andparseOpenAIStream
functions from the@/utils/openAI
module.verifySignature
function from the@/utils/auth
module.APIRoute
type from theastro
module.apiKey
,httpsProxy
,baseUrl
,sitePassword
, andua
using values fromimport.meta.env
.FORWARD_HEADERS
containing headers to be forwarded.post
function that handles the API request.messages
field and returned an error response if it is empty.encoder
instance from thetiktoken-js
library.minMessages
andmaxTokens
variables for message truncation.countTokens
function to count the tokens in a message using theencoder
instance.maxTokens
limit.initOptions
variable with the newmessages
array.headers
object with the appropriate values.baseUrl
and forwarded specific headers if it exists.ua
header if it is defined.httpsProxy
and updated theinitOptions
object with a newdispatcher
property if it exists.Fixes #17.
🎉 Latest improvements to Sweep:
rope
library to refactor Python! Check out Large Language Models are Bad at Refactoring Code. To have Sweep refactor your code, trysweep: Refactor <your_file>.py
!💡 To get Sweep to edit this pull request, you can: