Commit 1118e560 authored by jaden's avatar jaden

feat: add query check

parent 484b7b14
import { ChatMessage } from './../../components/AITool/index';
import getConfig from 'next/config';
import APi, { backendApi } from '.';
const {
......@@ -17,4 +18,7 @@ export default class getView {
static getViewFunction(params: { data: Record<string, any>; need: string }) {
return backendApi.post('/openAi/api/code', params);
}
static checkChatResult(messageList: ChatMessage[]) {
return backendApi.post('/openAi/api/checkQuery', { messageList });
}
}
......@@ -25,6 +25,7 @@ import React, {
} from 'react';
import { IconRecord, IconRobot, IconSend, IconSwap, IconVoice } from '@arco-design/web-react/icon';
import OpenAI from '@/client/api/openAI';
import getView from '@/client/api/getView';
let useSpeechToText: any;
export interface ChatMessage {
......@@ -52,7 +53,8 @@ type AIProps = {
noHistory?: boolean;
functions: any[];
stream?: boolean;
function_call: any;
function_call?: any;
isCheck?: boolean;
};
export function AIWrapper({
......@@ -74,6 +76,7 @@ export function AIWrapper({
functions,
function_call,
stream,
isCheck = false,
}: AIProps) {
const input = useRef<any>();
const scrollContainer = useRef<any>();
......@@ -121,6 +124,28 @@ export function AIWrapper({
),
[]
);
const requestMessageList = useMemo(() => {
if (functions) {
return messageList.map(v => {
if (v.role === 'assistant') {
let fn;
try {
fn = JSON.parse(get(v, 'content'));
} catch (e) {
fn = '';
}
return {
...v,
content: functions && fn ? '' : get(v, 'content'),
function_call: functions && fn ? fn : undefined,
};
}
return v;
});
}
return messageList;
}, [messageList]);
const handleButtonClick = useCallback(
(message?: string, callBack?: (m: string) => void) => {
const inputRef = input.current?.dom;
......@@ -128,53 +153,107 @@ export function AIWrapper({
if (!inputValue) {
return;
}
const initMessageList = [
...messageList,
let initMessageList = [
...requestMessageList,
{
role: 'user',
content: inputValue,
},
];
setMessageList(initMessageList);
setMessageList([
...messageList,
{
role: 'user',
content: inputValue,
},
]);
// @ts-ignore
inputRef.value = '';
setLoading(true);
toView();
OpenAI.request(
noHistory
? initMessageList.slice(0, startView).concat({
role: 'user',
content: inputValue,
})
: initMessageList,
let check = !isCheck;
const request = () => {
OpenAI.request(
noHistory
? initMessageList.slice(0, startView).concat({
role: 'user',
content: inputValue,
})
: initMessageList,
currentAssistantMessageStr => {
// setTinking(true);
// setTimeout(() => {
setLoading(false);
// setTinking(false);
setTimeout(toView, 100);
// }, 100);
setCurrentAssistantMessage(currentAssistantMessageStr);
callBack && callBack(currentAssistantMessageStr);
doneFx && doneFx(currentAssistantMessageStr);
console.log(currentAssistantMessageStr, 'currentAssistantMessageStr');
},
val => {
setCurrentAssistantMessage(val);
toView();
},
() => {
Notification.error({
title: 'No Response',
content: undefined,
});
},
functions,
closeRef,
function_call,
stream || !functions
);
currentAssistantMessageStr => {
setTinking(true);
const done = () => {
setLoading(false);
setTimeout(toView, 100);
setCurrentAssistantMessage(currentAssistantMessageStr);
callBack && callBack(currentAssistantMessageStr);
doneFx && doneFx(currentAssistantMessageStr);
};
check
? done()
: getView
.checkChatResult([
...messageList,
{
role: 'user',
content: inputValue,
},
{
role: 'assistant',
function_call: functions
? JSON.parse(currentAssistantMessageStr)
: undefined,
content: functions ? '' : currentAssistantMessageStr,
},
])
.then((v: any) => {
const { answerMeetsRequirements, why } = get(
v,
'data.output',
{}
);
check = true;
setTinking(false);
if (answerMeetsRequirements) {
done();
} else {
initMessageList.push(
{
role: 'assistant',
function_call: functions
? JSON.parse(currentAssistantMessageStr)
: undefined,
content: functions
? ''
: currentAssistantMessageStr,
},
{
role: 'user',
content: why,
}
);
request();
}
});
},
val => {
setCurrentAssistantMessage(val);
toView();
},
() => {
Notification.error({
title: 'No Response',
content: undefined,
});
},
functions,
closeRef,
function_call,
stream || !functions
);
};
request();
},
[
loading,
......
......@@ -6,20 +6,26 @@ export const QUERY_FUNCTION = [
parameters: {
type: 'object',
properties: {
simulation: {
type: 'boolean',
description:
'This requirement needs you to generate simulated data, with true or false options.',
},
sql: {
type: 'string',
description:
'The SQL statement to execute must be standard and cannot contain any other code.',
description: `If the user's requirements include generating simulated data, you should generate this data first and ensure that it is included in the SQL output.,The SQL statement required to execute the operation must adhere to standard syntax. Additionally, the data inserted must comply with the existing table structure.`,
},
variablesArr: {
type: 'array',
description: 'The array containing variables and their descriptions',
description:
'Execute SQL statement containing an array of variables and their descriptions: If the requirement specifies simulated data, the parameter should be null.',
items: {
type: 'object',
properties: {
variable: {
type: 'string',
description: 'The variable',
description:
'The name of the variables.The information that needs to be filled in by the user should be treated as variables, and the variable names must be enclosed between $ signs.',
},
varDescription: {
type: 'string',
......@@ -38,7 +44,7 @@ export const QUERY_FUNCTION = [
description: 'The description of the query',
},
},
required: ['sql', 'variablesArr', 'queryName', 'queryDescription'],
required: ['sql', 'queryName', 'queryDescription', 'simulation'],
},
},
];
......@@ -7,22 +7,19 @@ export const GET_QUERY = (
${sql}
\`\`\`
按照需求生成结果之后,将结果按照xml语法,将模版中的标签中的内容进行分析,并且替换标签中的内容(但必须保留标签)作为最后结果输出,并且与模版排版必须保持一致(输出结果标签必须保留。变量的命名必须请放在$和$之间,例如:$tab$、$name$...)。:
将以下中的xml标签中的内容进行分析(用户需要填入的信息作为变量,如果需要变量:变量的命名必须请放在$和$之间,例如:$tab$、$name$...)。:
根据您提供的数据库模型,已为您生成查询:
<sql>按照需要生产的sql语句</sql>
执行所需变量:
判断解决当前问题的查询执行时是否需要变量。若条件成立,请使用:
<var>执行 SQL 所需变量名称</var>: <varDescription>变量解释</varDescription>
若不需要变量,则无需提供任何信息。
判断解决当前问题的查询执行时是否需要变量。若不需要变量,则无需提供任何信息。 若条件成立,请使用:
<var>$执行SQL所需变量名称$</var>: <varDescription>变量解释</varDescription>
查询命名和描述:
<queryName>查询名称</queryName>
<queryDescription>查询描述</queryDescription>
`;
export const GET_SCHEMA_INFO = (
sql: dbml
......
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
// Clone the request headers
// You can modify them with headers API: https://developer.mozilla.org/en-US/docs/Web/API/Headers
const requestHeaders = new Headers(request.headers)
// Clone the request headers
// You can modify them with headers API: https://developer.mozilla.org/en-US/docs/Web/API/Headers
const requestHeaders = new Headers(request.headers);
// Add new request headers
requestHeaders.set('authorization', `Bearer ${process.env.NEXT_PUBLIC_OPEN_AI_API_KEY || ''}`)
// Add new request headers
requestHeaders.set('authorization', `Bearer ${process.env.NEXT_PUBLIC_OPEN_AI_API_KEY || ''}`);
// You can also set request headers in NextResponse.rewrite
return NextResponse.next({
request: {
// New request headers
headers: requestHeaders,
},
})
}
\ No newline at end of file
//Todo 需要获取questionType,动态添加Prompt
// You can also set request headers in NextResponse.rewrite
return NextResponse.next({
request: {
// New request headers
headers: requestHeaders,
},
});
}
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment