mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 19:24:27 +00:00
feat(proxy_server.py): support custom llm handler on proxy
This commit is contained in:
parent
a2d07cfe64
commit
bd7af04a72
4 changed files with 140 additions and 2 deletions
|
@ -35,6 +35,101 @@ resp = completion(
|
||||||
assert resp.choices[0].message.content == "Hi!"
|
assert resp.choices[0].message.content == "Hi!"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## OpenAI Proxy Usage
|
||||||
|
|
||||||
|
1. Setup your `custom_handler.py` file
|
||||||
|
|
||||||
|
```python
|
||||||
|
import litellm
|
||||||
|
from litellm import CustomLLM, completion, get_llm_provider
|
||||||
|
|
||||||
|
|
||||||
|
class MyCustomLLM(CustomLLM):
|
||||||
|
def completion(self, *args, **kwargs) -> litellm.ModelResponse:
|
||||||
|
return litellm.completion(
|
||||||
|
model="gpt-3.5-turbo",
|
||||||
|
messages=[{"role": "user", "content": "Hello world"}],
|
||||||
|
mock_response="Hi!",
|
||||||
|
) # type: ignore
|
||||||
|
|
||||||
|
async def acompletion(self, *args, **kwargs) -> litellm.ModelResponse:
|
||||||
|
return litellm.completion(
|
||||||
|
model="gpt-3.5-turbo",
|
||||||
|
messages=[{"role": "user", "content": "Hello world"}],
|
||||||
|
mock_response="Hi!",
|
||||||
|
) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
my_custom_llm = MyCustomLLM()
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Add to `config.yaml`
|
||||||
|
|
||||||
|
In the config below, we pass
|
||||||
|
|
||||||
|
python_filename: `custom_handler.py`
|
||||||
|
custom_handler_instance_name: `my_custom_llm`. This is defined in Step 1
|
||||||
|
|
||||||
|
custom_handler: `custom_handler.my_custom_llm`
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
model_list:
|
||||||
|
- model_name: "test-model"
|
||||||
|
litellm_params:
|
||||||
|
model: "openai/text-embedding-ada-002"
|
||||||
|
- model_name: "my-custom-model"
|
||||||
|
litellm_params:
|
||||||
|
model: "my-custom-llm/my-model"
|
||||||
|
|
||||||
|
litellm_settings:
|
||||||
|
custom_provider_map:
|
||||||
|
- {"provider": "my-custom-llm", "custom_handler": custom_handler.my_custom_llm}
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
litellm --config /path/to/config.yaml
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Test it!
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -X POST 'http://0.0.0.0:4000/chat/completions' \
|
||||||
|
-H 'Content-Type: application/json' \
|
||||||
|
-H 'Authorization: Bearer sk-1234' \
|
||||||
|
-d '{
|
||||||
|
"model": "my-custom-model",
|
||||||
|
"messages": [{"role": "user", "content": "Say \"this is a test\" in JSON!"}],
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected Response
|
||||||
|
|
||||||
|
```
|
||||||
|
{
|
||||||
|
"id": "chatcmpl-06f1b9cd-08bc-43f7-9814-a69173921216",
|
||||||
|
"choices": [
|
||||||
|
{
|
||||||
|
"finish_reason": "stop",
|
||||||
|
"index": 0,
|
||||||
|
"message": {
|
||||||
|
"content": "Hi!",
|
||||||
|
"role": "assistant",
|
||||||
|
"tool_calls": null,
|
||||||
|
"function_call": null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"created": 1721955063,
|
||||||
|
"model": "gpt-3.5-turbo",
|
||||||
|
"object": "chat.completion",
|
||||||
|
"system_fingerprint": null,
|
||||||
|
"usage": {
|
||||||
|
"prompt_tokens": 10,
|
||||||
|
"completion_tokens": 20,
|
||||||
|
"total_tokens": 30
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Custom Handler Spec
|
## Custom Handler Spec
|
||||||
|
|
||||||
|
|
|
@ -2,3 +2,10 @@ model_list:
|
||||||
- model_name: "test-model"
|
- model_name: "test-model"
|
||||||
litellm_params:
|
litellm_params:
|
||||||
model: "openai/text-embedding-ada-002"
|
model: "openai/text-embedding-ada-002"
|
||||||
|
- model_name: "my-custom-model"
|
||||||
|
litellm_params:
|
||||||
|
model: "my-custom-llm/my-model"
|
||||||
|
|
||||||
|
litellm_settings:
|
||||||
|
custom_provider_map:
|
||||||
|
- {"provider": "my-custom-llm", "custom_handler": custom_handler.my_custom_llm}
|
21
litellm/proxy/custom_handler.py
Normal file
21
litellm/proxy/custom_handler.py
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
import litellm
|
||||||
|
from litellm import CustomLLM, completion, get_llm_provider
|
||||||
|
|
||||||
|
|
||||||
|
class MyCustomLLM(CustomLLM):
|
||||||
|
def completion(self, *args, **kwargs) -> litellm.ModelResponse:
|
||||||
|
return litellm.completion(
|
||||||
|
model="gpt-3.5-turbo",
|
||||||
|
messages=[{"role": "user", "content": "Hello world"}],
|
||||||
|
mock_response="Hi!",
|
||||||
|
) # type: ignore
|
||||||
|
|
||||||
|
async def acompletion(self, *args, **kwargs) -> litellm.ModelResponse:
|
||||||
|
return litellm.completion(
|
||||||
|
model="gpt-3.5-turbo",
|
||||||
|
messages=[{"role": "user", "content": "Hello world"}],
|
||||||
|
mock_response="Hi!",
|
||||||
|
) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
my_custom_llm = MyCustomLLM()
|
|
@ -1507,6 +1507,21 @@ class ProxyConfig:
|
||||||
verbose_proxy_logger.debug(
|
verbose_proxy_logger.debug(
|
||||||
f"litellm.post_call_rules: {litellm.post_call_rules}"
|
f"litellm.post_call_rules: {litellm.post_call_rules}"
|
||||||
)
|
)
|
||||||
|
elif key == "custom_provider_map":
|
||||||
|
from litellm.utils import custom_llm_setup
|
||||||
|
|
||||||
|
litellm.custom_provider_map = [
|
||||||
|
{
|
||||||
|
"provider": item["provider"],
|
||||||
|
"custom_handler": get_instance_fn(
|
||||||
|
value=item["custom_handler"],
|
||||||
|
config_file_path=config_file_path,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
for item in value
|
||||||
|
]
|
||||||
|
|
||||||
|
custom_llm_setup()
|
||||||
elif key == "success_callback":
|
elif key == "success_callback":
|
||||||
litellm.success_callback = []
|
litellm.success_callback = []
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue