| 799 | self.timeout_seconds = timeout_seconds |
| 800 | |
| 801 | def call(self, prompt: str) -> str: |
| 802 | result: Dict[str, Any] = {} |
| 803 | error: Dict[str, Any] = {} |
| 804 | |
| 805 | def target(): |
| 806 | try: |
| 807 | result["value"] = self.llm_fn(prompt) |
| 808 | except Exception as exc: |
| 809 | error["value"] = exc |
| 810 | |
| 811 | thread = threading.Thread(target=target, daemon=True) |
| 812 | thread.start() |
| 813 | thread.join(timeout=self.timeout_seconds) |
| 814 | |
| 815 | if thread.is_alive(): |
| 816 | raise LLMTimeoutError( |
| 817 | f"LLM call exceeded {self.timeout_seconds}s timeout" |
| 818 | ) |
| 819 | if "value" in error: |
| 820 | raise error["value"] |
| 821 | |
| 822 | return result.get("value", "") |
| 823 | |
| 824 | |
| 825 | # ============================================================================= |