Zum Hauptinhalt springen

/responses[Beta]

LiteLLM bietet einen BETA-Endpunkt im Stil der /responses API von OpenAI.

FeatureUnterstĂĽtztNotizen
Kostenverfolgungâś…Funktioniert mit allen unterstĂĽtzten Modellen
Protokollierungâś…Funktioniert mit allen Integrationen
Endbenutzerverfolgungâś…
Streamingâś…
Fallbacksâś…Funktioniert zwischen unterstĂĽtzten Modellen
Lastverteilungâś…Funktioniert zwischen unterstĂĽtzten Modellen
Unterstützte OperationenEine Antwort erstellen, Eine Antwort abrufen, Eine Antwort löschen
UnterstĂĽtzte LiteLLM-Versionen1.63.8+
UnterstĂĽtzte LLM-AnbieterAlle von LiteLLM unterstĂĽtzten Anbieteropenai, anthropic, bedrock, vertex_ai, gemini, azure, azure_ai usw.

Verwendung​

LiteLLM Python SDK​

Nicht-streaming​

OpenAI Nicht-Streaming-Antwort
import litellm

# Non-streaming response
response = litellm.responses(
model="openai/o1-pro",
input="Tell me a three sentence bedtime story about a unicorn.",
max_output_tokens=100
)

print(response)

Streaming​

OpenAI Streaming-Antwort
import litellm

# Streaming response
response = litellm.responses(
model="openai/o1-pro",
input="Tell me a three sentence bedtime story about a unicorn.",
stream=True
)

for event in response:
print(event)

Antwort abrufen (GET)​

Antwort nach ID abrufen
import litellm

# First, create a response
response = litellm.responses(
model="openai/o1-pro",
input="Tell me a three sentence bedtime story about a unicorn.",
max_output_tokens=100
)

# Get the response ID
response_id = response.id

# Retrieve the response by ID
retrieved_response = litellm.get_responses(
response_id=response_id
)

print(retrieved_response)

# For async usage
# retrieved_response = await litellm.aget_responses(response_id=response_id)

Antwort löschen (DELETE)​

Antwort nach ID löschen
import litellm

# First, create a response
response = litellm.responses(
model="openai/o1-pro",
input="Tell me a three sentence bedtime story about a unicorn.",
max_output_tokens=100
)

# Get the response ID
response_id = response.id

# Delete the response by ID
delete_response = litellm.delete_responses(
response_id=response_id
)

print(delete_response)

# For async usage
# delete_response = await litellm.adelete_responses(response_id=response_id)

LiteLLM Proxy mit OpenAI SDK​

Richten Sie zuerst Ihren LiteLLM-Proxy-Server ein und starten Sie ihn.

LiteLLM Proxy-Server starten
litellm --config /path/to/config.yaml

# RUNNING on http://0.0.0.0:4000

Fügen Sie zunächst dies zu Ihrer LiteLLM Proxy config.yaml hinzu

OpenAI Proxy-Konfiguration
model_list:
- model_name: openai/o1-pro
litellm_params:
model: openai/o1-pro
api_key: os.environ/OPENAI_API_KEY

Nicht-Streaming​

OpenAI Proxy Nicht-Streaming-Antwort
from openai import OpenAI

# Initialize client with your proxy URL
client = OpenAI(
base_url="https://:4000", # Your proxy URL
api_key="your-api-key" # Your proxy API key
)

# Non-streaming response
response = client.responses.create(
model="openai/o1-pro",
input="Tell me a three sentence bedtime story about a unicorn."
)

print(response)

Streaming​

OpenAI Proxy Streaming-Antwort
from openai import OpenAI

# Initialize client with your proxy URL
client = OpenAI(
base_url="https://:4000", # Your proxy URL
api_key="your-api-key" # Your proxy API key
)

# Streaming response
response = client.responses.create(
model="openai/o1-pro",
input="Tell me a three sentence bedtime story about a unicorn.",
stream=True
)

for event in response:
print(event)

Antwort abrufen (GET)​

Antwort mit OpenAI SDK nach ID abrufen
from openai import OpenAI

# Initialize client with your proxy URL
client = OpenAI(
base_url="https://:4000", # Your proxy URL
api_key="your-api-key" # Your proxy API key
)

# First, create a response
response = client.responses.create(
model="openai/o1-pro",
input="Tell me a three sentence bedtime story about a unicorn."
)

# Get the response ID
response_id = response.id

# Retrieve the response by ID
retrieved_response = client.responses.retrieve(response_id)

print(retrieved_response)

Antwort löschen (DELETE)​

Antwort mit OpenAI SDK nach ID löschen
from openai import OpenAI

# Initialize client with your proxy URL
client = OpenAI(
base_url="https://:4000", # Your proxy URL
api_key="your-api-key" # Your proxy API key
)

# First, create a response
response = client.responses.create(
model="openai/o1-pro",
input="Tell me a three sentence bedtime story about a unicorn."
)

# Get the response ID
response_id = response.id

# Delete the response by ID
delete_response = client.responses.delete(response_id)

print(delete_response)

Unterstützte Parameter der Responses API​

