Add chat_history form element

Created on 7 June 2025, 3 months ago

Problem/Motivation

A common thing that we will see more and more is that we want to be able to expose chat history in a nice way in forms. See for instance how its done in platform.openai.com

To make it easier to implement this, we need a form element that is a chat history, that takes common parts of a message like user, content (text/image), tool_id, tool_result.

It will then be represented by a array with associative arrays inside it when expressing default value.

This is already done in the coming ai_agent_tracing module, but it fits better here so it can be reused.

Later a field type could also be added if needed.

Steps to reproduce

Proposed resolution

Add the form elemt chat_history.

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

1.2

Component

AI Core module

Created by

🇩🇪Germany marcus_johansson

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

Merge Requests

Comments & Activities

  • Issue created by @marcus_johansson
  • 🇧🇬Bulgaria valthebald Sofia
  • 🇩🇪Germany marcus_johansson

    So the current version of this is good if you just need "user" and "assistant" interaction, but there are two I think important things missing for us to be able to replicate what exists on platform.openai.com (or used to).

    That is:

    1. Is that when you get a tool usage response back, that information is in a separate place in the response, so that should be possible to replicate with tool call id, function name and function input. Even if that is for now just an array of those three things as strings. See https://platform.openai.com/docs/guides/function-calling#handling-functi... (in this case called id, name, arguments)

    2. The next step is also that you can set a tool response. So that is the response that you give back after you have run the execute on your Drupal machine. This is unique because it has to add the tool call id referenced from the decision. The role is already added, but adding the tool call id should be based on if the tool role is "tool".

    3. A test would be nice on top of that.

    4. Developer documentation as well.

  • 🇮🇳India abhisekmazumdar India

    I got the gist of it and will give it a try
    My understanding is this chat_history FormElement will need the following changes next:

    - A existing / new section of assistant is created it should have the following as string as of now

    • tool_call_id
    • function_name
    • function_input

    - And for tool add a field tool_call_id_reference.

  • 🇩🇪Germany marcus_johansson

    That is almost correct, the minor thing is that it should be an array of

    • tool_call_id
    • function_name
    • funtion_input

    So it needs a way to add more, add less for a complex object. I think that makes sense to set to #tree=true then as well, but you could test that.

  • Pipeline finished with Failed
    10 days ago
    Total: 236s
    #591700
  • 🇮🇳India abhisekmazumdar India

    I made the update to the Draft changes: https://git.drupalcode.org/project/ai/-/merge_requests/719/diffs?commit_...

    This includes added Tool call section for the Role Assistant which has the above said fields.
    And Role Tool has a tool called Reference ID

    Example use of the element:

    $form['conversation'] = [
          '#type' => 'chat_history',
          '#title' => $this->t('Conversation'),
          '#default_value' => [
            [
              'role' => 'user', 
              'content' => 'What is the weather like?'
            ],
            [
              'role' => 'assistant',
              'content' => 'I can check that for you.',
              'tool_calls' => [
                [
                  'tool_call_id' => 'call_1',
                  'function_name' => 'get_weather',
                  'function_input' => '{"location":"Oslo","unit":"celsius"}',
                ],
              ],
            ],
            [
              'role' => 'tool', 
              'content' => '{"temp":25,"condition":"sunny"}', 
              'tool_call_id_reference' => 'call_1'
            ],
            [
              'role' => 'user', 
              'content' => 'Can you also check the time?'
            ],
          ],
          '#chat_history_add_more' => TRUE,
          '#rows' => 2,
        ];
    

    preview:

    Sharing my work in progress here to ensure I'm heading in the right direction.

  • Pipeline finished with Failed
    8 days ago
    Total: 396s
    #592879
  • Pipeline finished with Success
    8 days ago
    Total: 185s
    #592892
  • Pipeline finished with Success
    8 days ago
    Total: 493s
    #592935
  • 🇮🇳India abhisekmazumdar India

    I have made the final changes, added the document and test. I also made some changes to the CSS and cleaned up the other parts of the code. This is now up for review.

    Example usage:

    $form['conversation'] = [
          '#type' => 'chat_history',
          '#title' => $this->t('Conversation'),
          '#default_value' => [
            [
              'role' => 'user',
              'content' => 'What is the weather like?'
            ],
            [
              'role' => 'assistant',
              'content' => 'I can check that for you.',
              'tool_calls' => [
                [
                  'tool_call_id' => 'call_1',
                  'function_name' => 'get_weather',
                  'function_input' => '{"location":"Oslo","unit":"celsius"}',
                ],
              ],
            ],
            [
              'role' => 'tool',
              'content' => '{"temp":25,"condition":"sunny"}',
              'tool_call_id_reference' => 'call_1'
            ],
          ],
        ];
    

  • Pipeline finished with Success
    8 days ago
    Total: 245s
    #592943
  • 🇩🇪Germany marcus_johansson

    Thanks, tested, code reviewed - looks great. I added one linkage to the mkdocs file, so the documentation shows up on https://project.pages.drupalcode.org/ai/1.2.x/

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

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

    Maintainers, please credit people who helped resolve this issue.

Production build 0.71.5 2024