Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Sep 12, 2024
1 parent 0727f21 commit 20bf409
Show file tree
Hide file tree
Showing 19 changed files with 457 additions and 270 deletions.
68 changes: 0 additions & 68 deletions apps/database/fly.toml

This file was deleted.

126 changes: 126 additions & 0 deletions apps/web/src/actions/evaluations/connect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import {
DocumentVersion,
Project,
ProviderApiKey,
Providers,
User,
Workspace,
} from '@latitude-data/core/browser'
import * as factories from '@latitude-data/core/factories'
import { beforeEach, describe, expect, it, vi } from 'vitest'

import { connectEvaluationsAction } from './connect'

const mocks = vi.hoisted(() => {
return {
getSession: vi.fn(),
}
})

vi.mock('$/services/auth/getSession', () => ({
getSession: mocks.getSession,
}))

describe('connectEvaluationsAction', () => {
describe('unauthorized', () => {
it('errors when the user is not authenticated', async () => {
const [_, error] = await connectEvaluationsAction({
projectId: 1,
documentUuid: 'fake-document-uuid',
templateIds: [1],
evaluationUuids: ['fake-evaluation-uuid'],
})

expect(error!.name).toEqual('UnauthorizedError')
})
})

describe('authorized', () => {
let workspace: Workspace,
user: User,
document: DocumentVersion,
provider: ProviderApiKey,
project: Project

beforeEach(async () => {
const setup = await factories.createProject({
documents: { 'test-doc': 'Test content' },
})
workspace = setup.workspace
user = setup.user
document = setup.documents[0]!
project = setup.project

provider = await factories.createProviderApiKey({
workspace,
type: Providers.OpenAI,
name: 'Test Provider',
user,
})

mocks.getSession.mockReturnValue({
user,
workspace: { id: workspace.id, name: workspace.name },
})
})

it('connects evaluations and templates to a document', async () => {
const evaluation = await factories.createLlmAsJudgeEvaluation({
workspace,
name: 'Test Evaluation',
prompt: factories.helpers.createPrompt({ provider }),
})

const template = await factories.createEvaluationTemplate({
name: 'Test Template',
description: 'Test description',
prompt: 'Test prompt',
})

const [result, error] = await connectEvaluationsAction({
projectId: project.id,
documentUuid: document.documentUuid,
templateIds: [template.id],
evaluationUuids: [evaluation.uuid],
})

expect(error).toBeNull()
expect(result).toHaveLength(2)
expect(result).toEqual(
expect.arrayContaining([
expect.objectContaining({
documentUuid: document.documentUuid,
evaluationId: evaluation.id,
}),
expect.objectContaining({
documentUuid: document.documentUuid,
evaluationId: expect.any(Number),
}),
]),
)
})

it('returns an empty array when no evaluations or templates are provided', async () => {
const [result, error] = await connectEvaluationsAction({
projectId: project.id,
documentUuid: document.documentUuid,
templateIds: [],
evaluationUuids: [],
})

expect(error).toBeNull()
expect(result).toHaveLength(0)
})

it('fails when the document does not exist', async () => {
const [_, error] = await connectEvaluationsAction({
projectId: project.id,
documentUuid: 'non-existent-uuid',
templateIds: [],
evaluationUuids: [],
})

expect(error!.name).toEqual('NotFoundError')
})
})
})
26 changes: 26 additions & 0 deletions apps/web/src/actions/evaluations/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use server'

import { connectEvaluations } from '@latitude-data/core/services/evaluations/connect'
import { z } from 'zod'

import { withProject } from '../procedures'

export const connectEvaluationsAction = withProject
.createServerAction()
.input(
z.object({
documentUuid: z.string(),
templateIds: z.array(z.number()),
evaluationUuids: z.array(z.string()),
}),
)
.handler(async ({ ctx, input }) => {
const connectedEvaluations = await connectEvaluations({
workspace: ctx.workspace,
documentUuid: input.documentUuid,
evaluationUuids: input.evaluationUuids,
templateIds: input.templateIds,
}).then((r) => r.unwrap())

return connectedEvaluations
})
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default function ConnectedDocumentsTable({
.detail({ id: document.projectId })
.commits.detail({ uuid: HEAD_COMMIT })
.documents.detail({ uuid: document.documentUuid })
.evaluations.detail(document.evaluationUuid).root,
.evaluations.detail(document.evaluationId).root,
)
}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import DatasetForm from './DatasetForm'
import { useRunBatch } from './useRunBatch'
import { useRunBatchForm } from './useRunBatchForm'

export default function ConnectDatasetModal({
export default function CreateBatchEvaluationModal({
document,
evaluation,
documentMetadata,
Expand Down Expand Up @@ -66,19 +66,19 @@ export default function ConnectDatasetModal({
.detail({ id: Number(projectId) })
.commits.detail({ uuid: commitUuid })
.documents.detail({ uuid: documentUuid })
.evaluations.detail(evaluation.uuid).root,
.evaluations.detail(evaluation.id).root,
)
}}
footer={
<>
<CloseTrigger />
<Button
disabled={!form.selectedDataset || isRunningBatch}
fancy
onClick={onRunBatch}
>
{isRunningBatch ? 'Running...' : 'Run Evaluations'}
</Button>
<CloseTrigger />
<Button
disabled={!form.selectedDataset || isRunningBatch}
fancy
onClick={onRunBatch}
>
{isRunningBatch ? 'Running...' : 'Run Evaluations'}
</Button>
</>
}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export function useRunBatch({
document: DocumentVersion
commitUuid: string
}) {
const { execute: run, isPending: isRunning } =
useLatitudeAction(runBatchEvaluationAction)
const { execute: run, isPending: isRunning } = useLatitudeAction(
runBatchEvaluationAction,
)
const runBatch = useCallback(
async ({
evaluationIds,
Expand All @@ -33,7 +34,7 @@ export function useRunBatch({
parameters: RunBatchParameters
}) => {
console.log('RUNNING BATCH')
const [data, error] = await run({
const [_data, _error] = await run({
projectId: Number(projectId),
documentUuid: document.documentUuid,
commitUuid,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { readMetadata } from '@latitude-data/compiler'
import {
getDocumentByUuidCached,
getEvaluationByUuidCached,
} from '$/app/(private)/_data-access'

import CreateBatchEvaluationModal from './_components/CreateBatchEvaluationModal'

export default async function ConnectionEvaluationModal({
params,
}: {
params: {
projectId: string
commitUuid: string
documentUuid: string
evaluationUuid: string
}
}) {
const evaluation = await getEvaluationByUuidCached(params.evaluationUuid)
const projectId = Number(params.projectId)
const documentUuid = params.documentUuid
const commitUuid = params.commitUuid
const document = await getDocumentByUuidCached({
projectId,
commitUuid,
documentUuid,
})
const metadata = await readMetadata({
prompt: document.content ?? '',
fullPath: document.path,
})
return (
<CreateBatchEvaluationModal
evaluation={evaluation}
document={document}
documentMetadata={metadata}
projectId={params.projectId}
commitUuid={commitUuid}
/>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { EvaluationsRepository } from '@latitude-data/core/repositories'
import { getCurrentUser } from '$/services/auth/getCurrentUser'

export default async function EvaluationPage({
params: { evaluationId },
}: {
params: { evaluationId: string }
}) {
const { workspace } = await getCurrentUser()
const evaluationScope = new EvaluationsRepository(workspace.id)
const evaluation = await evaluationScope
.find(evaluationId)
.then((r) => r.unwrap())
return (
<div>
<h1>{evaluation.name}</h1>
<p>{evaluation.description || 'No description for this evaluation'}</p>
</div>
)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function BatchEvaluationsTable({
.detail({ id: project.id })
.commits.detail({ uuid: commit.uuid })
.documents.detail({ uuid: document.documentUuid })
.evaluations.detail(evaluation.uuid).root,
.evaluations.detail(evaluation.id).root,
)
}
>
Expand All @@ -57,13 +57,11 @@ export default function BatchEvaluationsTable({
</TableCell>
<TableCell onClick={(e) => e.stopPropagation()}>
<Link
href={
ROUTES.projects
.detail({ id: project.id })
.commits.detail({ uuid: commit.uuid })
.documents.detail({ uuid: document.documentUuid })
.evaluations.detail(evaluation.uuid).destroy
}
href={ROUTES.projects
.detail({ id: project.id })
.commits.detail({ uuid: commit.uuid })
.documents.detail({ uuid: document.documentUuid })
.evaluations.dashboard.destroy(evaluation.id)}
>
<Icon name='trash' />
</Link>
Expand Down
Loading

0 comments on commit 20bf409

Please sign in to comment.