sqlitestudio icon indicating copy to clipboard operation
sqlitestudio copied to clipboard

Added LLM Chat

Open grabani opened this issue 1 year ago • 9 comments

Hi There - as per our Discussion (#4919) and Issue (#4920) I have developed the primary functionality for a LLM Chat feature.

The LLM Chat allows users to interact with a LLM to obtain required responses to user generated queries directly from the Sqlite Studio application.

The feature has been placed under Tools --> LLM Chat:

image

The GUI for the LLM Chat is:

image

Current Capabilities:

  1. Can chat with OpenAI API
  2. Has options to send queries to models "gpt-3.5-turbo-1106" and "gpt-4-0125-preview" (see picture of chat GUI above)
  3. The user has to define an environment variable called "OPENAI_API_KEY".

Code Related Issues

  1. I was unable to refactor my code for the menu creation to be aligned with the existing functional calling/declaring formatl see SQLiteStudio3\guiSQLiteStudio\mainwindow.cpp

image

  1. The introduction of my code has had a slight impact on the applications main window GUI:

image

  1. There reason why there are a large number of commits is because I was using the Github Actions (your Windows 64-bit release build workflow) as my compile environment (to avoid setting up a compile environment locally).

  2. In order to get my code to compile and generate the required .exe, I commented out the "Compile Plugin" section in the Windows 64-bit release build workflow.

# - name: Compile Plugins
            #   working-directory: output/build/Plugins
            #   shell: bash_or_msys2 {0} 
            #   run: |
            #     qmake.exe \
            #       "QMAKE_CXX=${{ env.GXX_COMMAND }}" \
            #       CONFIG+=portable \
            #       PYTHON_VERSION=${{ env.PYTHON_VERSION }} "INCLUDEPATH+=${{ env.pythonLocation }}/include" "LIBS += -L${{ env.pythonLocation }}" \
            #       ../../../Plugins
            #     mingw32-make.exe -j 1

I am happy to look into these code issues but will need your help. If you can address them that would be great (as I suspect you will be able to isolate and resolve quicker than me).

Happy to discuss and hear your thoughts.

Take care.

grabani avatar Feb 03 '24 16:02 grabani

Later on the API key can be added to configuration dialog, but that's not important right now.

I see this definitely as a plugin. A good example for your case would be probably https://github.com/pawelsalawa/sqlitestudio/tree/master/Plugins/ConfigMigration because it's also a plugin with UI.

Here are more resources on writing plugins: https://github.com/pawelsalawa/sqlitestudio/wiki/Writting_plugins

Also please remember to cleanup (for example the menu action) in deinit(), because plugins can be freely loaded/unloaded during runtime. You can see example of how it's done in: https://github.com/pawelsalawa/sqlitestudio/blob/master/Plugins/DbAndroid/dbandroid.cpp

pawelsalawa avatar Feb 06 '24 10:02 pawelsalawa

@pawelsalawa - I'll look to convert my code into a plugin.

As mentioned above, I rely on your "Windows 64-bit release build workflow" for compiling. My code seems seems to cause the "Compile Plugin" section of the workflow to fail with the error:

In file included from ../../../../Plugins/Printing/printing.cpp:4: ../../../../SQLiteStudio3/guiSQLiteStudio/mainwindow.h:12:10: fatal error: QNetworkAccessManager: No such file or directory 12 | #include <QNetworkAccessManager> | ^~~~~~~~~~~~~~~~~~~~~~~ compilation terminated. mingw32-make[2]: *** [Makefile.Release:459: ../../../build/printing.o] Error 1 mingw32-make[2]: Leaving directory 'D:/a/sqlitestudio_GR/sqlitestudio_GR/output/build/Plugins/Printing' mingw32-make[1]: *** [Makefile:45: release] Error 2 mingw32-make[1]: Leaving directory 'D:/a/sqlitestudio_GR/sqlitestudio_GR/output/build/Plugins/Printing' mingw32-make: *** [Makefile:319: sub-Printing-make_first] Error 2 Error: Process completed with exit code 2.

If this issue is not resolved I will struggle to make progress. Can you help resolve this error or give me some guidance on what may be required to solve?

Also, when I click on any of the links on the the Writing Plugins page I get the following permissions error and the link referenced page doesn't load (I'm taken back to the Writing Plugins page).

image

Thanks.

grabani avatar Feb 06 '24 13:02 grabani

Regarding compilation errors - I'm not sure why plugins are not compiling forr you, since they compile here in the repo. The error says it cannot locate QNetworkManager when attempting to compile Printing plugin (which references mainwindow.h, which tries to #include the QNetworkManager. At the same time you do have the QNetworkManager, because otherwise you would not be able to compile the main application (with the mainwindow.h in it).

Maybe it's a matter of how your Qt is being set up in your build environment? Something to do with relative paths? I don't know. Just giving you some ideas to consider.

Regarding Wiki page - not all links on "writing Plugins" page exist, so when you click them, the Wiki attempts to take to to the "Edit/create new page", but you don't have permissions to do it. This Wiki is not 100% complete.

pawelsalawa avatar Feb 06 '24 14:02 pawelsalawa

@pawelsalawa you said "Maybe it's a matter of how your Qt is being set up in your build environment? Something to do with relative paths? I don't know. Just giving you some ideas to consider.", the fact that I do not have a build environment (no Qt installed locally) and am solely relying on the Github workflow for all build/compilation, I'm at a loss that it works for you and not me. How can I modify the workflow file to have absolute paths rather than relative ones?

grabani avatar Feb 06 '24 14:02 grabani

Okay. I took a closer look at your changes. The problem is that there are a lot of new headers in mainwindow.h. Various plugins make use of this header and it causes transient/chaining dependencies on any headers added here.

If you declare pointers in header class, just use "forward declaration" of a class, instead of including whole header. Then include the header in actual .cpp file. I mean this part: image

Move all these new includes into the mainwindow.cpp and then in the mainwindow.h add forward declarations, like there is a lot of them there already: image

Once you do these changes, the problem should go away.

pawelsalawa avatar Feb 06 '24 14:02 pawelsalawa

If you wonder why it caused plugin compilation to fail - it fails, because Plugins do not have "network manager" Qt module added by default (and there is no need for all plugins), so if particular plugin includes mainwindow.h, it encounters QNetworkManager, which plugin compilation has no knowledge about.

Once you move your code into a plugin, you will have to add Qt Network Manager dependency to your plugin's pro file.

pawelsalawa avatar Feb 06 '24 14:02 pawelsalawa

I believe we can clean all the changes from coreSQLiteStudio and guiSQLiteStudio, as everything seems to be implemented in the plugin, correct?

pawelsalawa avatar Apr 04 '24 09:04 pawelsalawa

Hi @pawelsalawa - due to the plugin architecture (which due to limited documentation) I struggled to complete in a timely fashion. Unfortunately, I will be unable to convert my solution from the current CORE code implementation to a PLUGIN based version due to other commitments. Hopefully you can use the code I have provided to achieve the desired outcome (I also believe any code you write will be infinitely better than what I have created 😊).

grabani avatar Apr 08 '24 11:04 grabani

I have tidied up my fork up so that it once again successfully compiles to produce the ChatGPT Chat GUI.

PLEASE NOTE THIS HAS BEEN IMPLEMENTED BY CHANGING THE CORE CODE AND NOT AS A PLUGIN.

grabani avatar Apr 23 '24 14:04 grabani

Since this is not a plugin, I don't see this to be merged into master branch. I'm closing the PR, but anyone interested can always pull it from the repo. Closing != deleting.

pawelsalawa avatar Aug 13 '24 17:08 pawelsalawa