-
Notifications
You must be signed in to change notification settings - Fork 65
Stream LLM logs and stats updates via WebSocket for RemoteConversation #1159
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
39ba8dd
Stream LLM completion logs via WebSocket events for RemoteConversation
openhands-agent 5d2d65d
Add stats streaming to RemoteConversation via callback mechanism
openhands-agent b3576a5
Include usage_id in LLM log events to preserve per-usage_id folders (…
enyst 78565bc
Fix critical issues in LLM telemetry streaming
openhands-agent fa7abf4
Merge main branch and resolve conflicts in telemetry.py
openhands-agent 2750cca
Merge branch 'main' into openhands/stream-llm-completion-logs
xingyaoww 9a352bc
Merge commit '49c42ee7be250cd40204010fda003b4f57d39fff' into openhand…
xingyaoww d6c53d3
Refactor: Simplify event streaming implementation
openhands-agent ba12154
Fix event persistence for LLM completion logs and stats updates
openhands-agent a45ab33
Merge branch 'main' into openhands/stream-llm-completion-logs
xingyaoww ef703b1
Remove _log_completion_folders from RemoteConversation
openhands-agent ac1401c
Merge branch 'main' into openhands/stream-llm-completion-logs
hieptl 823aea5
fix: set up callbacks
hieptl 298982c
refactor: _emit_event_from_thread
hieptl 0269846
Merge branch 'main' into openhands/stream-llm-completion-logs
xingyaoww decfd82
Rename set_log_callback to set_log_completions_callback
openhands-agent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import asyncio | ||
| import json | ||
| import os | ||
| import threading | ||
| import uuid | ||
| from collections.abc import Mapping | ||
|
|
@@ -26,6 +27,7 @@ | |
| FULL_STATE_KEY, | ||
| ConversationStateUpdateEvent, | ||
| ) | ||
| from openhands.sdk.event.llm_completion_log import LLMCompletionLogEvent | ||
| from openhands.sdk.llm import LLM, Message, TextContent | ||
| from openhands.sdk.logger import DEBUG, get_logger | ||
| from openhands.sdk.observability.laminar import observe | ||
|
|
@@ -423,6 +425,7 @@ class RemoteConversation(BaseConversation): | |
| max_iteration_per_run: int | ||
| workspace: RemoteWorkspace | ||
| _client: httpx.Client | ||
| _log_completion_folders: dict[str, str] | ||
|
|
||
| def __init__( | ||
| self, | ||
|
|
@@ -461,6 +464,13 @@ def __init__( | |
| self.workspace = workspace | ||
| self._client = workspace.client | ||
|
|
||
| # Build map of log directories for all LLMs in the agent | ||
| self._log_completion_folders = {} | ||
| for llm in agent.get_all_llms(): | ||
| if llm.log_completions: | ||
| # Map usage_id to log folder | ||
| self._log_completion_folders[llm.usage_id] = llm.log_completions_folder | ||
|
||
|
|
||
| if conversation_id is None: | ||
| payload = { | ||
| "agent": agent.model_dump( | ||
|
|
@@ -502,6 +512,11 @@ def __init__( | |
| state_update_callback = self._state.create_state_update_callback() | ||
| self._callbacks.append(state_update_callback) | ||
|
|
||
| # Add callback to handle LLM completion logs | ||
| if self._log_completion_folders: | ||
| llm_log_callback = self._create_llm_completion_log_callback() | ||
| self._callbacks.append(llm_log_callback) | ||
|
|
||
| # Handle visualization configuration | ||
| if isinstance(visualizer, ConversationVisualizerBase): | ||
| # Use custom visualizer instance | ||
|
|
@@ -541,6 +556,32 @@ def __init__( | |
|
|
||
| self._start_observability_span(str(self._id)) | ||
|
|
||
| def _create_llm_completion_log_callback(self) -> ConversationCallbackType: | ||
| """Create a callback that writes LLM completion logs to client filesystem.""" | ||
|
|
||
| def callback(event: Event) -> None: | ||
| if not isinstance(event, LLMCompletionLogEvent): | ||
| return | ||
|
|
||
| # Get the log directory for this LLM's usage_id | ||
| log_dir = self._log_completion_folders.get(event.usage_id) | ||
xingyaoww marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if not log_dir: | ||
| logger.debug( | ||
| f"No log directory configured for usage_id={event.usage_id}" | ||
| ) | ||
| return | ||
|
|
||
| try: | ||
| os.makedirs(log_dir, exist_ok=True) | ||
| log_path = os.path.join(log_dir, event.filename) | ||
| with open(log_path, "w") as f: | ||
| f.write(event.log_data) | ||
| logger.debug(f"Wrote LLM completion log to {log_path}") | ||
| except Exception as e: | ||
| logger.warning(f"Failed to write LLM completion log: {e}") | ||
|
|
||
| return callback | ||
|
|
||
| @property | ||
| def id(self) -> ConversationID: | ||
| return self._id | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| """Event for streaming LLM completion logs from remote agents to clients.""" | ||
|
|
||
| from pydantic import Field | ||
|
|
||
| from openhands.sdk.event.base import Event | ||
| from openhands.sdk.event.types import SourceType | ||
|
|
||
|
|
||
| class LLMCompletionLogEvent(Event): | ||
| """Event containing LLM completion log data. | ||
|
|
||
| When an LLM is configured with log_completions=True in a remote conversation, | ||
| this event streams the completion log data back to the client through WebSocket | ||
| instead of writing it to a file inside the Docker container. | ||
| """ | ||
|
|
||
| source: SourceType = "environment" | ||
| filename: str = Field( | ||
| ..., | ||
| description="The intended filename for this log (relative to log directory)", | ||
| ) | ||
| log_data: str = Field( | ||
| ..., | ||
| description="The JSON-encoded log data to be written to the file", | ||
| ) | ||
| model_name: str = Field( | ||
| default="unknown", | ||
| description="The model name for context", | ||
| ) | ||
| usage_id: str = Field( | ||
| default="default", | ||
| description="The LLM usage_id that produced this log", | ||
| ) | ||
|
|
||
| def __str__(self) -> str: | ||
| return ( | ||
| f"LLMCompletionLog(usage_id={self.usage_id}, model={self.model_name}, " | ||
| f"file={self.filename})" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.