-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: suggest evaluations form our templates with AI (#306)
- Loading branch information
Showing
12 changed files
with
546 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
apps/web/src/actions/evaluations/generateSuggestedEvaluations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use server' | ||
|
||
import { createHash } from 'crypto' | ||
|
||
import { ChainObjectResponse } from '@latitude-data/core/browser' | ||
import { cache } from '@latitude-data/core/cache' | ||
import { findAllEvaluationTemplates } from '@latitude-data/core/data-access' | ||
import { BadRequestError } from '@latitude-data/core/lib/errors' | ||
import { createSdk } from '$/app/(private)/_lib/createSdk' | ||
import env from '$/env' | ||
import { SuggestedEvaluation } from '$/stores/suggestedEvaluations' | ||
import { z } from 'zod' | ||
|
||
import { authProcedure } from '../procedures' | ||
|
||
export const generateSuggestedEvaluationsAction = authProcedure | ||
.createServerAction() | ||
.input( | ||
z.object({ | ||
documentContent: z.string(), | ||
}), | ||
) | ||
.handler(async ({ input }) => { | ||
if (!env.DATASET_GENERATOR_WORKSPACE_APIKEY) { | ||
throw new BadRequestError('DATASET_GENERATOR_WORKSPACE_APIKEY is not set') | ||
} | ||
if (!env.TEMPLATES_SUGGESTION_PROJECT_ID) { | ||
throw new BadRequestError('TEMPLATES_SUGGESTION_PROJECT_ID is not set') | ||
} | ||
if (!env.TEMPLATES_SUGGESTION_PROMPT_PATH) { | ||
throw new BadRequestError('TEMPLATES_SUGGESTION_PROMPT_PATH is not set') | ||
} | ||
|
||
const cacheInstance = await cache() | ||
const contentHash = createHash('sha1') | ||
.update(input.documentContent) | ||
.digest('hex') | ||
const cacheKey = `suggested_evaluations:${contentHash}` | ||
|
||
const cachedResult = await cacheInstance.get(cacheKey) | ||
if (cachedResult) { | ||
return JSON.parse(cachedResult) as SuggestedEvaluation[] | ||
} | ||
|
||
const templates = await findAllEvaluationTemplates().then((r) => r.unwrap()) | ||
const templateString = templates | ||
.map((t) => `${t.id}\n${t.name}\n${t.description}\n`) | ||
.join('\n') | ||
const sdk = await createSdk({ | ||
apiKey: env.DATASET_GENERATOR_WORKSPACE_APIKEY, | ||
projectId: env.TEMPLATES_SUGGESTION_PROJECT_ID, | ||
}).then((r) => r.unwrap()) | ||
const result = await sdk.run(env.TEMPLATES_SUGGESTION_PROMPT_PATH, { | ||
parameters: { | ||
templates: templateString, | ||
prompt: input.documentContent, | ||
}, | ||
}) | ||
|
||
if (!result) return [] | ||
|
||
const res = result.response as ChainObjectResponse | ||
if (!res.object) return [] | ||
|
||
const suggestedEvaluations = res.object[0] as SuggestedEvaluation[] | ||
|
||
await cacheInstance.set(cacheKey, JSON.stringify(suggestedEvaluations)) | ||
|
||
return suggestedEvaluations | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use client' | ||
|
||
import { generateSuggestedEvaluationsAction } from '$/actions/evaluations/generateSuggestedEvaluations' | ||
import useSWR, { SWRConfiguration } from 'swr' | ||
|
||
export interface SuggestedEvaluation { | ||
id: number | ||
title: string | ||
description: string | ||
} | ||
|
||
export default function useSuggestedEvaluations( | ||
documentContent?: string | null, | ||
opts?: SWRConfiguration, | ||
) { | ||
const { data, error, isLoading } = useSWR<SuggestedEvaluation[]>( | ||
[ | ||
'suggestedEvaluations', | ||
documentContent ? documentContent.slice(-100) : null, | ||
], | ||
async () => { | ||
if (!documentContent) return [] | ||
|
||
const [data, error] = await generateSuggestedEvaluationsAction({ | ||
documentContent, | ||
}) | ||
|
||
if (error) return [] | ||
|
||
return data | ||
}, | ||
opts, | ||
) | ||
|
||
return { | ||
data: data || [], | ||
isLoading, | ||
error, | ||
} | ||
} |
Oops, something went wrong.