-
Notifications
You must be signed in to change notification settings - Fork 4
Add AIassistant module #373
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
sekulya
wants to merge
5
commits into
main
Choose a base branch
from
aiassistant-module
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a12933f
Add AIassistant module
sekulya 7d075f3
Describe your changes here
e72f933
Ignore __pycache__ and cleanup tracked cache files
sekulya 1feef9d
Ignore venv and pycache folders
sekulya 900e202
with .gitignore and multiple xsl and doc files input and answer
sekulya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
backend/core-api/src/modules/AIassistant/db/definitions/generalSettings.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { Document, Model } from 'mongoose'; | ||
|
|
||
|
|
||
| export interface IGeneralSettings { | ||
| assistantName: string; | ||
| conversationStarter: string; | ||
| description: string; | ||
| promptSuggestions: string[]; | ||
| updatedAt?: Date; | ||
| } | ||
|
|
||
|
|
||
| export interface IGeneralSettingsDocument extends IGeneralSettings, Document { | ||
| _id: string; | ||
| createdAt: Date; | ||
| updatedAt: Date; | ||
| } | ||
|
|
||
|
|
||
| export interface IGeneralSettingsModel extends Model<IGeneralSettingsDocument> { | ||
| getSettings(): Promise<IGeneralSettingsDocument | null>; | ||
| updateSettings(doc: Partial<IGeneralSettings>): Promise<IGeneralSettingsDocument>; | ||
| } | ||
|
|
||
|
|
||
| export const generalSettingsFields = { | ||
| assistantName: { type: String, default: '' }, | ||
| conversationStarter: { type: String, default: '' }, | ||
| description: { type: String, default: '' }, | ||
| promptSuggestions: { type: [String], default: [] }, | ||
| }; |
34 changes: 34 additions & 0 deletions
34
backend/core-api/src/modules/AIassistant/db/definitions/ragInteractions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { Document } from 'mongoose'; | ||
|
|
||
| export interface IRagInteraction { | ||
| question: string; | ||
| answer: string; | ||
| sourceDocuments?: string[]; | ||
| userId: string; | ||
| orgId: string; | ||
| createdAt: Date; | ||
| modelUsed?: string; | ||
| responseTime?: number; | ||
| tokensUsed?: number; | ||
| confidenceScore?: number; | ||
| status: 'success' | 'error' | 'pending'; | ||
| errorMessage?: string; | ||
| } | ||
|
|
||
| export interface IRagInteractionDocument extends IRagInteraction, Document {} | ||
|
|
||
| // Export the fields as a string | ||
| export const ragInteractionFields = ` | ||
| question: String | ||
| answer: String | ||
| sourceDocuments: [String] | ||
| userId: String | ||
| orgId: String | ||
| createdAt: Date | ||
| modelUsed: String | ||
| responseTime: Float | ||
| tokensUsed: Float | ||
| confidenceScore: Float | ||
| status: String | ||
| errorMessage: String | ||
| `; |
21 changes: 21 additions & 0 deletions
21
backend/core-api/src/modules/AIassistant/db/definitions/systemPrompt.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { Document, Model } from "mongoose"; | ||
|
|
||
| export interface ISystemPrompt { | ||
| prompt: string; // Remove orgId | ||
| updatedAt?: Date; | ||
| } | ||
|
|
||
| export interface ISystemPromptDocument extends ISystemPrompt, Document { | ||
| _id: string; | ||
| createdAt: Date; | ||
| updatedAt: Date; | ||
| } | ||
|
|
||
| export interface ISystemPromptModel extends Model<ISystemPromptDocument> { | ||
| getPrompt(): Promise<ISystemPromptDocument | null>; // Remove orgId parameter | ||
| updatePrompt(prompt: string): Promise<ISystemPromptDocument>; // Remove orgId parameter | ||
| } | ||
|
|
||
| export const systemPromptFields = { | ||
| prompt: { type: String, default: "" } // Remove orgId field | ||
| }; |
22 changes: 22 additions & 0 deletions
22
backend/core-api/src/modules/AIassistant/db/models/GeneralSettings.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { Schema, model, Model } from "mongoose"; | ||
| import { | ||
| IGeneralSettingsDocument, | ||
| IGeneralSettingsModel, | ||
| generalSettingsFields, | ||
| } from "../definitions/generalSettings"; | ||
|
|
||
|
|
||
| export const GeneralSettingsSchema = new Schema<IGeneralSettingsDocument>( | ||
| generalSettingsFields, | ||
| { | ||
| timestamps: true, | ||
| collection: 'general_settings' | ||
| } | ||
| ); | ||
|
|
||
|
|
||
| // Create the model with proper typing | ||
| export const GeneralSettingsModel = model<IGeneralSettingsDocument, IGeneralSettingsModel>( | ||
| "general_settings", | ||
| GeneralSettingsSchema | ||
| ) |
67 changes: 67 additions & 0 deletions
67
backend/core-api/src/modules/AIassistant/db/models/RagInteractions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // backend/core-api/src/modules/AIassistant/db/models/RagInteractions.ts | ||
| import { Schema, model, models, Document, Model } from 'mongoose'; | ||
| import { field, stringField, numberField, dateField } from '../../utils/schemaField'; | ||
| import { IRagInteraction } from '../definitions/ragInteractions'; | ||
|
|
||
| export interface IRagInteractionDocument extends IRagInteraction, Document {} | ||
|
|
||
| export interface IRagInteractionModel extends Model<IRagInteractionDocument> { | ||
| getRagInteraction(_id: string): Promise<IRagInteractionDocument>; | ||
| } | ||
|
|
||
| export const ragInteractionSchema = new Schema({ | ||
| question: stringField({ label: 'Question' }), | ||
| answer: stringField({ label: 'Answer' }), | ||
| sourceDocuments: field({ | ||
| type: [String], | ||
| label: 'Source Documents', | ||
| optional: true | ||
| }), | ||
| userId: stringField({ label: 'User ID' }), | ||
| orgId: stringField({ label: 'Organization ID' }), | ||
| createdAt: dateField({ | ||
| default: Date.now, | ||
| label: 'Created at', | ||
| immutable: true | ||
| }), | ||
| modelUsed: stringField({ | ||
| label: 'Model Used', | ||
| optional: true | ||
| }), | ||
| responseTime: numberField({ | ||
| label: 'Response Time (ms)', | ||
| optional: true, | ||
| min: 0 | ||
| }), | ||
| tokensUsed: numberField({ | ||
| label: 'Tokens Used', | ||
| optional: true, | ||
| min: 0 | ||
| }), | ||
| confidenceScore: numberField({ | ||
| label: 'Confidence Score', | ||
| optional: true, | ||
| min: 0, | ||
| max: 1 | ||
| }), | ||
| status: stringField({ | ||
| label: 'Status', | ||
| enum: ['success', 'error', 'pending'], | ||
| default: 'success' | ||
| }), | ||
| errorMessage: stringField({ | ||
| label: 'Error Message', | ||
| optional: true | ||
| }) | ||
| }, { | ||
| timestamps: true, | ||
| collection: 'rag_interactions' | ||
| }); | ||
|
|
||
| // Add indexes for better query performance | ||
| ragInteractionSchema.index({ userId: 1, createdAt: -1 }); | ||
| ragInteractionSchema.index({ orgId: 1 }); | ||
| ragInteractionSchema.index({ status: 1 }); | ||
|
|
||
| export const RagInteractions = models.RagInteractions || | ||
| model<IRagInteractionDocument, IRagInteractionModel>('rag_interactions', ragInteractionSchema); |
39 changes: 39 additions & 0 deletions
39
backend/core-api/src/modules/AIassistant/db/models/SystemPrompt.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { Schema, model, Model } from "mongoose"; | ||
| import { | ||
| ISystemPromptDocument, | ||
| ISystemPromptModel, | ||
| systemPromptFields, | ||
| } from "../definitions/systemPrompt"; | ||
|
|
||
| export const SystemPromptSchema = new Schema<ISystemPromptDocument>( | ||
| systemPromptFields, | ||
| { | ||
| timestamps: true, | ||
| collection: 'system_prompts' | ||
| } | ||
| ); | ||
|
|
||
| SystemPromptSchema.statics.getPrompt = function () { | ||
| return this.findOne().exec(); | ||
| }; | ||
|
|
||
| SystemPromptSchema.statics.updatePrompt = async function (prompt: string) { | ||
| const updated = await this.findOneAndUpdate( | ||
| {}, | ||
| { | ||
| prompt, | ||
| updatedAt: new Date() | ||
| }, | ||
| { | ||
| new: true, | ||
| upsert: true | ||
| } | ||
| ).exec(); | ||
|
|
||
| return updated; | ||
| }; | ||
|
|
||
| export const SystemPromptModel = model<ISystemPromptDocument, ISystemPromptModel>( | ||
| "system_prompt", | ||
| SystemPromptSchema | ||
| ); |
23 changes: 23 additions & 0 deletions
23
backend/core-api/src/modules/AIassistant/db/models/loadGeneralSettingsClass.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { IModels } from '~/connectionResolvers'; | ||
| import { GeneralSettingsModel } from './GeneralSettings'; | ||
| import { IGeneralSettingsDocument, IGeneralSettingsModel } from '../definitions/generalSettings'; | ||
|
|
||
|
|
||
| export const loadGeneralSettingsClass = (models: IModels) => { | ||
| class GeneralSettings { | ||
| // Example static method | ||
| public static async getSettings(): Promise<IGeneralSettingsDocument | null> { | ||
| return models.GeneralSettings.findOne({}); | ||
| } | ||
|
|
||
|
|
||
| // You can add more static methods here | ||
| } | ||
|
|
||
|
|
||
| // Attach class methods to the existing model's schema | ||
| GeneralSettingsModel.schema.loadClass(GeneralSettings); | ||
|
|
||
|
|
||
| return GeneralSettingsModel; | ||
| }; |
20 changes: 20 additions & 0 deletions
20
backend/core-api/src/modules/AIassistant/db/models/loadInteractionClass.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { IModels } from '~/connectionResolvers'; | ||
| import { ragInteractionSchema } from './RagInteractions'; | ||
| import { IRagInteractionDocument, IRagInteraction } from '../definitions/ragInteractions'; | ||
|
|
||
| export const loadRagInteractionClass = (models: IModels) => { | ||
| class RagInteraction { | ||
| public static async getRagInteraction(_id: string) { | ||
| const ragInteraction = await models.RagInteractions.findOne({ _id }); | ||
|
|
||
| if (!ragInteraction) { | ||
| throw new Error('RagInteraction not found'); | ||
| } | ||
|
|
||
| return ragInteraction; | ||
| } | ||
| } | ||
|
|
||
| ragInteractionSchema.loadClass(RagInteraction); | ||
| return ragInteractionSchema; | ||
| }; |
17 changes: 17 additions & 0 deletions
17
backend/core-api/src/modules/AIassistant/docker-compose.dev.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| version: '3.8' | ||
|
|
||
| services: | ||
| rag-service: | ||
| build: ./rag-service | ||
| ports: | ||
| - "8000:8000" | ||
| env_file: | ||
| - ./rag-service/.env | ||
| volumes: | ||
| - ./rag-service/src:/app/src | ||
| restart: unless-stopped | ||
| environment: | ||
| - OPENAI_API_KEY=${OPENAI_API_KEY} | ||
| - EMBEDDING_MODEL=${EMBEDDING_MODEL} | ||
| - LLM_MODEL=${LLM_MODEL} | ||
| - ALLOWED_ORIGINS=http://localhost:3000,http://localhost:3001 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { Router, Request, Response } from "express"; | ||
| const router: Router = Router(); | ||
|
|
||
| // in-memory store | ||
| let generalSettings = { | ||
| assistantName: "Sparkles AI", | ||
| conversationStarter: "How can I help you today?", | ||
| description: | ||
| "Get quick answers and insights about your customers and sales pipeline.", | ||
| promptSuggestions: [ | ||
| "Summarize the last 10 conversations from Team Inbox", | ||
| "List all open leads assigned to me", | ||
| "Answer customer FAQs quickly", | ||
| ], | ||
| }; | ||
|
|
||
| // ✅ GET route (fixed) | ||
| router.get("/ai-assistant/general/:orgId", (req: Request, res: Response) => { | ||
| res.json(generalSettings); | ||
| }); | ||
|
|
||
| // ✅ POST route (already in your file, just make sure it updates generalSettings) | ||
| router.post("/ai-assistant/general/:orgId", (req: Request, res: Response) => { | ||
| const { assistantName, conversationStarter, description, promptSuggestions } = | ||
| req.body; | ||
|
|
||
| generalSettings = { | ||
| assistantName, | ||
| conversationStarter, | ||
| description, | ||
| promptSuggestions, | ||
| }; | ||
|
|
||
| res.json({ success: true, settings: generalSettings }); | ||
| }); | ||
|
|
||
| export default router; | ||
27 changes: 27 additions & 0 deletions
27
backend/core-api/src/modules/AIassistant/graphql/generalSchema.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { generalSettingsFields } from "../db/definitions/generalSettings"; | ||
|
|
||
| export const generaltypes = () => [ | ||
| ` | ||
| type GeneralSettings { | ||
| _id: String! | ||
| ${generalSettingsFields} | ||
| updatedAt: String | ||
| } | ||
|
|
||
| input GeneralSettingsInput { | ||
| assistantName: String | ||
| conversationStarter: String | ||
| description: String | ||
| promptSuggestions: [String] | ||
| } | ||
|
|
||
| extend type Query { | ||
| getGeneralSettings: GeneralSettings | ||
| } | ||
|
|
||
|
|
||
| extend type Mutation { | ||
| updateGeneralSettings(input: GeneralSettingsInput!): GeneralSettings | ||
| } | ||
| ` | ||
| ]; |
38 changes: 38 additions & 0 deletions
38
backend/core-api/src/modules/AIassistant/graphql/resolvers/generalMutations.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { IGeneralSettingsDocument } from "../../db/definitions/generalSettings"; | ||
| import { GeneralSettingsModel } from "../../db/models/GeneralSettings"; | ||
|
|
||
|
|
||
|
|
||
|
|
||
| export interface GeneralSettingsMutationResolvers { | ||
| updateGeneralSettings: ( | ||
| _parent: any, | ||
| args: { input: Partial<IGeneralSettingsDocument> } | ||
| ) => Promise<IGeneralSettingsDocument>; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| export const generalMutations: GeneralSettingsMutationResolvers = { | ||
| updateGeneralSettings: async ( | ||
| _parent: any, | ||
| { input }: { input: Partial<IGeneralSettingsDocument> } | ||
| ): Promise<IGeneralSettingsDocument> => { | ||
| const existingSettings = await GeneralSettingsModel.findOne().exec(); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| if (existingSettings) { | ||
| Object.assign(existingSettings, input, { updatedAt: new Date() }); | ||
| return await existingSettings.save(); | ||
| } else { | ||
| const newSettings = new GeneralSettingsModel({ | ||
| ...input, | ||
| updatedAt: new Date(), | ||
| }); | ||
| return await newSettings.save(); | ||
| } | ||
| }, | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistency: In-memory store contradicts database infrastructure.
The AI summary and related files indicate that database models (
IGeneralSettings,GeneralSettingsMongoose model,loadGeneralSettingsClass.ts) were added for persistent storage, but this implementation uses an in-memory store that will lose data on restart.Either:
Run this script to verify if database models exist:
🤖 Prompt for AI Agents