gus

module
v0.0.0-...-e11cae5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2025 License: Apache-2.0

README ยถ

gus

Prototype chatbot service simulating a production-ready backend for interacting with LLMs like LLaMA. Focuses on session handling, context management, and scalable API integration with Domain-Driven, Data-Oriented Architecture principles. Built using Go, ideal for experimenting with real-world chatbot architecture.

๐Ÿง  Managing Conversations with an LLM

When users interact with an LLM (e.g., via chat), the system must manage state, enforce rules, and optimize performance โ€” all while delivering relevant and secure responses. Below is an explanation of what needs to be tracked and some common features to implement.


๐Ÿ”„ What Needs to Be Tracked Per Conversation

Item Description
User ID Identify and associate requests with an authenticated user.
Conversation ID Group related messages to maintain context.
Message History Store both user and LLM messages to preserve continuity across turns.
Parent Message ID Track thread-like structures, useful for branching and follow-ups.
Timestamps Record when messages were sent and received.
Role Identify if the message is from the user, system, or assistant (LLM).
Model Usage Stats Track token count, latency, and model cost for each message.
Conversation State Manage ongoing session state, e.g., active/inactive, paused, archived.

๐Ÿ”ง Key Features to Implement in the Codebase

โœ… 1. Validate User Input
  • Check for empty messages, input length, forbidden content, or unsupported prompts.
  • Sanitize input to protect against injection attacks (especially with system prompts).
๐Ÿง‘โ€๐Ÿ’ผ 2. Validate User Roles & Access Control
  • Restrict certain users from accessing premium models or features.
  • Enforce role-based limitations (e.g., admin vs regular user vs guest).
  • Check subscription status or usage quota.
๐Ÿง  3. Maintain Conversation Context
  • Fetch and append previous messages in order before sending to the LLM.
  • Trim old messages if the token limit is reached (using sliding windows or summarization).
๐Ÿงฎ 4. Track LLM Response Time & Cost
  • Measure how long the model takes to respond (startTime โ†’ endTime).
  • Estimate or calculate token usage to bill or rate-limit users.
  • Keep a running total per user/session/day/month.
๐Ÿงฑ 5. Enforce Rate Limits & Quotas
  • Prevent overuse by setting message limits (per minute/hour/day).
  • Use the tracked usage to throttle or deny requests accordingly.
  • Implement soft limits (warn users) and hard limits (block requests).
๐Ÿ’พ 6. Persist Conversation History
  • Store messages in a database (e.g., PostgreSQL) under a structured schema.
  • Support search, filtering, or retrieval of past conversations.
  • Index data for analytics or summarization.
๐Ÿ“‰ 7. Monitor for Abnormal Behavior
  • Flag repeated or abusive requests.
  • Detect automated behavior (bot usage).
  • Analyze user patterns for misuse or errors.
๐Ÿ” 8. Logging & Analytics
  • Log request/response payloads (with privacy controls).
  • Track errors, latency, and model performance metrics.
  • Enable tracing per conversation for debugging or audits.
๐Ÿ“ฅ 9. System Prompts and Control Messages
  • Inject system-level instructions (e.g., "Answer in JSON format").
  • Dynamically alter behavior per user, session, or endpoint.
๐Ÿงต 10. Support for Threading or Forked Conversations
  • Allow users to branch off from a previous message.
  • Maintain a graph/tree of message relationships (e.g., parent/child IDs).

โœ… Best Practices

  • Use clear boundaries between API, App, Business, and Storage layers.
  • Keep domain logic (e.g., user permissions, LLM limits) in the business layer.
  • Use background workers for slow or expensive operations (e.g., logging, billing).
  • Store enough metadata to reconstruct context without reprocessing entire conversations.

Domain Driven Design

The above is a high-level overview of the software design. Each layer and domain is separated by clear boundaries, forming a grid-like structure. Every cell in this grid represents a specific responsibility, strictly limited to the domain it belongs to.

