Spaces:
Paused
Paused
Make sure preprompt is set on open ai endpoint type (#913)
Browse files* Make sure preprompt is set on open ai endpoint type
* fix preprompt passing to conversation
src/lib/server/endpoints/openai/endpointOai.ts
CHANGED
|
@@ -67,7 +67,11 @@ export async function endpointOai(
|
|
| 67 |
}));
|
| 68 |
|
| 69 |
if (messagesOpenAI?.[0]?.role !== "system") {
|
| 70 |
-
messagesOpenAI = [{ role: "system", content:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
}
|
| 72 |
|
| 73 |
return openAIChatToTextGenerationStream(
|
|
|
|
| 67 |
}));
|
| 68 |
|
| 69 |
if (messagesOpenAI?.[0]?.role !== "system") {
|
| 70 |
+
messagesOpenAI = [{ role: "system", content: "" }, ...messagesOpenAI];
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
if (messagesOpenAI?.[0]) {
|
| 74 |
+
messagesOpenAI[0].content = preprompt ?? "";
|
| 75 |
}
|
| 76 |
|
| 77 |
return openAIChatToTextGenerationStream(
|
src/routes/conversation/+server.ts
CHANGED
|
@@ -34,11 +34,29 @@ export const POST: RequestHandler = async ({ locals, request }) => {
|
|
| 34 |
);
|
| 35 |
}
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
let messages: Message[] = [
|
| 38 |
{
|
| 39 |
id: v4(),
|
| 40 |
from: "system",
|
| 41 |
-
content: values.preprompt
|
| 42 |
createdAt: new Date(),
|
| 43 |
updatedAt: new Date(),
|
| 44 |
children: [],
|
|
@@ -67,37 +85,19 @@ export const POST: RequestHandler = async ({ locals, request }) => {
|
|
| 67 |
embeddingModel = conversation.embeddingModel;
|
| 68 |
}
|
| 69 |
|
| 70 |
-
const model = models.find((m) => m.name === values.model);
|
| 71 |
-
|
| 72 |
-
if (!model) {
|
| 73 |
-
throw error(400, "Invalid model");
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
embeddingModel ??= model.embeddingModel ?? defaultEmbeddingModel.name;
|
| 77 |
|
| 78 |
if (model.unlisted) {
|
| 79 |
throw error(400, "Can't start a conversation with an unlisted model");
|
| 80 |
}
|
| 81 |
|
| 82 |
-
// Use the model preprompt if there is no conversation/preprompt in the request body
|
| 83 |
-
const preprompt = await (async () => {
|
| 84 |
-
if (values.assistantId) {
|
| 85 |
-
const assistant = await collections.assistants.findOne({
|
| 86 |
-
_id: new ObjectId(values.assistantId),
|
| 87 |
-
});
|
| 88 |
-
return assistant?.preprompt;
|
| 89 |
-
} else {
|
| 90 |
-
return values?.preprompt ?? model?.preprompt;
|
| 91 |
-
}
|
| 92 |
-
})();
|
| 93 |
-
|
| 94 |
const res = await collections.conversations.insertOne({
|
| 95 |
_id: new ObjectId(),
|
| 96 |
title: title || "New Chat",
|
| 97 |
rootMessageId,
|
| 98 |
messages,
|
| 99 |
model: values.model,
|
| 100 |
-
preprompt: preprompt
|
| 101 |
assistantId: values.assistantId ? new ObjectId(values.assistantId) : undefined,
|
| 102 |
createdAt: new Date(),
|
| 103 |
updatedAt: new Date(),
|
|
|
|
| 34 |
);
|
| 35 |
}
|
| 36 |
|
| 37 |
+
// get preprompt from assistant if it exists
|
| 38 |
+
|
| 39 |
+
const model = models.find((m) => m.name === values.model);
|
| 40 |
+
|
| 41 |
+
if (!model) {
|
| 42 |
+
throw error(400, "Invalid model");
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
const assistant = await collections.assistants.findOne({
|
| 46 |
+
_id: new ObjectId(values.assistantId),
|
| 47 |
+
});
|
| 48 |
+
|
| 49 |
+
if (assistant) {
|
| 50 |
+
values.preprompt = assistant.preprompt;
|
| 51 |
+
} else {
|
| 52 |
+
values.preprompt ??= model?.preprompt ?? "";
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
let messages: Message[] = [
|
| 56 |
{
|
| 57 |
id: v4(),
|
| 58 |
from: "system",
|
| 59 |
+
content: values.preprompt,
|
| 60 |
createdAt: new Date(),
|
| 61 |
updatedAt: new Date(),
|
| 62 |
children: [],
|
|
|
|
| 85 |
embeddingModel = conversation.embeddingModel;
|
| 86 |
}
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
embeddingModel ??= model.embeddingModel ?? defaultEmbeddingModel.name;
|
| 89 |
|
| 90 |
if (model.unlisted) {
|
| 91 |
throw error(400, "Can't start a conversation with an unlisted model");
|
| 92 |
}
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
const res = await collections.conversations.insertOne({
|
| 95 |
_id: new ObjectId(),
|
| 96 |
title: title || "New Chat",
|
| 97 |
rootMessageId,
|
| 98 |
messages,
|
| 99 |
model: values.model,
|
| 100 |
+
preprompt: values.preprompt,
|
| 101 |
assistantId: values.assistantId ? new ObjectId(values.assistantId) : undefined,
|
| 102 |
createdAt: new Date(),
|
| 103 |
updatedAt: new Date(),
|