ChatLiteLLM
LiteLLM is a library that simplifies calling Anthropic, Azure, Huggingface, Replicate, etc.
This notebook covers how to get started with using Langchain + the LiteLLM I/O library.
Overviewโ
Integration detailsโ
Class | Package | Local | Serializable | JS support | Package downloads | Package latest |
---|---|---|---|---|---|---|
ChatLiteLLM | langchain-litellm | โ | โ | โ |
Model featuresโ
Tool calling | Structured output | JSON mode | Image input | Audio input | Video input | Token-level streaming | Native async | Token usage | Logprobs |
---|---|---|---|---|---|---|---|---|---|
โ | โ | โ | โ | โ | โ | โ | โ | โ | โ |
Setupโ
To access ChatLiteLLM models you'll need to install the langchain-litellm
package and create an OpenAI, Anthropic, Azure, Replicate, OpenRouter, Hugging Face, Together AI or Cohere account. Then you have to get an API key, and export it as an environment variable.
Credentialsโ
You have to choose the LLM provider you want and sign up with them to get their API key.
Example - Anthropicโ
Head to https://console.anthropic.com/ to sign up for Anthropic and generate an API key. Once you've done this set the ANTHROPIC_API_KEY environment variable.
Example - OpenAIโ
Head to https://platform.openai.com/api-keys to sign up for OpenAI and generate an API key. Once you've done this set the OPENAI_API_KEY environment variable.
## set ENV variables
import os
os.environ["OPENAI_API_KEY"] = "your-openai-key"
os.environ["ANTHROPIC_API_KEY"] = "your-anthropic-key"
Installationโ
The LangChain LiteLLM integration lives in the langchain-litellm
package:
%pip install -qU langchain-litellm
Instantiationโ
Now we can instantiate our model object and generate chat completions:
from langchain_litellm.chat_models import ChatLiteLLM
llm = ChatLiteLLM(model="gpt-3.5-turbo")
Invocationโ
response = await llm.ainvoke(
"Classify the text into neutral, negative or positive. Text: I think the food was okay. Sentiment:"
)
print(response)
content='Neutral' additional_kwargs={} response_metadata={'token_usage': Usage(completion_tokens=2, prompt_tokens=30, total_tokens=32, completion_tokens_details=CompletionTokensDetailsWrapper(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0, text_tokens=None), prompt_tokens_details=PromptTokensDetailsWrapper(audio_tokens=0, cached_tokens=0, text_tokens=None, image_tokens=None)), 'model': 'gpt-3.5-turbo', 'finish_reason': 'stop', 'model_name': 'gpt-3.5-turbo'} id='run-ab6a3b21-eae8-4c27-acb2-add65a38221a-0' usage_metadata={'input_tokens': 30, 'output_tokens': 2, 'total_tokens': 32}
ChatLiteLLM
also supports async and streaming functionality:โ
async for token in llm.astream("Hello, please explain how antibiotics work"):
print(token.text(), end="")
Antibiotics are medications that fight bacterial infections in the body. They work by targeting specific bacteria and either killing them or preventing their growth and reproduction.
There are several different mechanisms by which antibiotics work. Some antibiotics work by disrupting the cell walls of bacteria, causing them to burst and die. Others interfere with the protein synthesis of bacteria, preventing them from growing and reproducing. Some antibiotics target the DNA or RNA of bacteria, disrupting their ability to replicate.
It is important to note that antibiotics only work against bacterial infections and not viral infections. It is also crucial to take antibiotics as prescribed by a healthcare professional and to complete the full course of treatment, even if symptoms improve before the medication is finished. This helps to prevent antibiotic resistance, where bacteria become resistant to the effects of antibiotics.
API referenceโ
For detailed documentation of all ChatLiteLLM
features and configurations head to the API reference: https://github.com/Akshay-Dongare/langchain-litellm
Relatedโ
- Chat model conceptual guide
- Chat model how-to guides