All LiteLLM models work out-of-the-box with the python openai library
pip install openai
import openai # openai v1.0.0+
client = openai.OpenAI(api_key="your-api-key",base_url="<https://litellm.sph-prod.ethz.ch/v1>") # set proxy to base_url
# request sent to model set on litellm proxy, `litellm --model`
response = client.chat.completions.create(model="anthropic/claude-sonnet-4-5", messages = [
{
"role": "user",
"content": "this is a test request, write a short poem"
}
])
print(response) # print the whole response
print(response.choices[0].message.content) # print the response from the model
ChatCompletion(
id='chatcmpl-fcd8c6ee-1787-4532-8fe7-c3b92c37e6db',
choices=[
Choice(
finish_reason='stop',
index=0,
logprobs=None,
message=ChatCompletionMessage(
content=(
"Here's a short poem for you:\\n\\n"
"Sunlight dancing through the trees,\\n"
"Gentle whispers in the breeze,\\n"
"Morning dew on grass so green,\\n"
"Nature's beauty rarely seen."
),
refusal=None,
role='assistant',
annotations=None,
audio=None,
function_call=None,
tool_calls=None
)
)
],
created=1745400929,
model='claude-3-5-sonnet-20241022',
object='chat.completion',
service_tier=None,
system_fingerprint=None,
usage=CompletionUsage(
completion_tokens=46,
prompt_tokens=17,
total_tokens=63,
completion_tokens_details=None,
prompt_tokens_details=PromptTokensDetails(
audio_tokens=None,
cached_tokens=0
),
cache_creation_input_tokens=0,
cache_read_input_tokens=0
)
)
import openai
client = openai.OpenAI(api_key="your-api-key",base_url="<https://litellm.sph-prod.ethz.ch/v1>") # set proxy to base_url
model = "claude-3-5-sonnet" # set model to use
# Prompt to set the mood for a chat
response = client.chat.completions.create(model=model, messages = [
{
"role": "user",
"content": "This is going to be a chat with a user. Each new prompt will include the previous chat history, with user input denoted \\"user input \\" and AI reply denoted \\"AI reply\\". The AI should respond to the user input in a conversational manner, and the chat history should be updated with each new message."
}
])
print(response.choices[0].message.content) # print the response from the model
chat_history = ""
while True:
user_input = input("You: ")
if user_input.lower() in ["exit", "quit"]:
break
print(f"User: {user_input}")
# chat_history.append("User input: " + user_input)
chat_history += "User input: " + user_input + "\\n"
response = client.chat.completions.create(model=model, messages=[
{
"role": "user",
"content": chat_history
}
],)
reply = response.choices[0].message.content
print(f"Bot: {reply}")
chat_history += "AI reply: " + reply + "\\n"