-
Notifications
You must be signed in to change notification settings - Fork 471
Add web search hook callbacks #1426
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
EricLBuehler
merged 3 commits into
master
from
codex/implement-hook-for-custom-search-result-handling
Jun 4, 2025
Merged
Changes from all commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,52 @@ | ||
| from mistralrs import ( | ||
| Runner, | ||
| Which, | ||
| ChatCompletionRequest, | ||
| Architecture, | ||
| WebSearchOptions, | ||
| ) | ||
| import os | ||
|
|
||
|
|
||
| def local_search(query: str): | ||
| results = [] | ||
| for root, _, files in os.walk("."): | ||
| for f in files: | ||
| if query in f: | ||
| path = os.path.join(root, f) | ||
| try: | ||
| content = open(path).read() | ||
| except Exception: | ||
| content = "" | ||
| results.append( | ||
| { | ||
| "title": f, | ||
| "description": path, | ||
| "url": path, | ||
| "content": content, | ||
| } | ||
| ) | ||
| results.sort(key=lambda r: r["title"], reverse=True) | ||
| return results | ||
|
|
||
|
|
||
| runner = Runner( | ||
| which=Which.Plain( | ||
| model_id="NousResearch/Hermes-3-Llama-3.1-8B", | ||
| arch=Architecture.Llama, | ||
| ), | ||
| enable_search=True, | ||
| search_callback=local_search, | ||
| ) | ||
|
|
||
| res = runner.send_chat_completion_request( | ||
| ChatCompletionRequest( | ||
| model="mistral", | ||
| messages=[{"role": "user", "content": "Where is Cargo.toml in this repo?"}], | ||
| max_tokens=64, | ||
| web_search_options=WebSearchOptions( | ||
| search_description="Local filesystem search" | ||
| ), | ||
| ) | ||
| ) | ||
| print(res.choices[0].message.content) | ||
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
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.
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.
🛠️ Refactor suggestion
Improve file handling with proper resource management.
The current file opening approach lacks proper resource management and error handling specificity.
Apply this diff to use a context manager and improve error handling:
📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.11.9)
17-17: Use a context manager for opening files
(SIM115)
🪛 Pylint (3.3.7)
[warning] 18-18: Catching too general exception Exception
(W0718)
[refactor] 17-17: Consider using 'with' for resource-allocating operations
(R1732)
[warning] 17-17: Using open without explicitly specifying an encoding
(W1514)
🤖 Prompt for AI Agents