mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-27 11:43:54 +00:00
add switching api keys + api bases to docs
This commit is contained in:
parent
a662ac6202
commit
7aaa857c90
4 changed files with 39 additions and 16 deletions
|
@ -1,12 +1,9 @@
|
||||||
# Reliability
|
# Reliability
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Helper utils
|
## Helper utils
|
||||||
LiteLLM supports the following functions for reliability:
|
LiteLLM supports the following functions for reliability:
|
||||||
* `litellm.longer_context_model_fallback_dict`: Dictionary which has a mapping for those models which have larger equivalents
|
* `litellm.longer_context_model_fallback_dict`: Dictionary which has a mapping for those models which have larger equivalents
|
||||||
* `completion_with_retries`: use tenacity retries
|
* `completion_with_retries`: use tenacity retries
|
||||||
* `completion()` with fallback models: set `fallback_models=['gpt-3.5-turbo', 'command-nightly', 'llama2`]. If primary model fails try fallback models
|
* `completion()` with fallbacks: switch between models/keys/api bases in case of errors.
|
||||||
|
|
||||||
## Context Window Errors
|
## Context Window Errors
|
||||||
|
|
||||||
|
@ -55,7 +52,8 @@ def test_completion_custom_provider_model_name():
|
||||||
printf"Error occurred: {e}")
|
printf"Error occurred: {e}")
|
||||||
```
|
```
|
||||||
|
|
||||||
## Specify fallback models
|
## Switch Models/API Keys/API Bases
|
||||||
|
|
||||||
LLM APIs can be unstable, completion() with fallbacks ensures you'll always get a response from your calls
|
LLM APIs can be unstable, completion() with fallbacks ensures you'll always get a response from your calls
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
@ -63,8 +61,19 @@ To use fallback models with `completion()`, specify a list of models in the `fal
|
||||||
|
|
||||||
The `fallbacks` list should include the primary model you want to use, followed by additional models that can be used as backups in case the primary model fails to provide a response.
|
The `fallbacks` list should include the primary model you want to use, followed by additional models that can be used as backups in case the primary model fails to provide a response.
|
||||||
|
|
||||||
|
### switch models
|
||||||
```python
|
```python
|
||||||
response = completion(model="bad-model", fallbacks=["gpt-3.5-turbo" "command-nightly"], messages=messages)
|
response = completion(model="bad-model", messages=messages,
|
||||||
|
fallbacks=["gpt-3.5-turbo" "command-nightly"])
|
||||||
|
```
|
||||||
|
|
||||||
|
### switch api keys/bases (E.g. azure deployment)
|
||||||
|
Switch between different keys for the same azure deployment, or use another deployment as well.
|
||||||
|
|
||||||
|
```python
|
||||||
|
api_key="bad-key"
|
||||||
|
response = completion(model="azure/gpt-4", messages=messages, api_key=api_key,
|
||||||
|
fallbacks=[{"api_key": "good-key-1"}, {"api_key": "good-key-2", "api_base": "good-api-base-2"}])
|
||||||
```
|
```
|
||||||
|
|
||||||
### Output from calls
|
### Output from calls
|
||||||
|
|
Binary file not shown.
|
@ -672,22 +672,29 @@ def completion(
|
||||||
litellm.openai_key or
|
litellm.openai_key or
|
||||||
get_secret("DEEPINFRA_API_KEY")
|
get_secret("DEEPINFRA_API_KEY")
|
||||||
)
|
)
|
||||||
|
openai.api_base = "https://api.deepinfra.com/v1/openai"
|
||||||
## LOGGING
|
## LOGGING
|
||||||
logging.pre_call(
|
logging.pre_call(
|
||||||
input=messages,
|
input=messages,
|
||||||
api_key=api_key,
|
api_key=api_key,
|
||||||
)
|
)
|
||||||
## COMPLETION CALL
|
## COMPLETION CALL
|
||||||
openai.api_key = api_key # set key for deep infra
|
openai.api_key = "1LntUh4fmg5z6iEW7UPPRhGdBDNuJx5y"
|
||||||
|
openai.api_base = "https://api.deepinfra.com/v1/openai"
|
||||||
try:
|
try:
|
||||||
response = openai.ChatCompletion.create(
|
chat_completion = openai.ChatCompletion.create(
|
||||||
model=model,
|
model="meta-llama/Llama-2-70b-chat-hf",
|
||||||
messages=messages,
|
messages=[{"role": "user", "content": "Hello world"}]
|
||||||
api_base="https://api.deepinfra.com/v1/openai", # use the deepinfra api base
|
|
||||||
api_type="openai",
|
|
||||||
api_version=api_version, # default None
|
|
||||||
**optional_params,
|
|
||||||
)
|
)
|
||||||
|
print(f"chat_completion: {chat_completion}")
|
||||||
|
# response = openai.ChatCompletion.create(
|
||||||
|
# model=model,
|
||||||
|
# messages=messages,
|
||||||
|
# api_base="https://api.deepinfra.com/v1/openai", # use the deepinfra api base
|
||||||
|
# api_type="openai",
|
||||||
|
# api_version=api_version, # default None
|
||||||
|
# **optional_params,
|
||||||
|
# )
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
## LOGGING - log the original exception returned
|
## LOGGING - log the original exception returned
|
||||||
logging.post_call(
|
logging.post_call(
|
||||||
|
|
|
@ -12,7 +12,7 @@ import litellm
|
||||||
from litellm import embedding, completion
|
from litellm import embedding, completion
|
||||||
|
|
||||||
|
|
||||||
litellm.set_verbose = True
|
# litellm.set_verbose = True
|
||||||
user_message = "Hello, how are you?"
|
user_message = "Hello, how are you?"
|
||||||
messages = [{"content": user_message, "role": "user"}]
|
messages = [{"content": user_message, "role": "user"}]
|
||||||
model_val = None
|
model_val = None
|
||||||
|
@ -41,7 +41,7 @@ def test_completion_with_no_provider():
|
||||||
print(f"error occurred: {e}")
|
print(f"error occurred: {e}")
|
||||||
pass
|
pass
|
||||||
|
|
||||||
test_completion_with_no_provider()
|
# test_completion_with_no_provider()
|
||||||
# # bad key
|
# # bad key
|
||||||
# temp_key = os.environ.get("OPENAI_API_KEY")
|
# temp_key = os.environ.get("OPENAI_API_KEY")
|
||||||
# os.environ["OPENAI_API_KEY"] = "bad-key"
|
# os.environ["OPENAI_API_KEY"] = "bad-key"
|
||||||
|
@ -53,3 +53,10 @@ test_completion_with_no_provider()
|
||||||
# print(f"error occurred: {traceback.format_exc()}")
|
# print(f"error occurred: {traceback.format_exc()}")
|
||||||
# pass
|
# pass
|
||||||
# os.environ["OPENAI_API_KEY"] = str(temp_key) # this passes linting#5
|
# os.environ["OPENAI_API_KEY"] = str(temp_key) # this passes linting#5
|
||||||
|
|
||||||
|
def logger_fn(model_details):
|
||||||
|
print(model_details)
|
||||||
|
os.environ['OPENAI_API_KEY'] = "1LntUh4fmg5z6iEW7UPPRhGdBDNuJx5y"
|
||||||
|
messages = [{"role":"user","content":"Request boss to grant me 1 day leave"}]
|
||||||
|
litellm.api_base = "https://api.deepinfra.com/v1/"
|
||||||
|
response = litellm.completion(model="meta-llama/Llama-2-70b-chat-hf", messages=messages, custom_llm_provider="openai", logger_fn=logger_fn)
|
Loading…
Add table
Add a link
Reference in a new issue