Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

707 refactor of chatgptchatcompletion #758

Merged
merged 41 commits into from
Jan 30, 2024

Conversation

pmarsh-scottlogic
Copy link
Contributor

@pmarsh-scottlogic pmarsh-scottlogic commented Jan 11, 2024

Description

REFACTOR: moves the logic for setting the system role in chat history to its own method (setSystemRoleInChatHistory), and then instead of calling that method from chatGptChatCompletion (openAI module), we call it from handleChatToGPT (chatController module). The idea of moving where it's invoked is to make methods responsible for fewer things.

Rewrites tests accordingly.

Notes

  • we also alter the logic to reduce mutation. Except we change the value of the chat history in the session once we've made the updated value (with the system role set)
  • unit tests the new method setSystemRoleInChatHistory on its own, then just checks that it gets called in handleChatToGPT
  • removes the old integration test that checked setting system roles in the chat history

Concerns

  • Wasn't sure where to put the definition of setSystemRoleInChatHistory, so I left it in openai. In my ideal word, we'd have some module "chatService" which sits in between the chatController and the lower level modules (openai, langchain) which would deal with this sort of thing.

Checklist

Have you done the following?

  • Linked the relevant Issue
  • Added tests
  • Ensured the workflow steps are passing

Copy link
Contributor

@gsproston-scottlogic gsproston-scottlogic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! The only thing I'd prefer would be to move setSystemRoleInChatHistory out of openai.ts.
I'd argue it can go in the chat controller, as that's the only place that uses it, although I appreciate that file is getting a little large.
Adding services for each controller would be nice, but a little overkill for this ticket. Could be worth making a new issue for it though? 👀
Alternatively, in #740 I added a chat utils file, could go there?

backend/src/openai.ts Outdated Show resolved Hide resolved
@pmarsh-scottlogic
Copy link
Contributor Author

pmarsh-scottlogic commented Jan 12, 2024

Very nice! The only thing I'd prefer would be to move setSystemRoleInChatHistory out of openai.ts. I'd argue it can go in the chat controller, as that's the only place that uses it, although I appreciate that file is getting a little large. Adding services for each controller would be nice, but a little overkill for this ticket. Could be worth making a new issue for it though? 👀 Alternatively, in #740 I added a chat utils file, could go there?

Re chat Service, I've just made #761 to introduce that. I don't think the other controllers are big enough to warrant a service however.

I reckon I'll wait for #740 and put setSystemRoleInChatHistory in there. Thus, consider this PR BLOCKED by #740

@pmarsh-scottlogic pmarsh-scottlogic added the blocked Blocked from progressing label Jan 12, 2024
Copy link
Member

@chriswilty chriswilty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few thoughts.

backend/src/openai.ts Outdated Show resolved Hide resolved
backend/test/integration/chatController.test.ts Outdated Show resolved Hide resolved
backend/test/integration/openai.test.ts Show resolved Hide resolved
backend/test/unit/openai.test.ts Outdated Show resolved Hide resolved
backend/test/unit/openai.test.ts Outdated Show resolved Hide resolved
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the tests for setSystemRoleInChatHistory then decided I could split the file apart into pushMessageToHistory.test.ts and setSystemRoleInChatHistory

@@ -297,6 +324,41 @@ describe('handleChatToGPT unit tests', () => {
})
);
});

test('GIVEN output filtering defence enabled WHEN handleChatToGPT called THEN it should return 200 and blocked reason', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test ain't got anything to do with this ticket, but I noticed it was missing and it's pretty straightforward so I added it in.

@pmarsh-scottlogic pmarsh-scottlogic removed the blocked Blocked from progressing label Jan 23, 2024
@pmarsh-scottlogic pmarsh-scottlogic changed the title 707 small refactor of chatgptchatcompletion 707 refactor of chatgptchatcompletion Jan 23, 2024
Copy link
Member

@chriswilty chriswilty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great 👌

One suggestion for test mocking. Mocking classes is pretty complex.... One very good reason to avoid classes in our own code!

backend/test/unit/openai.test.ts Outdated Show resolved Hide resolved
Copy link
Member

@chriswilty chriswilty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an import to correct and we're all good 👍

backend/test/unit/openai.test.ts Outdated Show resolved Hide resolved
Copy link
Member

@chriswilty chriswilty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚡ Go!

@chriswilty chriswilty dismissed gsproston-scottlogic’s stale review January 30, 2024 10:02

No longer on project :(

@chriswilty chriswilty merged commit 2901fb3 into dev Jan 30, 2024
2 checks passed
@chriswilty chriswilty deleted the 707-small-refactor-of-chatgptchatcompletion branch January 30, 2024 10:03
chriswilty pushed a commit that referenced this pull request Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Small refactor of chatGptChatCompletion()
3 participants