[], ]; foreach ($messages as $message) { if ($message['role'] === 'system') { // system messages go to extra array if (!isset($data['system_instructions'])) { $data['system_instructions'] = []; $data['system_instructions']['parts'] = []; } $data['system_instructions']['parts'][] = ['text' => $message['content']]; } else { $data['contents'][] = [ 'role' => $message['role'], 'parts' => [ ['text' => $message['content']] ] ]; } } $response = $this->request($this->getModelName(), 'generateContent', $data); return $response['candidates'][0]['content']['parts'][0]['text']; } }