Problem/Motivation

The chat() method signature accepts array|string|ChatInput, but when a string is passed, line 312 calls $input->isStreamedOutput() on the string, causing a fatal error.

Steps to reproduce

$provider = \Drupal::service("ai.provider")->createInstance("mistral");                                                                                                             
  $provider->chat("Hello", "mistral-small-latest");                                                                                                                                   
  // Error: Call to a member function isStreamedOutput() on string   

Proposed resolution

Normalize string and array inputs to ChatInput at the start of the chat() method, matching the interface contract. Additionally, guard the isStreamedOutput() call to only apply for ChatInput objects.

- String input: Convert to ChatInput with a single user message
- Array input: Convert to ChatInput by mapping array elements to ChatMessage objects
- Add type check before calling $input->isStreamedOutput()

Remaining tasks

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

petar_basic created an issue. See original summary.

petar_basic’s picture

Assigned: petar_basic » Unassigned
Status: Needs work » Needs review

Added support for string and array input types as declared in the ChatInterface.

Also added 2 tests for string & array as parms to the chat.

to test, with a valid API key configured:

// for the string input
$provider = \Drupal::service("ai.provider")->createInstance("mistral");                                                                                                             
  $output = $provider->chat("Hello, how are you?", "mistral-small-latest");                                                                                                           
  echo $output->getNormalized()->getText();                                                                                                                                           
                                                                                                                                                                                      
  // for array input                                                                                                                          
  $provider = \Drupal::service("ai.provider")->createInstance("mistral");                                                                                                             
  $output = $provider->chat([                                                                                                                                                         
    ['role' => 'user', 'content' => 'What is 2+2?'],                                                                                                                                  
    ['role' => 'assistant', 'content' => '4'],                                                                                                                                        
    ['role' => 'user', 'content' => 'And what is 3+3?'],                                                                                                                              
  ], "mistral-small-latest");                                                                                                                                                         
  echo $output->getNormalized()->getText();                                                                                                                                           
                                                                                                                                                                                      
  //Regression test                                                                                                                           
  use Drupal\ai\OperationType\Chat\ChatInput;                                                                                                                                         
  use Drupal\ai\OperationType\Chat\ChatMessage;                                                                                                                                       
                                                                                                                                                                                      
  $provider = \Drupal::service("ai.provider")->createInstance("mistral");                                                                                                             
  $input = new ChatInput([new ChatMessage('user', 'Hello!')]);                                                                                                                        
  $output = $provider->chat($input, "mistral-small-latest");                                                                                                                          
  echo $output->getNormalized()->getText();

There should be no error in any of the runs

fago made their first commit to this issue’s fork.

fago’s picture

Makes sense and works, however since we normalized the input to ChatInput we should simplify the code to not check it afterwards any more. Adding that, otherwise all good

  • fago committed 6d28f552 on 1.1.x authored by petar_basic
    fix: #3570605 Fix chat() method failing when string/array input is...
fago’s picture

Status: Needs review » Fixed

change works + pass tests - merged!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.