Calling ChatGPT API as service

Created on 2 March 2023, over 1 year ago
Updated 6 April 2023, over 1 year ago

Same code/logic is repeatedly used in the query forms and controllers for sending requests to the OpenAI API, such as in ChatGPTTranslateController, ChatGPTAssistToolForm, and ChatGPTForm. I suggest using a service for sending and processing responses, which means that all requests to OpenAI will be in one place. This will result in less code duplication and easier maintenance of the project.

✨ Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

🇨🇿Czech Republic siva01

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Issue created by @siva01
  • 🇩🇰Denmark ressa Copenhagen

    Great idea, and this would also mean that we can hook into the module from sub-modules, right?

  • 🇨🇿Czech Republic siva01

    I create MR with some code refactor and that service. It can be called form some hook too. I test it on our project, so I hope, that I didn't break anything.

  • 🇩🇰Denmark ressa Copenhagen

    Thanks. Should it be against 2.0.0?

    Though I don't see a 2.0.x-dev release here ... https://www.drupal.org/project/chatgpt_plugin/releases →

  • 🇮🇳India anup.sinha Bengaluru

    Hi @siva01, I totally agree with you that we should create a service for code reusability. I just created a new branch - 2.0.x from 1.0.x. Please create your merge request against 2.0.x.

    Thanks & Regards,
    Anup

  • 🇩🇰Denmark ressa Copenhagen

    Thanks @anup.sinha, perhaps 2.x can be made the default branch at https://git.drupalcode.org/project/chatgpt_plugin?

  • 🇨🇿Czech Republic siva01

    @anup.sinha I can see only branch 1.*. There is tag 2.0, but branch is still 1.*.

  • 🇮🇳India anup.sinha Bengaluru

    Hi @siva01, now dev branch is showing in release. PFA the screenshot. It seems we have to tag at least one release after a creating a branch to show the dev branch.

    Thanks & Regards,
    Anup

  • 🇮🇳India anup.sinha Bengaluru

    Hi @siva01 and @ressa,

    I have refactored the code completely by introducing two new Drupal service - 1 for GPT model and another for DALL.E. Please download this version - https://www.drupal.org/project/chatgpt_plugin/releases/2.0.x-dev → for the new refactored code or you can check this commit -
    https://git.drupalcode.org/project/chatgpt_plugin/-/commit/1aa6c42f096f5... .

    I have tested all the existing scenarios and can confirm the refactored code is working as expected. Request you guys also to validate and let me know please if there is any issue.

    Also I am planning to incorporate the newly released GPT3.5 API by this weekend.

    Thanks & Regards,
    Anup

  • Status changed to Fixed over 1 year ago
  • 🇮🇳India anup.sinha Bengaluru
  • Status changed to Needs work over 1 year ago
  • 🇩🇰Denmark ressa Copenhagen

    Thanks @anup.sinha, but I am not sure where DALL·E can be accessed? Perhaps you can expand 6. in README.md?

    Now a "ChatGPT Search" link will appear in content add and edit page, and DALL·E under ... (?)

  • 🇮🇳India anup.sinha Bengaluru

    Hi @ressa,

    Please check the readme file 2.0.x branch. I already added a new section like this -

    "**Act as a Content Assistance Tool:**
    This will add a new tab called "OpenAI Content Assistance Tool" in the
    content admin page (admin/content) and will give you give you different options
    like creating images from texr, extracting SEO keyowrds from a content etc."

    Thanks & Regards,
    Anup

  • @avanishyadav opened merge request.
  • 🇮🇳India anup.sinha Bengaluru

    Hi @Avanishyadav, thanks for working on it but your profile name is showing Avanishyadav but when I checked the commit it's showing "Luděk Kvapil". Sorry I am not going to merge it as your profile - https://www.drupal.org/u/avanishyadav → looks like spam profile.

    Thanks & Regards,
    Anup

  • 🇩🇰Denmark ressa Copenhagen

    Ah yes, I missed it somehow, thanks for adding that @anup.sinha, it works perfectly. Have a nice day!

  • 🇩🇰Denmark ressa Copenhagen

    I tried the translation, and got this error:

    Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'title' at row 2: [...]

  • 🇮🇳India anup.sinha Bengaluru

    Hi @ressa, the problem is if we are giving title like "blockchain article" then the prompt my plugin is sending like "Translate the following text into "- [Language] - "blockchain article". But instead of simply translating "blockchain article" into dutch or french, GPT API is writing an entire article in Dutch or French. As a result the title translated title is exceeding the max length of the field data column value of 255 chars.

    So it's a problem with GPT3 API that sometimes it's not able to understand if it should translate the text "blockchain article" or it should write an article in dutch. I have modified the translation prompt for better result. Please download the 2.0.x branch and validate. Please clear your cache as there are changes in routing file.

    Thanks & Regards,
    Anup

  • Status changed to Needs review over 1 year ago
  • 🇮🇳India anup.sinha Bengaluru

    Hi @ressa, can you please test if the admin config form is working for you or not in order to close this ticket.

    Thanks & Regards,
    Anup

  • 🇩🇰Denmark ressa Copenhagen

    Sorry @anup.sinha, but I don't plan on using the module, and don't have time to test it. I hope someone else can test the translation feature.

  • Status changed to Fixed over 1 year ago
  • 🇮🇳India anup.sinha Bengaluru

    Closing this ticket as I have received feedback from users of this module that they are able to save configuration form successfully without any issue.

    Thanks & Regards,
    Anup

  • Status changed to Fixed over 1 year ago
  • 🇮🇳India anup.sinha Bengaluru
Production build 0.71.5 2024