Skip to content

Commit c30d191

Browse files
alakKevin CATHALYnsarrazincoyotte508
authored
Add an endpoints to expose models and conversations (#694)
* Add an endpoint to expose available models * Add a route to list conversations history * use authCondition to filter current user's conversations * map models object to only keep needed params and also change the route path to /api/models * Put API feature behind feature flag (#695) * Puts the API behind a feature flag * Update src/hooks.server.ts Co-authored-by: Eliott C. <coyotte508@gmail.com> --------- Co-authored-by: Eliott C. <coyotte508@gmail.com> * add a better error when session's cookie is missing * rename error to message to be consistent --------- Co-authored-by: Kevin CATHALY <alakme@kevins-mbp.home> Co-authored-by: Nathan Sarrazin <sarrazin.nathan@gmail.com> Co-authored-by: Eliott C. <coyotte508@gmail.com>
1 parent 41f8b74 commit c30d191

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ PUBLIC_APP_DATA_SHARING=#set to 1 to enable options & text regarding data sharin
120120
PUBLIC_APP_DISCLAIMER=#set to 1 to show a disclaimer on login page
121121
LLM_SUMMERIZATION=true
122122

123+
EXPOSE_API=true
123124
# PUBLIC_APP_NAME=HuggingChat
124125
# PUBLIC_APP_ASSETS=huggingchat
125126
# PUBLIC_APP_COLOR=yellow

.env.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,5 @@ PUBLIC_GOOGLE_ANALYTICS_ID=G-8Q63TH4CSL
225225
# Not part of the .env but set as other variables in the space
226226
# ADDRESS_HEADER=X-Forwarded-For
227227
# XFF_DEPTH=2
228+
229+
EXPOSE_API=false

src/hooks.server.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { COOKIE_NAME, MESSAGES_BEFORE_LOGIN } from "$env/static/private";
1+
import { COOKIE_NAME, EXPOSE_API, MESSAGES_BEFORE_LOGIN } from "$env/static/private";
22
import type { Handle } from "@sveltejs/kit";
33
import {
44
PUBLIC_GOOGLE_ANALYTICS_ID,
@@ -13,6 +13,10 @@ import { sha256 } from "$lib/utils/sha256";
1313
import { addWeeks } from "date-fns";
1414

1515
export const handle: Handle = async ({ event, resolve }) => {
16+
if (event.url.pathname.startsWith(`${base}/api/`) && EXPOSE_API !== "true") {
17+
return new Response("API is disabled", { status: 403 });
18+
}
19+
1620
function errorResponse(status: number, message: string) {
1721
const sendJson =
1822
event.request.headers.get("accept")?.includes("application/json") ||
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { collections } from "$lib/server/database";
2+
import { authCondition } from "$lib/server/auth";
3+
4+
export async function GET({ locals }) {
5+
if (locals.user?._id || locals.sessionId) {
6+
const res = await collections.conversations
7+
.find({
8+
...authCondition(locals),
9+
})
10+
.toArray();
11+
12+
return Response.json(res);
13+
} else {
14+
return Response.json({ message: "Must have session cookie" }, { status: 401 });
15+
}
16+
}

src/routes/api/models/+server.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { models } from "$lib/server/models";
2+
3+
export async function GET() {
4+
const res = models.map((model) => ({
5+
id: model.id,
6+
name: model.name,
7+
websiteUrl: model.websiteUrl,
8+
modelUrl: model.modelUrl,
9+
datasetName: model.datasetName,
10+
datasetUrl: model.datasetUrl,
11+
displayName: model.displayName,
12+
description: model.description,
13+
promptExamples: model.promptExamples,
14+
preprompt: model.preprompt,
15+
multimodal: model.multimodal,
16+
unlisted: model.unlisted,
17+
}));
18+
return Response.json(res);
19+
}

0 commit comments

Comments
 (0)