Using Command R7B on Hugging Face
This page contains detailed instructions about
- How to set preambles for Command R7B in Hugging Face
- How to run Command R7B in Hugging Face for Chat, RAG, Tool Use and Agents use cases.
Chat Capabilities
Command R7B can be configured as both a conversational model and an instruct model.
- The conversational mode conditions the model on interactive behaviour, meaning it is expected to reply in a conversational fashion, provides introductory statements and follow-up questions, and uses Markdown as well as LaTeX where appropriate. It is optimized for interactive experiences, such as chatbots, where the model engages in dialogue.
- The instruct mode, in contrast, conditions the model to provide concise yet comprehensive responses, and does not use Markdown / LaTeX by default. It is designed for non-interactive, task-focused use cases like extracting information, summarizing text, translation, and categorization.
Conversational Mode
The system preamble for conversational mode is as follows:
Where {Safety Preamble}
represents either the contextual or the strict safety mode preamble.
The contextual safety mode preamble is as follows:
The strict safety mode preamble is as follows:
Instruct Mode
The instruct mode preamble is as follows:
Where {Safety Preamble}
represents either the contextual or the strict safety mode preamble.
Example
An example of how the model can be called using the conversational mode preamble can be found below.
Grounded Generation and RAG Capabilities:
Command R7B has been trained specifically for tasks like summarization and the final step of Retrieval Augmented Generation (RAG). The model takes a conversation as input (with an optional user-supplied system preamble, indicating task, context and desired output style), along with a list of document snippets. This behavior has been trained into the model via a mixture of supervised fine-tuning and preference fine-tuning.
For these tasks, you can use Command R7B in two ways.
Option 1: Grounded Generation
Grounded generation in Command R7B is supported through chat templates in Transformers. Simply provide document snippets using the documents
parameter of Hugging Face’s apply_chat_template()
. Document snippets should be short chunks, rather than long documents, typically around 100-400 words per chunk, formatted as key-value pairs. The keys should be short descriptive strings, the values can be text or semi-structured. Under the hood, this builds a specific prompt template that the model has been trained on. The code snippet below shows a minimal working example.
Option 2: Regular Generation
You may find that simply including relevant documents directly in a user message works just as well, or better than using the documents parameter to render the special grounded generation template. Grounded Generation is generally a strong default, but Regular Generation can offer more control and customization over the prompt, at the cost of some effort to find an optimal prompt. We encourage users to play with both Grounded Generation and Regular Generation, and to evaluate which mode works best for their specific use case.
Tool use, Function Calling & Agent capabilities
Command R7B has been specifically trained with conversational tool use capabilities. This allows the model to interact with external tools like APIs, databases, or search engines. These capabilities have been trained into the model via a mixture of supervised fine-tuning and preference fine-tuning, using a specific prompt template. Deviating from this prompt template will likely reduce performance, but we encourage experimentation.
These tool use capabilities unlock two use cases:
- Function Calling: A single inference where Command R7B selects relevant tools to fulfill a user request.
- Agents: Several inference cycles where Command R7B iterates through Plan → Action → Observation loops until it arrives at a final response.
Both Function Calling and Agents work in the same way. Given a conversation as input (with an optional preamble), along with a list of available tools, the model will generate one of the following:
- Tool Selection: A high-level plan followed by a json-formatted list of actions to execute on a subset of the supplied tools. Command R7B may select multiple tools in parallel, and it may select a tool more than once. It is then up to the developer to execute these tool calls and obtain tool results.
- Or, a Response: A final response to the user. This can occur if the model chooses not to use any tools, such as when greeting the user or asking clarifying questions, or after processing tool results to formulate a final answer.
Tool use in Command R7B is supported through chat templates in Transformers. We recommend providing tool descriptions using JSON schema. Here is a quick example showing tool use.
If the model generates tool calls, you should add them to the chat history like so:
and then call the tool and append the result, with the tool role, like below. It is crucial to format the tool results as a dictionary:
After that, you can generate() again to let the model use the tool result in the chat.