diff --git a/docs/my-website/docs/tutorials/text_completion.md b/docs/my-website/docs/tutorials/text_completion.md new file mode 100644 index 000000000..1d210076e --- /dev/null +++ b/docs/my-website/docs/tutorials/text_completion.md @@ -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) +``` \ No newline at end of file diff --git a/docs/my-website/sidebars.js b/docs/my-website/sidebars.js index cfb24121a..05d80b0b1 100644 --- a/docs/my-website/sidebars.js +++ b/docs/my-website/sidebars.js @@ -43,6 +43,7 @@ const sidebars = { 'tutorials/TogetherAI_liteLLM', 'tutorials/fallbacks', 'tutorials/finetuned_chat_gpt', + 'tutorials/text_completion', 'tutorials/ab_test_llms', 'tutorials/litellm_Test_Multiple_Providers' ], diff --git a/litellm/main.py b/litellm/main.py index 81e22fa7b..f12725e61 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -1,7 +1,7 @@ import os, openai, sys from typing import Any from functools import partial -import dotenv, traceback, random, asyncio, time +import dotenv, traceback, random, asyncio, time, contextvars from copy import deepcopy import litellm from litellm import ( # type: ignore @@ -49,8 +49,12 @@ async def acompletion(*args, **kwargs): # Use a partial function to pass your keyword arguments 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 - return await loop.run_in_executor(None, func) + return await loop.run_in_executor(None, func_with_context) @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` def completion( model, - messages, # required params # Optional OpenAI params: see https://platform.openai.com/docs/api-reference/chat/create + messages=[], functions=[], function_call="", # optional params temperature=1, @@ -212,9 +216,9 @@ def completion( # note: if a user sets a custom base - we should ensure this works # allow for the setting of dynamic and stateful api-bases 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 if litellm.organization: openai.organization = litellm.organization @@ -861,6 +865,13 @@ def embedding( 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 ################ ## Set verbose to true -> ```litellm.set_verbose = True``` diff --git a/litellm/tests/test_completion.py b/litellm/tests/test_completion.py index 1bd36790b..e59448450 100644 --- a/litellm/tests/test_completion.py +++ b/litellm/tests/test_completion.py @@ -10,7 +10,7 @@ sys.path.insert( ) # Adds the parent directory to the system path import pytest import litellm -from litellm import embedding, completion +from litellm import embedding, completion, text_completion # from infisical import InfisicalClient @@ -142,6 +142,17 @@ def test_completion_openai(): except Exception as 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(): try: diff --git a/litellm/utils.py b/litellm/utils.py index 309d90396..eac0079cf 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -1444,13 +1444,12 @@ def get_secret(secret_name): if litellm.secret_manager_client != None: # TODO: check which secret manager is being used # currently only supports Infisical - secret = litellm.secret_manager_client.get_secret( - secret_name).secret_value - if secret != None: - return secret # if secret found in secret manager return it - else: - raise ValueError( - f"Secret '{secret_name}' not found in secret manager") + try: + secret = litellm.secret_manager_client.get_secret( + secret_name).secret_value + except: + secret = None + return secret elif litellm.api_key != None: # if users use litellm default key return litellm.api_key else: diff --git a/pyproject.toml b/pyproject.toml index c4de3c368..d787fa222 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "litellm" -version = "0.1.492" +version = "0.1.494" description = "Library to easily interface with LLM API providers" authors = ["BerriAI"] license = "MIT License"