# inquisitour/knowledge-based-agent-pgai architecture

> Architecture diagram of the GitHub repository inquisitour/knowledge-based-agent-pgai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/inquisitour/knowledge-based-agent-pgai
- Repository: https://github.com/inquisitour/knowledge-based-agent-pgai

## Overview

##### Component Representation

a. Client Layer:

- lambda_interface.py
- agent.py
- data_processing.py
- AWS S3 bucket for FAQ data storage

c. Database Layer:
- Three sub-components within PostgreSQL (show as internal modules):
- pgvector
- pgvectorscale
- pgai

d. External Services Layer:
- OpenAI API (for initial embedding generation)

##### Connection and Data Flow
- User → AWS Lex (input query)
- AWS Lex → Lambda (lambda_interface.py)
- Lambda → PostgreSQL (vector search and processing)
- PostgreSQL internal flows between extensions
- Lambda → S3 (FAQ data updates)
- Final response path back to user

##### Additional Elements
- Small annotations for key processes:
- Vector Transformation
- Similarity Search
- Embedding Generation
- Scaling indicators where applicable

##### Technical Details
- Database Extension Capabilities:
- pgvector: Vector storage & similarity search
- pgvectorscale: StreamingDiskANN optimization
- pgai: In-database embedding generation
- Lambda Function Roles:
- data_processing.py: FAQ data handling
- agent.py: Core chatbot logic
- lambda_interface.py: AWS service integration

##### Performance Optimization Indicators
- Caching layers if implemented
- Vector indexing optimization points
- Areas where parallel processing occurs

Key Considerations:
- Keep the main flow clear and easy to follow
- System boundaries and security zones
- Scaling points and potential bottlenecks
- Asynchronous vs synchronous operations

##### The data flow optimization through in-database processing

Remember to maintain a balance between detail and clarity, focusing on the unique aspects of this project, particularly the PostgreSQL AI capabilities and vector search optimization.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Client Layer
    User((User)):::client

    %% AWS Services Layer
    subgraph AWS["AWS Cloud"]
        Lex[AWS Lex]:::aws
        
        subgraph Lambda["AWS Lambda Functions"]
            Interface["Lambda Interface"]:::lambda
            Agent["Chatbot Agent"]:::lambda
            DataProc["Data Processor"]:::lambda
        end
        
        S3[(S3 Bucket)]:::aws
    end

    %% Database Layer
    subgraph DB["PostgreSQL Database"]
        Postgres[(PostgreSQL)]:::db
        PGVector["pgvector"]:::db
        PGVectorScale["pgvectorscale"]:::db
        PGAI["pgai"]:::db
    end

    %% External Services Layer
    OpenAI["OpenAI API"]:::external

    %% Connections
    User --> Lex
    Lex --> Interface
    Interface --> Agent
    Agent --> DataProc
    DataProc --> S3
    
    Agent --> Postgres
    Postgres --> PGVector
    Postgres --> PGVectorScale
    Postgres --> PGAI
    
    PGAI <--> OpenAI
    
    %% Return Flow
    Postgres --> Agent
    Agent --> Interface
    Interface --> Lex
    Lex --> User

    %% Click Events
    click Interface "https://github.com/inquisitour/knowledge-based-agent-pgai/blob/main/lambda_interface.py"
    click Agent "https://github.com/inquisitour/knowledge-based-agent-pgai/blob/main/agent.py"
    click DataProc "https://github.com/inquisitour/knowledge-based-agent-pgai/blob/main/data_processing.py"

    %% Styles
    classDef client fill:#gray,stroke:#333,stroke-width:2px
    classDef aws fill:#FF9900,stroke:#333,stroke-width:2px
    classDef lambda fill:#FF9900,stroke:#333,stroke-width:2px
    classDef db fill:#336791,stroke:#333,stroke-width:2px,color:#fff
    classDef external fill:#FFA500,stroke:#333,stroke-width:2px

    %% Legend
    subgraph Legend
        Client[Client]:::client
        AWSService[AWS Service]:::aws
        Database[Database]:::db
        ExternalService[External Service]:::external
    end
```