AnbieterUnterstĂĽtzte Parameter
openaiAlle Parameter der Responses API werden unterstĂĽtzt
azureAlle Parameter der Responses API werden unterstĂĽtzt
anthropicUnterstĂĽtzte Parameter hier einsehen
bedrockUnterstĂĽtzte Parameter hier einsehen
geminiUnterstĂĽtzte Parameter hier einsehen
vertex_aiUnterstĂĽtzte Parameter hier einsehen
azure_aiUnterstĂĽtzte Parameter hier einsehen
Alle anderen LLM API-AnbieterUnterstĂĽtzte Parameter hier einsehen

Lastverteilung mit Sitzungskontinuität.​

Bei Verwendung der Responses API mit mehreren Bereitstellungen desselben Modells (z. B. mehrere Azure OpenAI-Endpunkte) bietet LiteLLM eine Sitzungskontinuität. Dies stellt sicher, dass Folgeanfragen, die eine previous_response_id verwenden, an dieselbe Bereitstellung weitergeleitet werden, die die ursprüngliche Antwort generiert hat.

Beispielverwendung​

Python SDK mit Sitzungskontinuität
import litellm

# Set up router with multiple deployments of the same model
router = litellm.Router(
model_list=[
{
"model_name": "azure-gpt4-turbo",
"litellm_params": {
"model": "azure/gpt-4-turbo",
"api_key": "your-api-key-1",
"api_version": "2024-06-01",
"api_base": "https://endpoint1.openai.azure.com",
},
},
{
"model_name": "azure-gpt4-turbo",
"litellm_params": {
"model": "azure/gpt-4-turbo",
"api_key": "your-api-key-2",
"api_version": "2024-06-01",
"api_base": "https://endpoint2.openai.azure.com",
},
},
],
optional_pre_call_checks=["responses_api_deployment_check"],
)

# Initial request
response = await router.aresponses(
model="azure-gpt4-turbo",
input="Hello, who are you?",
truncation="auto",
)

# Store the response ID
response_id = response.id

# Follow-up request - will be automatically routed to the same deployment
follow_up = await router.aresponses(
model="azure-gpt4-turbo",
input="Tell me more about yourself",
truncation="auto",
previous_response_id=response_id # This ensures routing to the same deployment
)

Sitzungsverwaltung - Nicht-OpenAI-Modelle​

Der LiteLLM Proxy unterstützt die Sitzungsverwaltung für Nicht-OpenAI-Modelle. Dies ermöglicht es Ihnen, den Konversationsverlauf (Zustand) im LiteLLM Proxy zu speichern und abzurufen.

Verwendung​

  1. Speicherung des Anfrage-/Antwortinhalts in der Datenbank aktivieren

Setzen Sie store_prompts_in_spend_logs: true in Ihrer proxy config.yaml. Wenn dies aktiviert ist, speichert LiteLLM den Anfrage- und Antwortinhalt in der Datenbank.

general_settings:
store_prompts_in_spend_logs: true
  1. Anfrage 1 ohne previous_response_id stellen (neue Sitzung)

Starten Sie eine neue Konversation, indem Sie eine Anfrage stellen, ohne eine vorherige Antwort-ID anzugeben.

curl https://:4000/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"model": "anthropic/claude-3-5-sonnet-latest",
"input": "who is Michael Jordan"
}'

Antwort

{
"id":"resp_123abc",
"model":"claude-3-5-sonnet-20241022",
"output":[{
"type":"message",
"content":[{
"type":"output_text",
"text":"Michael Jordan is widely considered one of the greatest basketball players of all time. He played for the Chicago Bulls (1984-1993, 1995-1998) and Washington Wizards (2001-2003), winning 6 NBA Championships with the Bulls."
}]
}]
}
  1. Anfrage 2 mit previous_response_id stellen (gleiche Sitzung)

Setzen Sie die Konversation fort, indem Sie die vorherige Antwort-ID referenzieren, um den Konversationskontext beizubehalten.

curl https://:4000/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"model": "anthropic/claude-3-5-sonnet-latest",
"input": "can you tell me more about him",
"previous_response_id": "resp_123abc"
}'

Antwort

{
"id":"resp_456def",
"model":"claude-3-5-sonnet-20241022",
"output":[{
"type":"message",
"content":[{
"type":"output_text",
"text":"Michael Jordan was born February 17, 1963. He attended University of North Carolina before being drafted 3rd overall by the Bulls in 1984. Beyond basketball, he built the Air Jordan brand with Nike and later became owner of the Charlotte Hornets."
}]
}]
}
  1. Anfrage 3 ohne previous_response_id stellen (neue Sitzung)

Starten Sie eine brandneue Konversation, ohne sich auf vorherigen Kontext zu beziehen, um zu demonstrieren, wie der Kontext zwischen den Sitzungen nicht aufrechterhalten wird.

curl https://:4000/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"model": "anthropic/claude-3-5-sonnet-latest",
"input": "can you tell me more about him"
}'

Antwort

{
"id":"resp_789ghi",
"model":"claude-3-5-sonnet-20241022",
"output":[{
"type":"message",
"content":[{
"type":"output_text",
"text":"I don't see who you're referring to in our conversation. Could you let me know which person you'd like to learn more about?"
}]
}]
}