mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 21:13:50 +00:00
perf: log和向量对话
This commit is contained in:
@@ -14,6 +14,7 @@ import { connectRedis } from '@/service/redis';
|
|||||||
import { VecModelDataPrefix } from '@/constants/redis';
|
import { VecModelDataPrefix } from '@/constants/redis';
|
||||||
import { vectorToBuffer } from '@/utils/tools';
|
import { vectorToBuffer } from '@/utils/tools';
|
||||||
import { openaiCreateEmbedding } from '@/service/utils/openai';
|
import { openaiCreateEmbedding } from '@/service/utils/openai';
|
||||||
|
import { gpt35StreamResponse } from '@/service/utils/openai';
|
||||||
|
|
||||||
/* 发送提示词 */
|
/* 发送提示词 */
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
@@ -152,49 +153,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
|
|
||||||
console.log('api response time:', `${(Date.now() - startTime) / 1000}s`);
|
console.log('api response time:', `${(Date.now() - startTime) / 1000}s`);
|
||||||
|
|
||||||
// 创建响应流
|
|
||||||
res.setHeader('Content-Type', 'text/event-stream;charset-utf-8');
|
|
||||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
||||||
res.setHeader('X-Accel-Buffering', 'no');
|
|
||||||
res.setHeader('Cache-Control', 'no-cache, no-transform');
|
|
||||||
step = 1;
|
step = 1;
|
||||||
|
|
||||||
let responseContent = '';
|
const { responseContent } = await gpt35StreamResponse({
|
||||||
stream.pipe(res);
|
res,
|
||||||
|
stream,
|
||||||
const onParse = async (event: ParsedEvent | ReconnectInterval) => {
|
chatResponse
|
||||||
if (event.type !== 'event') return;
|
});
|
||||||
const data = event.data;
|
|
||||||
if (data === '[DONE]') return;
|
|
||||||
try {
|
|
||||||
const json = JSON.parse(data);
|
|
||||||
const content: string = json?.choices?.[0].delta.content || '';
|
|
||||||
if (!content || (responseContent === '' && content === '\n')) return;
|
|
||||||
|
|
||||||
responseContent += content;
|
|
||||||
// console.log('content:', content)
|
|
||||||
!stream.destroyed && stream.push(content.replace(/\n/g, '<br/>'));
|
|
||||||
} catch (error) {
|
|
||||||
error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const decoder = new TextDecoder();
|
|
||||||
try {
|
|
||||||
for await (const chunk of chatResponse.data as any) {
|
|
||||||
if (stream.destroyed) {
|
|
||||||
// 流被中断了,直接忽略后面的内容
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
const parser = createParser(onParse);
|
|
||||||
parser.feed(decoder.decode(chunk));
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.log('pipe error', error);
|
|
||||||
}
|
|
||||||
// close stream
|
|
||||||
!stream.destroyed && stream.push(null);
|
|
||||||
stream.destroy();
|
|
||||||
|
|
||||||
const promptsContent = formatPrompts.map((item) => item.content).join('');
|
const promptsContent = formatPrompts.map((item) => item.content).join('');
|
||||||
// 只有使用平台的 key 才计费
|
// 只有使用平台的 key 才计费
|
||||||
|
@@ -14,7 +14,6 @@ import {
|
|||||||
} from '@chakra-ui/react';
|
} from '@chakra-ui/react';
|
||||||
import { useToast } from '@/hooks/useToast';
|
import { useToast } from '@/hooks/useToast';
|
||||||
import { useSelectFile } from '@/hooks/useSelectFile';
|
import { useSelectFile } from '@/hooks/useSelectFile';
|
||||||
import { customAlphabet } from 'nanoid';
|
|
||||||
import { encode } from 'gpt-token-utils';
|
import { encode } from 'gpt-token-utils';
|
||||||
import { useConfirm } from '@/hooks/useConfirm';
|
import { useConfirm } from '@/hooks/useConfirm';
|
||||||
import { readTxtContent, readPdfContent, readDocContent } from '@/utils/tools';
|
import { readTxtContent, readPdfContent, readDocContent } from '@/utils/tools';
|
||||||
@@ -22,8 +21,6 @@ import { useMutation } from '@tanstack/react-query';
|
|||||||
import { postModelDataSplitData } from '@/api/model';
|
import { postModelDataSplitData } from '@/api/model';
|
||||||
import { formatPrice } from '@/utils/user';
|
import { formatPrice } from '@/utils/user';
|
||||||
|
|
||||||
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 12);
|
|
||||||
|
|
||||||
const fileExtension = '.txt,.doc,.docx,.pdf,.md';
|
const fileExtension = '.txt,.doc,.docx,.pdf,.md';
|
||||||
|
|
||||||
const SelectFileModal = ({
|
const SelectFileModal = ({
|
||||||
|
@@ -34,7 +34,6 @@ export const pushChatBill = async ({
|
|||||||
// 计算价格
|
// 计算价格
|
||||||
const unitPrice = modelItem?.price || 5;
|
const unitPrice = modelItem?.price || 5;
|
||||||
const price = unitPrice * tokens;
|
const price = unitPrice * tokens;
|
||||||
console.log(`unit price: ${unitPrice}, price: ${formatPrice(price)}元`);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 插入 Bill 记录
|
// 插入 Bill 记录
|
||||||
@@ -91,8 +90,6 @@ export const pushSplitDataBill = async ({
|
|||||||
// 计算价格
|
// 计算价格
|
||||||
const price = unitPrice * tokenLen;
|
const price = unitPrice * tokenLen;
|
||||||
|
|
||||||
console.log(`price: ${formatPrice(price)}元`);
|
|
||||||
|
|
||||||
// 插入 Bill 记录
|
// 插入 Bill 记录
|
||||||
const res = await Bill.create({
|
const res = await Bill.create({
|
||||||
userId,
|
userId,
|
||||||
@@ -143,8 +140,6 @@ export const pushGenerateVectorBill = async ({
|
|||||||
let price = unitPrice * tokenLen;
|
let price = unitPrice * tokenLen;
|
||||||
price = price > 1 ? price : 1;
|
price = price > 1 ? price : 1;
|
||||||
|
|
||||||
console.log(`price: ${formatPrice(price)}元`);
|
|
||||||
|
|
||||||
// 插入 Bill 记录
|
// 插入 Bill 记录
|
||||||
const res = await Bill.create({
|
const res = await Bill.create({
|
||||||
userId,
|
userId,
|
||||||
|
@@ -30,13 +30,11 @@ export const jsonRes = <T = any>(
|
|||||||
} else if (openaiError[error?.response?.statusText]) {
|
} else if (openaiError[error?.response?.statusText]) {
|
||||||
msg = openaiError[error.response.statusText];
|
msg = openaiError[error.response.statusText];
|
||||||
}
|
}
|
||||||
console.log('error->');
|
console.log(`error-> msg:${msg}`);
|
||||||
console.log('code:', error.code);
|
|
||||||
console.log('msg:', msg);
|
|
||||||
// request 时候报错
|
// request 时候报错
|
||||||
if (error?.response) {
|
if (error?.response) {
|
||||||
console.log('statusText:', error?.response?.statusText);
|
console.log('statusText:', error?.response?.statusText);
|
||||||
console.log('type:', error?.response?.data?.error?.type);
|
console.log('error data:', error?.response?.data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user