Skip to content

Commit

Permalink
Feature/Custom Assistant Builder (#3631)
Browse files Browse the repository at this point in the history
* add custom assistant builder

* add tools to custom assistant

* add save assistant button
  • Loading branch information
HenryHengZJ authored Dec 6, 2024
1 parent e020452 commit fe2ed26
Show file tree
Hide file tree
Showing 46 changed files with 3,129 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class AWSChatBedrock_ChatModels implements INode {
name: 'model',
type: 'asyncOptions',
loadMethod: 'listModels',
default: 'anthropic.claude-3-haiku'
default: 'anthropic.claude-3-haiku-20240307-v1:0'
},
{
label: 'Custom Model Name',
Expand Down
4 changes: 3 additions & 1 deletion packages/server/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { IAction, ICommonObject, IFileUpload, INode, INodeData as INodeDataFromC

export type MessageType = 'apiMessage' | 'userMessage'

export type ChatflowType = 'CHATFLOW' | 'MULTIAGENT'
export type ChatflowType = 'CHATFLOW' | 'MULTIAGENT' | 'ASSISTANT'

export type AssistantType = 'CUSTOM' | 'OPENAI' | 'AZURE'

export enum ChatType {
INTERNAL = 'INTERNAL',
Expand Down
52 changes: 50 additions & 2 deletions packages/server/src/controllers/assistants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Request, Response, NextFunction } from 'express'
import assistantsService from '../../services/assistants'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { StatusCodes } from 'http-status-codes'
import { AssistantType } from '../../Interface'

const createAssistant = async (req: Request, res: Response, next: NextFunction) => {
try {
Expand Down Expand Up @@ -35,7 +36,8 @@ const deleteAssistant = async (req: Request, res: Response, next: NextFunction)

const getAllAssistants = async (req: Request, res: Response, next: NextFunction) => {
try {
const apiResponse = await assistantsService.getAllAssistants()
const type = req.query.type as AssistantType
const apiResponse = await assistantsService.getAllAssistants(type)
return res.json(apiResponse)
} catch (error) {
next(error)
Expand Down Expand Up @@ -78,10 +80,56 @@ const updateAssistant = async (req: Request, res: Response, next: NextFunction)
}
}

const getChatModels = async (req: Request, res: Response, next: NextFunction) => {
try {
const apiResponse = await assistantsService.getChatModels()
return res.json(apiResponse)
} catch (error) {
next(error)
}
}

const getDocumentStores = async (req: Request, res: Response, next: NextFunction) => {
try {
const apiResponse = await assistantsService.getDocumentStores()
return res.json(apiResponse)
} catch (error) {
next(error)
}
}

const getTools = async (req: Request, res: Response, next: NextFunction) => {
try {
const apiResponse = await assistantsService.getTools()
return res.json(apiResponse)
} catch (error) {
next(error)
}
}

const generateAssistantInstruction = async (req: Request, res: Response, next: NextFunction) => {
try {
if (!req.body) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: assistantsController.generateAssistantInstruction - body not provided!`
)
}
const apiResponse = await assistantsService.generateAssistantInstruction(req.body.task, req.body.selectedChatModel)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}

export default {
createAssistant,
deleteAssistant,
getAllAssistants,
getAssistantById,
updateAssistant
updateAssistant,
getChatModels,
getDocumentStores,
getTools,
generateAssistantInstruction
}
21 changes: 20 additions & 1 deletion packages/server/src/controllers/documentstore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,24 @@ const refreshDocStoreMiddleware = async (req: Request, res: Response, next: Next
}
}

const generateDocStoreToolDesc = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params.id === 'undefined' || req.params.id === '') {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: documentStoreController.generateDocStoreToolDesc - storeId not provided!`
)
}
if (typeof req.body === 'undefined') {
throw new Error('Error: documentStoreController.generateDocStoreToolDesc - body not provided!')
}
const apiResponse = await documentStoreService.generateDocStoreToolDesc(req.params.id, req.body.selectedChatModel)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}

export default {
deleteDocumentStore,
createDocumentStore,
Expand All @@ -434,5 +452,6 @@ export default {
getRateLimiterMiddleware,
upsertDocStoreMiddleware,
refreshDocStoreMiddleware,
saveProcessingLoader
saveProcessingLoader,
generateDocStoreToolDesc
}
5 changes: 4 additions & 1 deletion packages/server/src/database/entities/Assistant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable */
import { Entity, Column, CreateDateColumn, UpdateDateColumn, PrimaryGeneratedColumn } from 'typeorm'
import { IAssistant } from '../../Interface'
import { AssistantType, IAssistant } from '../../Interface'

@Entity()
export class Assistant implements IAssistant {
Expand All @@ -16,6 +16,9 @@ export class Assistant implements IAssistant {
@Column({ nullable: true })
iconSrc?: string

@Column({ nullable: true, type: 'text' })
type?: AssistantType

@Column({ type: 'timestamp' })
@CreateDateColumn()
createdDate: Date
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from 'typeorm'

export class AddTypeToAssistant1733011290987 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const columnExists = await queryRunner.hasColumn('assistant', 'type')
if (!columnExists) {
await queryRunner.query(`ALTER TABLE \`assistant\` ADD COLUMN \`type\` TEXT;`)
await queryRunner.query(`UPDATE \`assistant\` SET \`type\` = 'OPENAI';`)
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`assistant\` DROP COLUMN \`type\`;`)
}
}
4 changes: 3 additions & 1 deletion packages/server/src/database/migrations/mariadb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { LongTextColumn1722301395521 } from './1722301395521-LongTextColumn'
import { AddCustomTemplate1725629836652 } from './1725629836652-AddCustomTemplate'
import { AddArtifactsToChatMessage1726156258465 } from './1726156258465-AddArtifactsToChatMessage'
import { AddFollowUpPrompts1726666318346 } from './1726666318346-AddFollowUpPrompts'
import { AddTypeToAssistant1733011290987 } from './1733011290987-AddTypeToAssistant'

export const mariadbMigrations = [
Init1693840429259,
Expand Down Expand Up @@ -57,5 +58,6 @@ export const mariadbMigrations = [
LongTextColumn1722301395521,
AddCustomTemplate1725629836652,
AddArtifactsToChatMessage1726156258465,
AddFollowUpPrompts1726666318346
AddFollowUpPrompts1726666318346,
AddTypeToAssistant1733011290987
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from 'typeorm'

export class AddTypeToAssistant1733011290987 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const columnExists = await queryRunner.hasColumn('assistant', 'type')
if (!columnExists) {
await queryRunner.query(`ALTER TABLE \`assistant\` ADD COLUMN \`type\` TEXT;`)
await queryRunner.query(`UPDATE \`assistant\` SET \`type\` = 'OPENAI';`)
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`assistant\` DROP COLUMN \`type\`;`)
}
}
4 changes: 3 additions & 1 deletion packages/server/src/database/migrations/mysql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { LongTextColumn1722301395521 } from './1722301395521-LongTextColumn'
import { AddCustomTemplate1725629836652 } from './1725629836652-AddCustomTemplate'
import { AddArtifactsToChatMessage1726156258465 } from './1726156258465-AddArtifactsToChatMessage'
import { AddFollowUpPrompts1726666302024 } from './1726666302024-AddFollowUpPrompts'
import { AddTypeToAssistant1733011290987 } from './1733011290987-AddTypeToAssistant'

export const mysqlMigrations = [
Init1693840429259,
Expand Down Expand Up @@ -57,5 +58,6 @@ export const mysqlMigrations = [
LongTextColumn1722301395521,
AddCustomTemplate1725629836652,
AddArtifactsToChatMessage1726156258465,
AddFollowUpPrompts1726666302024
AddFollowUpPrompts1726666302024,
AddTypeToAssistant1733011290987
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from 'typeorm'

export class AddTypeToAssistant1733011290987 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const columnExists = await queryRunner.hasColumn('assistant', 'type')
if (!columnExists) {
await queryRunner.query(`ALTER TABLE "assistant" ADD COLUMN "type" TEXT;`)
await queryRunner.query(`UPDATE "assistant" SET "type" = 'OPENAI';`)
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "assistant" DROP COLUMN "type";`)
}
}
4 changes: 3 additions & 1 deletion packages/server/src/database/migrations/postgres/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { AddActionToChatMessage1721078251523 } from './1721078251523-AddActionTo
import { AddCustomTemplate1725629836652 } from './1725629836652-AddCustomTemplate'
import { AddArtifactsToChatMessage1726156258465 } from './1726156258465-AddArtifactsToChatMessage'
import { AddFollowUpPrompts1726666309552 } from './1726666309552-AddFollowUpPrompts'
import { AddTypeToAssistant1733011290987 } from './1733011290987-AddTypeToAssistant'

export const postgresMigrations = [
Init1693891895163,
Expand Down Expand Up @@ -57,5 +58,6 @@ export const postgresMigrations = [
AddActionToChatMessage1721078251523,
AddCustomTemplate1725629836652,
AddArtifactsToChatMessage1726156258465,
AddFollowUpPrompts1726666309552
AddFollowUpPrompts1726666309552,
AddTypeToAssistant1733011290987
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from 'typeorm'

export class AddTypeToAssistant1733011290987 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const columnExists = await queryRunner.hasColumn('assistant', 'type')
if (!columnExists) {
await queryRunner.query(`ALTER TABLE "assistant" ADD COLUMN "type" TEXT;`)
await queryRunner.query(`UPDATE "assistant" SET "type" = 'OPENAI';`)
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "assistant" DROP COLUMN "type";`)
}
}
4 changes: 3 additions & 1 deletion packages/server/src/database/migrations/sqlite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { AddActionToChatMessage1721078251523 } from './1721078251523-AddActionTo
import { AddArtifactsToChatMessage1726156258465 } from './1726156258465-AddArtifactsToChatMessage'
import { AddCustomTemplate1725629836652 } from './1725629836652-AddCustomTemplate'
import { AddFollowUpPrompts1726666294213 } from './1726666294213-AddFollowUpPrompts'
import { AddTypeToAssistant1733011290987 } from './1733011290987-AddTypeToAssistant'

export const sqliteMigrations = [
Init1693835579790,
Expand Down Expand Up @@ -55,5 +56,6 @@ export const sqliteMigrations = [
AddActionToChatMessage1721078251523,
AddArtifactsToChatMessage1726156258465,
AddCustomTemplate1725629836652,
AddFollowUpPrompts1726666294213
AddFollowUpPrompts1726666294213,
AddTypeToAssistant1733011290987
]
7 changes: 7 additions & 0 deletions packages/server/src/routes/assistants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ router.put(['/', '/:id'], assistantsController.updateAssistant)
// DELETE
router.delete(['/', '/:id'], assistantsController.deleteAssistant)

router.get('/components/chatmodels', assistantsController.getChatModels)
router.get('/components/docstores', assistantsController.getDocumentStores)
router.get('/components/tools', assistantsController.getTools)

// Generate Assistant Instruction
router.post('/generate/instruction', assistantsController.generateAssistantInstruction)

export default router
3 changes: 3 additions & 0 deletions packages/server/src/routes/documentstore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@ router.get('/components/recordmanager', documentStoreController.getRecordManager
// update the selected vector store from the playground
router.post('/vectorstore/update', documentStoreController.updateVectorStoreConfigOnly)

// generate docstore tool description
router.post('/generate-tool-desc/:id', documentStoreController.generateDocStoreToolDesc)

export default router
Loading

0 comments on commit fe2ed26

Please sign in to comment.