diff --git a/.github/workflows/dockerToHub-dev.yml b/.github/workflows/dockerToHub-dev.yml index df9ccf1bb..bdd9662cf 100644 --- a/.github/workflows/dockerToHub-dev.yml +++ b/.github/workflows/dockerToHub-dev.yml @@ -15,7 +15,7 @@ jobs: build: name: build image to aly # runs-on: "103.200" - runs-on: z4 + runs-on: thinkpad # runs-on: ubuntu-latest # runs-on: self-hosted steps: @@ -34,7 +34,7 @@ jobs: deploy: name: 部署到dev服务器 needs: build - runs-on: z4 + runs-on: thinkpad steps: - name: Check out the repo uses: actions/checkout@v3 diff --git a/.github/workflows/dockerToHub.yml b/.github/workflows/dockerToHub.yml index f3b364ebc..68bf8b178 100644 --- a/.github/workflows/dockerToHub.yml +++ b/.github/workflows/dockerToHub.yml @@ -15,7 +15,7 @@ jobs: build: name: build image to aly # runs-on: "103.200" - runs-on: z4 + runs-on: thinkpad # runs-on: ubuntu-latest # runs-on: self-hosted steps: @@ -33,7 +33,7 @@ jobs: deploy: name: 部署到服务器 needs: build - runs-on: z4 + runs-on: thinkpad steps: - name: Check out the repo uses: actions/checkout@v3 diff --git a/Dockerfile b/Dockerfile index 16c0e3bb8..45892c8f7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,6 +21,9 @@ RUN # 清理遗留的缓存 RUN yarn cache clean RUN yarn install +# 避免下面那个报错 +RUN mkdir -p "/app/node_modules/tiktoken" + FROM base AS builder ENV OPENAI_API_KEY="" @@ -48,10 +51,14 @@ COPY --from=builder /app/.next/standalone ./ COPY --from=builder /app/.next/static ./.next/static COPY --from=builder /app/.next/server ./.next/server +# 一个插件一直有问题。 +COPY --from=deps /app/node_modules/tiktoken ./node_modules/tiktoken + RUN rm -f .env EXPOSE 3000 ENV KEEP_ALIVE_TIMEOUT=30 +ENV HOSTNAME="" CMD if [ -n "$PROXY_URL" ]; then \ export HOSTNAME="127.0.0.1"; \ diff --git a/app/api/logs/[...path]/route.ts b/app/api/logs/[...path]/route.ts index accf77036..f9a885247 100644 --- a/app/api/logs/[...path]/route.ts +++ b/app/api/logs/[...path]/route.ts @@ -1,7 +1,27 @@ import { NextRequest, NextResponse } from "next/server"; import prisma from "@/lib/prisma"; import { insertUser } from "@/lib/auth"; -import { getTokenLength } from "@/app/utils/token"; +// import { getTokenLength } from "@/app/utils/token"; +// import { Tiktoken } from "tiktoken/lite" +// import cl100k_base from "tiktoken/encoders/cl100k_base.json" +import "tiktoken"; +import { get_encoding } from "tiktoken"; + +function getTokenLength(input: string): number { + // const { Tiktoken } = require("tiktoken/lite"); + // const cl100k_base = require("tiktoken/encoders/cl100k_base.json"); + // const encoding = new Tiktoken( + // cl100k_base.bpe_ranks, + // cl100k_base.special_tokens, + // cl100k_base.pat_str, + // ); + const encoding = get_encoding("cl100k_base"); + + const tokenLength = encoding.encode(input).length; + // console.log('[TOKEN],=========', input, tokenLength) + + return tokenLength; +} async function handle( req: NextRequest, @@ -13,13 +33,17 @@ async function handle( await insertUser({ name: request_data?.userName }); } // console.log("===========4", request_data); - if (request_data?.logEntry) { - const regex = /\[(.*)]/g; - const matchResponse = request_data.logEntry.match(regex); - if (matchResponse.length > 0) { - request_data.logToken = getTokenLength(matchResponse[0]); + try { + if (request_data?.logEntry) { + const regex = /\[(.*)]/g; + const matchResponse = request_data.logEntry.match(regex); + if (matchResponse.length > 0) { + request_data.logToken = getTokenLength(matchResponse[0]); + } } - // console.log('=======', request_data.logEntry, '=====', matchResponse); + } catch (e) { + console.log("[LOG]", "logToken", e); + request_data.logToken = 0; } await prisma.logEntry.create({ diff --git a/app/api/openai/[...path]/route.ts b/app/api/openai/[...path]/route.ts index e153138aa..8885baf97 100644 --- a/app/api/openai/[...path]/route.ts +++ b/app/api/openai/[...path]/route.ts @@ -77,7 +77,8 @@ async function handle( try { const protocol = req.headers.get("x-forwarded-proto") || "http"; - const baseUrl = process.env.NEXTAUTH_URL ?? "http://localhost:3000"; + //const baseUrl = process.env.NEXTAUTH_URL ?? "http://localhost:3000"; + const baseUrl = "http://localhost:3000"; const ip = getIP(req); // 对其进行 Base64 解码 let h_userName = req.headers.get("x-request-name"); diff --git a/app/app/(admin)/admin/usage-by-model.tsx b/app/app/(admin)/admin/usage-by-model.tsx index dfcbd8d54..a3495eec0 100644 --- a/app/app/(admin)/admin/usage-by-model.tsx +++ b/app/app/(admin)/admin/usage-by-model.tsx @@ -65,14 +65,16 @@ export default async function UsageByModel() { 0, 0, ); - const endOfTheDayInTimeZone = new Date( - today.getFullYear(), - today.getMonth(), - today.getDate(), - 23, - 59, - 59, - ); // 当天的结束时间 + // const endOfTheDayInTimeZone = new Date( + // today.getFullYear(), + // today.getMonth(), + // today.getDate(), + // 23, + // 59, + // 59, + // ); // 当天的结束时间 + const endOfTheDayInTimeZone = addHours(startOfTheDayInTimeZone, +24); // 当天的结束时间 + // const startDate = addHours(startOfTheDayInTimeZone, -8); // const endDate = addHours(endOfTheDayInTimeZone, -8); console.log("===", today, startOfTheDayInTimeZone, endOfTheDayInTimeZone); diff --git a/next.config.mjs b/next.config.mjs index 3fb779f62..dc1a5bca7 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -11,8 +11,8 @@ console.log("[Next] build with chunk: ", disableChunk); // 为了修复tiktoken的插件问题 import CopyPlugin from "copy-webpack-plugin"; - const nextConfig = { + // transpilePackages: ['tiktoken'], webpack(config) { config.module.rules.push({ test: /\.svg$/, @@ -24,17 +24,22 @@ const nextConfig = { new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }), ) } - config.plugins.push( - new CopyPlugin({ - patterns: [ - { - from: ".//node_modules/tiktoken/tiktoken_bg.wasm", - to: "tiktoken_bg.wasm", - toType: "file", - } - ] - }) - ) + // turn off static file serving of WASM files + // we need to let Webpack handle WASM import + // config.module.rules + // .find((i) => "oneOf" in i) + // .oneOf.find((i) => i.type === "asset/resource") + // .exclude.push(/\.wasm$/); + // config.plugins.push( + // new CopyPlugin({ + // patterns: [ + // { + // from: ".//node_modules/tiktoken/, + // to: "", + // } + // ] + // }) + // ) config.optimization.minimize = true config.optimization.splitChunks = {