Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

Get generations (#146)

Maximilian Deichmanndfb18d8d686b
diff --git a/fern/server/definition/generations.yml b/fern/server/definition/generations.yml
index 0da37e6d58..af0c552321 100644
--- a/fern/server/definition/generations.yml
+++ b/fern/server/definition/generations.yml
@@ -1,6 +1,7 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/fern-api/fern/main/fern.schema.json
imports:
commons: ./commons.yml
+ pagination: ./utils/pagination.yml
service:
auth: true
base-path: /api/public
@@ -15,6 +16,17 @@ service:
path: /generations
request: UpdateGenerationRequest
response: commons.Observation
+ get:
+ docs: Get a list of generations
+ method: GET
+ path: /generations
+ request:
+ name: GetGenerationsRequest
+ query-parameters:
+ page: optional<integer>
+ limit: optional<integer>
+ name: optional<string>
+ response: Generations
types:
UpdateGenerationRequest:
@@ -33,3 +45,7 @@ types:
usage: optional<commons.LLMUsage>
level: optional<commons.ObservationLevel>
statusMessage: optional<string>
+ Generations:
+ properties:
+ data: list<commons.Observation>
+ meta: pagination.MetaResponse
diff --git a/fern/server/definition/observations.yml b/fern/server/definition/observations.yml
index 5da7beef96..dcfabf849f 100644
--- a/fern/server/definition/observations.yml
+++ b/fern/server/definition/observations.yml
@@ -1,6 +1,7 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/fern-api/fern/main/fern.schema.json
imports:
commons: ./commons.yml
+ pagination: ./utils/pagination.yml
service:
auth: true
base-path: /api/public
diff --git a/generated/openapi-server/openapi.yml b/generated/openapi-server/openapi.yml
index 5683a6027c..59597e0166 100644
--- a/generated/openapi-server/openapi.yml
+++ b/generated/openapi-server/openapi.yml
@@ -154,6 +154,68 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/UpdateGenerationRequest'
+ get:
+ description: Get a list of generations
+ operationId: generations_get
+ tags:
+ - Generations
+ parameters:
+ - name: page
+ in: query
+ required: false
+ schema:
+ type: integer
+ nullable: true
+ - name: limit
+ in: query
+ required: false
+ schema:
+ type: integer
+ nullable: true
+ - name: name
+ in: query
+ required: false
+ schema:
+ type: string
+ nullable: true
+ responses:
+ '200':
+ description: ''
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/Generations'
+ '400':
+ description: ''
+ content:
+ application/json:
+ schema:
+ type: string
+ '401':
+ description: ''
+ content:
+ application/json:
+ schema:
+ type: string
+ '403':
+ description: ''
+ content:
+ application/json:
+ schema:
+ type: string
+ '404':
+ description: ''
+ content:
+ application/json:
+ schema:
+ type: string
+ '405':
+ description: ''
+ content:
+ application/json:
+ schema:
+ type: string
+ security: *ref_0
/api/public/observations/{observationId}:
get:
description: Get a specific observation
@@ -917,6 +979,19 @@ components:
nullable: true
required:
- generationId
+ Generations:
+ title: Generations
+ type: object
+ properties:
+ data:
+ type: array
+ items:
+ $ref: '#/components/schemas/Observation'
+ meta:
+ $ref: '#/components/schemas/utilsMetaResponse'
+ required:
+ - data
+ - meta
CreateScoreRequest:
title: CreateScoreRequest
type: object
diff --git a/src/pages/api/public/generations.ts b/src/pages/api/public/generations.ts
index 019387440e..cff1b0659f 100644
--- a/src/pages/api/public/generations.ts
+++ b/src/pages/api/public/generations.ts
@@ -13,6 +13,12 @@ import { v4 as uuidv4 } from "uuid";
import { backOff } from "exponential-backoff";
import { RessourceNotFoundError } from "../../../utils/exceptions";
+const GenerationsGetSchema = z.object({
+ page: z.coerce.number().int().positive().default(1),
+ limit: z.coerce.number().int().positive().lte(100).default(50),
+ name: z.string().nullish(),
+});
+
export const GenerationsCreateSchema = z.object({
id: z.string().nullish(),
traceId: z.string().nullish(),
@@ -285,6 +291,50 @@ export default async function handler(
});
}
+ const errorMessage =
+ error instanceof Error ? error.message : "An unknown error occurred";
+ res.status(400).json({
+ success: false,
+ message: "Invalid request data",
+ error: errorMessage,
+ });
+ }
+ } else if (req.method === "GET") {
+ try {
+ console.log(
+ "trying to get generation, project ",
+ authCheck.scope.projectId,
+ ", body:",
+ JSON.stringify(req.query, null, 2),
+ );
+
+ if (authCheck.scope.accessLevel !== "all") {
+ return res.status(401).json({
+ success: false,
+ message:
+ "Access denied - need to use basic auth with secret key to GET generations",
+ });
+ }
+
+ const searchParams = GenerationsGetSchema.parse(req.query);
+
+ const [generations, totalGenerations] = await getGenerations(
+ prisma,
+ authCheck.scope.projectId,
+ searchParams,
+ );
+
+ return res.status(200).json({
+ data: generations,
+ meta: {
+ page: searchParams.page,
+ limit: searchParams.limit,
+ totalItems: totalGenerations,
+ totalPages: Math.ceil(totalGenerations / searchParams.limit),
+ },
+ });
+ } catch (error: unknown) {
+ console.error(error);
const errorMessage =
error instanceof Error ? error.message : "An unknown error occurred";
res.status(400).json({
@@ -298,6 +348,31 @@ export default async function handler(
}
}
+const getGenerations = async (
+ prisma: PrismaClient,
+ authenticatedProjectId: string,
+ query: z.infer<typeof GenerationsGetSchema>,
+) => {
+ return await Promise.all([
+ prisma.observation.findMany({
+ where: {
+ projectId: authenticatedProjectId,
+ type: ObservationType.GENERATION,
+ ...(query.name ? { name: query.name } : undefined),
+ },
+ skip: (query.page - 1) * query.limit,
+ take: query.limit,
+ }),
+ prisma.observation.count({
+ where: {
+ projectId: authenticatedProjectId,
+ type: ObservationType.GENERATION,
+ ...(query.name ? { name: query.name } : undefined),
+ },
+ }),
+ ]);
+};
+
const patchGeneration = async (
prisma: PrismaClient,
generationPatch: z.infer<typeof GenerationPatchSchema>,