提示格式化
LiteLLM 自动将 OpenAI ChatCompletions 提示格式转换为其他模型。你也可以通过为模型设置自定义提示模板来控制这一点。
Huggingface 模型
LiteLLM 支持 Huggingface 聊天模板,并会自动检查你的 Huggingface 模型是否有注册的聊天模板(例如 Mistral-7b)。
对于流行的模型(例如 meta-llama/llama2),我们将它们的模板作为包的一部分保存。
存储的模板
| 模型名称 | 适用于模型 | 完成调用 |
|---|---|---|
| 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 指令模型 | 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 指令模型 | 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
# 创建你自己的自定义提示模板
litellm.register_prompt_template(
model="togethercomputer/LLaMA-2-7B-32K",
initial_prompt_value="You are a good assistant" # [可选]
roles={
"system": {
"pre_message": "[INST] <<SYS>>\n", # [可选]
"post_message": "\n<</SYS>>\n [/INST]\n" # [可选]
},
"user": {
"pre_message": "[INST] ", # [可选]
"post_message": " [/INST]" # [可选]
},
"assistant": {
"pre_message": "\n" # [可选]
"post_message": "\n" # [可选]
}
}
final_prompt_value="Now answer as best you can:" # [可选]
)
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/ 开头的模型名称 | 代码 |