This structure helps maintain a clear mental model of the codebase and provides a well-defined view of how layers and domains should interact. It also makes it easier to detect code smells and enforce separation of concerns.

๐Ÿ“˜ Domain-Driven Design (DDD) โ€“ Summary

Domain-Driven Design is a software development approach focused on modeling software based on the core business domain. It emphasizes collaboration between technical and domain experts to create a shared understanding and structure the system around the businessโ€™s real-world processes and rules.


โœ… Key Benefits of DDD
  • Aligns code with business logic
    Structures the application around real-world concepts, making it easier to understand and evolve.

  • Improves communication
    Encourages a shared language (ubiquitous language) between developers and domain experts.

  • Encourages modularity
    Promotes the use of bounded contexts, allowing complex systems to be broken into manageable, decoupled parts.

  • Enhances maintainability
    Well-defined models reduce technical debt and make changes safer and more predictable.

  • Facilitates scaling
    Modular domains make it easier to scale teams, services, and infrastructure independently.

๐Ÿงฑ Code Layering Breakdown

Hereโ€™s a breakdown of what each layer should handle within this grid-based domain-driven architecture:

๐Ÿ“ก API Layer (Interface)
  • Defines public-facing HTTP/GraphQL/WebSocket endpoints.
  • Parses and validates incoming requests.
  • Translates domain responses into HTTP responses or errors.
  • Avoids containing business logic.
โš™๏ธ App Layer (Application Coordination)
  • Orchestrates use cases and workflows across domains.
  • Manages authorization, transactions, and context propagation.
  • Coordinates service calls, message queues, or async jobs.
  • Acts as a bridge between API and domain logic.
๐Ÿง  Business Layer (Domain Logic)
  • Encapsulates the core rules, policies, and behaviors of the system.
  • Owns entities, value objects, and aggregates.
  • Remains independent of infrastructure and frameworks.
  • Highly testable and focused on correctness.
๐Ÿ’พ Storage Layer (Persistence)
  • Implements interfaces defined by the domain (e.g., repositories).
  • Maps domain models to database records (using SQL or ORM).
  • Manages data access, caching, and external service calls.
  • Should be replaceable without affecting business logic.

Directories ยถ

