提示格式
LiteLLM 自动将 OpenAI ChatCompletions 的提示格式转换为其他模型。您也可以通过为模型设置自定义提示模板来控制此行为。
Huggingface 模型
LiteLLM 支持 Huggingface Chat Templates,并会自动检查您的 Huggingface 模型是否注册了聊天模板(例如 Mistral-7b)。
对于流行模型(例如 meta-llama/llama2),我们已将其模板作为软件包的一部分保存。
存储的模板
模型名称 | 适用的模型 | Completion 调用 |
---|---|---|
mistralai/Mistral-7B-Instruct-v0.1 | mistralai/Mistral-7B-Instruct-v0.1 | completion(model='huggingface/mistralai/Mistral-7B-Instruct-v0.1', messages=messages, api_base="your_api_endpoint") |
meta-llama/Llama-2-7b-chat | 所有 meta-llama llama2 聊天模型 | completion(model='huggingface/meta-llama/Llama-2-7b', messages=messages, api_base="your_api_endpoint") |
tiiuae/falcon-7b-instruct | 所有 falcon instruct 模型 | completion(model='huggingface/tiiuae/falcon-7b-instruct', messages=messages, api_base="your_api_endpoint") |
mosaicml/mpt-7b-chat | 所有 mpt 聊天模型 | completion(model='huggingface/mosaicml/mpt-7b-chat', messages=messages, api_base="your_api_endpoint") |
codellama/CodeLlama-34b-Instruct-hf | 所有 codellama instruct 模型 | completion(model='huggingface/codellama/CodeLlama-34b-Instruct-hf', messages=messages, api_base="your_api_endpoint") |
WizardLM/WizardCoder-Python-34B-V1.0 | 所有 wizardcoder 模型 | completion(model='huggingface/WizardLM/WizardCoder-Python-34B-V1.0', messages=messages, api_base="your_api_endpoint") |
Phind/Phind-CodeLlama-34B-v2 | 所有 phind-codellama 模型 | completion(model='huggingface/Phind/Phind-CodeLlama-34B-v2', messages=messages, api_base="your_api_endpoint") |
自行格式化提示
您也可以自行格式化提示。方法如下
import litellm
# Create your own custom prompt template
litellm.register_prompt_template(
model="togethercomputer/LLaMA-2-7B-32K",
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]
)
def test_huggingface_custom_model():
model = "huggingface/togethercomputer/LLaMA-2-7B-32K"
response = completion(model=model, messages=messages, api_base="https://my-huggingface-endpoint")
print(response['choices'][0]['message']['content'])
return response
test_huggingface_custom_model()
目前支持 Huggingface、TogetherAI、Ollama 和 Petals。
其他提供商要么具有固定的提示模板(例如 Anthropic),要么自行格式化(例如 Replicate)。如果遗漏了对某个提供商的支持,请告知我们!
所有提供商
这是我们如何格式化所有提供商的代码。如果您有改进建议,请告诉我们
提供商 | 模型名称 | 代码 |
---|---|---|
Anthropic | claude-instant-1 , claude-instant-1.2 , claude-2 | 代码 |
OpenAI 文本补全 | text-davinci-003 , text-curie-001 , text-babbage-001 , text-ada-001 , babbage-002 , davinci-002 , | 代码 |
Replicate | 所有以 replicate/ 开头的模型名称 | 代码 |
Cohere | command-nightly , command , command-light , command-medium-beta , command-xlarge-beta , command-r-plus | 代码 |
Huggingface | 所有以 huggingface/ 开头的模型名称 | 代码 |
OpenRouter | 所有以 openrouter/ 开头的模型名称 | 代码 |
AI21 | j2-mid , j2-light , j2-ultra | 代码 |
VertexAI | text-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-32k | 代码 |
Bedrock | 所有以 bedrock/ 开头的模型名称 | 代码 |
Sagemaker | sagemaker/jumpstart-dft-meta-textgeneration-llama-2-7b | 代码 |
TogetherAI | 所有以 together_ai/ 开头的模型名称 | 代码 |
AlephAlpha | 所有以 aleph_alpha/ 开头的模型名称 | 代码 |
Palm | 所有以 palm/ 开头的模型名称 | 代码 |
NLP Cloud | 所有以 palm/ 开头的模型名称 | 代码 |
Petals | 所有以 petals/ 开头的模型名称 | 代码 |