Skip to content

Commit

Permalink
fix: add a timeout to langchain callback handler (#17296)
Browse files Browse the repository at this point in the history
  • Loading branch information
masci authored Dec 17, 2024
1 parent 98fd497 commit d1ecfb7
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion llama-index-core/llama_index/core/langchain_helpers/streaming.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
from queue import Queue
from threading import Event
from typing import Any, Generator, List, Optional
Expand Down Expand Up @@ -35,10 +36,25 @@ def on_llm_error(
) -> None:
self._done.set()

def get_response_gen(self) -> Generator:
def get_response_gen(self, timeout: float = 120.0) -> Generator:
"""Get response generator with timeout.
Args:
timeout (float): Maximum time in seconds to wait for the complete response.
Defaults to 120 seconds.
"""
start_time = time.time()
while True:
if time.time() - start_time > timeout:
raise TimeoutError(
f"Response generation timed out after {timeout} seconds"
)

if not self._token_queue.empty():
token = self._token_queue.get_nowait()
yield token
elif self._done.is_set():
break
else:
# Small sleep to prevent CPU spinning
time.sleep(0.01)

0 comments on commit d1ecfb7

Please sign in to comment.