nsarrazin HF staff commited on
Commit
e91b76c
1 Parent(s): f2c9036

fix auth on websearch (#410)

Browse files
src/lib/buildPrompt.ts CHANGED
@@ -2,24 +2,42 @@ import type { BackendModel } from "./server/models";
2
  import type { Message } from "./types/Message";
3
  import { collections } from "$lib/server/database";
4
  import { ObjectId } from "mongodb";
 
5
  /**
6
  * Convert [{user: "assistant", content: "hi"}, {user: "user", content: "hello"}] to:
7
  *
8
  * <|assistant|>hi<|endoftext|><|prompter|>hello<|endoftext|><|assistant|>
9
  */
10
 
11
- export async function buildPrompt(
12
- messages: Pick<Message, "from" | "content">[],
13
- model: BackendModel,
14
- webSearchId?: string,
15
- preprompt?: string
16
- ): Promise<string> {
 
 
 
 
 
 
 
 
 
17
  if (webSearchId) {
18
  const webSearch = await collections.webSearches.findOne({
19
  _id: new ObjectId(webSearchId),
20
  });
21
 
22
  if (!webSearch) throw new Error("Web search not found");
 
 
 
 
 
 
 
 
23
 
24
  if (webSearch.summary) {
25
  messages = [
 
2
  import type { Message } from "./types/Message";
3
  import { collections } from "$lib/server/database";
4
  import { ObjectId } from "mongodb";
5
+ import { authCondition } from "./server/auth";
6
  /**
7
  * Convert [{user: "assistant", content: "hi"}, {user: "user", content: "hello"}] to:
8
  *
9
  * <|assistant|>hi<|endoftext|><|prompter|>hello<|endoftext|><|assistant|>
10
  */
11
 
12
+ interface buildPromptOptions {
13
+ messages: Pick<Message, "from" | "content">[];
14
+ model: BackendModel;
15
+ locals?: App.Locals;
16
+ webSearchId?: string;
17
+ preprompt?: string;
18
+ }
19
+
20
+ export async function buildPrompt({
21
+ messages,
22
+ model,
23
+ locals,
24
+ webSearchId,
25
+ preprompt,
26
+ }: buildPromptOptions): Promise<string> {
27
  if (webSearchId) {
28
  const webSearch = await collections.webSearches.findOne({
29
  _id: new ObjectId(webSearchId),
30
  });
31
 
32
  if (!webSearch) throw new Error("Web search not found");
33
+ if (!locals) throw new Error("User not authenticated");
34
+
35
+ const conversation = await collections.conversations.findOne({
36
+ _id: webSearch.convId,
37
+ ...authCondition(locals),
38
+ });
39
+
40
+ if (!conversation) throw new Error("Conversation not found");
41
 
42
  if (webSearch.summary) {
43
  messages = [
src/routes/conversation/[id]/+server.ts CHANGED
@@ -98,12 +98,13 @@ export async function POST({ request, fetch, locals, params }) {
98
  ];
99
  })() satisfies Message[];
100
 
101
- const prompt = await buildPrompt(
102
  messages,
103
  model,
104
- web_search_id,
105
- settings?.customPrompts?.[model.id]
106
- );
 
107
 
108
  const randomEndpoint = modelEndpoint(model);
109
 
 
98
  ];
99
  })() satisfies Message[];
100
 
101
+ const prompt = await buildPrompt({
102
  messages,
103
  model,
104
+ webSearchId: web_search_id,
105
+ preprompt: settings?.customPrompts?.[model.id],
106
+ locals: locals,
107
+ });
108
 
109
  const randomEndpoint = modelEndpoint(model);
110
 
src/routes/conversation/[id]/message/[messageId]/prompt/+server.ts CHANGED
@@ -31,7 +31,10 @@ export async function GET({ params, locals }) {
31
  throw error(404, "Conversation model not found");
32
  }
33
 
34
- const prompt = await buildPrompt(conv.messages.slice(0, messageIndex + 1), model);
 
 
 
35
 
36
  return new Response(
37
  JSON.stringify(
 
31
  throw error(404, "Conversation model not found");
32
  }
33
 
34
+ const prompt = await buildPrompt({
35
+ messages: conv.messages.slice(0, messageIndex + 1),
36
+ model: model,
37
+ });
38
 
39
  return new Response(
40
  JSON.stringify(
src/routes/conversation/[id]/summarize/+server.ts CHANGED
@@ -24,7 +24,10 @@ export async function POST({ params, locals }) {
24
  `Please summarize the following message as a single sentence of less than 5 words:\n` +
25
  firstMessage?.content;
26
 
27
- const prompt = await buildPrompt([{ from: "user", content: userPrompt }], defaultModel);
 
 
 
28
  const generated_text = await generateFromDefaultEndpoint(prompt);
29
 
30
  if (generated_text) {
 
24
  `Please summarize the following message as a single sentence of less than 5 words:\n` +
25
  firstMessage?.content;
26
 
27
+ const prompt = await buildPrompt({
28
+ messages: [{ from: "user", content: userPrompt }],
29
+ model: defaultModel,
30
+ });
31
  const generated_text = await generateFromDefaultEndpoint(prompt);
32
 
33
  if (generated_text) {
src/routes/r/[id]/message/[messageId]/prompt/+server.ts CHANGED
@@ -26,7 +26,7 @@ export async function GET({ params }) {
26
  throw error(404, "Conversation model not found");
27
  }
28
 
29
- const prompt = await buildPrompt(conv.messages.slice(0, messageIndex + 1), model);
30
 
31
  return new Response(
32
  JSON.stringify(
 
26
  throw error(404, "Conversation model not found");
27
  }
28
 
29
+ const prompt = await buildPrompt({ messages: conv.messages.slice(0, messageIndex + 1), model });
30
 
31
  return new Response(
32
  JSON.stringify(