Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/agents/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def main(host: str, port: int):
print(f"Available shields found: {available_shields}")

available_models = [
model.identifier for model in client.models.list() if model.model_type == "llm"
model.identifier
for model in client.models.list()
if model.model_type == "llm" and "405B" not in model.identifier
]
if not available_models:
print(colored("No available models. Exiting.", "red"))
Expand Down
72 changes: 72 additions & 0 deletions examples/agents/react_agent.py
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(
Copy link
Copy Markdown
Contributor

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 ReActAgent vs ReActAgentConfig + 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReActAgent is a simple wrapper and helper class that hides the configuration needed to create a ReActAgentConfig + Agent, otherwise they are the same. We can override configurations with custom_agent_config, and that brings it the same as using Agent.

Copy link
Copy Markdown
Contributor

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 need custom_agent_config.

Copy link
Copy Markdown
Contributor Author

@yanxi0830 yanxi0830 Feb 6, 2025

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:

agent = ReActAgent(client, model, builtin_toolgroups, client_tools)

v.s.

agent_config = get_react_agent_config(builtin_toolgroups, client_tools, json_response_format)
agent = Agent(client, agent_config, client_tools, output_parser=ReActOutputParser())

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

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)