Kevin CATHALY Kevin CATHALY nsarrazin HF staff coyotte508 HF staff commited on
Commit
c30d191
1 Parent(s): 41f8b74

Add an endpoints to expose models and conversations (#694)

Browse files

* 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>

.env CHANGED
@@ -120,6 +120,7 @@ PUBLIC_APP_DATA_SHARING=#set to 1 to enable options & text regarding data sharin
120
  PUBLIC_APP_DISCLAIMER=#set to 1 to show a disclaimer on login page
121
  LLM_SUMMERIZATION=true
122
 
 
123
  # PUBLIC_APP_NAME=HuggingChat
124
  # PUBLIC_APP_ASSETS=huggingchat
125
  # PUBLIC_APP_COLOR=yellow
 
120
  PUBLIC_APP_DISCLAIMER=#set to 1 to show a disclaimer on login page
121
  LLM_SUMMERIZATION=true
122
 
123
+ EXPOSE_API=true
124
  # PUBLIC_APP_NAME=HuggingChat
125
  # PUBLIC_APP_ASSETS=huggingchat
126
  # PUBLIC_APP_COLOR=yellow
.env.template CHANGED
@@ -225,3 +225,5 @@ PUBLIC_GOOGLE_ANALYTICS_ID=G-8Q63TH4CSL
225
  # Not part of the .env but set as other variables in the space
226
  # ADDRESS_HEADER=X-Forwarded-For
227
  # XFF_DEPTH=2
 
 
 
225
  # Not part of the .env but set as other variables in the space
226
  # ADDRESS_HEADER=X-Forwarded-For
227
  # XFF_DEPTH=2
228
+
229
+ EXPOSE_API=false
src/hooks.server.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { COOKIE_NAME, MESSAGES_BEFORE_LOGIN } from "$env/static/private";
2
  import type { Handle } from "@sveltejs/kit";
3
  import {
4
  PUBLIC_GOOGLE_ANALYTICS_ID,
@@ -13,6 +13,10 @@ import { sha256 } from "$lib/utils/sha256";
13
  import { addWeeks } from "date-fns";
14
 
15
  export const handle: Handle = async ({ event, resolve }) => {
 
 
 
 
16
  function errorResponse(status: number, message: string) {
17
  const sendJson =
18
  event.request.headers.get("accept")?.includes("application/json") ||
 
1
+ import { COOKIE_NAME, EXPOSE_API, MESSAGES_BEFORE_LOGIN } from "$env/static/private";
2
  import type { Handle } from "@sveltejs/kit";
3
  import {
4
  PUBLIC_GOOGLE_ANALYTICS_ID,
 
13
  import { addWeeks } from "date-fns";
14
 
15
  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
+
20
  function errorResponse(status: number, message: string) {
21
  const sendJson =
22
  event.request.headers.get("accept")?.includes("application/json") ||
src/routes/api/conversations/+server.ts ADDED
@@ -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 ADDED
@@ -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
+ }