Skip to content

Reorganise backend modules used for chat #761

Open
@pmarsh-scottlogic

Description

@pmarsh-scottlogic

Feature Request

Completing this will greatly ease unit testing and documentation, which are two priorities for the end of the project.

Description

Lil' refactor to separate concerns and hopefully ease testing.

At the moment we have chatController.ts and openai.ts modules which contain the bulk of the logic for chatting with the chatbot. Both files are a bit bloated and seperation of concerns is wish-washy.

I propose that we introduce a new module chatService.ts, so that the concerns are separated into 3 layers (plus utils, introduced in #740 ):

  • chatController.ts deals with the API side of things. It validates the request parameters and constructs a sensible response based on how the chat goes (including unhappy paths).
  • chatService.ts contains the business logic for most of the interaction. Its job it to take an (already validated) message from the user, and get a response, including dealing with the tool calls faff.
  • openai.ts is only concerned with directly interfacing with the openAI API.

In concrete terms, I reckon we should:

  • Move handleLowLevelChat, handleHigherLevelChat and about half of the logic in handleChatToGPT to a fresh module chatService.ts.
  • Move performToolCalls, getFinalReplyAfterAllToolCalls, chatGptSendMessage also to chatService.ts.
  • Move getChatCompletionsFromHistory, pushCompletionToHistory, getBlankChatResponse to utils/chat.ts (introduced in 708 move logic for detecting output defence bot filtering #740 )

After all this it should be easier to unit test the exported methods from each layer.

NB: AFter a bunch of backend refactoring (#740, #743, #758) some of the above details will change, but the general gist is the same

Acceptance criteria

Refactor, so only regression testing.

Question: Do we want this, or does the benefit not outweigh the effort?

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendRequires work on the backendrefactorImprove code qualitytriageNew tickets to be checked by the maintainers

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions