프롬프트 포매팅

프롬프트 포매팅 (Prompt Formatting)

LiteLLM은 OpenAI ChatCompletions 프롬프트 형식을 다른 모델로 자동 변환합니다. 모델에 커스텀 프롬프트 템플릿을 설정해 제어할 수도 있어요.

Huggingface 모델

LiteLLM은 Huggingface Chat Templates 를 지원하며, 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 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 Code
OpenAI Text Completion text-davinci-003, text-curie-001, text-babbage-001, text-ada-001, babbage-002, davinci-002 Code
Replicate replicate/로 시작하는 모든 모델 이름 Code
Cohere command-nightly, command, command-light, command-medium-beta, command-xlarge-beta, command-r-plus Code
Huggingface huggingface/로 시작하는 모든 모델 이름 Code
OpenRouter openrouter/로 시작하는 모든 모델 이름 Code
AI21 j2-mid, j2-light, j2-ultra Code
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 Code
Bedrock bedrock/로 시작하는 모든 모델 이름 Code
Sagemaker sagemaker/jumpstart-dft-meta-textgeneration-llama-2-7b Code
TogetherAI together_ai/로 시작하는 모든 모델 이름 Code
AlephAlpha aleph_alpha/로 시작하는 모든 모델 이름 Code
Palm palm/로 시작하는 모든 모델 이름 Code
NLP Cloud palm/로 시작하는 모든 모델 이름 Code
Petals petals/로 시작하는 모든 모델 이름 Code

더 알아보기 (Learn more)