-
Notifications
You must be signed in to change notification settings - Fork 479
feat(sdk): add ask_oracle tool #3673
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 all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
9c2b227
feat(sdk): add ask oracle tool
enyst 3daa1ce
Revise Oracle description for clarity and intent
enyst ed274a8
refactor(sdk): simplify oracle consultation
openhands-agent ce9712f
Merge main into ask oracle tool
enyst 41f70f7
Fix ask oracle profile precedence
openhands-agent 994c508
Clarify SDK built-in tool grouping
enyst 27da708
Merge main into ask oracle tool
enyst 759cf96
Fix pyright warnings after main merge
enyst 8737df2
Fix ask oracle example numbering
enyst 2faff61
Merge branch 'main' into feat/ask-oracle-tool
enyst 4a5d3cd
Merge branch 'main' into feat/ask-oracle-tool
enyst 32f8ce3
refactor(tools): move ask_oracle to openhands-tools, resolve 'oracle'…
enyst 0b6c173
test(ask_oracle): make example + .pr evidence end-to-end
enyst 1c7012d
Merge remote-tracking branch 'upstream/main' into feat/ask-oracle-tool
enyst b977f4d
chore(ask_oracle): adapt to main after merge
enyst f9d6c59
fix(ask_oracle): renumber example to 58 to avoid collision with main
openhands-agent 081096c
chore: merge main into feat/ask-oracle-tool
openhands-agent 0aa2097
Merge branch 'main' into feat/ask-oracle-tool
enyst a66c2a5
fix(ask_oracle): route Oracle calls through conversation LLM registry
openhands-agent acd9d9a
chore: Remove PR-only artifacts [automated]
83a5862
fix(tools): use canonical Oracle observation text
enyst 5dfdf7b
fix(ci): allowlist Oracle tool metadata schema
enyst c4fec19
fix(examples): isolate Oracle profile storage
enyst 194c8ce
chore: remove unrelated security test changes
enyst 6753afc
Merge branch 'main' into feat/ask-oracle-tool
enyst 78092eb
test: refresh ask oracle PR evidence
enyst fbde5e3
Merge branch 'main' into feat/ask-oracle-tool
enyst 5a094a6
chore: remove temporary PR evidence
enyst 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 |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """Consult the Oracle end-to-end with the ask_oracle tool. | ||
|
|
||
| The Oracle is a saved LLM profile resolved by convention under the name | ||
| ``oracle``. This example wires two profiles — the agent's primary model and a | ||
| separate ``oracle`` model — adds ``Tool(name="ask_oracle")`` to the agent, then | ||
| drives a normal conversation: the agent decides to call ``ask_oracle``, the tool | ||
| consults the ``oracle`` profile, and the agent uses the Oracle's answer to reply. | ||
|
|
||
| Usage: | ||
| LLM_API_KEY=... LLM_BASE_URL=https://llm-proxy.app.all-hands.dev \ | ||
| uv run python examples/01_standalone_sdk/58_ask_oracle_tool/main.py | ||
|
|
||
| Note: | ||
| The example saves the ``oracle`` profile in a temporary directory so it | ||
| does not modify the user's default profile store. | ||
| """ | ||
|
|
||
| import os | ||
| import tempfile | ||
|
|
||
| from pydantic import SecretStr | ||
|
|
||
| from openhands.sdk import LLM, Agent, LocalConversation, Tool | ||
| from openhands.sdk.llm.llm_profile_store import LLMProfileStore | ||
| from openhands.tools.ask_oracle import ORACLE_PROFILE_NAME | ||
|
|
||
|
|
||
| DEFAULT_BASE_URL = "https://llm-proxy.app.all-hands.dev" | ||
| # The agent's primary model (follows the standard LLM_MODEL env like other | ||
| # examples). The Oracle defaults to the same model; override ASK_ORACLE_MODEL to | ||
| # point the "oracle" profile at a different/stronger model. | ||
| PRIMARY_MODEL = os.getenv("ASK_ORACLE_PRIMARY_MODEL") or os.getenv( | ||
| "LLM_MODEL", "openai/gpt-5.5" | ||
| ) | ||
| ORACLE_MODEL = os.getenv("ASK_ORACLE_MODEL", PRIMARY_MODEL) | ||
|
|
||
| api_key = os.getenv("LLM_API_KEY") | ||
| assert api_key is not None, "LLM_API_KEY environment variable is not set." | ||
| base_url = os.getenv("LLM_BASE_URL", DEFAULT_BASE_URL) | ||
|
|
||
| with tempfile.TemporaryDirectory() as profile_store_dir: | ||
| store = LLMProfileStore(profile_store_dir) | ||
| # The Oracle model is saved under the conventional profile name "oracle". | ||
| store.save( | ||
| ORACLE_PROFILE_NAME, | ||
| LLM( | ||
| model=ORACLE_MODEL, | ||
| api_key=SecretStr(api_key), | ||
| base_url=base_url, | ||
| usage_id="oracle", | ||
| ), | ||
| include_secrets=True, | ||
| ) | ||
|
|
||
| primary_llm = LLM( | ||
| model=PRIMARY_MODEL, | ||
| api_key=SecretStr(api_key), | ||
| base_url=base_url, | ||
| usage_id="primary", | ||
| ) | ||
| agent = Agent(llm=primary_llm, tools=[Tool(name="ask_oracle")]) | ||
| conversation = LocalConversation( | ||
| agent=agent, | ||
| workspace=os.getcwd(), | ||
| profile_store_dir=profile_store_dir, | ||
| ) | ||
|
|
||
| print(f"Primary model: {conversation.agent.llm.model}") | ||
| print(f"Oracle model: {ORACLE_MODEL}") | ||
| conversation.send_message( | ||
| "Call the oracle to ask it for its opinion on the weather today, " | ||
| "then just tell me in two words how it's like." | ||
| ) | ||
| conversation.run() | ||
|
|
||
| combined = conversation.state.stats.get_combined_metrics() | ||
| print(f"Total cost: ${combined.accumulated_cost:.6f}") | ||
| print(f"EXAMPLE_COST: {combined.accumulated_cost}") |
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
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,36 @@ | ||
| """Ask-Oracle tool package. | ||
|
|
||
| Provides a read-only ``ask_oracle`` tool: a one-shot, tool-less sub-agent that | ||
| consults a stronger/more-capable LLM for a second opinion. The Oracle model is a | ||
| saved LLM profile, resolved by convention under the name ``oracle`` (see | ||
| ``ORACLE_PROFILE_NAME``). | ||
|
|
||
| Usage: | ||
| from openhands.tools.ask_oracle import AskOracleTool | ||
|
|
||
| agent = Agent( | ||
| llm=llm, | ||
| tools=[Tool(name=AskOracleTool.name)], | ||
| ) | ||
|
|
||
| The agent's active conversation LLM is never switched. The Oracle call sends only | ||
| the Oracle system prompt plus the agent's question and optional context, without | ||
| forwarding conversation history or tools. | ||
| """ | ||
|
|
||
| from openhands.tools.ask_oracle.definition import ( | ||
| ORACLE_PROFILE_NAME, | ||
| AskOracleAction, | ||
| AskOracleObservation, | ||
| AskOracleTool, | ||
| ) | ||
| from openhands.tools.ask_oracle.impl import AskOracleExecutor | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "ORACLE_PROFILE_NAME", | ||
| "AskOracleAction", | ||
| "AskOracleObservation", | ||
| "AskOracleExecutor", | ||
| "AskOracleTool", | ||
| ] |
114 changes: 114 additions & 0 deletions
114
openhands-tools/openhands/tools/ask_oracle/definition.py
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,114 @@ | ||
| """Action, observation, and tool definitions for the ask_oracle tool.""" | ||
|
|
||
| from collections.abc import Sequence | ||
| from typing import TYPE_CHECKING, Final, Self | ||
|
|
||
| from pydantic import Field | ||
| from rich.text import Text | ||
|
|
||
| from openhands.sdk.tool.registry import register_tool | ||
| from openhands.sdk.tool.tool import ( | ||
| Action, | ||
| Observation, | ||
| ToolAnnotations, | ||
| ToolDefinition, | ||
| ) | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from openhands.sdk.conversation.state import ConversationState | ||
|
|
||
|
|
||
| # The Oracle model is a saved LLM profile resolved by convention under this | ||
| # name. Save a profile named "oracle" (e.g. via LLMProfileStore.save("oracle", | ||
| # llm)) and the tool will consult it. No agent setting or wiring is required. | ||
| ORACLE_PROFILE_NAME: Final[str] = "oracle" | ||
|
|
||
|
|
||
| class AskOracleAction(Action): | ||
| """Action for asking the Oracle for advice.""" | ||
|
|
||
| question: str = Field( | ||
| description=( | ||
| "The specific question or dilemma to ask the Oracle about. Use this " | ||
| "when you are stuck, uncertain, or need a second opinion." | ||
| ) | ||
| ) | ||
| context: str | None = Field( | ||
| default=None, | ||
| description=( | ||
| "Optional extra context, such as approaches already tried, constraints, " | ||
| "or the recommendation you are considering." | ||
| ), | ||
| ) | ||
|
|
||
| @property | ||
| def visualize(self) -> Text: | ||
| content = Text() | ||
| content.append("Ask Oracle: ", style="bold cyan") | ||
| content.append(self.question) | ||
| if self.context: | ||
| content.append("\nContext: ", style="bold") | ||
| content.append(self.context) | ||
| return content | ||
|
|
||
|
|
||
| class AskOracleObservation(Observation): | ||
| """Observation returned by the Oracle consultation.""" | ||
|
|
||
| @property | ||
| def visualize(self) -> Text: | ||
| content = Text() | ||
| if self.is_error: | ||
| content.append("Oracle consultation failed", style="bold red") | ||
| else: | ||
| content.append("Oracle recommendation", style="bold green") | ||
| if self.text: | ||
| content.append("\n") | ||
| content.append(self.text) | ||
| return content | ||
|
|
||
|
|
||
| _DESCRIPTION = ( | ||
| "Ask the Oracle for a second opinion. The Oracle is a smart model intended " | ||
| "to help with difficult reasoning.\n\n" | ||
| "Use this when you are stuck, uncertain, comparing approaches, or need a " | ||
| "higher-quality recommendation before proceeding.\n\n" | ||
| "Treat the Oracle's response as strong guidance and follow its recommendation " | ||
| "unless you have a clear reason not to." | ||
| ) | ||
|
|
||
|
|
||
| class AskOracleTool(ToolDefinition[AskOracleAction, AskOracleObservation]): | ||
| """Tool for consulting the Oracle (a saved LLM profile named "oracle").""" | ||
|
|
||
| @classmethod | ||
| def create( | ||
| cls, | ||
| conv_state: "ConversationState | None" = None, # noqa: ARG003 | ||
| **params, | ||
| ) -> Sequence[Self]: | ||
| if params: | ||
| raise ValueError("AskOracleTool does not accept parameters") | ||
|
|
||
| # Import here to keep module import light and avoid any import cycles. | ||
| from openhands.tools.ask_oracle.impl import AskOracleExecutor | ||
|
|
||
| return [ | ||
| cls( | ||
| description=_DESCRIPTION, | ||
| action_type=AskOracleAction, | ||
| observation_type=AskOracleObservation, | ||
| executor=AskOracleExecutor(), | ||
| annotations=ToolAnnotations( | ||
| readOnlyHint=True, | ||
| destructiveHint=False, | ||
| idempotentHint=False, | ||
| openWorldHint=True, | ||
| ), | ||
| ) | ||
| ] | ||
|
|
||
|
|
||
| # Automatically register when this module is imported. | ||
| register_tool(AskOracleTool.name, AskOracleTool) | ||
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.