Skip to content

Commit

Permalink
Add an endpoints to expose models and conversations (#694)
Browse files Browse the repository at this point in the history
* 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. <[email protected]>

---------

Co-authored-by: Eliott C. <[email protected]>

* add a better error when session's cookie is missing

* rename error to message to be consistent

---------

Co-authored-by: Kevin CATHALY <[email protected]>
Co-authored-by: Nathan Sarrazin <[email protected]>
Co-authored-by: Eliott C. <[email protected]>
  • Loading branch information
4 people authored Jan 16, 2024
1 parent 41f8b74 commit c30d191
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ PUBLIC_APP_DATA_SHARING=#set to 1 to enable options & text regarding data sharin
PUBLIC_APP_DISCLAIMER=#set to 1 to show a disclaimer on login page
LLM_SUMMERIZATION=true

EXPOSE_API=true
# PUBLIC_APP_NAME=HuggingChat
# PUBLIC_APP_ASSETS=huggingchat
# PUBLIC_APP_COLOR=yellow
Expand Down
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,5 @@ PUBLIC_GOOGLE_ANALYTICS_ID=G-8Q63TH4CSL
# Not part of the .env but set as other variables in the space
# ADDRESS_HEADER=X-Forwarded-For
# XFF_DEPTH=2

EXPOSE_API=false
6 changes: 5 additions & 1 deletion src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { COOKIE_NAME, MESSAGES_BEFORE_LOGIN } from "$env/static/private";
import { COOKIE_NAME, EXPOSE_API, MESSAGES_BEFORE_LOGIN } from "$env/static/private";
import type { Handle } from "@sveltejs/kit";
import {
PUBLIC_GOOGLE_ANALYTICS_ID,
Expand All @@ -13,6 +13,10 @@ import { sha256 } from "$lib/utils/sha256";
import { addWeeks } from "date-fns";

export const handle: Handle = async ({ event, resolve }) => {
if (event.url.pathname.startsWith(`${base}/api/`) && EXPOSE_API !== "true") {
return new Response("API is disabled", { status: 403 });
}

function errorResponse(status: number, message: string) {
const sendJson =
event.request.headers.get("accept")?.includes("application/json") ||
Expand Down
16 changes: 16 additions & 0 deletions src/routes/api/conversations/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { collections } from "$lib/server/database";
import { authCondition } from "$lib/server/auth";

export async function GET({ locals }) {
if (locals.user?._id || locals.sessionId) {
const res = await collections.conversations
.find({
...authCondition(locals),
})
.toArray();

return Response.json(res);
} else {
return Response.json({ message: "Must have session cookie" }, { status: 401 });
}
}
19 changes: 19 additions & 0 deletions src/routes/api/models/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { models } from "$lib/server/models";

export async function GET() {
const res = models.map((model) => ({
id: model.id,
name: model.name,
websiteUrl: model.websiteUrl,
modelUrl: model.modelUrl,
datasetName: model.datasetName,
datasetUrl: model.datasetUrl,
displayName: model.displayName,
description: model.description,
promptExamples: model.promptExamples,
preprompt: model.preprompt,
multimodal: model.multimodal,
unlisted: model.unlisted,
}));
return Response.json(res);
}

0 comments on commit c30d191

Please sign in to comment.