Skip to content

Add name field to OpenAI compatible API Messages #1558

@amihalik

Description

@amihalik

Feature request

OpenAI Message contain content, role, and an optional name field. OpenAI Explains:

Messages can also contain an optional name field, which give the messenger a name. E.g., example-user, Alice, BlackbeardBot. Names may not contain spaces.
Reference

This request is to add the name field to the existing OpenAI Message in TGI.

Motivation

PR #1427 by @drbh is fantastically useful. It has really lowered the barrier to trial new models off of hugging face with tools that require OpenAI API interfaces.

However, some tools (e.g. AutoGen) make use of the name field in messages to identify different participants in the conversation. Adding the optional name field would be a minor change that could really improve integration with other tools.

A valid concern is that none of the current chat_template values in tokenizer_config.json use the name field. However, I've found it's straightforward to overwrite chat_template with jq. I've provided a chat_template for llama2 in the contribution section.

Your contribution

I'll take a look at #1427 to see how @drbh implemented the OpenAI Messages. Hopefully, it's a small, straightforward change. Otherwise, I won't be able to contribute much to the actual implementation.

Additionally, here is a llama2 chat_template that incorporates name that others are free to use:

"chat_template": "{% if messages[0]['role'] == 'system' %}{% set loop_messages = messages[1:] %}{% set system_message = messages[0]['content'] %}{% else %}{% set loop_messages = messages %}{% set system_message = false %}{% endif %}{% for message in loop_messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% set content = message['content']|trim %}{% if message['name'] %}{% set content = '@' + message['name'] + ': ' + content %}{% endif %}{% if loop.index0 == 0 and system_message != false %}{% set content = '<<SYS>>\\n' + system_message + '\\n<</SYS>>\\n\\n' + content %}{% endif %}{% if message['role'] == 'user' %}{{ bos_token + '[INST] ' + content + ' [/INST]' }}{% elif message['role'] == 'assistant' %}{{ ' '  + content + ' ' + eos_token }}{% endif %}{% endfor %}"

Here is pretty printed version of the template:

{% if messages[0]['role'] == 'system' %}
  {% set loop_messages = messages[1:] %}
  {% set system_message = messages[0]['content'] %}
{% else %}
  {% set loop_messages = messages %}
  {% set system_message = false %}
{% endif %}

{% for message in loop_messages %}
  {% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}
    {{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}
  {% endif %}

  {% set content = message['content']|trim %}
  {% if message['name'] %}
    {% set content = '@' + message['name'] + ': ' + content %}
  {% endif %}

  {% if loop.index0 == 0 and system_message != false %}
    {% set content = '<<SYS>>\n' + system_message + '\n<</SYS>>\n\n' + content %}
  {% endif %}
  {% if message['role'] == 'user' %}
    {{ bos_token + '[INST] ' + content + ' [/INST]' }}
  {% elif message['role'] == 'assistant' %}
    {{ ' '  + content + ' ' + eos_token }}
  {% endif %}
{% endfor %}

Finally, here is a conversation generated with that template and meta-llama/Llama-2-7b-chat-hf

"messages": [
 {"role": "system", "content": "You are a succinct but helpful AI Assistant listening to a chat server.  Address everyone by @<username>"},
 {"role": "user", "name", "Aaron", "content": "Hello There!"},
 {"role": "assistant", "content": "  Hello @Aaron! How can I assist you today?"},
 {"role": "user", "name": "Sally", "content": "Hiya everyone.  Is @Aaron is this room?"},
 {"role": "assistant", "content": " Hello @Sally! Yes, @Aaron is currently in this room. How can I help you?"}
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions