Skip to content

Commit

Permalink
feat: add prompt service
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Mar 21, 2024
1 parent c2c87d0 commit ebd35ba
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- CreateEnum
CREATE TYPE "AiPromptRole" AS ENUM ('system', 'assistant', 'user');

-- CreateTable
CREATE TABLE "ai_prompts" (
"id" VARCHAR NOT NULL,
"name" VARCHAR(20) NOT NULL,
"idx" INTEGER NOT NULL,
"role" "AiPromptRole" NOT NULL,
"content" TEXT NOT NULL,
"created_at" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "ai_prompts_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "ai_prompts_name_key" ON "ai_prompts"("name");
21 changes: 21 additions & 0 deletions packages/backend/server/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,27 @@ model UserInvoice {
@@map("user_invoices")
}

enum AiPromptRole {
system
assistant
user
}

model AiPrompt {
id String @id @default(uuid()) @db.VarChar
// prompt name
name String @unique @db.VarChar(20)
// if a group of prompts contains multiple sentences, idx specifies the order of each sentence
idx Int @db.Integer
// system/assistant/user
role AiPromptRole
// prompt content
content String @db.Text
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
@@map("ai_prompts")
}

model DataMigration {
id String @id @default(uuid()) @db.VarChar(36)
name String @db.VarChar
Expand Down
5 changes: 3 additions & 2 deletions packages/backend/server/src/plugins/copilot/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ServerFeature } from '../../core/config';
import { Plugin } from '../registry';
import { assertProvidersConfigs, CopilotProviderService } from './provider';
import { CopilotPromptService } from './prompt';
import { assertProvidersConfigs, CopilotProviderService } from './providers';

@Plugin({
name: 'copilot',
providers: [CopilotProviderService],
providers: [CopilotPromptService, CopilotProviderService],
contributesTo: ServerFeature.Copilot,
if: config => {
if (config.flavor.graphql) {
Expand Down
52 changes: 52 additions & 0 deletions packages/backend/server/src/plugins/copilot/prompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Injectable } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

import { ChatMessage } from './types';

@Injectable()
export class CopilotPromptService {
constructor(private readonly db: PrismaClient) {}

// list prompt names
async list() {
return this.db.aiPrompt
.findMany({ select: { name: true } })
.then(prompts => prompts.map(p => p.name));
}

async get(name: string): Promise<ChatMessage[]> {
return this.db.aiPrompt.findMany({
where: {
name,
},
select: {
role: true,
content: true,
},
orderBy: {
idx: 'asc',
},
});
}

async set(name: string, messages: ChatMessage[]) {
return this.db.$transaction(async tx => {
if (await tx.aiPrompt.findUnique({ where: { name } })) {
return 0;
}
return tx.aiPrompt
.createMany({
data: messages.map((m, idx) => ({ name, idx, ...m })),
})
.then(ret => ret.count);
});
}

async delete(name: string) {
return this.db.aiPrompt
.deleteMany({
where: { name },
})
.then(ret => ret.count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import assert from 'node:assert';

import { Injectable, Logger } from '@nestjs/common';

import { Config } from '../../fundamentals';
import { Config } from '../../../fundamentals';
import {
CapabilityToCopilotProvider,
CopilotConfig,
CopilotProvider,
CopilotProviderCapability,
CopilotProviderType,
} from './types';
} from '../types';

type CopilotProviderConfig = CopilotConfig[keyof CopilotConfig];

Expand Down
4 changes: 3 additions & 1 deletion packages/backend/server/src/plugins/copilot/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ export interface CopilotProvider {
getCapabilities(): CopilotProviderCapability[];
}

export const ChatMessageRole = ['system', 'assistant', 'user'] as const;

export type ChatMessage = {
role: 'system' | 'assistant' | 'user';
role: (typeof ChatMessageRole)[number];
content: string;
};

Expand Down

0 comments on commit ebd35ba

Please sign in to comment.