Merge branch 'main' into dev/openai_api_base

This commit is contained in:
Ishaan Jaff 2023-08-28 08:41:46 -07:00 committed by GitHub
commit 69beeba025
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 75 additions and 14 deletions

View file

@ -0,0 +1,39 @@
# Using Text Completion Format - with Completion()
If your prefer interfacing with the OpenAI Text Completion format this tutorial covers how to use LiteLLM in this format
```python
response = openai.Completion.create(
model="text-davinci-003",
prompt='Write a tagline for a traditional bavarian tavern',
temperature=0,
max_tokens=100)
```
## Using LiteLLM in the Text Completion format
### With gpt-3.5-turbo
```python
from litellm import text_completion
response = text_completion(
model="gpt-3.5-turbo",
prompt='Write a tagline for a traditional bavarian tavern',
temperature=0,
max_tokens=100)
```
### With text-davinci-003
```python
response = text_completion(
model="text-davinci-003",
prompt='Write a tagline for a traditional bavarian tavern',
temperature=0,
max_tokens=100)
```
### With llama2
```python
response = text_completion(
model="togethercomputer/llama-2-70b-chat",
prompt='Write a tagline for a traditional bavarian tavern',
temperature=0,
max_tokens=100)
```

View file

@ -43,6 +43,7 @@ const sidebars = {
'tutorials/TogetherAI_liteLLM', 'tutorials/TogetherAI_liteLLM',
'tutorials/fallbacks', 'tutorials/fallbacks',
'tutorials/finetuned_chat_gpt', 'tutorials/finetuned_chat_gpt',
'tutorials/text_completion',
'tutorials/ab_test_llms', 'tutorials/ab_test_llms',
'tutorials/litellm_Test_Multiple_Providers' 'tutorials/litellm_Test_Multiple_Providers'
], ],

View file

@ -1,7 +1,7 @@
import os, openai, sys import os, openai, sys
from typing import Any from typing import Any
from functools import partial from functools import partial
import dotenv, traceback, random, asyncio, time import dotenv, traceback, random, asyncio, time, contextvars
from copy import deepcopy from copy import deepcopy
import litellm import litellm
from litellm import ( # type: ignore from litellm import ( # type: ignore
@ -49,8 +49,12 @@ async def acompletion(*args, **kwargs):
# Use a partial function to pass your keyword arguments # Use a partial function to pass your keyword arguments
func = partial(completion, *args, **kwargs) func = partial(completion, *args, **kwargs)
# Add the context to the function
ctx = contextvars.copy_context()
func_with_context = partial(ctx.run, func)
# Call the synchronous function using run_in_executor # Call the synchronous function using run_in_executor
return await loop.run_in_executor(None, func) return await loop.run_in_executor(None, func_with_context)
@client @client
@ -60,8 +64,8 @@ async def acompletion(*args, **kwargs):
) ## set timeouts, in case calls hang (e.g. Azure) - default is 600s, override with `force_timeout` ) ## set timeouts, in case calls hang (e.g. Azure) - default is 600s, override with `force_timeout`
def completion( def completion(
model, model,
messages, # required params
# Optional OpenAI params: see https://platform.openai.com/docs/api-reference/chat/create # Optional OpenAI params: see https://platform.openai.com/docs/api-reference/chat/create
messages=[],
functions=[], functions=[],
function_call="", # optional params function_call="", # optional params
temperature=1, temperature=1,
@ -212,9 +216,9 @@ def completion(
# note: if a user sets a custom base - we should ensure this works # note: if a user sets a custom base - we should ensure this works
# allow for the setting of dynamic and stateful api-bases # allow for the setting of dynamic and stateful api-bases
api_base = ( api_base = (
custom_api_base or litellm.api_base or get_secret("OPENAI_API_BASE") custom_api_base or litellm.api_base or get_secret("OPENAI_API_BASE") or "https://api.openai.com/v1"
) )
openai.api_base = api_base or "https://api.openai.com/v1" openai.api_base = api_base
openai.api_version = None openai.api_version = None
if litellm.organization: if litellm.organization:
openai.organization = litellm.organization openai.organization = litellm.organization
@ -861,6 +865,13 @@ def embedding(
custom_llm_provider="azure" if azure == True else None, custom_llm_provider="azure" if azure == True else None,
) )
###### Text Completion ################
def text_completion(*args, **kwargs):
if 'prompt' in kwargs:
messages = [{'role': 'system', 'content': kwargs['prompt']}]
kwargs['messages'] = messages
kwargs.pop('prompt')
return completion(*args, **kwargs)
####### HELPER FUNCTIONS ################ ####### HELPER FUNCTIONS ################
## Set verbose to true -> ```litellm.set_verbose = True``` ## Set verbose to true -> ```litellm.set_verbose = True```

View file

@ -10,7 +10,7 @@ sys.path.insert(
) # Adds the parent directory to the system path ) # Adds the parent directory to the system path
import pytest import pytest
import litellm import litellm
from litellm import embedding, completion from litellm import embedding, completion, text_completion
# from infisical import InfisicalClient # from infisical import InfisicalClient
@ -142,6 +142,17 @@ def test_completion_openai():
except Exception as e: except Exception as e:
pytest.fail(f"Error occurred: {e}") pytest.fail(f"Error occurred: {e}")
def test_completion_openai_prompt():
try:
response = text_completion(model="gpt-3.5-turbo", prompt="What's the weather in SF?")
response_str = response["choices"][0]["message"]["content"]
response_str_2 = response.choices[0].message.content
print(response)
assert response_str == response_str_2
assert type(response_str) == str
assert len(response_str) > 1
except Exception as e:
pytest.fail(f"Error occurred: {e}")
def test_completion_text_openai(): def test_completion_text_openai():
try: try:

View file

@ -1444,13 +1444,12 @@ def get_secret(secret_name):
if litellm.secret_manager_client != None: if litellm.secret_manager_client != None:
# TODO: check which secret manager is being used # TODO: check which secret manager is being used
# currently only supports Infisical # currently only supports Infisical
try:
secret = litellm.secret_manager_client.get_secret( secret = litellm.secret_manager_client.get_secret(
secret_name).secret_value secret_name).secret_value
if secret != None: except:
return secret # if secret found in secret manager return it secret = None
else: return secret
raise ValueError(
f"Secret '{secret_name}' not found in secret manager")
elif litellm.api_key != None: # if users use litellm default key elif litellm.api_key != None: # if users use litellm default key
return litellm.api_key return litellm.api_key
else: else:

View file

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "litellm" name = "litellm"
version = "0.1.492" version = "0.1.494"
description = "Library to easily interface with LLM API providers" description = "Library to easily interface with LLM API providers"
authors = ["BerriAI"] authors = ["BerriAI"]
license = "MIT License" license = "MIT License"