Skip to main content

Prompt Formatting

LiteLLM automatically translates the OpenAI ChatCompletions prompt format, to other models. You can control this by setting a custom prompt template for a model as well.

Stored Templates​

Prompt templates only apply to providers that take a single raw text prompt, such as ollama/, petals/, replicate/, sagemaker/, and predibase/. huggingface/ and together_ai/ call the providers' OpenAI-compatible chat completions APIs, so LiteLLM sends your messages as-is and the provider applies the model's own chat template. Neither the stored templates below nor templates registered with register_prompt_template are used for them

For raw-prompt providers, LiteLLM supports Huggingface Chat Templates and falls back to the model's registered chat template on the Hugging Face Hub (e.g. Mistral-7b). On sagemaker/, pass hf_model_name to pick the template for your endpoint's base model. For popular models, the templates are saved as part of the package

Model NameWorks for Models
mistralai/Mistral-7B-Instruct-v0.1mistralai/Mistral-7B-Instruct-v0.1
meta-llama/Llama-2-7b-chatAll meta-llama llama2 chat models
tiiuae/falcon-7b-instructAll falcon instruct models
mosaicml/mpt-7b-chatAll mpt chat models
codellama/CodeLlama-34b-Instruct-hfAll codellama instruct models
WizardLM/WizardCoder-Python-34B-V1.0All wizardcoder models
Phind/Phind-CodeLlama-34B-v2All phind-codellama models

Jump to code

Format Prompt Yourself​

You can also format the prompt yourself. Register the template under the model name without the provider prefix. Here's how:

import litellm
from litellm import completion

# Create your own custom prompt template
litellm.register_prompt_template(
model="llama2",
initial_prompt_value="You are a good assistant", # [OPTIONAL]
roles={
"system": {
"pre_message": "[INST] <<SYS>>\n", # [OPTIONAL]
"post_message": "\n<</SYS>>\n [/INST]\n" # [OPTIONAL]
},
"user": {
"pre_message": "[INST] ", # [OPTIONAL]
"post_message": " [/INST]" # [OPTIONAL]
},
"assistant": {
"pre_message": "\n", # [OPTIONAL]
"post_message": "\n" # [OPTIONAL]
}
},
final_prompt_value="Now answer as best you can:" # [OPTIONAL]
)

messages = [{"role": "user", "content": "Hey, how's it going?"}]
response = completion(model="ollama/llama2", messages=messages, api_base="http://localhost:11434")
print(response['choices'][0]['message']['content'])

This is supported for raw-prompt providers such as Ollama (ollama/, not ollama_chat/), Petals, Replicate, SageMaker, and Predibase. It has no effect on huggingface/ or together_ai/ chat models

Other providers either have fixed prompt templates (e.g. Anthropic), or accept chat messages and format them server-side (e.g. Hugging Face, Together AI). If there's a provider we're missing coverage for, let us know!

All Providers​

Here's the code for how we format all providers. Let us know how we can improve this further

ProviderModel NameCode
Anthropicclaude-instant-1, claude-instant-1.2, claude-2Code
OpenAI Text Completiontext-davinci-003, text-curie-001, text-babbage-001, text-ada-001, babbage-002, davinci-002,Code
Replicateall model names starting with replicate/Code
Coherecommand-nightly, command, command-light, command-medium-beta, command-xlarge-beta, command-r-plusCode
Huggingfaceall model names starting with huggingface/Code
OpenRouterall model names starting with openrouter/Code
AI21j2-mid, j2-light, j2-ultraCode
VertexAItext-bison, text-bison@001, chat-bison, chat-bison@001, chat-bison-32k, code-bison, code-bison@001, code-gecko@001, code-gecko@latest, codechat-bison, codechat-bison@001, codechat-bison-32kCode
Bedrockall model names starting with bedrock/Code
Sagemakersagemaker/jumpstart-dft-meta-textgeneration-llama-2-7bCode
TogetherAIall model names starting with together_ai/Code
AlephAlphaall model names starting with aleph_alpha/Code
Palmall model names starting with palm/Code
NLP Cloudall model names starting with palm/Code
Petalsall model names starting with petals/Code
LiteLLM Enterprise
SSO/SAML, audit logs, spend tracking, multi-team management, and guardrails, built for production.
Learn more →