Function call implementation in custom backend

Created on 4 October 2023, about 1 year ago
Updated 6 October 2023, about 1 year ago

Hello, first thanks for the great work ! Based on your example i could create my own custom backend successfully.

I then wanted to implement function calls, but perhaps i missed some point.

Problem/Motivation

Implement function calls as documented in openai-php repo

Steps to reproduce

create a custom backend and try function call implementation like this :

/**
   * Helper method to query OpenAI.
   */
  public function queryOpenAI(MessageInterface $response_message): array {
    $messages_array = $this->getMessageHistoryArray();

    try {
      $client = \OpenAI::factory()
        ->withApiKey($this->getApiKey())
        ->withHttpClient(\Drupal::httpClient())
        ->make();

      // $fn_test for function call test ********************************************************
      $fn_test = [
        [
          "name" => "get_current_weather",
          "description" => "Gets the current weather information",
          "parameters" => [
              "type" => "object",
              "properties" => [
                  "location" => [
                      "type" => "string",
                      "description" => "Location for which to get the weather information",
                  ],
              ],
              "required" => ["location"]
          
      ]
        ]
            ];

      $response = $client->chat()->create([
        'model' => $this->getBackendConfigurationValue('model'),
        'messages' => $messages_array,
        // function call test *********************************************************************
        'functions' => $fn_test,
        'function_call' => 'auto',
      ]);
      $response_message->setResponseData($response->toArray());
      $response_message->save();
      dpm($response_message);
      return [];
    }
    catch (\Exception $exception) {
      $response_message->setException($exception);
      $response_message->save();
      return ['warning' => $this->t('There was a problem sending or receiving answer from artificial intelligence.')];
    }
  }

Logged error

When the function is called the following error occurs :

ypeError : OpenAI\Responses\Chat\CreateResponseMessage::__construct(): Argument #2 ($content) must be of type string, null given, called in /var/www/html/aidev/vendor/openai-php/client/src/Responses/Chat/CreateResponseMessage.php on line 22 dans OpenAI\Responses\Chat\CreateResponseMessage->__construct() (ligne 9 de /var/www/html/aidev/vendor/openai-php/client/src/Responses/Chat/CreateResponseMessage.php).

Remaining tasks

Understanding function call in custom backend

💬 Support request
Status

Closed: works as designed

Version

1.0

Component

Code

Created by

🇫🇷France Roman_L

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

Comments & Activities

  • Issue created by @Roman_L
  • Status changed to Fixed about 1 year ago
  • 🇱🇹Lithuania mindaugasd

    Hi, you are welcome.
    This error appears in openai-php/client library.
    Someone else described this error in https://github.com/openai-php/client/issues/189 and closed the bug report without explanation.
    But we can now see that this bug is already fixed in latest code https://github.com/openai-php/client/blob/main/src/Responses/Chat/Create...
    To solve this error, please update to the latest openai library version (as listed in https://github.com/openai-php/client/tags)

  • 🇫🇷France Roman_L

    Hi,

    right, updating openai lib solved this issue.

    Also, do you plan to add 'name' param in MessageBase.php for function calls usage?

    as there are not setName/getName OpenAI lib will return :

    Missing parameter 'name': messages with role 'function' must have a 'name'

    Thanks and let me know if i can help on this.

  • 🇱🇹Lithuania mindaugasd

    aichat is meant to display things to the user (messages with audio, images, files, code etc.)
    And function calling part belongs to the backend.

    In case you would like to display something to the user within the message, there are MessageBase.php methods called setContent() and getContent(). At the moment, only text was tested implementing methods called setText() and getText().

    There is a task on the roadmap called 8. Test with images, audio and other media display, which would allow for me to test and adapt for multimedia content. Development of this needs funding.

    Here is a demo video https://youtu.be/V-0EqaQvAXo?feature=shared&t=41 showing aichat could allow to interact with AI in all kinds of ways, like reviewing code, providing buttons to click (interactive features) etc. But this demo is built in custom code back then.

    let me know if i can help on this.

    It is either funding aichat features on the roadmap, or contributing code (for content display).

    Function calling as a default Drupal feature is meant to be implemented within AI Work ( https://www.drupal.org/project/aiwork ) backend module, which would allow to interact with AI with all kinds of different ways (like displayed in the video). AI work module is for "AI doing the work" (includes function calling). And "AI chat user interface" is a frontend module for displaying things and providing user interface for the user to interact with AI. Since AI Work is unfunded, so you have to implement function calling with your custom code (within your custom backend).

    do you plan to add 'name' param

    Maybe no, because there are infinite number of variations of the data that aichat could display (from different service providers like Google, Meta, open source etc.) To save message data, maybe you want to look at ExampleBackend.php methods called loadMessages() and saveMessage() in custom backend, or modifying

    Message.php

    file in your custom backend. Full response data is stored within Message array keyed 'response_data', but you can also extend Message.php.

  • Status changed to Closed: works as designed about 1 year ago
  • 🇫🇷France Roman_L

    Thank you for this complete explanation!

Production build 0.71.5 2024