Skip to content
This repository was archived by the owner on Apr 28, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 23 additions & 11 deletions examples/agents/simple_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# the root directory of this source tree.
import os

import inspect

import fire
from llama_stack_client import LlamaStackClient, Agent, AgentEventLogger
from termcolor import colored
Expand Down Expand Up @@ -43,15 +45,25 @@ def main(host: str, port: int, model_id: str | None = None):

print(f"Using model: {model_id}")

agent = Agent(
client,
model=model_id,
instructions="",
tools=["builtin::websearch"],
input_shields=available_shields,
output_shields=available_shields,
enable_session_persistence=False,
)
agent_kwargs = {
"model": model_id,
"instructions": "",
# OpenAI Responses tool schema requires a type discriminator.
"tools": [{"type": "web_search"}],
"input_shields": available_shields,
"output_shields": available_shields,
"enable_session_persistence": False,
}
allowed_params = set(inspect.signature(Agent.__init__).parameters)
filtered_kwargs = {k: v for k, v in agent_kwargs.items() if k in allowed_params}

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.

it is not clear that any developer will write code like this when creating agents using llama stack client. Can you make it so that the code here is something a new developer can just copy? We dont need any backward compatibility here either. We could just use the latest version. We can have copies of the examples for older versions if needed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

try:
agent = Agent(client, **filtered_kwargs)
except TypeError as exc:
# Fallback for older clients that only accept string tool names.
if "Unsupported tool type" not in str(exc):
raise
filtered_kwargs["tools"] = ["builtin::websearch"]
agent = Agent(client, **filtered_kwargs)
user_prompts = [
"Hello",
"Search web for which players played in the winning team of the NBA western conference semifinals of 2024",
Expand All @@ -65,8 +77,8 @@ def main(host: str, port: int, model_id: str | None = None):
session_id=session_id,
)

for log in AgentEventLogger().log(response):
log.print()
for printable in AgentEventLogger().log(response):
print(printable, end="", flush=True)


if __name__ == "__main__":
Expand Down
32 changes: 28 additions & 4 deletions examples/agents/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,34 @@
from termcolor import colored


def _get_model_type(model) -> str | None:
for attr in ("model_type", "type", "model_kind", "kind", "model_family"):
value = getattr(model, attr, None)
if isinstance(value, str):
return value
return None


def _is_llm_model(model) -> bool:
model_type = _get_model_type(model)
# If the client schema doesn't expose type fields, assume LLM.
return model_type is None or model_type == "llm"


def _get_model_id(model) -> str | None:
for attr in ("identifier", "model_id", "id", "name"):
value = getattr(model, attr, None)
if isinstance(value, str):
return value
return None


def check_model_is_available(client: LlamaStackClient, model: str):
available_models = [
model.identifier
model_id
for model in client.models.list()
if model.model_type == "llm" and "guard" not in model.identifier
for model_id in [_get_model_id(model)]
if model_id and _is_llm_model(model) and "guard" not in model_id
]

if model not in available_models:
Expand All @@ -23,9 +46,10 @@ def check_model_is_available(client: LlamaStackClient, model: str):

def get_any_available_model(client: LlamaStackClient):
available_models = [
model.identifier
model_id
for model in client.models.list()
if model.model_type == "llm" and "guard" not in model.identifier
for model_id in [_get_model_id(model)]
if model_id and _is_llm_model(model) and "guard" not in model_id
]
if not available_models:
print(colored("No available models.", "red"))
Expand Down