fix(utils.py): allow user to disable streaming logging

fixes event loop issue for litellm.disable_streaming_logging
This commit is contained in:
Krrish Dholakia 2024-03-25 14:28:46 -07:00
parent 16ade7e556
commit f153889738
2 changed files with 22 additions and 5 deletions

View file

@ -56,6 +56,7 @@ baseten_key: Optional[str] = None
aleph_alpha_key: Optional[str] = None aleph_alpha_key: Optional[str] = None
nlp_cloud_key: Optional[str] = None nlp_cloud_key: Optional[str] = None
use_client: bool = False use_client: bool = False
disable_streaming_logging: bool = False
### GUARDRAILS ### ### GUARDRAILS ###
llamaguard_model_name: Optional[str] = None llamaguard_model_name: Optional[str] = None
presidio_ad_hoc_recognizers: Optional[str] = None presidio_ad_hoc_recognizers: Optional[str] = None

View file

@ -9617,15 +9617,31 @@ class CustomStreamWrapper:
) )
def set_logging_event_loop(self, loop): def set_logging_event_loop(self, loop):
"""
import litellm, asyncio
loop = asyncio.get_event_loop() # 👈 gets the current event loop
response = litellm.completion(.., stream=True)
response.set_logging_event_loop(loop=loop) # 👈 enables async_success callbacks for sync logging
for chunk in response:
...
"""
self.logging_loop = loop self.logging_loop = loop
async def your_async_function(self):
# Your asynchronous code here
return "Your asynchronous code is running"
def run_success_logging_in_thread(self, processed_chunk): def run_success_logging_in_thread(self, processed_chunk):
# Create an event loop for the new thread if litellm.disable_streaming_logging == True:
"""
[NOT RECOMMENDED]
Set this via `litellm.disable_streaming_logging = True`.
Disables streaming logging.
"""
return
## ASYNC LOGGING ## ASYNC LOGGING
# Create an event loop for the new thread
if self.logging_loop is not None: if self.logging_loop is not None:
future = asyncio.run_coroutine_threadsafe( future = asyncio.run_coroutine_threadsafe(
self.logging_obj.async_success_handler(processed_chunk), self.logging_obj.async_success_handler(processed_chunk),