Zum Hauptinhalt springen

Benutzerdefiniertes Prompt-Management

Verbinden Sie LiteLLM mit Ihrem Prompt-Management-System durch benutzerdefinierte Hooks.

Übersicht

Wie es funktioniert

Schnellstart

1. Erstellen Sie Ihren benutzerdefinierten Prompt-Manager

Erstellen Sie eine Klasse, die von CustomPromptManagement erbt, um die Prompt-Abfrage und -Formatierung zu handhaben.

Beispielimplementierung

Erstellen Sie eine neue Datei namens custom_prompt.py und fügen Sie diesen Code hinzu. Die wichtigste Methode hier ist get_chat_completion_prompt. Sie können benutzerdefinierte Logik implementieren, um Prompts basierend auf prompt_id und prompt_variables abzurufen und zu formatieren.

from typing import List, Tuple, Optional
from litellm.integrations.custom_prompt_management import CustomPromptManagement
from litellm.types.llms.openai import AllMessageValues
from litellm.types.utils import StandardCallbackDynamicParams

class MyCustomPromptManagement(CustomPromptManagement):
def get_chat_completion_prompt(
self,
model: str,
messages: List[AllMessageValues],
non_default_params: dict,
prompt_id: str,
prompt_variables: Optional[dict],
dynamic_callback_params: StandardCallbackDynamicParams,
) -> Tuple[str, List[AllMessageValues], dict]:
"""
Retrieve and format prompts based on prompt_id.

Returns:
- model: The model to use
- messages: The formatted messages
- non_default_params: Optional parameters like temperature
"""
# Example matching the diagram: Add system message for prompt_id "1234"
if prompt_id == "1234":
# Prepend system message while preserving existing messages
new_messages = [
{"role": "system", "content": "Be a good Bot!"},
] + messages
return model, new_messages, non_default_params

# Default: Return original messages if no prompt_id match
return model, messages, non_default_params

prompt_management = MyCustomPromptManagement()

2. Konfigurieren Sie Ihren Prompt-Manager in LiteLLM config.yaml

model_list:
- model_name: gpt-4
litellm_params:
model: openai/gpt-4
api_key: os.environ/OPENAI_API_KEY

litellm_settings:
callbacks: custom_prompt.prompt_management # sets litellm.callbacks = [prompt_management]

3. Starten Sie das LiteLLM Gateway

Binden Sie Ihre custom_logger.py in den LiteLLM Docker-Container ein.

docker run -d \
-p 4000:4000 \
-e OPENAI_API_KEY=$OPENAI_API_KEY \
--name my-app \
-v $(pwd)/my_config.yaml:/app/config.yaml \
-v $(pwd)/custom_logger.py:/app/custom_logger.py \
my-app:latest \
--config /app/config.yaml \
--port 4000 \
--detailed_debug \

4. Testen Sie Ihren benutzerdefinierten Prompt-Manager

Wenn Sie prompt_id="1234" übergeben, fügt der benutzerdefinierte Prompt-Manager Ihrer Konversation eine Systemnachricht "Sei ein guter Bot!" hinzu.

from openai import OpenAI

client = OpenAI(
api_key="sk-1234",
base_url="http://0.0.0.0:4000"
)

response = client.chat.completions.create(
model="gemini-1.5-pro",
messages=[{"role": "user", "content": "hi"}],
prompt_id="1234"
)

print(response.choices[0].message.content)

Die Anfrage wird transformiert von

{
"model": "gemini-1.5-pro",
"messages": [{"role": "user", "content": "hi"}],
"prompt_id": "1234"
}

Zu

{
"model": "gemini-1.5-pro",
"messages": [
{"role": "system", "content": "Be a good Bot!"},
{"role": "user", "content": "hi"}
]
}