Files
litellm/cookbook/liteLLM_OpenAI.ipynb
T

11 KiB
Vendored

🚅 liteLLM Demo

TLDR: Call 50+ LLM APIs using chatGPT Input/Output format

https://github.com/BerriAI/litellm

liteLLM is package to simplify calling OpenAI, Azure, Llama2, Cohere, Anthropic, Huggingface API Endpoints. LiteLLM manages

  • Translating inputs to the provider's completion() and embedding() endpoints
  • Guarantees consistent 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

Installation and setting Params

In [ ]:
!pip install litellm
In [1]:
from litellm import completion
import os

Set your API keys

  • liteLLM reads your .env, env variables or key manager for Auth

Set keys for the models you want to use below

In [4]:
# Only set keys for the LLMs you want to use
os.environ['OPENAI_API_KEY'] = "" #@param
os.environ["ANTHROPIC_API_KEY"] = "" #@param
os.environ["AZURE_API_BASE"] = "" #@param
os.environ["AZURE_API_VERSION"] = "" #@param
os.environ["AZURE_API_KEY"] = "" #@param
os.environ["REPLICATE_API_TOKEN"] = "" #@param
os.environ["COHERE_API_KEY"] = "" #@param
os.environ["HF_TOKEN"] = "" #@param
In [2]:
messages = [{ "content": "what's the weather in SF","role": "user"}]

Call chatGPT

In [3]:
completion(model="gpt-3.5-turbo", messages=messages)
Out [3]:
<OpenAIObject chat.completion id=chatcmpl-7vYWJYYUeFuhjCiOjI9JXK6gNmWk3 at 0x1067d42c0> JSON: {
  "id": "chatcmpl-7vYWJYYUeFuhjCiOjI9JXK6gNmWk3",
  "object": "chat.completion",
  "created": 1693951747,
  "model": "gpt-3.5-turbo-0613",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "I apologize, but as an AI language model, I do not have real-time data. However, you can easily find the current weather conditions in San Francisco, California by checking a trusted weather website or using a weather app on your smartphone."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 13,
    "completion_tokens": 48,
    "total_tokens": 61
  }
}

Call Claude-2

In [ ]:
completion(model="claude-2", messages=messages)
{'choices': [{'finish_reason': 'stop',
   'index': 0,
   'message': {'role': 'assistant',
    'content': " Unfortunately I do not have enough context to provide the current weather in San Francisco. To get the most accurate weather report, it's helpful if I know details like:\n\n- Exact location (city name, zip code, etc)\n- Time frame (current conditions, forecast for a certain day/week, etc)\n\nIf you can provide some more specifics about what weather information you need for San Francisco, I'd be happy to look that up for you!"}}],
 'created': 1691880836.974166,
 'model': 'claude-2',
 'usage': {'prompt_tokens': 18, 'completion_tokens': 95, 'total_tokens': 113}}

Call llama2 on replicate

In [ ]:
model = "replicate/llama-2-70b-chat:2c1608e18606fad2812020dc541930f2d0495ce32eee50074220b87300bc16e1"
completion(model=model, messages=messages)
{'choices': [{'finish_reason': 'stop',
   'index': 0,
   'message': {'role': 'assistant',
    'content': ' I\'m happy to help! However, I must point out that the question "what\'s the weather in SF" doesn\'t make sense as "SF" could refer to multiple locations (San Francisco, South Florida, San Fernando, etc.). Could you please provide more context or specify which location you\'re referring to? That way, I can give you an accurate answer.'}}],
 'created': 1691880930.9003325,
 'model': 'replicate/llama-2-70b-chat:2c1608e18606fad2812020dc541930f2d0495ce32eee50074220b87300bc16e1',
 'usage': {'prompt_tokens': 6, 'completion_tokens': 74, 'total_tokens': 80}}

Call Command-Nightly

In [7]:
completion(model="command-nightly", messages=messages)
Out [7]:
<ModelResponse at 0x11cb0c3b0> JSON: {
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": " The weather in San Francisco can be quite unpredictable and varies throughout the year. In general, the city",
        "role": "assistant",
        "logprobs": null
      }
    }
  ],
  "created": 1693951797.3149078,
  "model": "command-nightly",
  "usage": {
    "prompt_tokens": 6,
    "completion_tokens": 20,
    "total_tokens": 26
  }
}

Call Azure OpenAI

For azure openai calls ensure to add the azure/ prefix to model. If your deployment-id is chatgpt-test set model = azure/chatgpt-test

In [ ]:
completion(model="azure/chatgpt-test", messages=messages)