Skip to content

15. Ai Model Implementations

myrtp edited this page Dec 27, 2023 · 11 revisions

#15. AI Model Implementations

Overview

This section outlines the process of integrating different AI models into the application.

15.1 AiModel Entity

In Gendox the AiModel Entity contains the necessary information for AI models employed. The following properties are entailed:

  • id: A unique identifier (UUID) for the AI model.
  • model: The identifier (string) of the AI model as provided by its documentation(required).
  • url: The url of the API endpoint corresponding to the AI model.
  • name: The name of the model.
  • price: The cost of usage per 1k tokens(required).
  • createdAt: The timestamp when the AI model was added.
  • updatedAt: The timestamp when the AI model was last updated.
  • updatedBy: The unique identifier (UUID) of the user that updated the AI model.
  • createdBy: The unique identifier (UUID) of the user that added the AI model.

15.1.1 Associations

The AiModel entity is associated with the projectAgent entity by:

  • The fields semantic_search_model_id and semantic_completion_model_id. These contain the id of the AI models used by the project agent to perform semantic search and completion respectively.

15.2 AiModel Service Interface

The aiModelService Interface contains all the methods that are implemented by all the AI models. These methods are the askEmbeddings, askCompletion and moderationCheck. Also a boolean supports method is included. It is used when it is necessary to check if an AI model is supported. Currently the AI models supported are for cohere: command and embed-multilingual-v3.0, and for openAI: gpt-4, gpt-3.5-turbo, openai-moderation and text-embedding-ada-002.

15.2.1 Ask embeddings

Description: Get embeddings for the given BotRequest using the specified AI model.

  • Parameters:
    • botRequest: Object of type BotRequest containing information for embedding computation.
    • aiModelName: Name of the AI model to be used (String).
  • Return Type: A general EmbeddingResponse object.

15.2.2 Ask completion

  • Description: Get completion of messages using the specified AI model.
  • Parameters:
    • messages: List of AiModelMessage objects.
    • agentRole: Role identifier for the agent requesting completion (String).
    • aiModelName: Name of the AI model to be used (String).
    • aiModelRequestParams: Additional parameters for AI model requests( e.g. temperature).
  • Return Type: A general CompletionResponse object.

15.2.3 Check moderation

  • Description: Checks message content against moderation criteria using GPT-3.5. This is currently the only supported model for moderation. From this point forward it is implied that when the moderationCheck method is applied, the response is only of OpenAiGpt35ModerationResponse type. Therefore we will not expand further on this method.
  • Parameters:
    • message: Message content to be checked.
  • Return Type: OpenAiGpt35ModerationResponse

15.3 AI Model Adapter

The AI Model Adapter acts as a bridge between various AI models and the application's AI Engine, enabling seamless integration and interaction with different AI capabilities. The adapter architecture enables easy incorporation of diverse AI models into the system by handling the different features of each AI model.

15.3.1 Components

  • Model Support Detection: Checks whether a specific model is supported by the adapter. This ensures that the adapter that matches the model is used.
  • Request Handling: Sends requests tailored to the models of a specific AI model and manages their corresponding responses.
  • Response Converters: Functions for transforming AI model responses into general response format.

15.3.2 Methods

The boolean method supports in implemented in order to verify whether the specific adapter supports the aiModel used. Afterwards the methods askEmbedding, askCompletionand moderationCheck from the AImodelSErvice interface are applied. These methods receive a specific AI model response object from the request.

Request Handling Methods

  • buildHeader: Constructs and returns HTTP headers necessary for communication with AI model services

  • getEmbeddingResponse: Sends an embedding request to the specified AI model service

  • getCompletionResponse: dispatches a completion request to the specified AI model service

  • getModerationResponse: forwards a moderation request to the designated AI model service. Each of these methods receive a parameter that is the specific AI request and return a response specific to the AI model.For instance:

  • In getEmbeddingResponse, the parameter is a specific request from an AI service (e.g., OpenAI's Ada2 model) and the method returns the specific response from the AI service.

Request/Response Conversion

It is worth noting that each adapter interacts with its associated AI service and the response received from each request is of the certain type of the AI model used. Consequently a converter method is applied in each adapter that transforms the specific response to the general response object. For instance:

  • In askEmbedding, the specific response from an AI service (e.g., OpenAI's Ada2 model) is converted into a generalized EmbeddingResponse using embeddingResponseConverter.

This conversion process facilitates consistent output formats across various AI models.

15.4 Add a new AI model

To add a new AI model, you must follow the steps below.

  • STEP 1: Add the AI model data to AiModel table on the database.
  • STEP 2: Create the Response and Request classes for the specific AI model, for the embeddings and the completion respectively.
  • STEP 3: Construct the corresponding converters that transform the specific completion and embedding AI responses to general embeddings and completion response formats.
  • STEP 4: Create a new adapter for the AI model, that implements all the methods of the AiModelService interface. In order to build the request and its headers consult the client's website.

TIP:

  • For testing purposes a mockAiAdapter is also available for testing purposes.**.
Clone this wiki locally