project-logo

PROMPT-LOGGING-SYSTEM-BE

Empowering seamless API interactions, unleash innovation.

last-commit repo-top-language repo-language-count


Table of Contents

Overview

The Prompt-Logging-System-be project is a NestJS API logging system that manages business logic, API endpoints, and HTTP exception handling. It integrates services such as ClickHouse for data storage, OpenAI for chat completion, and validation tools for input integrity. This project facilitates seamless interaction between components within a robust architecture, enabling developers to store and query data efficiently while ensuring system reliability and data security.


Features

Feature Description
โš™๏ธ Architecture NestJS framework used with modular structure and Decorators for abstraction.
๐Ÿ”ฉ Code Quality Linting with ESLint, testing with Jest, and strong TypeScript usage.
๐Ÿ“„ Documentation Moderate documentation covering config, modules, and API endpoints.
๐Ÿ”Œ Integrations Integrates Axios, OpenAI SDK, ClickHouse client for data handling.
๐Ÿงฉ Modularity Codebase structured into modules for reusability and maintainability.
๐Ÿงช Testing Jest for unit testing, End-to-End testing with Supertest.
โšก๏ธ Performance Efficient data handling with ClickHouse, async methods improve speed.
๐Ÿ›ก๏ธ Security Configurable validation pipe for input data, handled bad requests.
๐Ÿ“ฆ Dependencies Key libs: NestJS, Axios, ClickHouse, OpenAI SDK, Jest, ESLint.

Repository Structure

โ””โ”€โ”€ Prompt-Logging-System-be/
    โ”œโ”€โ”€ README.md
    โ”œโ”€โ”€ dist
    โ”‚   โ”œโ”€โ”€ app.controller.d.ts
    โ”‚   โ”œโ”€โ”€ app.controller.js
    โ”‚   โ”œโ”€โ”€ app.controller.js.map
    โ”‚   โ”œโ”€โ”€ app.module.d.ts
    โ”‚   โ”œโ”€โ”€ app.module.js
    โ”‚   โ”œโ”€โ”€ app.module.js.map
    โ”‚   โ”œโ”€โ”€ app.service.d.ts
    โ”‚   โ”œโ”€โ”€ app.service.js
    โ”‚   โ”œโ”€โ”€ app.service.js.map
    โ”‚   โ”œโ”€โ”€ http-exception.filter.d.ts
    โ”‚   โ”œโ”€โ”€ http-exception.filter.js
    โ”‚   โ”œโ”€โ”€ http-exception.filter.js.map
    โ”‚   โ”œโ”€โ”€ main.d.ts
    โ”‚   โ”œโ”€โ”€ main.js
    โ”‚   โ”œโ”€โ”€ main.js.map
    โ”‚   โ”œโ”€โ”€ proxy-api
    โ”‚   โ””โ”€โ”€ tsconfig.build.tsbuildinfo
    โ”œโ”€โ”€ nest-cli.json
    โ”œโ”€โ”€ package-lock.json
    โ”œโ”€โ”€ package.json
    โ”œโ”€โ”€ src
    โ”‚   โ”œโ”€โ”€ app.controller.spec.ts
    โ”‚   โ”œโ”€โ”€ app.controller.ts
    โ”‚   โ”œโ”€โ”€ app.module.ts
    โ”‚   โ”œโ”€โ”€ app.service.ts
    โ”‚   โ”œโ”€โ”€ http-exception.filter.ts
    โ”‚   โ”œโ”€โ”€ main.ts
    โ”‚   โ””โ”€โ”€ proxy-api
    โ”œโ”€โ”€ test
    โ”‚   โ”œโ”€โ”€ app.e2e-spec.ts
    โ”‚   โ””โ”€โ”€ jest-e2e.json
    โ”œโ”€โ”€ tsconfig.build.json
    โ””โ”€โ”€ tsconfig.json

Modules

