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 getConfig from 'next/config';
import APi, { backendApi } from '.'; import APi, { backendApi } from '.';
const { const {
...@@ -17,4 +18,7 @@ export default class getView { ...@@ -17,4 +18,7 @@ export default class getView {
static getViewFunction(params: { data: Record<string, any>; need: string }) { static getViewFunction(params: { data: Record<string, any>; need: string }) {
return backendApi.post('/openAi/api/code', params); return backendApi.post('/openAi/api/code', params);
} }
static checkChatResult(messageList: ChatMessage[]) {
return backendApi.post('/openAi/api/checkQuery', { messageList });
}
} }
...@@ -25,6 +25,7 @@ import React, { ...@@ -25,6 +25,7 @@ import React, {
} from 'react'; } from 'react';
import { IconRecord, IconRobot, IconSend, IconSwap, IconVoice } from '@arco-design/web-react/icon'; import { IconRecord, IconRobot, IconSend, IconSwap, IconVoice } from '@arco-design/web-react/icon';
import OpenAI from '@/client/api/openAI'; import OpenAI from '@/client/api/openAI';
import getView from '@/client/api/getView';
let useSpeechToText: any; let useSpeechToText: any;
export interface ChatMessage { export interface ChatMessage {
...@@ -52,7 +53,8 @@ type AIProps = { ...@@ -52,7 +53,8 @@ type AIProps = {
noHistory?: boolean; noHistory?: boolean;
functions: any[]; functions: any[];
stream?: boolean; stream?: boolean;
function_call: any; function_call?: any;
isCheck?: boolean;
}; };
export function AIWrapper({ export function AIWrapper({
...@@ -74,6 +76,7 @@ export function AIWrapper({ ...@@ -74,6 +76,7 @@ export function AIWrapper({
functions, functions,
function_call, function_call,
stream, stream,
isCheck = false,
}: AIProps) { }: AIProps) {
const input = useRef<any>(); const input = useRef<any>();
const scrollContainer = useRef<any>(); const scrollContainer = useRef<any>();
...@@ -121,6 +124,28 @@ export function AIWrapper({ ...@@ -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( const handleButtonClick = useCallback(
(message?: string, callBack?: (m: string) => void) => { (message?: string, callBack?: (m: string) => void) => {
const inputRef = input.current?.dom; const inputRef = input.current?.dom;
...@@ -128,18 +153,26 @@ export function AIWrapper({ ...@@ -128,18 +153,26 @@ export function AIWrapper({
if (!inputValue) { if (!inputValue) {
return; return;
} }
const initMessageList = [ let initMessageList = [
...messageList, ...requestMessageList,
{ {
role: 'user', role: 'user',
content: inputValue, content: inputValue,
}, },
]; ];
setMessageList(initMessageList); setMessageList([
...messageList,
{
role: 'user',
content: inputValue,
},
]);
// @ts-ignore // @ts-ignore
inputRef.value = ''; inputRef.value = '';
setLoading(true); setLoading(true);
toView(); toView();
let check = !isCheck;
const request = () => {
OpenAI.request( OpenAI.request(
noHistory noHistory
? initMessageList.slice(0, startView).concat({ ? initMessageList.slice(0, startView).concat({
...@@ -149,16 +182,60 @@ export function AIWrapper({ ...@@ -149,16 +182,60 @@ export function AIWrapper({
: initMessageList, : initMessageList,
currentAssistantMessageStr => { currentAssistantMessageStr => {
// setTinking(true); setTinking(true);
// setTimeout(() => { const done = () => {
setLoading(false); setLoading(false);
// setTinking(false);
setTimeout(toView, 100); setTimeout(toView, 100);
// }, 100);
setCurrentAssistantMessage(currentAssistantMessageStr); setCurrentAssistantMessage(currentAssistantMessageStr);
callBack && callBack(currentAssistantMessageStr); callBack && callBack(currentAssistantMessageStr);
doneFx && doneFx(currentAssistantMessageStr); doneFx && doneFx(currentAssistantMessageStr);
console.log(currentAssistantMessageStr, '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 => { val => {
setCurrentAssistantMessage(val); setCurrentAssistantMessage(val);
...@@ -175,6 +252,8 @@ export function AIWrapper({ ...@@ -175,6 +252,8 @@ export function AIWrapper({
function_call, function_call,
stream || !functions stream || !functions
); );
};
request();
}, },
[ [
loading, loading,
......
...@@ -6,20 +6,26 @@ export const QUERY_FUNCTION = [ ...@@ -6,20 +6,26 @@ export const QUERY_FUNCTION = [
parameters: { parameters: {
type: 'object', type: 'object',
properties: { properties: {
simulation: {
type: 'boolean',
description:
'This requirement needs you to generate simulated data, with true or false options.',
},
sql: { sql: {
type: 'string', type: 'string',
description: 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.`,
'The SQL statement to execute must be standard and cannot contain any other code.',
}, },
variablesArr: { variablesArr: {
type: 'array', 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: { items: {
type: 'object', type: 'object',
properties: { properties: {
variable: { variable: {
type: 'string', 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: { varDescription: {
type: 'string', type: 'string',
...@@ -38,7 +44,7 @@ export const QUERY_FUNCTION = [ ...@@ -38,7 +44,7 @@ export const QUERY_FUNCTION = [
description: 'The description of the query', description: 'The description of the query',
}, },
}, },
required: ['sql', 'variablesArr', 'queryName', 'queryDescription'], required: ['sql', 'queryName', 'queryDescription', 'simulation'],
}, },
}, },
]; ];
...@@ -7,22 +7,19 @@ export const GET_QUERY = ( ...@@ -7,22 +7,19 @@ export const GET_QUERY = (
${sql} ${sql}
\`\`\` \`\`\`
按照需求生成结果之后,将结果按照xml语法,将模版中的标签中的内容进行分析,并且替换标签中的内容(但必须保留标签)作为最后结果输出,并且与模版排版必须保持一致(输出结果标签必须保留。变量的命名必须请放在$和$之间,例如:$tab$、$name$...)。: 将以下中的xml标签中的内容进行分析(用户需要填入的信息作为变量,如果需要变量:变量的命名必须请放在$和$之间,例如:$tab$、$name$...)。:
根据您提供的数据库模型,已为您生成查询: 根据您提供的数据库模型,已为您生成查询:
<sql>按照需要生产的sql语句</sql> <sql>按照需要生产的sql语句</sql>
执行所需变量: 执行所需变量:
判断解决当前问题的查询执行时是否需要变量。若条件成立,请使用: 判断解决当前问题的查询执行时是否需要变量。若不需要变量,则无需提供任何信息。 若条件成立,请使用:
<var>执行 SQL 所需变量名称</var>: <varDescription>变量解释</varDescription> <var>$执行SQL所需变量名称$</var>: <varDescription>变量解释</varDescription>
若不需要变量,则无需提供任何信息。
查询命名和描述: 查询命名和描述:
<queryName>查询名称</queryName> <queryName>查询名称</queryName>
<queryDescription>查询描述</queryDescription> <queryDescription>查询描述</queryDescription>
`; `;
export const GET_SCHEMA_INFO = ( export const GET_SCHEMA_INFO = (
sql: dbml sql: dbml
......
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server' import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) { export function middleware(request: NextRequest) {
// Clone the request headers // Clone the request headers
// You can modify them with headers API: https://developer.mozilla.org/en-US/docs/Web/API/Headers // You can modify them with headers API: https://developer.mozilla.org/en-US/docs/Web/API/Headers
const requestHeaders = new Headers(request.headers) const requestHeaders = new Headers(request.headers);
// Add new request headers // Add new request headers
requestHeaders.set('authorization', `Bearer ${process.env.NEXT_PUBLIC_OPEN_AI_API_KEY || ''}`) requestHeaders.set('authorization', `Bearer ${process.env.NEXT_PUBLIC_OPEN_AI_API_KEY || ''}`);
//Todo 需要获取questionType,动态添加Prompt
// You can also set request headers in NextResponse.rewrite // You can also set request headers in NextResponse.rewrite
return NextResponse.next({ return NextResponse.next({
...@@ -15,5 +17,5 @@ export function middleware(request: NextRequest) { ...@@ -15,5 +17,5 @@ export function middleware(request: NextRequest) {
// New request headers // New request headers
headers: requestHeaders, 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