-
Notifications
You must be signed in to change notification settings - Fork 641
feat: ReACT agent example #166
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
12 commits
Select commit
Hold shift + click to select a range
e8dc718
react agent
yanxi0830 de94244
tmp
yanxi0830 1a493a9
example
yanxi0830 e28b16e
update example
yanxi0830 a8ac2f5
refactor
yanxi0830 c205e6a
use sdk
yanxi0830 9364c5b
use sdk
yanxi0830 ea4cf42
wip builtin tool
yanxi0830 3f81f87
enable builtin tool
yanxi0830 695a192
use sdk react
yanxi0830 f955808
tool decorator working
yanxi0830 fc4fae7
update agent
yanxi0830 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,72 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the terms described in the LICENSE file in | ||
| # the root directory of this source tree. | ||
| import uuid | ||
|
|
||
| import fire | ||
|
|
||
| from llama_stack_client import LlamaStackClient | ||
| from llama_stack_client.lib.agents.client_tool import client_tool | ||
| from llama_stack_client.lib.agents.event_logger import EventLogger | ||
| from llama_stack_client.lib.agents.react.agent import ReActAgent | ||
|
|
||
|
|
||
| @client_tool | ||
| def torchtune(query: str = "torchtune"): | ||
| """ | ||
| Answer information about torchtune. | ||
|
|
||
| :param query: The query to use for querying the internet | ||
| :returns: Information about torchtune | ||
| """ | ||
| dummy_response = """ | ||
| torchtune is a PyTorch library for easily authoring, finetuning and experimenting with LLMs. | ||
|
|
||
| torchtune provides: | ||
|
|
||
| PyTorch implementations of popular LLMs from Llama, Gemma, Mistral, Phi, and Qwen model families | ||
| Hackable training recipes for full finetuning, LoRA, QLoRA, DPO, PPO, QAT, knowledge distillation, and more | ||
| Out-of-the-box memory efficiency, performance improvements, and scaling with the latest PyTorch APIs | ||
| YAML configs for easily configuring training, evaluation, quantization or inference recipes | ||
| Built-in support for many popular dataset formats and prompt templates | ||
| """ | ||
| return dummy_response | ||
|
|
||
|
|
||
| def main(): | ||
| client = LlamaStackClient( | ||
| base_url="http://localhost:8321", | ||
| ) | ||
|
|
||
| model = "meta-llama/Llama-3.1-8B-Instruct" | ||
| print(type(torchtune)) | ||
| agent = ReActAgent( | ||
| client=client, | ||
| model=model, | ||
| builtin_toolgroups=["builtin::websearch"], | ||
| client_tools=[torchtune], | ||
| ) | ||
|
|
||
| session_id = agent.create_session(f"ttest-session-{uuid.uuid4().hex}") | ||
|
|
||
| response = agent.create_turn( | ||
| messages=[{"role": "user", "content": "What's the current time in new york?"}], | ||
| session_id=session_id, | ||
| stream=True, | ||
| ) | ||
| for log in EventLogger().log(response): | ||
| log.print() | ||
|
|
||
| response2 = agent.create_turn( | ||
| messages=[{"role": "user", "content": "What is torchtune?"}], | ||
| session_id=session_id, | ||
| stream=True, | ||
| ) | ||
| for log in EventLogger().log(response2): | ||
| log.print() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| fire.Fire(main) | ||
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.
What's your thinking behind having
ReActAgentvsReActAgentConfig+Agent? I seems that the former hides some configurations that were available, e.g. max_infer_iters? And the latter is consistent with how Agent is used.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.
ReActAgentis a simple wrapper and helper class that hides the configuration needed to create aReActAgentConfig+Agent, otherwise they are the same. We can override configurations withcustom_agent_config, and that brings it the same as usingAgent.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.
Isn't it just as simple with
ReActAgentConfig + Agent? Just one extra line to instantiate the Agent? Then we don't needcustom_agent_config.Uh oh!
There was an error while loading. Please reload this page.
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.
Yeah, I guess its the difference b/w:
v.s.
former hides agent_config & output_parser, while latter needs users to know about output_parser, I guess its a matter of how much we want to hide from users