fix function calling + add clear all on chats

This commit is contained in:
Emily
2024-12-17 16:25:27 +01:00
parent 0a9474d00c
commit 68d362d1b3
6 changed files with 174 additions and 82 deletions

View File

@@ -1,8 +1,8 @@
import { EventModel } from "@schema/metrics/EventSchema";
import { AdvancedTimelineAggregationOptions, executeAdvancedTimelineAggregation, executeTimelineAggregation, fillAndMergeTimelineAggregationV2 } from "~/server/services/TimelineService";
import { executeTimelineAggregation } from "~/server/services/TimelineService";
import { Types } from "mongoose";
import { AIPlugin, AIPlugin_TTool } from "../Plugin";
import dayjs from 'dayjs';
const getEventsCountTool: AIPlugin_TTool<'getEventsCount'> = {
type: 'function',
@@ -46,12 +46,12 @@ export class AiEvents extends AIPlugin<['getEventsCount', 'getEventsTimeline']>
super({
'getEventsCount': {
handler: async (data: { project_id: string, from?: string, to?: string, name?: string, metadata?: string }) => {
handler: async (data: { project_id: string, from: string, to: string, name?: string, metadata?: string }) => {
const query: any = {
project_id: data.project_id,
created_at: {
$gt: data.from ? new Date(data.from).getTime() : new Date(2023).getTime(),
$lt: data.to ? new Date(data.to).getTime() : new Date().getTime(),
$gt: new Date(data.from),
$lt: new Date(data.to),
}
}
if (data.metadata) query.metadata = data.metadata;
@@ -62,21 +62,17 @@ export class AiEvents extends AIPlugin<['getEventsCount', 'getEventsTimeline']>
tool: getEventsCountTool
},
'getEventsTimeline': {
handler: async (data: { project_id: string, from: string, to: string, name?: string, metadata?: string }) => {
const query: AdvancedTimelineAggregationOptions & { customMatch: Record<string, any> } = {
projectId: new Types.ObjectId(data.project_id) as any,
model: EventModel,
from: dayjs(data.from).startOf('day').toISOString(),
to: dayjs(data.to).startOf('day').toISOString(),
slice: 'day',
customMatch: {}
}
if (data.metadata) query.customMatch.metadata = data.metadata;
if (data.name) query.customMatch.name = data.name;
handler: async (data: { project_id: string, from: string, to: string, time_offset: number, name?: string, metadata?: string }) => {
const timelineData = await executeAdvancedTimelineAggregation(query);
const timelineFilledMerged = fillAndMergeTimelineAggregationV2(timelineData, 'day', data.from, data.to);
return { data: timelineFilledMerged };
const timelineData = await executeTimelineAggregation({
projectId: new Types.ObjectId(data.project_id),
model: EventModel,
from: data.from,
to: data.to,
slice: 'day',
timeOffset: data.time_offset
});
return { data: timelineData };
},
tool: getEventsTimelineTool
}

View File

@@ -0,0 +1,86 @@
import { VisitModel } from "@schema/metrics/VisitSchema";
import { executeTimelineAggregation } from "~/server/services/TimelineService";
import { Types } from "mongoose";
import { AIPlugin, AIPlugin_TTool } from "../Plugin";
import { SessionModel } from "@schema/metrics/SessionSchema";
const getSessionsCountsTool: AIPlugin_TTool<'getSessionsCount'> = {
type: 'function',
function: {
name: 'getSessionsCount',
description: 'Gets the number of sessions received on a date range',
parameters: {
type: 'object',
properties: {
from: { type: 'string', description: 'ISO string of start date' },
to: { type: 'string', description: 'ISO string of end date' },
min_duration: { type: 'number', description: 'Minimum duration of the session' },
max_duration: { type: 'number', description: 'Maximum duration of the session' },
},
required: ['from', 'to']
}
}
}
const getSessionsTimelineTool: AIPlugin_TTool<'getSessionsTimeline'> = {
type: 'function',
function: {
name: 'getSessionsTimeline',
description: 'Gets an array of date and count for events received on a date range. Should be used to create charts.',
parameters: {
type: 'object',
properties: {
from: { type: 'string', description: 'ISO string of start date' },
to: { type: 'string', description: 'ISO string of end date' },
},
required: ['from', 'to']
}
}
}
export class AiSessions extends AIPlugin<['getSessionsCount', 'getSessionsTimeline']> {
constructor() {
super({
'getSessionsCount': {
handler: async (data: { project_id: string, from: string, to: string, min_duration?: number, max_duration?: number }) => {
const query: any = {
project_id: data.project_id,
created_at: {
$gt: new Date(data.from),
$lt: new Date(data.to),
},
duration: {
$gte: data.min_duration || 0,
$lte: data.max_duration || 999_999_999,
}
}
const result = await VisitModel.countDocuments(query);
return { count: result };
},
tool: getSessionsCountsTool
},
'getSessionsTimeline': {
handler: async (data: { project_id: string, from: string, to: string, time_offset: number, website?: string, page?: string }) => {
const timelineData = await executeTimelineAggregation({
projectId: new Types.ObjectId(data.project_id),
model: SessionModel,
from: data.from,
to: data.to,
slice: 'day',
timeOffset: data.time_offset
});
return { data: timelineData };
},
tool: getSessionsTimelineTool
}
})
}
}
export const ASessionsInstance = new AiSessions();

View File

@@ -1,13 +1,7 @@
import { VisitModel } from "@schema/metrics/VisitSchema";
import { AdvancedTimelineAggregationOptions, executeAdvancedTimelineAggregation, executeTimelineAggregation, fillAndMergeTimelineAggregationV2 } from "~/server/services/TimelineService";
import { executeTimelineAggregation } from "~/server/services/TimelineService";
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',
@@ -39,11 +33,6 @@ const getVisitsTimelineTool: AIPlugin_TTool<'getVisitsTimeline'> = {
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' },
slice: {
type: 'string',
description: 'The slice for the visit data',
enum: ['hour', 'day', 'month', 'year']
}
},
required: ['from', 'to']
}
@@ -56,13 +45,13 @@ export class AiVisits extends AIPlugin<['getVisitsCount', 'getVisitsTimeline']>
super({
'getVisitsCount': {
handler: async (data: { project_id: string, from?: string, to?: string, website?: string, page?: string }) => {
handler: async (data: { project_id: string, from: string, to: string, website?: string, page?: string }) => {
const query: any = {
project_id: data.project_id,
created_at: {
$gt: data.from ? new Date(data.from).getTime() : new Date(2023).getTime(),
$lt: data.to ? new Date(data.to).getTime() : new Date().getTime(),
$gt: new Date(data.from),
$lt: new Date(data.to),
}
}
@@ -78,33 +67,17 @@ export class AiVisits extends AIPlugin<['getVisitsCount', 'getVisitsTimeline']>
tool: getVisitsCountsTool
},
'getVisitsTimeline': {
handler: async (data: { project_id: string, from: string, to: string, time_offset: number, website?: string, page?: string, slice?: string }) => {
handler: async (data: { project_id: string, from: string, to: string, time_offset: number, website?: string, page?: string }) => {
const timelineData = await executeTimelineAggregation({
projectId: new Types.ObjectId(data.project_id),
model: VisitModel,
from: data.from,
to: data.to,
slice: (data.slice || 'day') as Slice,
slice: 'day',
timeOffset: data.time_offset
});
return { data: timelineData };
// 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: {}
// }
// 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
}