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

@@ -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: [] };