mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-26 07:28:49 -05:00
chore: moved insights model to database package (#4575)
This commit is contained in:
@@ -2,7 +2,7 @@ import { createDocument } from "@/app/api/(internal)/insights/lib/document";
|
||||
import { doesResponseHasAnyOpenTextAnswer } from "@/app/api/(internal)/insights/lib/utils";
|
||||
import { documentCache } from "@/lib/cache/document";
|
||||
import { insightCache } from "@/lib/cache/insight";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Insight, InsightCategory, Prisma } from "@prisma/client";
|
||||
import { embed } from "ai";
|
||||
import { prisma } from "@formbricks/database";
|
||||
import { embeddingsModel } from "@formbricks/lib/aiModels";
|
||||
@@ -12,12 +12,6 @@ import { validateInputs } from "@formbricks/lib/utils/validate";
|
||||
import { ZId } from "@formbricks/types/common";
|
||||
import { TCreatedDocument } from "@formbricks/types/documents";
|
||||
import { DatabaseError } from "@formbricks/types/errors";
|
||||
import {
|
||||
TInsight,
|
||||
TInsightCategory,
|
||||
TInsightCreateInput,
|
||||
ZInsightCreateInput,
|
||||
} from "@formbricks/types/insights";
|
||||
import {
|
||||
TSurvey,
|
||||
TSurveyQuestionId,
|
||||
@@ -25,6 +19,7 @@ import {
|
||||
ZSurveyQuestions,
|
||||
} from "@formbricks/types/surveys/types";
|
||||
import { getContactAttributes } from "./contact-attribute";
|
||||
import { TInsightCreateInput, TNearestInsights, ZInsightCreateInput } from "./types";
|
||||
|
||||
export const generateInsightsForSurveyResponsesConcept = async (
|
||||
survey: Pick<TSurvey, "id" | "name" | "environmentId" | "questions">
|
||||
@@ -328,23 +323,16 @@ export const getQuestionResponseReferenceId = (surveyId: string, questionId: TSu
|
||||
return `${surveyId}-${questionId}`;
|
||||
};
|
||||
|
||||
export const createInsight = async (insightGroupInput: TInsightCreateInput): Promise<TInsight> => {
|
||||
export const createInsight = async (insightGroupInput: TInsightCreateInput): Promise<Insight> => {
|
||||
validateInputs([insightGroupInput, ZInsightCreateInput]);
|
||||
|
||||
try {
|
||||
// create document
|
||||
const { vector, ...data } = insightGroupInput;
|
||||
const prismaInsight = await prisma.insight.create({
|
||||
const insight = await prisma.insight.create({
|
||||
data,
|
||||
});
|
||||
|
||||
const insight = {
|
||||
...prismaInsight,
|
||||
_count: {
|
||||
documentInsights: 0,
|
||||
},
|
||||
};
|
||||
|
||||
// update document vector with the embedding
|
||||
const vectorString = `[${insightGroupInput.vector.join(",")}]`;
|
||||
await prisma.$executeRaw`
|
||||
@@ -373,7 +361,7 @@ export const handleInsightAssignments = async (
|
||||
insight: {
|
||||
title: string;
|
||||
description: string;
|
||||
category: TInsightCategory;
|
||||
category: InsightCategory;
|
||||
}
|
||||
) => {
|
||||
try {
|
||||
@@ -427,21 +415,15 @@ export const findNearestInsights = async (
|
||||
vector: number[],
|
||||
limit: number = 5,
|
||||
threshold: number = 0.5
|
||||
): Promise<TInsight[]> => {
|
||||
): Promise<TNearestInsights[]> => {
|
||||
validateInputs([environmentId, ZId]);
|
||||
// Convert the embedding array to a JSON-like string representation
|
||||
const vectorString = `[${vector.join(",")}]`;
|
||||
|
||||
// Execute raw SQL query to find nearest neighbors and exclude the vector column
|
||||
const insights: TInsight[] = await prisma.$queryRaw`
|
||||
const insights: TNearestInsights[] = await prisma.$queryRaw`
|
||||
SELECT
|
||||
id,
|
||||
created_at AS "createdAt",
|
||||
updated_at AS "updatedAt",
|
||||
title,
|
||||
description,
|
||||
category,
|
||||
"environmentId"
|
||||
id
|
||||
FROM "Insight" d
|
||||
WHERE d."environmentId" = ${environmentId}
|
||||
AND d."vector" <=> ${vectorString}::vector(512) <= ${threshold}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Insight } from "@prisma/client";
|
||||
import { z } from "zod";
|
||||
import { ZInsight } from "@formbricks/database/zod/insights";
|
||||
|
||||
export const ZInsightCreateInput = ZInsight.pick({
|
||||
environmentId: true,
|
||||
title: true,
|
||||
description: true,
|
||||
category: true,
|
||||
}).extend({
|
||||
vector: z.array(z.number()).length(512),
|
||||
});
|
||||
|
||||
export type TInsightCreateInput = z.infer<typeof ZInsightCreateInput>;
|
||||
|
||||
export type TNearestInsights = Pick<Insight, "id">;
|
||||
@@ -4,6 +4,7 @@ import { Prisma } from "@prisma/client";
|
||||
import { embed, generateObject } from "ai";
|
||||
import { z } from "zod";
|
||||
import { prisma } from "@formbricks/database";
|
||||
import { ZInsight } from "@formbricks/database/zod/insights";
|
||||
import { embeddingsModel, llmModel } from "@formbricks/lib/aiModels";
|
||||
import { validateInputs } from "@formbricks/lib/utils/validate";
|
||||
import {
|
||||
@@ -13,7 +14,6 @@ import {
|
||||
ZDocumentSentiment,
|
||||
} from "@formbricks/types/documents";
|
||||
import { DatabaseError } from "@formbricks/types/errors";
|
||||
import { ZInsightCategory } from "@formbricks/types/insights";
|
||||
|
||||
export const createDocumentAndAssignInsight = async (
|
||||
surveyName: string,
|
||||
@@ -38,7 +38,7 @@ export const createDocumentAndAssignInsight = async (
|
||||
z.object({
|
||||
title: z.string().describe("insight title, very specific"),
|
||||
description: z.string().describe("very brief insight description"),
|
||||
category: ZInsightCategory,
|
||||
category: ZInsight.shape.category,
|
||||
})
|
||||
),
|
||||
isSpam: z.boolean(),
|
||||
|
||||
Reference in New Issue
Block a user