Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add a timeout to langchain callback handler #17296

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading