litellm/README.md

212 lines
9.0 KiB
Markdown
Raw Normal View History

2023-08-24 22:51:02 +08:00
<h1 align="center">
2023-08-24 23:32:50 +08:00
🚅 LiteLLM
</h1>
<p align="center">
<p align="center">Call all LLM APIs using the OpenAI format [Anthropic, Huggingface, Cohere, TogetherAI, Azure, OpenAI, etc.]
<br>
<br>
2023-10-18 04:33:17 +08:00
<a href="https://calendly.com/d/4mp-gd3-k5k/berriai-1-1-onboarding-litellm-hosted-version?month=2023-10">Schedule Demo</a>
·
<a href="https://github.com/BerriAI/litellm/issues/new?assignees=&labels=enhancement&projects=&template=feature_request.yml&title=%5BFeature%5D%3A+">Feature Request</a>
2023-08-24 23:32:50 +08:00
</p>
2023-08-24 22:55:02 +08:00
<h4 align="center">
2023-08-24 23:32:50 +08:00
<a href="https://pypi.org/project/litellm/" target="_blank">
2023-08-24 22:55:02 +08:00
<img src="https://img.shields.io/pypi/v/litellm.svg" alt="PyPI Version">
</a>
2023-08-24 23:32:50 +08:00
<a href="https://dl.circleci.com/status-badge/redirect/gh/BerriAI/litellm/tree/main" target="_blank">
2023-08-24 22:55:02 +08:00
<img src="https://dl.circleci.com/status-badge/img/gh/BerriAI/litellm/tree/main.svg?style=svg" alt="CircleCI">
</a>
2023-08-27 05:04:51 +08:00
<a href="https://www.ycombinator.com/companies/berriai">
<img src="https://img.shields.io/badge/Y%20Combinator-W23-orange?style=flat-square" alt="Y Combinator W23">
</a>
2023-10-18 04:57:41 +08:00
<a href="https://wa.link/huol9n">
<img src="https://img.shields.io/static/v1?label=Chat%20on&message=WhatsApp&color=success&logo=WhatsApp&style=flat-square" alt="Whatsapp">
</a>
<a href="https://discord.gg/wuPM9dRgDw">
<img src="https://img.shields.io/static/v1?label=Chat%20on&message=Discord&color=blue&logo=Discord&style=flat-square" alt="Discord">
</a>
2023-08-24 22:55:02 +08:00
</h4>
2023-09-27 09:17:28 +08:00
<table align="center" style="border:0; padding-top: 20px; border-collapse: separate; border-spacing: 10px;">
<tr>
<td style="border:none;"><a href="https://docs.litellm.ai/docs/" target="_blank">Docs</a></td>
<td style="border:none;"><a href="https://docs.litellm.ai/docs/providers" target="_blank">100+ Supported Models</a></td>
2023-10-05 00:49:01 +08:00
<td style="border:none;"><a href="https://www.loom.com/share/bd4e9029a1da4fd4a3e40e41c87ebb3a?sid=d6c82c80-1e57-4519-a9df-0c5cc8c4c40d" target="_blank" rel="noopener noreferrer">Demo Video</a></td>
2023-10-04 13:03:38 +08:00
2023-09-27 09:17:28 +08:00
</tr>
</table>
2023-09-27 23:03:39 +08:00
2023-08-24 23:22:12 +08:00
LiteLLM manages
2023-08-24 23:29:06 +08:00
- Translating inputs to the provider's completion and embedding endpoints
2023-09-09 06:03:22 +08:00
- Guarantees [consistent output](https://docs.litellm.ai/docs/completion/output), text responses will always be available at `['choices'][0]['message']['content']`
- Exception mapping - common exceptions across providers are mapped to the OpenAI exception types.
2023-09-03 08:42:59 +08:00
2023-10-17 06:34:18 +08:00
**10/05/2023:** LiteLLM is adopting Semantic Versioning for all commits. [Learn more](https://github.com/BerriAI/litellm/issues/532)
**10/16/2023:** **Self-hosted OpenAI-proxy server** [Learn more](https://docs.litellm.ai/docs/proxy_server#deploy-proxy)
2023-10-06 02:34:17 +08:00
2023-08-25 03:31:39 +08:00
# Usage
2023-08-24 23:43:18 +08:00
2023-10-15 01:32:29 +08:00
<a target="_blank" href="https://colab.research.google.com/github/BerriAI/litellm/blob/main/cookbook/liteLLM_Getting_Started.ipynb">
2023-08-24 23:19:57 +08:00
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
</a>
2023-09-27 23:04:37 +08:00
2023-07-31 22:58:10 +08:00
```
pip install litellm
```
2023-07-27 08:44:43 +08:00
```python
2023-07-27 08:46:23 +08:00
from litellm import completion
2023-08-30 01:39:33 +08:00
import os
2023-09-20 08:10:55 +08:00
## set ENV variables
2023-09-30 02:20:38 +08:00
os.environ["OPENAI_API_KEY"] = "your-openai-key"
os.environ["COHERE_API_KEY"] = "your-cohere-key"
2023-07-27 08:46:23 +08:00
2023-07-27 08:44:34 +08:00
messages = [{ "content": "Hello, how are you?","role": "user"}]
2023-07-27 08:46:23 +08:00
2023-07-27 08:44:34 +08:00
# openai call
response = completion(model="gpt-3.5-turbo", messages=messages)
# cohere call
2023-08-25 00:42:58 +08:00
response = completion(model="command-nightly", messages=messages)
2023-09-20 08:04:54 +08:00
print(response)
2023-07-27 08:44:34 +08:00
```
2023-08-01 23:18:44 +08:00
2023-10-01 00:48:45 +08:00
## Streaming ([Docs](https://docs.litellm.ai/docs/completion/stream))
2023-08-06 06:10:49 +08:00
liteLLM supports streaming the model response back, pass `stream=True` to get a streaming iterator in response.
2023-08-16 00:49:44 +08:00
Streaming is supported for OpenAI, Azure, Anthropic, Huggingface models
2023-08-06 06:12:34 +08:00
```python
2023-08-06 06:10:49 +08:00
response = completion(model="gpt-3.5-turbo", messages=messages, stream=True)
for chunk in response:
print(chunk['choices'][0]['delta'])
2023-08-09 07:22:18 +08:00
2023-08-09 07:19:34 +08:00
# claude 2
2023-08-09 07:23:59 +08:00
result = completion('claude-2', messages, stream=True)
2023-08-09 07:19:34 +08:00
for chunk in result:
print(chunk['choices'][0]['delta'])
2023-08-06 06:10:49 +08:00
```
2023-09-27 05:57:30 +08:00
2023-10-01 00:48:45 +08:00
## OpenAI Proxy Server ([Docs](https://docs.litellm.ai/docs/proxy_server))
2023-10-18 04:40:20 +08:00
Create an OpenAI API compatible server to call any non-openai model (e.g. Huggingface, TogetherAI, Ollama, etc.)
2023-09-27 05:57:30 +08:00
This works for async + streaming as well.
```python
litellm --model <model_name>
2023-10-17 06:35:38 +08:00
#INFO: litellm proxy running on http://0.0.0.0:8000
```
2023-10-18 04:54:41 +08:00
Running your model locally or on a custom endpoint ? Set the `--api-base` parameter [see how](https://docs.litellm.ai/docs/proxy_server)
2023-10-17 06:35:38 +08:00
### Self-host server ([Docs](https://docs.litellm.ai/docs/proxy_server#deploy-proxy))
2023-10-17 06:34:18 +08:00
2023-10-17 06:35:38 +08:00
1. Clone the repo
2023-10-17 06:34:18 +08:00
```shell
git clone https://github.com/BerriAI/litellm.git
```
2. Modify `template_secrets.toml`
```shell
[keys]
OPENAI_API_KEY="sk-..."
[general]
default_model = "gpt-3.5-turbo"
```
3. Deploy
```shell
docker build -t litellm . && docker run -p 8000:8000 litellm
```
2023-10-01 00:48:45 +08:00
## Supported Provider ([Docs](https://docs.litellm.ai/docs/providers))
2023-10-03 03:06:20 +08:00
| Provider | [Completion](https://docs.litellm.ai/docs/#basic-usage) | [Streaming](https://docs.litellm.ai/docs/completion/stream#streaming-responses) | [Async Completion](https://docs.litellm.ai/docs/completion/stream#async-completion) | [Async Streaming](https://docs.litellm.ai/docs/completion/stream#async-streaming) |
2023-10-01 00:48:45 +08:00
| ------------- | ------------- | ------------- | ------------- | ------------- |
| [openai](https://docs.litellm.ai/docs/providers/openai) | ✅ | ✅ | ✅ | ✅ |
| [cohere](https://docs.litellm.ai/docs/providers/cohere) | ✅ | ✅ | ✅ | ✅ |
| [anthropic](https://docs.litellm.ai/docs/providers/anthropic) | ✅ | ✅ | ✅ | ✅ |
| [replicate](https://docs.litellm.ai/docs/providers/replicate) | ✅ | ✅ | ✅ | ✅ |
| [huggingface](https://docs.litellm.ai/docs/providers/huggingface) | ✅ | ✅ | ✅ | ✅ |
| [together_ai](https://docs.litellm.ai/docs/providers/togetherai) | ✅ | ✅ | ✅ | ✅ |
| [openrouter](https://docs.litellm.ai/docs/providers/openrouter) | ✅ | ✅ | ✅ | ✅ |
| [vertex_ai](https://docs.litellm.ai/docs/providers/vertex) | ✅ | ✅ | ✅ | ✅ |
| [palm](https://docs.litellm.ai/docs/providers/palm) | ✅ | ✅ | ✅ | ✅ |
| [ai21](https://docs.litellm.ai/docs/providers/ai21) | ✅ | ✅ | ✅ | ✅ |
| [baseten](https://docs.litellm.ai/docs/providers/baseten) | ✅ | ✅ | ✅ | ✅ |
| [azure](https://docs.litellm.ai/docs/providers/azure) | ✅ | ✅ | ✅ | ✅ |
| [sagemaker](https://docs.litellm.ai/docs/providers/aws_sagemaker) | ✅ | ✅ | ✅ | ✅ |
| [bedrock](https://docs.litellm.ai/docs/providers/bedrock) | ✅ | ✅ | ✅ | ✅ |
| [vllm](https://docs.litellm.ai/docs/providers/vllm) | ✅ | ✅ | ✅ | ✅ |
| [nlp_cloud](https://docs.litellm.ai/docs/providers/nlp_cloud) | ✅ | ✅ | ✅ | ✅ |
| [aleph alpha](https://docs.litellm.ai/docs/providers/aleph_alpha) | ✅ | ✅ | ✅ | ✅ |
2023-10-03 03:04:03 +08:00
| [petals](https://docs.litellm.ai/docs/providers/petals) | ✅ | ✅ | ✅ | ✅ |
2023-10-01 00:48:45 +08:00
| [ollama](https://docs.litellm.ai/docs/providers/ollama) | ✅ | ✅ | ✅ | ✅ |
| [deepinfra](https://docs.litellm.ai/docs/providers/deepinfra) | ✅ | ✅ | ✅ | ✅ |
[**Read the Docs**](https://docs.litellm.ai/docs/)
2023-10-18 04:38:55 +08:00
## Logging Observability - Log LLM Input/Output ([Docs](https://docs.litellm.ai/docs/observability/callbacks))
LiteLLM exposes pre defined callbacks to send data to LLMonitor, Langfuse, Helicone, Promptlayer, Traceloop, Slack
2023-10-18 04:44:47 +08:00
```python
from litellm import completion
## set env variables for logging tools
os.environ["PROMPTLAYER_API_KEY"] = "your-promptlayer-key"
os.environ["LLMONITOR_APP_ID"] = "your-llmonitor-app-id"
2023-10-18 04:38:55 +08:00
2023-10-18 04:44:47 +08:00
os.environ["OPENAI_API_KEY"]
# set callbacks
litellm.success_callback = ["promptlayer", "llmonitor"] # log input/output to promptlayer, llmonitor, supabase
#openai call
response = completion(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hi 👋 - i'm openai"}])
```
2023-10-18 04:38:55 +08:00
## Contributing
To contribute: Clone the repo locally -> Make a change -> Submit a PR with the change.
Here's how to modify the repo locally:
Step 1: Clone the repo
```
git clone https://github.com/BerriAI/litellm.git
```
Step 2: Navigate into the project, and install dependencies:
```
cd litellm
poetry install
```
Step 3: Test your change:
```
cd litellm/tests # pwd: Documents/litellm/litellm/tests
pytest .
```
Step 4: Submit a PR with your changes! 🚀
2023-10-04 00:16:01 +08:00
- push your fork to your GitHub repo
- submit a PR from there
2023-08-28 23:59:34 +08:00
# Support / talk with founders
2023-08-24 23:43:18 +08:00
- [Schedule Demo 👋](https://calendly.com/d/4mp-gd3-k5k/berriai-1-1-onboarding-litellm-hosted-version)
2023-08-09 21:09:28 +08:00
- [Community Discord 💭](https://discord.gg/wuPM9dRgDw)
- Our numbers 📞 +1 (770) 8783-106 / +1 (412) 618-6238
- Our emails ✉️ ishaan@berri.ai / krrish@berri.ai
2023-07-29 00:11:27 +08:00
2023-08-28 23:59:34 +08:00
# Why did we build this
- **Need for simplicity**: Our code started to get extremely complicated managing & translating calls between Azure, OpenAI and Cohere.
2023-08-28 23:50:12 +08:00
2023-08-28 23:59:34 +08:00
# Contributors
2023-08-28 23:50:12 +08:00
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
2023-08-28 23:56:19 +08:00
<a href="https://github.com/BerriAI/litellm/graphs/contributors">
<img src="https://contrib.rocks/image?repo=BerriAI/litellm" />
</a>