-
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.
This commit implements the flow to execute a batch evaluation up until the point of actually running the evaluation.
- Loading branch information
Showing
39 changed files
with
1,059 additions
and
273 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
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,171 @@ | ||
import { | ||
Commit, | ||
Dataset, | ||
DocumentVersion, | ||
EvaluationDto, | ||
Project, | ||
ProviderApiKey, | ||
Providers, | ||
User, | ||
Workspace, | ||
} from '@latitude-data/core/browser' | ||
import * as factories from '@latitude-data/core/factories' | ||
import { queues } from '@latitude-data/jobs' | ||
import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
||
import { runBatchAction } from './runBatch' | ||
|
||
const mocks = vi.hoisted(() => ({ | ||
getSession: vi.fn(), | ||
})) | ||
|
||
vi.mock('$/services/auth/getSession', () => ({ | ||
getSession: mocks.getSession, | ||
})) | ||
|
||
vi.mock('@latitude-data/jobs', () => ({ | ||
queues: { | ||
defaultQueue: { | ||
addRunBatchEvaluationJob: vi.fn(), | ||
}, | ||
eventsQueue: { | ||
addCreateEventJob: vi.fn(), | ||
addPublishEventJob: vi.fn(), | ||
}, | ||
}, | ||
})) | ||
|
||
describe('runBatchAction', () => { | ||
describe('unauthorized', () => { | ||
it('errors when the user is not authenticated', async () => { | ||
const [_, error] = await runBatchAction({ | ||
datasetId: 1, | ||
projectId: 1, | ||
documentUuid: 'doc-uuid', | ||
commitUuid: 'commit-uuid', | ||
runCount: 5, | ||
evaluationId: 1, | ||
}) | ||
|
||
expect(error!.name).toEqual('UnauthorizedError') | ||
}) | ||
}) | ||
|
||
describe('authorized', () => { | ||
let workspace: Workspace, | ||
user: User, | ||
project: Project, | ||
document: DocumentVersion, | ||
commit: Commit, | ||
dataset: Dataset, | ||
evaluation: EvaluationDto, | ||
provider: ProviderApiKey | ||
|
||
beforeEach(async () => { | ||
const setup = await factories.createProject({ | ||
documents: { 'test-doc': 'Test content' }, | ||
}) | ||
workspace = setup.workspace | ||
user = setup.user | ||
project = setup.project | ||
document = setup.documents[0]! | ||
commit = setup.commit | ||
|
||
provider = await factories.createProviderApiKey({ | ||
workspace, | ||
type: Providers.OpenAI, | ||
name: 'Test Provider', | ||
user, | ||
}) | ||
|
||
dataset = await factories | ||
.createDataset({ | ||
name: 'Test Dataset', | ||
workspace, | ||
author: user, | ||
}) | ||
.then((result) => result.dataset) | ||
|
||
evaluation = await factories.createEvaluation({ | ||
provider, | ||
name: 'Test Evaluation', | ||
}) | ||
|
||
mocks.getSession.mockReturnValue({ | ||
user, | ||
workspace: { id: workspace.id, name: workspace.name }, | ||
}) | ||
}) | ||
|
||
it('successfully enqueues a batch evaluation job', async () => { | ||
const [result, error] = await runBatchAction({ | ||
datasetId: dataset.id, | ||
projectId: project.id, | ||
documentUuid: document.documentUuid, | ||
commitUuid: commit.uuid, | ||
runCount: 5, | ||
evaluationId: evaluation.id, | ||
}) | ||
|
||
expect(error).toBeNull() | ||
expect(result).toEqual({ | ||
success: true, | ||
batchId: expect.any(String), | ||
}) | ||
|
||
expect(queues.defaultQueue.addRunBatchEvaluationJob).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
evaluation: expect.objectContaining({ id: evaluation.id }), | ||
dataset: expect.objectContaining({ id: dataset.id }), | ||
document: expect.objectContaining({ | ||
documentUuid: document.documentUuid, | ||
}), | ||
runCount: 5, | ||
offset: 0, | ||
parametersMap: undefined, | ||
batchId: expect.any(String), | ||
}), | ||
) | ||
}) | ||
|
||
it('handles optional parameters', async () => { | ||
const [result, error] = await runBatchAction({ | ||
datasetId: dataset.id, | ||
projectId: project.id, | ||
documentUuid: document.documentUuid, | ||
commitUuid: commit.uuid, | ||
runCount: 5, | ||
evaluationId: evaluation.id, | ||
offset: 10, | ||
parameters: { 1: 100, 2: 200 }, | ||
}) | ||
|
||
expect(error).toBeNull() | ||
expect(result).toEqual({ | ||
success: true, | ||
batchId: expect.any(String), | ||
}) | ||
|
||
expect(queues.defaultQueue.addRunBatchEvaluationJob).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
offset: 10, | ||
parametersMap: { 1: 100, 2: 200 }, | ||
}), | ||
) | ||
}) | ||
|
||
it('handles errors when resources are not found', async () => { | ||
const [_, error] = await runBatchAction({ | ||
datasetId: 999999, | ||
projectId: project.id, | ||
documentUuid: document.documentUuid, | ||
commitUuid: commit.uuid, | ||
runCount: 5, | ||
evaluationId: evaluation.id, | ||
}) | ||
|
||
expect(error).not.toBeNull() | ||
expect(error!.message).toContain('not found') | ||
}) | ||
}) | ||
}) |
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,58 @@ | ||
import { | ||
DatasetsRepository, | ||
DocumentVersionsRepository, | ||
EvaluationsRepository, | ||
} from '@latitude-data/core/repositories' | ||
import { queues } from '@latitude-data/jobs' | ||
import { nanoid } from 'nanoid' | ||
import { z } from 'zod' | ||
|
||
import { authProcedure } from '../procedures' | ||
|
||
export const runBatchAction = authProcedure | ||
.createServerAction() | ||
.input( | ||
z.object({ | ||
datasetId: z.number(), | ||
projectId: z.number(), | ||
documentUuid: z.string(), | ||
commitUuid: z.string(), | ||
runCount: z.number(), | ||
offset: z.number().optional().default(0), | ||
parameters: z.record(z.number()).optional(), | ||
evaluationId: z.number(), | ||
}), | ||
) | ||
.handler(async ({ input, ctx }) => { | ||
const evaluationsRepo = new EvaluationsRepository(ctx.workspace.id) | ||
const evaluation = await evaluationsRepo | ||
.find(input.evaluationId) | ||
.then((r) => r.unwrap()) | ||
|
||
const datasetsRepo = new DatasetsRepository(ctx.workspace.id) | ||
const dataset = await datasetsRepo | ||
.find(input.datasetId) | ||
.then((r) => r.unwrap()) | ||
|
||
const docsRepo = new DocumentVersionsRepository(ctx.workspace.id) | ||
const document = await docsRepo | ||
.getDocumentAtCommit({ | ||
projectId: input.projectId, | ||
commitUuid: input.commitUuid, | ||
documentUuid: input.documentUuid, | ||
}) | ||
.then((r) => r.unwrap()) | ||
|
||
const batchId = nanoid() | ||
await queues.defaultQueue.addRunBatchEvaluationJob({ | ||
evaluation, | ||
dataset, | ||
document, | ||
runCount: input.runCount, | ||
offset: input.offset, | ||
parametersMap: input.parameters, | ||
batchId, | ||
}) | ||
|
||
return { success: true, batchId } | ||
}) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.