-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rohit/s3 en 2232 get prompts api #88
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4551d7e
untrack .env
rohit-kadhe c7b05ca
Revert "untrack .env"
rohit-kadhe f67f7fb
Merge branch 'development' of github.com:Scale3-Labs/langtrace into r…
rohit-kadhe 7f32b5a
Merge branch 'development' of github.com:Scale3-Labs/langtrace into r…
rohit-kadhe fd347a2
api to fetch promptset with prompt filters
rohit-kadhe 66c1293
bug fixes
rohit-kadhe e039e5c
fix invalid redirect
rohit-kadhe c3f6ade
fix invalid status code
rohit-kadhe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,204 @@ | ||
import { authOptions } from "@/lib/auth/options"; | ||
import prisma from "@/lib/prisma"; | ||
import { authApiKey, fillPromptStringTemplate, parseQueryString } from "@/lib/utils"; | ||
import { getServerSession } from "next-auth"; | ||
import { redirect } from "next/navigation"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export async function GET(req: NextRequest) { | ||
try { | ||
const session = await getServerSession(authOptions); | ||
if (!session || !session.user) { | ||
redirect("/login"); | ||
} | ||
|
||
const id = req.nextUrl.searchParams.get("id") as string; | ||
const promptsetId = req.nextUrl.searchParams.get("promptset_id") as string; | ||
const pageParam = req.nextUrl.searchParams.get("page"); | ||
let page = pageParam ? parseInt(pageParam, 10) : 1; | ||
const pageSizeParam = req.nextUrl.searchParams.get("pageSize"); | ||
const pageSize = pageSizeParam ? parseInt(pageSizeParam, 10) : 10; | ||
|
||
if (!promptsetId && !id) { | ||
return NextResponse.json( | ||
{ | ||
message: "No promptset id or project id provided", | ||
}, | ||
{ status: 404 } | ||
); | ||
} | ||
const apiKey = req.headers.get("x-api-key"); | ||
if (apiKey !== null) { | ||
const response = await authApiKey(apiKey); | ||
if (response.status !== 200) { | ||
return response; | ||
} | ||
const { promptset_id, variables, version } = parseQueryString(req.url); | ||
|
||
if (promptsetId) { | ||
// get the dataset and all the data for this dataset and sort them by createdAt | ||
const promptset = await prisma.promptset.findFirst({ | ||
const projectData = await response.json(); | ||
const projectId = projectData.data.project.id; | ||
const promptSet = await prisma.promptset.findFirst({ | ||
where: { | ||
id: promptsetId, | ||
projectId: projectId, | ||
id: promptset_id as string, | ||
}, | ||
include: { | ||
Prompt: { | ||
where: { | ||
OR: version !== undefined ? [{ version: version }] : [{ live: true }], | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
if (!promptset) { | ||
let prompts = promptSet?.Prompt ?? []; | ||
if (prompts.length === 0 && version === undefined) { | ||
return NextResponse.json( | ||
{ | ||
message: "No promptset found", | ||
error: "No live prompts found. A prompt version must be specified", | ||
}, | ||
{ status: 400 } | ||
); | ||
} else if (prompts.length === 0 && version !== undefined) { | ||
return NextResponse.json( | ||
{ | ||
error: "No prompts found with the specified version", | ||
}, | ||
{ status: 404 } | ||
); | ||
} | ||
if (variables !== undefined) { | ||
const errors: string[] = []; | ||
const variablesSet = new Set( | ||
Object.entries(variables as Record<string, string>).map((variable) => | ||
variable.join(",") | ||
) | ||
); | ||
const livePromptVariables = prompts[0].variables; | ||
|
||
const totalLen = await prisma.prompt.count({ | ||
where: { | ||
promptsetId: promptset.id, | ||
}, | ||
livePromptVariables.forEach((key) => { | ||
const value = | ||
variables !== null ? variables[key as keyof typeof variables] ?? "" : ""; | ||
if (!variablesSet.has(`${key},${value.length > 0 ? value : "undefined"}`)) { | ||
errors.push(key); | ||
} | ||
}); | ||
if (errors.length > 0) { | ||
const moreThanOneError = errors.length > 1; | ||
return NextResponse.json( | ||
{ | ||
error: `${moreThanOneError ? "Variables" : "Variable"} ${errors.join(", ")} ${moreThanOneError ? "are" : "is"} missing`, | ||
}, | ||
{ status: 400 } | ||
); | ||
} | ||
prompts[0].value = fillPromptStringTemplate(prompts[0].value, variables as Record<string, string>); | ||
} | ||
return NextResponse.json({ | ||
...promptSet, | ||
Prompt: undefined, | ||
prompts: prompts | ||
}); | ||
} else { | ||
const session = await getServerSession(authOptions); | ||
if (!session || !session.user) { | ||
return NextResponse.json( | ||
{ | ||
message: "Unauthorized", | ||
}, | ||
{ status: 401 } | ||
); | ||
} | ||
|
||
const id = req.nextUrl.searchParams.get("id") as string; | ||
const promptsetId = req.nextUrl.searchParams.get( | ||
"promptset_id" | ||
) as string; | ||
const pageParam = req.nextUrl.searchParams.get("page"); | ||
|
||
let page = pageParam ? parseInt(pageParam, 10) : 1; | ||
const pageSizeParam = req.nextUrl.searchParams.get("pageSize"); | ||
const pageSize = pageSizeParam ? parseInt(pageSizeParam, 10) : 10; | ||
|
||
if (!promptsetId && !id) { | ||
return NextResponse.json( | ||
{ | ||
message: "No promptset id or project id provided", | ||
}, | ||
{ status: 404 } | ||
); | ||
} | ||
|
||
if (promptsetId) { | ||
// get the dataset and all the data for this dataset and sort them by createdAt | ||
const promptset = await prisma.promptset.findFirst({ | ||
where: { | ||
id: promptsetId, | ||
}, | ||
}); | ||
|
||
const totalPages = | ||
Math.ceil(totalLen / pageSize) === 0 | ||
? 1 | ||
: Math.ceil(totalLen / pageSize); | ||
const md = { page, page_size: pageSize, total_pages: totalPages }; | ||
if (!promptset) { | ||
return NextResponse.json( | ||
{ | ||
message: "No promptset found", | ||
}, | ||
{ status: 404 } | ||
); | ||
} | ||
|
||
if (page! > totalPages) { | ||
page = totalPages; | ||
const totalLen = await prisma.prompt.count({ | ||
where: { | ||
promptsetId: promptset.id, | ||
}, | ||
}); | ||
|
||
const totalPages = | ||
Math.ceil(totalLen / pageSize) === 0 | ||
? 1 | ||
: Math.ceil(totalLen / pageSize); | ||
const md = { page, page_size: pageSize, total_pages: totalPages }; | ||
|
||
if (page! > totalPages) { | ||
page = totalPages; | ||
} | ||
|
||
const relatedPrompt = await prisma.prompt.findMany({ | ||
where: { | ||
promptsetId: promptset.id, | ||
}, | ||
orderBy: { | ||
createdAt: "desc", | ||
}, | ||
take: pageSize, | ||
skip: (page - 1) * pageSize, | ||
}); | ||
|
||
// Combine dataset with its related, ordered Data | ||
const promptsetWithOrderedData = { | ||
...promptset, | ||
prompts: relatedPrompt, | ||
}; | ||
|
||
return NextResponse.json({ | ||
promptsets: promptsetWithOrderedData, | ||
metadata: md, | ||
}); | ||
} | ||
|
||
const relatedPrompt = await prisma.prompt.findMany({ | ||
const project = await prisma.project.findFirst({ | ||
where: { | ||
id, | ||
}, | ||
}); | ||
if (!project) { | ||
return NextResponse.json( | ||
{ | ||
message: "No projects found", | ||
}, | ||
{ status: 404 } | ||
); | ||
} | ||
// get all the datasets for this project | ||
const promptsets = await prisma.promptset.findMany({ | ||
where: { | ||
promptsetId: promptset.id, | ||
projectId: id, | ||
}, | ||
orderBy: { | ||
createdAt: "desc", | ||
}, | ||
take: pageSize, | ||
skip: (page - 1) * pageSize, | ||
include: { | ||
_count: { | ||
select: { | ||
Prompt: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// Combine dataset with its related, ordered Data | ||
const promptsetWithOrderedData = { | ||
...promptset, | ||
prompts: relatedPrompt, | ||
}; | ||
|
||
return NextResponse.json({ | ||
promptsets: promptsetWithOrderedData, | ||
metadata: md, | ||
promptsets: promptsets, | ||
}); | ||
} | ||
|
||
const project = await prisma.project.findFirst({ | ||
where: { | ||
id, | ||
}, | ||
}); | ||
|
||
if (!project) { | ||
return NextResponse.json( | ||
{ | ||
message: "No projects found", | ||
}, | ||
{ status: 404 } | ||
); | ||
} | ||
|
||
// get all the datasets for this project | ||
const promptsets = await prisma.promptset.findMany({ | ||
where: { | ||
projectId: id, | ||
}, | ||
orderBy: { | ||
createdAt: "desc", | ||
}, | ||
include: { | ||
_count: { | ||
select: { | ||
Prompt: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
return NextResponse.json({ | ||
promptsets: promptsets, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you remove this |
||
return NextResponse.json( | ||
{ | ||
message: "Internal server error", | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you revert this back to redirect("/login"). we need to find a better way to do this but for now this is the approach we are taking for apis used by the client