Skip to content

Commit

Permalink
add other id generate functions -- with role prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
mantagen committed Aug 28, 2024
1 parent dc422c0 commit 8b78d97
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export function ChatProvider({
} = useChat({
sendExtraMessageFields: true,
initialMessages,
generateId: generateMessageId,
generateId: () => generateMessageId({ role: "user" }),
id,
body: {
id,
Expand Down
8 changes: 4 additions & 4 deletions packages/aila/src/core/chat/AilaChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
unsupportedSubjects,
subjectWarnings,
} from "@oakai/core/src/utils/subjects";
import { nanoid } from "nanoid";
import invariant from "tiny-invariant";

import { AilaChatService, AilaServices } from "../..";
Expand All @@ -12,6 +11,7 @@ import {
AilaGeneration,
AilaGenerationStatus,
} from "../../features/generation";
import { generateMessageId } from "../../helpers/chat/generateMessageId";
import { JsonPatchDocumentOptional } from "../../protocol/jsonPatchProtocol";
import { LLMService } from "../llm/LLMService";
import { OpenAIService } from "../llm/OpenAIService";
Expand Down Expand Up @@ -123,7 +123,7 @@ export class AilaChat implements AilaChatService {
public async systemMessage() {
invariant(this._generation?.systemPrompt, "System prompt not initialised");
return {
id: nanoid(16),
id: generateMessageId({ role: "system" }),
content: this._generation?.systemPrompt,
role: "system" as const,
};
Expand All @@ -139,7 +139,7 @@ export class AilaChat implements AilaChatService {

if (this._aila?.lesson.hasSetInitialState) {
applicableMessages.push({
id: nanoid(16),
id: generateMessageId({ role: "user" }),
role: "user",
content:
"Now that you have the title, key stage and subject, let's start planning the lesson. Could you tell me if there are Oak lessons I could base my lesson on, or if there are none available let's get going with the first step of the lesson plan creation process!",
Expand Down Expand Up @@ -256,7 +256,7 @@ export class AilaChat implements AilaChatService {
return;
}
const assistantMessage: Message = {
id: nanoid(16),
id: generateMessageId({ role: "assistant" }),
role: "assistant",
content,
};
Expand Down
8 changes: 6 additions & 2 deletions packages/aila/src/helpers/chat/generateMessageId.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { nanoid } from "nanoid";

export function generateMessageId() {
return `${nanoid(16)}`;
export function generateMessageId({
role,
}: {
role: "user" | "assistant" | "system";
}) {
return `${role[0]}-${nanoid(16)}`;
}
10 changes: 5 additions & 5 deletions packages/api/src/router/appSessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { RateLimitExceededError } from "@oakai/core/src/utils/rateLimiting/userB
import { Prisma, PrismaClientWithAccelerate } from "@oakai/db";
import * as Sentry from "@sentry/nextjs";
import { TRPCError } from "@trpc/server";
import { nanoid } from "nanoid";
import { isTruthy } from "remeda";
import { z } from "zod";

import { getSessionModerations } from "../../../aila/src/features/moderation/getSessionModerations";
import { generateChatId } from "../../../aila/src/helpers/chat/generateChatId";
import {
AilaPersistedChat,
chatSchema,
Expand Down Expand Up @@ -135,11 +135,11 @@ export const appSessionsRouter = router({

await checkMutationPermissions(userId);

const id = nanoid(16);
const chatId = generateChatId();

const output: AilaPersistedChat = {
id,
path: `/aila/${id}`,
id: chatId,
path: `/aila/${chatId}`,
title: "",
topic: "",
userId,
Expand All @@ -154,7 +154,7 @@ export const appSessionsRouter = router({

const appSession = {
data: {
id: nanoid(16),
id: chatId,
appId,
userId,
output,
Expand Down

0 comments on commit 8b78d97

Please sign in to comment.