adjust ai

This commit is contained in:
Emily
2024-12-16 16:57:52 +01:00
parent 6307e09dc3
commit 0a9474d00c
9 changed files with 134 additions and 63 deletions

View File

@@ -35,7 +35,8 @@
"vue-chart-3": "^3.1.8",
"vue-markdown-render": "^2.2.1",
"vue-router": "^4.3.0",
"winston": "^3.14.2"
"winston": "^3.14.2",
"zod": "^3.24.1"
},
"devDependencies": {
"@nuxt/ui": "^2.15.2",

View File

@@ -4,13 +4,17 @@ import VueMarkdown from 'vue-markdown-render';
definePageMeta({ layout: 'dashboard' });
const debugModeAi = ref<boolean>(false);
const { userRoles } = useLoggedUser();
const { project } = useProject();
const { data: chatsList, refresh: reloadChatsList } = useFetch(`/api/ai/chats_list`, {
headers: useComputedHeaders({ useSnapshotDates: false })
});
const viewChatsList = computed(() => (chatsList.value || []).toReversed());
const { data: chatsRemaining, refresh: reloadChatsRemaining } = useFetch(`/api/ai/chats_remaining`, {
@@ -21,14 +25,14 @@ const currentText = ref<string>("");
const loading = ref<boolean>(false);
const currentChatId = ref<string>("");
const currentChatMessages = ref<{ role: string, content: string, charts?: any[] }[]>([]);
const currentChatMessages = ref<{ role: string, content: string, charts?: any[], tool_calls?: any }[]>([]);
const currentChatMessageDelta = ref<string>('');
const currentChatMessageDeltaHtml = computed(() => {
const lastData = currentChatMessageDelta.value.match(/\[(data:(.*?))\]/g);
const cleanMessage = currentChatMessageDelta.value.replace(/\[(data:(.*?))\]/g, '');
if (!lastData || lastData.length == 0) return cleanMessage;
return `<div> <span>LOADER HERE: ${lastData.at(-1)}</span> ${cleanMessage} </div>`;
return `<div class="flex items-center gap-1"> <i class="fas fa-loader animate-spin"></i> <div> ${lastData.at(-1)}</div> </div> <div> ${cleanMessage} </div>`;
});
const scroller = ref<HTMLDivElement | null>(null);
@@ -36,7 +40,7 @@ const scroller = ref<HTMLDivElement | null>(null);
async function pollSendMessageStatus(chat_id: string, times: number, updateStatus: (status: string) => any) {
if (times > 20) return;
if (times > 100) return;
const res = await $fetch(`/api/ai/${chat_id}/status`, {
headers: useComputedHeaders({
@@ -48,14 +52,22 @@ async function pollSendMessageStatus(chat_id: string, times: number, updateStatu
updateStatus(res.status);
if (res.completed === false) {
setTimeout(() => pollSendMessageStatus(chat_id, times + 1, updateStatus), 200);
setTimeout(() => pollSendMessageStatus(chat_id, times + 1, updateStatus), (times > 20 ? 1000 : 500));
} else {
currentChatMessages.value.push({
role: 'assistant',
content: currentChatMessageDelta.value.replace(/\[data:.*?\]/g, ''),
const messages = await $fetch(`/api/ai/${chat_id}/get_messages`, {
headers: useComputedHeaders({ useSnapshotDates: false }).value
});
if (!messages) return;
currentChatMessages.value = messages.map(e => ({ ...e, charts: e.charts.map(k => JSON.parse(k)) })) as any;
currentChatMessageDelta.value = '';
// currentChatMessages.value.push({
// role: 'assistant',
// content: currentChatMessageDelta.value.replace(/\[data:.*?\]/g, ''),
// });
}
}
@@ -67,7 +79,7 @@ async function sendMessage() {
loading.value = true;
const body: any = { text: currentText.value }
const body: any = { text: currentText.value, timeOffset: new Date().getTimezoneOffset() }
if (currentChatId.value) body.chat_id = currentChatId.value
currentChatMessages.value.push({ role: 'user', content: currentText.value });
@@ -92,6 +104,8 @@ async function sendMessage() {
currentChatMessageDelta.value = status;
});
} catch (ex: any) {
if (ex.message.includes('CHAT_LIMIT_REACHED')) {
@@ -179,6 +193,8 @@ async function deleteChat(chat_id: string) {
const { visible: pricingDrawerVisible } = usePricingDrawer()
</script>
<template>
@@ -208,7 +224,7 @@ const { visible: pricingDrawerVisible } = usePricingDrawer()
<div ref="scroller" class="flex flex-col w-full gap-6 px-6 xl:px-28 overflow-y-auto pb-20">
<div class="flex w-full flex-col" v-for="message of currentChatMessages">
<div class="flex w-full flex-col" v-for="(message, messageIndex) of currentChatMessages">
<div class="flex justify-end w-full poppins text-[1.1rem]" v-if="message.role === 'user'">
<div class="bg-lyx-widget-light px-5 py-3 rounded-lg">
@@ -217,15 +233,32 @@ const { visible: pricingDrawerVisible } = usePricingDrawer()
</div>
<div class="flex items-center gap-3 justify-start w-full poppins text-[1.1rem]"
v-if="message.role === 'assistant' && message.content">
v-if="message.role === 'assistant' && (debugModeAi ? true : message.content)">
<div class="flex items-center justify-center shrink-0">
<img class="h-[3.5rem] w-auto" :src="'analyst.png'">
</div>
<div class="max-w-[70%] text-text/90 ai-message">
<vue-markdown :source="message.content" :options="{
<vue-markdown v-if="message.content" :source="message.content" :options="{
html: true,
breaks: true,
}" />
<div v-if="debugModeAi && !message.content">
<div class="flex flex-col"
v-if="message.tool_calls && message.tool_calls.length > 0">
<div> {{ message.tool_calls[0].function.name }}</div>
<div> {{ message.tool_calls[0].function.arguments }} </div>
</div>
</div>
<div v-if="debugModeAi && !message.content"
class="text-[.8rem] flex gap-1 items-center w-fit hover:text-[#CCCCCC] cursor-pointer">
<i class="fas fa-info text-[.7rem]"></i>
<div class="mt-1">Debug</div>
</div>
</div>
</div>
@@ -295,6 +328,9 @@ const { visible: pricingDrawerVisible } = usePricingDrawer()
</div>
</div>
<div :class="{ '!text-green-500': debugModeAi }" class="cursor-pointer text-red-500 w-fit"
v-if="userRoles.isAdmin.value" @click="debugModeAi = !debugModeAi"> Debug mode </div>
<div class="flex justify-between items-center pt-3">
<div class="flex items-center gap-2">
<div class="bg-accent w-5 h-5 rounded-full animate-pulse">
@@ -358,6 +394,9 @@ const { visible: pricingDrawerVisible } = usePricingDrawer()
color: white;
}
p:last-of-type {
margin-bottom: 0;
}
p {
line-height: 1.8;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 9.4 KiB

View File

@@ -12,8 +12,8 @@ const getEventsCountTool: AIPlugin_TTool<'getEventsCount'> = {
parameters: {
type: 'object',
properties: {
from: { type: 'string', description: 'ISO string of start date including hours' },
to: { type: 'string', description: 'ISO string of end date including hours' },
from: { type: 'string', description: 'ISO string of start date' },
to: { type: 'string', description: 'ISO string of end date' },
name: { type: 'string', description: 'Name of the events to get' },
metadata: { type: 'object', description: 'Metadata of events to get' },
},
@@ -30,8 +30,8 @@ const getEventsTimelineTool: AIPlugin_TTool<'getEventsTimeline'> = {
parameters: {
type: 'object',
properties: {
from: { type: 'string', description: 'ISO string of start date including hours' },
to: { type: 'string', description: 'ISO string of end date including hours' },
from: { type: 'string', description: 'ISO string of start date' },
to: { type: 'string', description: 'ISO string of end date' },
name: { type: 'string', description: 'Name of the events to get' },
metadata: { type: 'object', description: 'Metadata of events to get' },
},

View File

@@ -4,6 +4,11 @@ import { Types } from "mongoose";
import { AIPlugin, AIPlugin_TTool } from "../Plugin";
import dayjs from 'dayjs';
import { zodFunction } from "openai/helpers/zod";
import { z } from 'zod';
import { Slice } from "@services/DateService";
const getVisitsCountsTool: AIPlugin_TTool<'getVisitsCount'> = {
type: 'function',
function: {
@@ -12,8 +17,8 @@ const getVisitsCountsTool: AIPlugin_TTool<'getVisitsCount'> = {
parameters: {
type: 'object',
properties: {
from: { type: 'string', description: 'ISO string of start date including hours' },
to: { type: 'string', description: 'ISO string of end date including hours' },
from: { type: 'string', description: 'ISO string of start date' },
to: { type: 'string', description: 'ISO string of end date' },
website: { type: 'string', description: 'The website of the visits' },
page: { type: 'string', description: 'The page of the visit' }
},
@@ -30,10 +35,15 @@ const getVisitsTimelineTool: AIPlugin_TTool<'getVisitsTimeline'> = {
parameters: {
type: 'object',
properties: {
from: { type: 'string', description: 'ISO string of start date including hours' },
to: { type: 'string', description: 'ISO string of end date including hours' },
from: { type: 'string', description: 'ISO string of start date' },
to: { type: 'string', description: 'ISO string of end date' },
website: { type: 'string', description: 'The website of the visits' },
page: { type: 'string', description: 'The page of the visit' }
page: { type: 'string', description: 'The page of the visit' },
slice: {
type: 'string',
description: 'The slice for the visit data',
enum: ['hour', 'day', 'month', 'year']
}
},
required: ['from', 'to']
}
@@ -47,6 +57,7 @@ export class AiVisits extends AIPlugin<['getVisitsCount', 'getVisitsTimeline']>
super({
'getVisitsCount': {
handler: async (data: { project_id: string, from?: string, to?: string, website?: string, page?: string }) => {
const query: any = {
project_id: data.project_id,
created_at: {
@@ -54,31 +65,46 @@ export class AiVisits extends AIPlugin<['getVisitsCount', 'getVisitsTimeline']>
$lt: data.to ? new Date(data.to).getTime() : new Date().getTime(),
}
}
if (data.website) query.website = data.website;
if (data.page) query.page = data.page;
const result = await VisitModel.countDocuments(query);
return { count: result };
},
tool: getVisitsCountsTool
},
'getVisitsTimeline': {
handler: async (data: { project_id: string, from: string, to: string, website?: string, page?: string }) => {
handler: async (data: { project_id: string, from: string, to: string, time_offset: number, website?: string, page?: string, slice?: string }) => {
const query: AdvancedTimelineAggregationOptions & { customMatch: Record<string, any> } = {
projectId: new Types.ObjectId(data.project_id) as any,
const timelineData = await executeTimelineAggregation({
projectId: new Types.ObjectId(data.project_id),
model: VisitModel,
from: dayjs(data.from).startOf('day').toISOString(),
to: dayjs(data.to).startOf('day').toISOString(),
slice: 'day',
customMatch: {}
}
from: data.from,
to: data.to,
slice: (data.slice || 'day') as Slice,
timeOffset: data.time_offset
});
return { data: timelineData };
if (data.website) query.customMatch.website = data.website;
if (data.page) query.customMatch.page = data.page;
// const query: AdvancedTimelineAggregationOptions & { customMatch: Record<string, any> } = {
// projectId: new Types.ObjectId(data.project_id) as any,
// model: VisitModel,
// from: dayjs(data.from).startOf('day').toISOString(),
// to: dayjs(data.to).startOf('day').toISOString(),
// slice: 'day',
// customMatch: {}
// }
const timelineData = await executeAdvancedTimelineAggregation(query);
const timelineFilledMerged = fillAndMergeTimelineAggregationV2(timelineData, 'day', data.from, data.to);
return { data: timelineFilledMerged };
// if (data.website) query.customMatch.website = data.website;
// if (data.page) query.customMatch.page = data.page;
// const timelineData = await executeAdvancedTimelineAggregation(query);
// const timelineFilledMerged = fillAndMergeTimelineAggregationV2(timelineData, 'day', data.from, data.to);
// return { data: timelineFilledMerged };
},
tool: getVisitsTimelineTool
}

View File

@@ -7,6 +7,8 @@ export default defineEventHandler(async event => {
const data = await getRequestData(event);
if (!data) return;
const isAdmin = data.user.user.roles.includes('ADMIN');
const { project_id } = data;
if (!event.context.params) return;
@@ -16,13 +18,13 @@ export default defineEventHandler(async event => {
if (!chat) return;
return (chat.messages as OpenAI.Chat.Completions.ChatCompletionMessageParam[])
.filter(e => e.role === 'assistant' || e.role === 'user')
.filter(e => isAdmin ? true : (e.role === 'assistant' || e.role === 'user'))
.map(e => {
const charts = getChartsInMessage(e);
const content = e.content;
return { role: e.role, content, charts }
return { ...e, charts }
})
.filter(e=>{
return e.charts.length > 0 || e.content
.filter(e => {
return isAdmin ? true : (e.charts.length > 0 || e.content);
})
});

View File

@@ -9,7 +9,7 @@ export default defineEventHandler(async event => {
const { pid } = data;
const { text, chat_id } = await readBody(event);
const { text, chat_id, timeOffset } = await readBody(event);
if (!text) return setResponseStatus(event, 400, 'text parameter missing');
const chatsRemaining = await getAiChatRemainings(pid);
@@ -21,7 +21,7 @@ export default defineEventHandler(async event => {
let targetChatId = '';
await sendMessageOnChat(text, pid, chat_id, {
await sendMessageOnChat(text, pid, timeOffset, chat_id, {
onChatId: async chat_id => {
if (!responseSent) {
event.node.res.setHeader('Content-Type', 'application/json');

View File

@@ -65,7 +65,7 @@ export function getChartsInMessage(message: OpenAI.Chat.Completions.ChatCompleti
if (message.role != 'assistant') return [];
if (!message.tool_calls) return [];
if (message.tool_calls.length == 0) return [];
return message.tool_calls.filter(e => e.function.name === 'createComposableChart').map(e => e.function.arguments);
return message.tool_calls.filter((e: any) => e.function.name === 'createComposableChart').map((e: any) => e.function.arguments);
}
@@ -87,7 +87,7 @@ type ElaborateResponseCallbacks = {
onChatId?: (chat_id: string) => any
}
async function elaborateResponse(messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[], pid: string, chat_id: string, callbacks?: ElaborateResponseCallbacks) {
async function elaborateResponse(messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[], pid: string, time_offset: number, chat_id: string, callbacks?: ElaborateResponseCallbacks) {
const responseStream = await openai.beta.chat.completions.stream({ model: OPENAI_MODEL, messages, n: 1, tools });
@@ -124,35 +124,27 @@ async function elaborateResponse(messages: OpenAI.Chat.Completions.ChatCompletio
const functionCall: FunctionCall = functionCalls.at(-1) as FunctionCall;
await callbacks?.onFunctionCall?.(functionCall.name);
const args = JSON.parse(functionCall.argsRaw.join(''));
const functionResult = await functions[functionCall.name]({ project_id: pid, ...args });
const functionResult = await functions[functionCall.name]({ project_id: pid, time_offset, ...args });
functionCall.result = functionResult;
await callbacks?.onFunctionResult?.(functionCall.name, functionResult);
addMessageToChat({
tool_call_id: functionCall.tool_call_id,
role: 'tool',
content: JSON.stringify(functionResult)
}, chat_id);
addMessageToChat({
await addMessageToChat({
role: 'assistant',
content: delta.content,
refusal: delta.refusal,
tool_calls: [
{
id: functionCall.tool_call_id,
type: 'function',
id: functionCall.tool_call_id, type: 'function',
function: {
name: functionCall.name,
arguments: functionCall.argsRaw.join('')
name: functionCall.name, arguments: functionCall.argsRaw.join('')
}
}
]
}, chat_id);
await addMessageToChat({ tool_call_id: functionCall.tool_call_id, role: 'tool', content: JSON.stringify(functionCall.result) }, chat_id);
functionCall.collecting = false;
lastFinishReason = finishReason;
@@ -166,13 +158,13 @@ async function elaborateResponse(messages: OpenAI.Chat.Completions.ChatCompletio
return { tool_call_id: e.tool_call_id, role: "tool", content: JSON.stringify(e.result) }
});
if (lastFinishReason == 'tool_calls') return await elaborateResponse([...responseStream.messages, ...toolResponseMesages], pid, chat_id, callbacks);
if (lastFinishReason == 'tool_calls') return await elaborateResponse([...responseStream.messages, ...toolResponseMesages], pid, time_offset, chat_id, callbacks);
return responseStream;
}
export async function sendMessageOnChat(text: string, pid: string, initial_chat_id?: string, callbacks?: ElaborateResponseCallbacks) {
export async function sendMessageOnChat(text: string, pid: string, time_offset: number, initial_chat_id?: string, callbacks?: ElaborateResponseCallbacks) {
const messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = []
@@ -183,6 +175,7 @@ export async function sendMessageOnChat(text: string, pid: string, initial_chat_
if (chatMessages && chatMessages.length > 0) {
messages.push(...chatMessages);
await ProjectLimitModel.updateOne({ project_id: pid }, { $inc: { ai_messages: 1 } })
await updateChatStatus(chat_id, '', false);
} else {
const roleMessage: OpenAI.Chat.Completions.ChatCompletionMessageParam = {
@@ -200,7 +193,7 @@ export async function sendMessageOnChat(text: string, pid: string, initial_chat_
await addMessageToChat(userMessage, chat_id);
try {
const streamResponse = await elaborateResponse(messages, pid, chat_id, callbacks);
const streamResponse = await elaborateResponse(messages, pid, time_offset, chat_id, callbacks);
const finalContent = await streamResponse.finalContent();
await addMessageToChat({ role: 'assistant', refusal: null, content: finalContent }, chat_id);
return { content: finalContent, charts: [] };

14
pnpm-lock.yaml generated
View File

@@ -87,7 +87,7 @@ importers:
version: 0.0.11(magicast@0.3.5)(rollup@4.27.2)(vue@3.5.13(typescript@5.6.3))
openai:
specifier: ^4.61.0
version: 4.72.0
version: 4.72.0(zod@3.24.1)
pdfkit:
specifier: ^0.15.0
version: 0.15.1
@@ -118,6 +118,9 @@ importers:
winston:
specifier: ^3.14.2
version: 3.17.0
zod:
specifier: ^3.24.1
version: 3.24.1
devDependencies:
'@nuxt/ui':
specifier: ^2.15.2
@@ -4883,6 +4886,9 @@ packages:
resolution: {integrity: sha512-zK7YHHz4ZXpW89AHXUPbQVGKI7uvkd3hzusTdotCg1UxyaVtg0zFJSTfW/Dq5f7OBBVnq6cZIaC8Ti4hb6dtCA==}
engines: {node: '>= 14'}
zod@3.24.1:
resolution: {integrity: sha512-muH7gBL9sI1nciMZV67X5fTKKBLtwpZ5VBp1vsOQzj1MhrBZ4wlVCm3gedKZWLp0Oyel8sIGfeiz54Su+OVT+A==}
snapshots:
'@alloc/quick-lru@5.2.0': {}
@@ -8731,7 +8737,7 @@ snapshots:
is-docker: 2.2.1
is-wsl: 2.2.0
openai@4.72.0:
openai@4.72.0(zod@3.24.1):
dependencies:
'@types/node': 18.19.64
'@types/node-fetch': 2.6.12
@@ -8740,6 +8746,8 @@ snapshots:
form-data-encoder: 1.7.2
formdata-node: 4.4.1
node-fetch: 2.7.0
optionalDependencies:
zod: 3.24.1
transitivePeerDependencies:
- encoding
@@ -10185,3 +10193,5 @@ snapshots:
archiver-utils: 5.0.2
compress-commons: 6.0.2
readable-stream: 4.5.2
zod@3.24.1: {}