.
File Summary
package.json Implements key npm scripts for building, testing, and starting the NestJS API. Dependencies include @nestjs packages, Axios, ClickHouse client, OpenAI SDK, and Date-fns. Dev dependencies cover testing with Jest and linting with ESLint.
package-lock.json The code file app.service.js in the Prompt-Logging-System-be repository serves a critical role in handling and managing the applications business logic. It provides essential services and functionality to support the systems core operations. Its main purpose is to encapsulate key business logic, enabling seamless interaction between different components within the application.
tsconfig.json Defines TypeScript compiler options to enable ES2021 features and generate CommonJS modules. Maintains source maps, metadata, and synthetic imports for the NestJS logging system. Implements decorators for abstraction with experimental decorators support.
nest-cli.json Enables Nest.js schematics in the project. Specifies source root and compiler options for the @nestjs/schematics collection. Facilitates configuring project settings for efficient development and build processes.
tsconfig.build.json Defines build configuration, inheriting settings, and excluding unnecessary folders and test files. Facilitates TypeScript compilation for production, optimizing build output.
test
File Summary
jest-e2e.json Configures Jest for end-to-end tests in the project. Sets up module extensions, test environment, regex patterns, and transformation rules to enable testing of TypeScript files.
app.e2e-spec.ts Tests the API endpoints by creating a NestJS testing module with the AppModule, initializing the application, and sending a GET request to check the response status and content.
src
File Summary
http-exception.filter.ts Implements HTTP exception handling for the NestJS app. Captures and processes HTTP exceptions, extracting status codes and messages to be sent as JSON responses.
app.module.ts Defines core app structure, linking controllers and services. Utilizes ValidationPipe for input validation and HttpExceptionFilter for error handling. Integrates ProxyApiModule for extended functionality within the Nest.js server.
app.controller.ts Defines AppController with a single method to fetch a greeting from AppService. This file orchestrates service interactions within the NestJS API architecture.
app.controller.spec.ts Verifies AppController functionality to return Hello World! in the NestJS project.
app.service.ts Implements a service for the NestJS framework that retrieves a simple greeting message.
main.ts Implements the main function to initialize a NestJS application, setting up global exception filtering and defining the port for the application to listen on, as part of the larger logging system architecture in the parent repository.
src.proxy-api
File Summary
clickhouse.service.ts Handles storing and querying data in ClickHouse dataset. Provides methods to insert responses and query data based on filters and time period. Additionally, calculates total input and output tokens from the queried data.
proxy-api.module.ts Defines a module for proxy API services including ProxyApiController, ProxyApiService, OpenAIService, ClickHouseService, and ResponseApiService. Manages controllers and providers for seamless integration within the NestJS architecture of the parent repository.
proxy-api.service.spec.ts Verifies the definition of the ProxyApiService by creating a testing module and checking if the service is defined. Contributes to ensuring the functionality and correctness of the proxy API service in the project architecture.
openaiService.ts Implements OpenAI API integration for chat completion using custom headers in parent repositorys proxy-api module. Fetches response from OpenAI and logs it.
proxy-api.controller.spec.ts Verifies ProxyApiController definition. Sets up test module with ProxyApiController and ProxyApiService, ensuring controller is defined.
responseAPIUtils.service.ts Defines ResponseAPI structure and operations for creating, updating successful and failed responses in the Prompt Logging System. Manages request details and token counts to generate response data.
proxy-api.service.ts Creates a service to interact with external APIs, store responses in a ClickHouse dataset, and query the dataset with filters. Calculates total input and output tokens from the dataset for metrics reporting.
proxy-api.controller.ts Defines API endpoints for a proxy service to create, retrieve, and get metrics using NestJS controllers. Validates input data with a configurable validation pipe. Handles bad requests with a defined HTTP exception.
src.proxy-api.dto
File Summary
create-proxy-api.dto.ts Defines MetadataDto and CreateProxyApiDto classes with validation rules for user input, central to data integrity in the proxy API functionality.
update-proxy-api.dto.ts Defines DTO for updating a proxy API by extending the create DTO with partial properties, enabling API updates with only specified fields.
QueryParams.dto.ts Defines classes for filtering data through query parameters in the parent repository. FiltersDto specifies filter options like model, status, and environment. QueryParamsDto utilizes FiltersDto as a nested object to enable data filtering based on query parameters.
src.proxy-api.entities
File Summary
proxy-api.entity.ts Defines Proxy API entity model for the logging system. It aims to encapsulate data attributes and behavior related to proxy APIs in the system architecture.

Getting Started

System Requirements:

  • TypeScript

Installation

From source

  1. Clone the Prompt-Logging-System-be repository:
$ git clone https://github.com/kashishvjain/Prompt-Logging-System-be
  1. Change to the project directory:
$ cd Prompt-Logging-System-be
  1. Install the dependencies:
$ npm install

Usage

From source

Run Prompt-Logging-System-be using the command below:

$ npm run build && node dist/main.js

Tests

Run the test suite using the command below:

$ npm test