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..3eb95592a --- /dev/null +++ b/docs/my-website/docs/tutorials/text_completion.md @@ -0,0 +1,38 @@ +# 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 +response = openai.Completion.create( + 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 = openai.Completion.create( + model="text-davinci-003", + prompt='Write a tagline for a traditional bavarian tavern', + temperature=0, + max_tokens=100) +``` + +### With llama2 +```python +response = openai.Completion.create( + 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/litellm/main.py b/litellm/main.py index a327a7420..a161e663b 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -64,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, @@ -91,6 +91,7 @@ def completion( custom_llm_provider=None, custom_api_base=None, litellm_call_id=None, + prompt="", # allow completion to be used as textCompletion or as ChatCompletion # model specific optional params # used by text-bison only top_k=40, @@ -101,6 +102,8 @@ def completion( try: if fallbacks != []: return completion_with_fallbacks(**args) + if messages == [] and prompt!="": + messages = [{"role": "user", "content": prompt}] if litellm.model_alias_map and model in litellm.model_alias_map: args["model_alias_map"] = litellm.model_alias_map model = litellm.model_alias_map[model] # update the model to the actual value if an alias has been passed in diff --git a/litellm/tests/test_completion.py b/litellm/tests/test_completion.py index 1bd36790b..6aa7612d6 100644 --- a/litellm/tests/test_completion.py +++ b/litellm/tests/test_completion.py @@ -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 = 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: