Skip to content

Add Introductory Message for Personas #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/features/chat-page/chat-services/chat-thread-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ import { RedirectToChatThread } from "@/features/common/navigation-helpers";
import { ServerActionResponse } from "@/features/common/server-action-response";
import { uniqueId } from "@/features/common/util";
import {
AI_NAME,
CHAT_DEFAULT_PERSONA,
NEW_CHAT_NAME,
} from "@/features/theme/theme-config";
import { SqlQuerySpec } from "@azure/cosmos";
import { HistoryContainer } from "../../common/services/cosmos";
import { DeleteDocuments } from "./azure-ai-search/azure-ai-search";
import { FindAllChatDocuments } from "./chat-document-service";
import { FindAllChatMessagesForCurrentUser } from "./chat-message-service";
import { CreateChatMessage, FindAllChatMessagesForCurrentUser } from "./chat-message-service";
import {
CHAT_THREAD_ATTRIBUTE,
ChatDocumentModel,
ChatRole,
ChatThreadModel,
} from "./models";

Expand Down Expand Up @@ -334,6 +336,18 @@ export const UpdateChatTitle = async (
}
};

export const CreateIntroMessage = async (chatThreadId: string, messageText: string) => {
let role: ChatRole = "assistant";
const messageModel = {
name: AI_NAME,
role: role,
content: messageText,
chatThreadId: chatThreadId,
multiModalImage: "",
};
void CreateChatMessage(messageModel);
}

export const CreateChatAndRedirect = async () => {
const response = await CreateChatThread();
if (response.status === "OK") {
Expand Down
11 changes: 11 additions & 0 deletions src/features/persona-page/add-new-persona.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
personaStore,
usePersonaState,
} from "./persona-store";
import { AI_NAME } from "@/features/theme/theme-config";

interface Props {}

Expand Down Expand Up @@ -109,6 +110,16 @@ export const AddNewPersona: FC<Props> = (props) => {
placeholder="Personality of your persona"
/>
</div>
<div className="grid grid-cols-[20px_1fr] gap-2">
<input className="h-5"
type="checkbox"
name="useIntroductionMessage"
defaultChecked={persona.useIntroductionMessage}
/>
<div className="flex items-center">
<Label htmlFor="useIntroductionMessage">{AI_NAME} uses a customized introduction message for this persona</Label>
</div>
</div>
</div>
</ScrollArea>
<SheetFooter className="py-2 flex sm:justify-between flex-row">
Expand Down
13 changes: 13 additions & 0 deletions src/features/persona-page/persona-card/persona-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
SheetTrigger,
} from "../../ui/sheet";
import { PersonaModel } from "../persona-services/models";
import { AI_NAME } from "@/features/theme/theme-config";

interface Props {
persona: PersonaModel;
Expand Down Expand Up @@ -41,6 +42,18 @@ export const ViewPersona: FC<Props> = (props) => {
name="personaMessage"
placeholder="Personality of your persona"
/>
<div className="grid grid-cols-[20px_1fr] gap-2">
<input className="h-5"
type="checkbox"
name="useIntroductionMessage"
defaultChecked={persona.useIntroductionMessage}
disabled
aria-disabled
/>
<div className="flex items-center text-sm text-muted-foreground">
{AI_NAME} uses a customized introduction message for this persona
</div>
</div>
<p className="text-xs text-muted-foreground">
{persona.isPublished
? `This is published and everyone in your organisation can use ${persona.name} persona`
Expand Down
1 change: 1 addition & 0 deletions src/features/persona-page/persona-services/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const PersonaModelSchema = z.object({
.min(1)
.refine(refineFromEmpty, "System message cannot be empty"),
isPublished: z.boolean(),
useIntroductionMessage: z.boolean(),
type: z.literal(PERSONA_ATTRIBUTE),
createdAt: z.date(),
});
29 changes: 28 additions & 1 deletion src/features/persona-page/persona-services/persona-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import "server-only";

import { getCurrentUser, userHashedId } from "@/features/auth-page/helpers";
import { UpsertChatThread } from "@/features/chat-page/chat-services/chat-thread-service";
import { CreateIntroMessage, UpsertChatThread } from "@/features/chat-page/chat-services/chat-thread-service";
import {
CHAT_THREAD_ATTRIBUTE,
ChatThreadModel,
Expand All @@ -15,12 +15,15 @@ import { HistoryContainer } from "@/features/common/services/cosmos";
import { uniqueId } from "@/features/common/util";
import { SqlQuerySpec } from "@azure/cosmos";
import { PERSONA_ATTRIBUTE, PersonaModel, PersonaModelSchema } from "./models";
import { INTRODUCTION_MESSAGE_PROMPT} from "@/features/theme/theme-config";
import { OpenAIInstance } from "@/features/common/services/openai";

interface PersonaInput {
name: string;
description: string;
personaMessage: string;
isPublished: boolean;
useIntroductionMessage: boolean;
}

export const FindPersonaByID = async (
Expand Down Expand Up @@ -84,6 +87,7 @@ export const CreatePersona = async (
description: props.description,
personaMessage: props.personaMessage,
isPublished: user.isAdmin ? props.isPublished : false,
useIntroductionMessage: props.useIntroductionMessage,
userId: await userHashedId(),
createdAt: new Date(),
type: "PERSONA",
Expand Down Expand Up @@ -197,6 +201,7 @@ export const UpsertPersona = async (
isPublished: user.isAdmin
? personaInput.isPublished
: persona.isPublished,
useIntroductionMessage: personaInput.useIntroductionMessage,
createdAt: new Date(),
};

Expand Down Expand Up @@ -305,6 +310,9 @@ export const CreatePersonaChat = async (
personaMessageTitle: persona.name,
extension: [],
});
if (response.status === "OK" && persona.useIntroductionMessage) {
await CreatePersonaIntroMessage(response.response.id, persona.personaMessage)
}

return response;
}
Expand All @@ -326,3 +334,22 @@ const ValidateSchema = (model: PersonaModel): ServerActionResponse => {
response: model,
};
};

const CreatePersonaIntroMessage = async (chatThreadId: string, personaMessage: string) => {
let completionText = "";
const openAI = OpenAIInstance();
try {
const completion = await openAI.chat.completions.create({
model: "",
messages: [
{ role: "system", content: INTRODUCTION_MESSAGE_PROMPT },
{ role: "user", content: `These are your general capabilities: ${personaMessage}`},
],
});

completionText = (completion.choices[0]?.message?.content || "");
await CreateIntroMessage(chatThreadId, completionText);
} catch (error) {
console.error("Error during OpenAI completion:", error);
}
}
2 changes: 2 additions & 0 deletions src/features/persona-page/persona-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class PersonaState {
personaMessage: "",
createdAt: new Date(),
isPublished: false,
useIntroductionMessage: false,
type: "PERSONA",
userId: "",
};
Expand Down Expand Up @@ -92,6 +93,7 @@ export const FormDataToPersonaModel = (formData: FormData): PersonaModel => {
description: formData.get("description") as string,
personaMessage: formData.get("personaMessage") as string,
isPublished: formData.get("isPublished") === "on" ? true : false,
useIntroductionMessage: formData.get("useIntroductionMessage") === "on" ? true : false,
userId: "", // the user id is set on the server once the user is authenticated
createdAt: new Date(),
type: PERSONA_ATTRIBUTE,
Expand Down
2 changes: 2 additions & 0 deletions src/features/theme/theme-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ You have access to the following functions:
1. create_img: You must only use the function create_img if the user asks you to create an image.`;

export const NEW_CHAT_NAME = "New chat";

export const INTRODUCTION_MESSAGE_PROMPT = "Don't follow your capabilities for now and just greet the user and explain what you are capable of. You start with a h2 headline first, so that the user understands your main purpose. Make sure that headline includes a brief summary of your role.";