Path Synopsis
api
services/auth/build/all
Package all binds all the routes into the specified app.
Package all binds all the routes into the specified app.
services/gus/build/all
Package all binds all the routes into the specified app.
Package all binds all the routes into the specified app.
services/gus/build/crud
Package crud binds the crud domain set of routes into the specified app.
Package crud binds the crud domain set of routes into the specified app.
services/gus/build/reporting
Package reporting binds the reporting domain set of routes into the specified app.
Package reporting binds the reporting domain set of routes into the specified app.
services/metrics/collector
Package collector is a simple collector for
Package collector is a simple collector for
services/metrics/publisher
Package publisher manages the publishing of metrics.
Package publisher manages the publishing of metrics.
services/metrics/publisher/datadog
Package datadog provides support for publishing metrics to DD.
Package datadog provides support for publishing metrics to DD.
services/metrics/publisher/expvar
Package expvar manages the publishing of metrics to stdout.
Package expvar manages the publishing of metrics to stdout.
services/metrics/publisher/prometheus
Package prometheus provides suppoert for sending metrics to prometheus.
Package prometheus provides suppoert for sending metrics to prometheus.
tooling/admin
This program performs administrative tasks for the gus service.
This program performs administrative tasks for the gus service.
tooling/admin/commands
Package commands contains the functionality for the set of commands currently supported by the CLI tooling.
Package commands contains the functionality for the set of commands currently supported by the CLI tooling.
tooling/logfmt
This program takes the structured log output and makes it readable.
This program takes the structured log output and makes it readable.
app
domain/authapp
Package authapp maintains the web based api for auth access.
Package authapp maintains the web based api for auth access.
domain/checkapp
Package checkapp maintains the app layer api for the check domain.
Package checkapp maintains the app layer api for the check domain.
domain/genesisapp
Package genesisapp maintains the app layer api for the tran domain.
Package genesisapp maintains the app layer api for the tran domain.
domain/oauthapp
Package oauthapp maintains the web based api for oauth support.
Package oauthapp maintains the web based api for oauth support.
domain/rawapp
Package rawapp provides an example of using a raw handler.
Package rawapp provides an example of using a raw handler.
domain/userapp
Package userapp maintains the app layer api for the user domain.
Package userapp maintains the app layer api for the user domain.
sdk/apitest
Package apitest provides support for excuting api test logic.
Package apitest provides support for excuting api test logic.
sdk/auth
Package auth provides authentication and authorization support.
Package auth provides authentication and authorization support.
sdk/authclient
Package authclient provides support to access the auth service.
Package authclient provides support to access the auth service.
sdk/debug
Package debug provides handler support for the debugging endpoints.
Package debug provides handler support for the debugging endpoints.
sdk/errs
Package errs provides types and support related to web error functionality.
Package errs provides types and support related to web error functionality.
sdk/metrics
Package metrics constructs the metrics the application will track.
Package metrics constructs the metrics the application will track.
sdk/mid
Package mid provides app level middleware support.
Package mid provides app level middleware support.
sdk/mux
Package mux provides support to bind domain level routes to the application mux.
Package mux provides support to bind domain level routes to the application mux.
sdk/query
Package query provides support for query paging.
Package query provides support for query paging.
business
domain/userbus
Package userbus provides business access to user domain.
Package userbus provides business access to user domain.
domain/userbus/stores/usercache
Package usercache contains user related CRUD functionality with caching.
Package usercache contains user related CRUD functionality with caching.
domain/userbus/stores/userdb
Package userdb contains user related CRUD functionality.
Package userdb contains user related CRUD functionality.
sdk/dbtest
Package dbtest contains supporting code for running tests that hit the DB.
Package dbtest contains supporting code for running tests that hit the DB.
sdk/delegate
Package delegate provides the ability to make function calls between different domain packages when an import is not possible.
Package delegate provides the ability to make function calls between different domain packages when an import is not possible.
sdk/migrate
Package migrate contains the database schema, migrations and seeding data.
Package migrate contains the database schema, migrations and seeding data.
sdk/order
Package order provides support for describing the ordering of data.
Package order provides support for describing the ordering of data.
sdk/page
Package page provides support for query paging.
Package page provides support for query paging.
sdk/sqldb
Package sqldb provides support for access the database.
Package sqldb provides support for access the database.
sdk/sqldb/dbarray
Package dbarray provides support for database array types.
Package dbarray provides support for database array types.
sdk/unitest
Package unitest provides support for excuting unit test logic.
Package unitest provides support for excuting unit test logic.
types/hometype
Package hometype represents the home type in the system.
Package hometype represents the home type in the system.
types/money
Package money represents a money in the system.
Package money represents a money in the system.
types/name
Package name represents a name in the system.
Package name represents a name in the system.
types/quantity
Package quantity represents a quantity in the system.
Package quantity represents a quantity in the system.
types/role
Package role represents the role type in the system.
Package role represents the role type in the system.
foundation
docker
Package docker provides support for starting and stopping docker containers for running tests.
Package docker provides support for starting and stopping docker containers for running tests.
keystore
Package keystore implements the auth.KeyLookup interface.
Package keystore implements the auth.KeyLookup interface.
logger
Package logger provides support for initializing the log system.
Package logger provides support for initializing the log system.
otel
Package otel provides otel support.
Package otel provides otel support.
web
Package web contains a small web framework extension.
Package web contains a small web framework extension.
worker
Package worker manages a set of registered jobs that execute on demand.
Package worker manages a set of registered jobs that execute on demand.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL