-
-
Notifications
You must be signed in to change notification settings - Fork 225
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
feat: added think tool to let agent query a reasoner #416
Open
ErikBjare
wants to merge
3
commits into
master
Choose a base branch
from
dev/think-tool
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,128 @@ | ||
""" | ||
This tool enables deeper thinking on complex problems by sending requests to more powerful LLMs. | ||
|
||
The tool allows the assistant to request help with complex reasoning tasks by: | ||
1. Sending the current context and problem to a more powerful model | ||
2. Getting back enhanced reasoning and analysis | ||
3. Using that to provide better solutions | ||
""" | ||
|
||
# TODO: Implement Karpathy-style consortium | ||
# - Send the same problem to multiple models | ||
# - Aggregate and compare their responses | ||
# - Use voting/consensus mechanisms | ||
# - Consider different specialties/capabilities | ||
|
||
import logging | ||
from collections.abc import Generator | ||
|
||
from ..llm import reply | ||
from ..llm.models import get_model | ||
from ..message import Message | ||
from .base import ConfirmFunc, ToolSpec | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def execute( | ||
code: str | None, | ||
args: list[str] | None, | ||
kwargs: dict[str, str] | None, | ||
confirm: ConfirmFunc, | ||
) -> Generator[Message, None, None]: | ||
"""Execute the think tool with the given content.""" | ||
if not code: | ||
yield Message("system", "No content provided for thinking") | ||
return | ||
|
||
default_model = "openai/o1-preview" | ||
|
||
# Get current model | ||
current_model = get_model() | ||
|
||
# Use specialized reasoning models if available | ||
# TODO: choose reasoner based on provider, similar to summary models | ||
model: str = kwargs.get("model", default_model) if kwargs else default_model | ||
reasoning_model = get_model(model) | ||
|
||
if reasoning_model.model == current_model.model: | ||
logger.warning( | ||
"No more powerful model available for reasoning, using current model" | ||
) | ||
|
||
# Extract any referenced files from the content | ||
files_content = "" | ||
for line in code.split("\n"): | ||
if line.strip().startswith("- "): | ||
filepath = line.strip("- ").strip() | ||
try: | ||
with open(filepath) as f: | ||
files_content += ( | ||
f"\nContents of {filepath}:\n```\n{f.read()}\n```\n" | ||
) | ||
except Exception as e: | ||
logger.warning(f"Failed to read file {filepath}: {e}") | ||
|
||
# Combine file contents with the thinking request | ||
if files_content: | ||
full_content = files_content + "\n" + code | ||
else: | ||
full_content = code | ||
|
||
# Prepare thinking prompt | ||
thinking_prompt = f"""Please analyze this problem carefully and thoroughly: | ||
|
||
{full_content} | ||
|
||
Think step by step and consider: | ||
1. Key aspects and components | ||
2. Potential challenges and edge cases | ||
3. Alternative approaches | ||
4. Best practices and principles | ||
5. Implementation details""" | ||
|
||
# Log that we're using a different model for thinking | ||
logger.info(f"Using {reasoning_model.full} for enhanced reasoning") | ||
|
||
# Get enhanced reasoning | ||
messages = [Message("user", thinking_prompt)] | ||
response = reply(messages, reasoning_model.full, stream=False) | ||
|
||
# Return the enhanced reasoning | ||
yield Message( | ||
"system", | ||
f"Enhanced reasoning from {reasoning_model.model}:\n\n{response.content}", | ||
) | ||
|
||
|
||
tool = ToolSpec( | ||
"think", | ||
desc="Tool to enable deeper thinking on challenging or complex problems.", | ||
block_types=["think"], | ||
execute=execute, | ||
instructions=""" | ||
Use this tool when you need help with complex reasoning or problem-solving. | ||
Wrap your thinking request in a code block with the language tag: `think` | ||
Include any relevant files or context to help with the analysis. | ||
|
||
The tool will: | ||
1. Send your request to a more powerful model | ||
2. Get back enhanced reasoning and analysis | ||
3. Help you provide better solutions | ||
|
||
Example: | ||
```think | ||
How should we architect this system to be scalable and maintainable? | ||
Key considerations: | ||
- Multiple users | ||
- Real-time updates | ||
- Data consistency | ||
- Error handling | ||
|
||
Files: | ||
- README.md | ||
- src/api.py | ||
- docs/architecture.md | ||
``` | ||
""", | ||
) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't use markdown specific format instructions.