coyotte508 HF staff commited on
Commit
06e879d
1 Parent(s): 7764421

🗃️ Store model inside conversation instead (#165)

Browse files
src/hooks.server.ts CHANGED
@@ -6,7 +6,6 @@ import {
6
  PUBLIC_DEPRECATED_GOOGLE_ANALYTICS_ID,
7
  } from "$env/static/public";
8
  import { addYears } from "date-fns";
9
- import { inspect } from "node:util";
10
 
11
  export const handle: Handle = async ({ event, resolve }) => {
12
  const token = event.cookies.get(COOKIE_NAME);
 
6
  PUBLIC_DEPRECATED_GOOGLE_ANALYTICS_ID,
7
  } from "$env/static/public";
8
  import { addYears } from "date-fns";
 
9
 
10
  export const handle: Handle = async ({ event, resolve }) => {
11
  const token = event.cookies.get(COOKIE_NAME);
src/lib/types/Conversation.ts CHANGED
@@ -8,6 +8,8 @@ export interface Conversation extends Timestamps {
8
  // Can be undefined for shared convo then deleted
9
  sessionId: string;
10
 
 
 
11
  title: string;
12
  messages: Message[];
13
 
 
8
  // Can be undefined for shared convo then deleted
9
  sessionId: string;
10
 
11
+ model: string;
12
+
13
  title: string;
14
  messages: Message[];
15
 
src/lib/types/Message.ts CHANGED
@@ -2,6 +2,4 @@ export interface Message {
2
  from: "user" | "assistant";
3
  id: ReturnType<typeof crypto.randomUUID>;
4
  content: string;
5
- // Only for "assistant" messages
6
- model?: string;
7
  }
 
2
  from: "user" | "assistant";
3
  id: ReturnType<typeof crypto.randomUUID>;
4
  content: string;
 
 
5
  }
src/lib/types/SharedConversation.ts CHANGED
@@ -6,6 +6,7 @@ export interface SharedConversation extends Timestamps {
6
 
7
  hash: string;
8
 
 
9
  title: string;
10
  messages: Message[];
11
  }
 
6
 
7
  hash: string;
8
 
9
+ model: string;
10
  title: string;
11
  messages: Message[];
12
  }
src/routes/+page.svelte CHANGED
@@ -16,6 +16,7 @@
16
  headers: {
17
  "Content-Type": "application/json",
18
  },
 
19
  });
20
 
21
  if (!res.ok) {
 
16
  headers: {
17
  "Content-Type": "application/json",
18
  },
19
+ body: JSON.stringify({ model: data.models[0].name }),
20
  });
21
 
22
  if (!res.ok) {
src/routes/conversation/+server.ts CHANGED
@@ -5,29 +5,35 @@ import { error, redirect } from "@sveltejs/kit";
5
  import { base } from "$app/paths";
6
  import { z } from "zod";
7
  import type { Message } from "$lib/types/Message";
 
8
 
9
  export const POST: RequestHandler = async (input) => {
10
  const body = await input.request.text();
11
 
12
  let title = "";
13
  let messages: Message[] = [];
14
- let fromShareId: string | undefined;
15
 
16
- if (body) {
17
- fromShareId = z.object({ fromShare: z.string().optional() }).parse(JSON.parse(body)).fromShare;
 
 
 
 
 
 
18
 
19
- if (fromShareId) {
20
- const conversation = await collections.sharedConversations.findOne({
21
- _id: fromShareId,
22
- });
23
 
24
- if (!conversation) {
25
- throw error(404, "Conversation not found");
26
- }
27
-
28
- title = conversation.title;
29
- messages = conversation.messages;
30
  }
 
 
 
 
31
  }
32
 
33
  const res = await collections.conversations.insertOne({
@@ -38,10 +44,11 @@ export const POST: RequestHandler = async (input) => {
38
  ((await collections.conversations.countDocuments({ sessionId: input.locals.sessionId })) +
39
  1),
40
  messages,
 
41
  createdAt: new Date(),
42
  updatedAt: new Date(),
43
  sessionId: input.locals.sessionId,
44
- ...(fromShareId ? { meta: { fromShareId } } : {}),
45
  });
46
 
47
  return new Response(
 
5
  import { base } from "$app/paths";
6
  import { z } from "zod";
7
  import type { Message } from "$lib/types/Message";
8
+ import { defaultModel, modelNames } from "$lib/server/models";
9
 
10
  export const POST: RequestHandler = async (input) => {
11
  const body = await input.request.text();
12
 
13
  let title = "";
14
  let messages: Message[] = [];
 
15
 
16
+ const values = z
17
+ .object({
18
+ fromShare: z.string().optional(),
19
+ model: z
20
+ .enum([modelNames[0].name, ...modelNames.slice(1).map((m) => m.name)])
21
+ .default(defaultModel.name),
22
+ })
23
+ .parse(JSON.parse(body));
24
 
25
+ if (values.fromShare) {
26
+ const conversation = await collections.sharedConversations.findOne({
27
+ _id: values.fromShare,
28
+ });
29
 
30
+ if (!conversation) {
31
+ throw error(404, "Conversation not found");
 
 
 
 
32
  }
33
+
34
+ title = conversation.title;
35
+ messages = conversation.messages;
36
+ values.model = conversation.model;
37
  }
38
 
39
  const res = await collections.conversations.insertOne({
 
44
  ((await collections.conversations.countDocuments({ sessionId: input.locals.sessionId })) +
45
  1),
46
  messages,
47
+ model: values.model,
48
  createdAt: new Date(),
49
  updatedAt: new Date(),
50
  sessionId: input.locals.sessionId,
51
+ ...(values.fromShare ? { meta: { fromShareId: values.fromShare } } : {}),
52
  });
53
 
54
  return new Response(
src/routes/conversation/[id]/+server.ts CHANGED
@@ -3,7 +3,7 @@ import { buildPrompt } from "$lib/buildPrompt.js";
3
  import { abortedGenerations } from "$lib/server/abortedGenerations.js";
4
  import { collections } from "$lib/server/database.js";
5
  import { modelEndpoint } from "$lib/server/modelEndpoint.js";
6
- import { defaultModel, modelNames } from "$lib/server/models.js";
7
  import type { Message } from "$lib/types/Message.js";
8
  import { concatUint8Arrays } from "$lib/utils/concatUint8Arrays.js";
9
  import { streamToAsyncIterable } from "$lib/utils/streamToAsyncIterable";
@@ -28,17 +28,15 @@ export async function POST({ request, fetch, locals, params }) {
28
  throw error(404, "Conversation not found");
29
  }
30
 
 
 
31
  const json = await request.json();
32
  const {
33
  inputs: newPrompt,
34
- model,
35
  options: { id: messageId, is_retry },
36
  } = z
37
  .object({
38
  inputs: z.string().trim().min(1),
39
- model: z
40
- .enum([modelNames[0].name, ...modelNames.slice(1).map((m) => m.name)])
41
- .default(defaultModel.name),
42
  options: z.object({
43
  id: z.optional(z.string().uuid()),
44
  is_retry: z.optional(z.boolean()),
@@ -104,7 +102,7 @@ export async function POST({ request, fetch, locals, params }) {
104
 
105
  generated_text = trimSuffix(trimPrefix(generated_text, "<|startoftext|>"), PUBLIC_SEP_TOKEN);
106
 
107
- messages.push({ from: "assistant", content: generated_text, id: crypto.randomUUID(), model });
108
 
109
  await collections.conversations.updateOne(
110
  {
 
3
  import { abortedGenerations } from "$lib/server/abortedGenerations.js";
4
  import { collections } from "$lib/server/database.js";
5
  import { modelEndpoint } from "$lib/server/modelEndpoint.js";
6
+ import { defaultModel } from "$lib/server/models.js";
7
  import type { Message } from "$lib/types/Message.js";
8
  import { concatUint8Arrays } from "$lib/utils/concatUint8Arrays.js";
9
  import { streamToAsyncIterable } from "$lib/utils/streamToAsyncIterable";
 
28
  throw error(404, "Conversation not found");
29
  }
30
 
31
+ const model = conv.model ?? defaultModel.name;
32
+
33
  const json = await request.json();
34
  const {
35
  inputs: newPrompt,
 
36
  options: { id: messageId, is_retry },
37
  } = z
38
  .object({
39
  inputs: z.string().trim().min(1),
 
 
 
40
  options: z.object({
41
  id: z.optional(z.string().uuid()),
42
  is_retry: z.optional(z.boolean()),
 
102
 
103
  generated_text = trimSuffix(trimPrefix(generated_text, "<|startoftext|>"), PUBLIC_SEP_TOKEN);
104
 
105
+ messages.push({ from: "assistant", content: generated_text, id: crypto.randomUUID() });
106
 
107
  await collections.conversations.updateOne(
108
  {
src/routes/conversation/[id]/share/+server.ts CHANGED
@@ -37,6 +37,7 @@ export async function POST({ params, url, locals }) {
37
  hash,
38
  updatedAt: new Date(),
39
  title: conversation.title,
 
40
  };
41
 
42
  await collections.sharedConversations.insertOne(shared);
 
37
  hash,
38
  updatedAt: new Date(),
39
  title: conversation.title,
40
+ model: conversation.model,
41
  };
42
 
43
  await collections.sharedConversations.insertOne(shared);