Closed sweep-ai[bot] closed 7 months ago
Name | Link |
---|---|
Latest commit | 0b278c6e111249382eb1edf83ce2f19b9e62f9fe |
Latest deploy log | https://app.netlify.com/sites/endless-chat/deploys/6569aa9c6fed690008602cd7 |
The latest updates on your projects. Learn more about Vercel for Git βοΈ
Name | Status | Preview | Comments | Updated (UTC) |
---|---|---|---|---|
44444444444 | β Failed (Inspect) | Dec 1, 2023 9:44am | ||
emphasize | β Failed (Inspect) | Dec 1, 2023 9:44am | ||
endless-chat | β Failed (Inspect) | Dec 1, 2023 9:44am | ||
free-chat | β Failed (Inspect) | Dec 1, 2023 9:44am | ||
free-chat-personal | β Failed (Inspect) | Dec 1, 2023 9:44am |
Hi there! :wave: Thanks for opening a PR. :tada: 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. :rocket:
Hey there! π Here's a report on the changes made in the src/pages/api/generate.ts
file:
ProxyAgent
and fetch
from the undici
library. (View Diff)generatePayload
and parseOpenAIStream
from the @/utils/openAI
module. (View Diff)verifySignature
from the @/utils/auth
module. (View Diff)APIRoute
from the astro
module. (View Diff)apiKey
using the import.meta.env.OPENAI_API_KEY
environment variable. (View Diff)httpsProxy
using the import.meta.env.HTTPS_PROXY
environment variable. (View Diff)baseUrl
using the import.meta.env.OPENAI_API_BASE_URL
environment variable. If the variable is not defined, the default value is set to 'https://api.openai.com'
. (View Diff)sitePassword
using the import.meta.env.SITE_PASSWORD
environment variable. (View Diff)ua
using the import.meta.env.UNDICI_UA
environment variable. (View Diff)messages
property in the request body. If it is not present, a 400 Bad Request
response is returned with an error message. (View Diff)sitePassword
and pass
values. If they are not equal, a 401 Unauthorized
response is returned with an error message. (View Diff)import.meta.env.PROD
flag and the signature verification. If the signature is invalid, a 401 Unauthorized
response is returned with an error message. (View Diff)tiktoken-js
library and obtained an encoder instance for the 'en'
language. (View Diff)minMessages
and maxTokens
variables for message truncation. Adjusted as needed. (View Diff)countTokens
to count the tokens for a message using tiktoken-js
. (View Diff)maxTokens
limit. (View Diff)initOptions
object with the generated payload, headers, and other options. (View Diff)headers
object. (View Diff)user-agent
header if the ua
variable is defined. (View Diff)httpsProxy
variable and set the dispatcher
option to a ProxyAgent
instance if it is defined. (View Diff)fetch
request and returned a 500 Internal Server Error
response with the error details. (View Diff)parseOpenAIStream
as the API response. (View Diff)ua
could be renamed to userAgent
. (View Diff)I 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. π
- import { ProxyAgent, fetch } from 'undici'
+ import { ProxyAgent, fetch } from 'undici'
- import { generatePayload, parseOpenAIStream } from '@/utils/openAI'
+ import { generatePayload, parseOpenAIStream } from '@/utils/openAI'
- import { verifySignature } from '@/utils/auth'
+ import { verifySignature } from '@/utils/auth'
- import type { APIRoute } from 'astro'
+ import type { APIRoute } from 'astro'
- const apiKey = import.meta.env.OPENAI_API_KEY
+ const apiKey = import.meta.env.OPENAI_API_KEY
- const httpsProxy = import.meta.env.HTTPS_PROXY
+ const httpsProxy = import.meta.env.HTTPS_PROXY
- 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(/\/$/, '')
- const sitePassword = import.meta.env.SITE_PASSWORD
+ const sitePassword = import.meta.env.SITE_PASSWORD
+ const ua = import.meta.env.UNDICI_UA
+ if (!messages) {
+ return new Response(JSON.stringify({
+ error: {
+ message: 'No input text.',
+ },
+ }), { status: 400 })
+ }
+ if (sitePassword && sitePassword !== pass) {
+ return new Response(JSON.stringify({
+ error: {
+ message: 'Invalid password.',
+ },
+ }), { status: 401 })
+ }
+ 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 })
+ }
+ const encoder = new Encoder('en');
+ const minMessages = 5;
+ const maxTokens = 1024;
+ function countTokens(message) {
+ return encoder.encode(message).length;
+ }
+ let totalTokens = messages.reduce((sum, msg) => sum + countTokens(msg.content), 0);
+ while (totalTokens > maxTokens && messages.length > minMessages) {
+ totalTokens -= countTokens(messages[0].content);
+ messages.shift();
+ }
+ const initOptions = generatePayload(request.headers.get('Authorization') ?? `Bearer ${apiKey}`, messages, model);
+ if (baseUrl) request.headers.forEach((val, key) => (FORWARD_HEADERS.includes(key) || key.startsWith('sec-') || key
Name | Link |
---|---|
Latest commit | 0b278c6e111249382eb1edf83ce2f19b9e62f9fe |
Latest deploy log | https://app.netlify.com/sites/chat-for-free/deploys/6569aa9ca4c97f00088a2feb |
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: