hiyaryan / the-cdj

The Cognitive Distortion Journal (CDJ) is a smart journaling tool that helps remedy distorted thinking. It can feel impossible to follow the CBT technique of labeling distorted thinking and finding alternative modes of thought (i.e. reframing) while cognitive distortions are occurring. The CDJ does that work for you. -- The CDJ is in beta testing!!
https://thecdj.app
3 stars 0 forks source link

Add CdGpt Assistant. #22

Closed hiyaryan closed 9 months ago

hiyaryan commented 9 months ago

This PR establishes the backend assistants application. The assistants application provides the API necessary to establish connections to the LLMs, in particular, to the OpenAI API. The assistants API provides the ability for users to customize this connection. Currently, they may choose any existing model from OpenAI, in future PRs this should be extended to allowing users to extend (concatenate onto) the instructions of the CdGpt (a unique assistant), such as changing its tone or behavior, and the possibly the temperature (if its too high it will be too wild and may reduce the quality of The CDJ).

The first few commits on this new PR adds a generic ChatAssistant class. This class represents the necessary headers of a request to establish an authenticated and authorized connection. That is, the user must be a bearer of an OpenAI account and must specify the model they wish to use. This is tested with a few class methods, testConnection and testModelAvailability which themselves are tested with associated unit tests.

Note that unit tests require an OPENAI_API_KEY loaded into the application using environment variables in order to pass.

hiyaryan commented 9 months ago

The last commits for this PR refactors the Assistants and CdGpt after determining which class should be handling what. CdGpt should be handling chat completion related items which includes application/json related content.

Additionally CdGpt initial implementation has been implemented. This involves building the context that describes its role. This is used to seed a generic GPT to become the CdGpt. It also describes the format it should return and expected data structure. JSON mode is used to ensure it responds in json format, this is only available using gpt-3.5-turbo-1106 and gpt-4-1106-preview (with this, the analyses must be restricted to these models). It includes the ability to add messages to the context to create the messages field in the body used for the chat completion. Finally, it can make api calls to get the chat completion.

To wrap this PR up, CdGpt has been connected to the Thought Analysis view and can now make an analysis for an entry on entry creation. This involves appending a new method called getAnalysisContent onto the entryAnalysisSchema that uses CdGpt to retrieve the analysis_content for the entry. In addition, it also names the entry, infers the mood of the user, and creates the tags that are added to the new entry.