update streaming docs to show it working for async completion calls

This commit is contained in:
Krrish Dholakia 2023-09-05 09:18:37 -07:00
parent 5870dabae1
commit dc130efac6
5 changed files with 29 additions and 7 deletions

View file

@ -34,9 +34,11 @@ print(response)
## Async Streaming
We've implemented an `__anext__()` function in the streaming object returned. This enables async iteration over the streaming object.
### Usage
Here's an example of using it with openai. But this
```
from litellm import acompletion
from litellm import completion
import asyncio
def logger_fn(model_call_object: dict):
@ -46,11 +48,10 @@ def logger_fn(model_call_object: dict):
user_message = "Hello, how are you?"
messages = [{"content": user_message, "role": "user"}]
# # test on ai21 completion call
async def ai21_async_completion_call():
async def completion_call():
try:
response = completion(
model="j2-ultra", messages=messages, stream=True, logger_fn=logger_fn
model="gpt-3.5-turbo", messages=messages, stream=True, logger_fn=logger_fn
)
print(f"response: {response}")
complete_response = ""
@ -67,5 +68,5 @@ async def ai21_async_completion_call():
print(f"error occurred: {traceback.format_exc()}")
pass
asyncio.run(ai21_async_completion_call())
asyncio.run(completion_call())
```

View file

@ -1,7 +1,7 @@
#### What this tests ####
# This tests streaming for the completion endpoint
import sys, os
import sys, os, asyncio
import traceback
import time
@ -102,6 +102,27 @@ def test_openai_chat_completion_call():
print(f"error occurred: {traceback.format_exc()}")
pass
async def completion_call():
try:
response = completion(
model="gpt-3.5-turbo", messages=messages, stream=True, logger_fn=logger_fn
)
print(f"response: {response}")
complete_response = ""
start_time = time.time()
# Change for loop to async for loop
async for chunk in response:
chunk_time = time.time()
print(f"time since initial request: {chunk_time - start_time:.5f}")
print(chunk["choices"][0]["delta"])
complete_response += chunk["choices"][0]["delta"]["content"]
if complete_response == "":
raise Exception("Empty response received")
except:
print(f"error occurred: {traceback.format_exc()}")
pass
asyncio.run(completion_call())
# # test on azure completion call
# try:
@ -191,7 +212,7 @@ def test_together_ai_completion_call_starcoder():
print(f"error occurred: {traceback.format_exc()}")
pass
# test on aleph alpha completion call
# test on aleph alpha completion call - commented out as it's expensive to run this on circle ci for every build
# def test_aleph_alpha_call():
# try:
# start_time = time.time()