Skip to content

Commit

Permalink
Make sure preprompt is set on open ai endpoint type (#913)
Browse files Browse the repository at this point in the history
* Make sure preprompt is set on open ai endpoint type

* fix preprompt passing to conversation
  • Loading branch information
nsarrazin committed Mar 7, 2024
1 parent 09eb258 commit bcfa394
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
6 changes: 5 additions & 1 deletion src/lib/server/endpoints/openai/endpointOai.ts
Expand Up @@ -67,7 +67,11 @@ export async function endpointOai(
}));

if (messagesOpenAI?.[0]?.role !== "system") {
messagesOpenAI = [{ role: "system", content: preprompt ?? "" }, ...messagesOpenAI];
messagesOpenAI = [{ role: "system", content: "" }, ...messagesOpenAI];
}

if (messagesOpenAI?.[0]) {
messagesOpenAI[0].content = preprompt ?? "";
}

return openAIChatToTextGenerationStream(
Expand Down
40 changes: 20 additions & 20 deletions src/routes/conversation/+server.ts
Expand Up @@ -34,11 +34,29 @@ export const POST: RequestHandler = async ({ locals, request }) => {
);
}

// get preprompt from assistant if it exists

const model = models.find((m) => m.name === values.model);

if (!model) {
throw error(400, "Invalid model");
}

const assistant = await collections.assistants.findOne({
_id: new ObjectId(values.assistantId),
});

if (assistant) {
values.preprompt = assistant.preprompt;
} else {
values.preprompt ??= model?.preprompt ?? "";
}

let messages: Message[] = [
{
id: v4(),
from: "system",
content: values.preprompt ?? "",
content: values.preprompt,
createdAt: new Date(),
updatedAt: new Date(),
children: [],
Expand Down Expand Up @@ -67,37 +85,19 @@ export const POST: RequestHandler = async ({ locals, request }) => {
embeddingModel = conversation.embeddingModel;
}

const model = models.find((m) => m.name === values.model);

if (!model) {
throw error(400, "Invalid model");
}

embeddingModel ??= model.embeddingModel ?? defaultEmbeddingModel.name;

if (model.unlisted) {
throw error(400, "Can't start a conversation with an unlisted model");
}

// Use the model preprompt if there is no conversation/preprompt in the request body
const preprompt = await (async () => {
if (values.assistantId) {
const assistant = await collections.assistants.findOne({
_id: new ObjectId(values.assistantId),
});
return assistant?.preprompt;
} else {
return values?.preprompt ?? model?.preprompt;
}
})();

const res = await collections.conversations.insertOne({
_id: new ObjectId(),
title: title || "New Chat",
rootMessageId,
messages,
model: values.model,
preprompt: preprompt === model?.preprompt ? model?.preprompt : preprompt,
preprompt: values.preprompt,
assistantId: values.assistantId ? new ObjectId(values.assistantId) : undefined,
createdAt: new Date(),
updatedAt: new Date(),
Expand Down

0 comments on commit bcfa394

Please sign in to comment.