From 8845430cdb64fd1376e749e3b98487302f308f07 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Mon, 4 Mar 2024 22:00:29 -0800 Subject: [PATCH] docs(anthropic.md): add proxy usage tutorial to anthropic docs --- docs/my-website/docs/providers/anthropic.md | 93 +++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/docs/my-website/docs/providers/anthropic.md b/docs/my-website/docs/providers/anthropic.md index aff3415d376..21140f29be0 100644 --- a/docs/my-website/docs/providers/anthropic.md +++ b/docs/my-website/docs/providers/anthropic.md @@ -1,3 +1,6 @@ +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + # Anthropic LiteLLM supports @@ -45,7 +48,97 @@ for chunk in response: print(chunk["choices"][0]["delta"]["content"]) # same as openai format ``` +## OpenAI Proxy Usage +Here's how to call Anthropic with the LiteLLM Proxy Server + +### 1. Save key in your environment + +```bash +export ANTHROPIC_API_KEY="your-api-key" +``` + +### 2. Start the proxy + +```bash +$ litellm --model claude-3-opus-20240229 + +# Server running on http://0.0.0.0:8000 +``` + +### 3. Test it + + + + + +```shell +curl --location 'http://0.0.0.0:8000/chat/completions' \ +--header 'Content-Type: application/json' \ +--data ' { + "model": "gpt-3.5-turbo", + "messages": [ + { + "role": "user", + "content": "what llm are you" + } + ] + } +' +``` + + + +```python +import openai +client = openai.OpenAI( + api_key="anything", + base_url="http://0.0.0.0:8000" +) + +# request sent to model set on litellm proxy, `litellm --model` +response = client.chat.completions.create(model="gpt-3.5-turbo", messages = [ + { + "role": "user", + "content": "this is a test request, write a short poem" + } +]) + +print(response) + +``` + + + +```python +from langchain.chat_models import ChatOpenAI +from langchain.prompts.chat import ( + ChatPromptTemplate, + HumanMessagePromptTemplate, + SystemMessagePromptTemplate, +) +from langchain.schema import HumanMessage, SystemMessage + +chat = ChatOpenAI( + openai_api_base="http://0.0.0.0:8000", # set openai_api_base to the LiteLLM Proxy + model = "gpt-3.5-turbo", + temperature=0.1 +) + +messages = [ + SystemMessage( + content="You are a helpful assistant that im using to make a test request to." + ), + HumanMessage( + content="test from litellm. tell me why it's amazing in 1 sentence" + ), +] +response = chat(messages) + +print(response) +``` + + ## Supported Models