Skip to content
This repository was archived by the owner on Apr 28, 2026. It is now read-only.

Commit 869e7e6

Browse files
committed
Address Ragu's comments
1 parent 8ee939d commit 869e7e6

2 files changed

Lines changed: 45 additions & 32 deletions

File tree

examples/agents/simple_chat.py

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
# the root directory of this source tree.
66
import os
77

8-
import inspect
9-
108
import fire
119
from llama_stack_client import LlamaStackClient, Agent, AgentEventLogger
1210
from termcolor import colored
1311

14-
from .utils import check_model_is_available, get_any_available_model
12+
from .utils import check_model_is_available, get_any_available_chat_model
1513

1614

1715
def main(host: str, port: int, model_id: str | None = None):
@@ -29,14 +27,8 @@ def main(host: str, port: int, model_id: str | None = None):
2927
provider_data={"tavily_search_api_key": os.getenv("TAVILY_SEARCH_API_KEY")},
3028
)
3129

32-
available_shields = [shield.identifier for shield in client.shields.list()]
33-
if not available_shields:
34-
print(colored("No available shields. Disabling safety.", "yellow"))
35-
else:
36-
print(f"Available shields found: {available_shields}")
37-
3830
if model_id is None:
39-
model_id = get_any_available_model(client)
31+
model_id = get_any_available_chat_model(client)
4032
if model_id is None:
4133
return
4234
else:
@@ -45,25 +37,13 @@ def main(host: str, port: int, model_id: str | None = None):
4537

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

48-
agent_kwargs = {
49-
"model": model_id,
50-
"instructions": "",
40+
agent = Agent(
41+
client,
42+
model=model_id,
43+
instructions="",
5144
# OpenAI Responses tool schema requires a type discriminator.
52-
"tools": [{"type": "web_search"}],
53-
"input_shields": available_shields,
54-
"output_shields": available_shields,
55-
"enable_session_persistence": False,
56-
}
57-
allowed_params = set(inspect.signature(Agent.__init__).parameters)
58-
filtered_kwargs = {k: v for k, v in agent_kwargs.items() if k in allowed_params}
59-
try:
60-
agent = Agent(client, **filtered_kwargs)
61-
except TypeError as exc:
62-
# Fallback for older clients that only accept string tool names.
63-
if "Unsupported tool type" not in str(exc):
64-
raise
65-
filtered_kwargs["tools"] = ["builtin::websearch"]
66-
agent = Agent(client, **filtered_kwargs)
45+
tools=[{"type": "web_search"}],
46+
)
6747
user_prompts = [
6848
"Hello",
6949
"Search web for which players played in the winning team of the NBA western conference semifinals of 2024",

examples/agents/utils.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44

55
def _get_model_type(model) -> str | None:
6-
for attr in ("model_type", "type", "model_kind", "kind", "model_family"):
7-
value = getattr(model, attr, None)
8-
if isinstance(value, str):
9-
return value
6+
for metadata_attr in ("custom_metadata", "metadata"):
7+
metadata = getattr(model, metadata_attr, None)
8+
if isinstance(metadata, dict):
9+
value = metadata.get("model_type") or metadata.get("type")
10+
if isinstance(value, str):
11+
return value
1012
return None
1113

1214

@@ -56,3 +58,34 @@ def get_any_available_model(client: LlamaStackClient):
5658
return None
5759

5860
return available_models[0]
61+
62+
63+
def can_model_chat(client: LlamaStackClient, model_id: str) -> bool:
64+
# Lightweight probe to ensure the model supports chat completions.
65+
try:
66+
client.chat.completions.create(
67+
model=model_id,
68+
messages=[{"role": "user", "content": "ping"}],
69+
max_tokens=1,
70+
)
71+
except Exception:
72+
return False
73+
return True
74+
75+
def get_any_available_chat_model(client: LlamaStackClient):
76+
available_models = [
77+
model_id
78+
for model in client.models.list()
79+
for model_id in [_get_model_id(model)]
80+
if model_id and _is_llm_model(model) and "guard" not in model_id
81+
]
82+
if not available_models:
83+
print(colored("No available models.", "red"))
84+
return None
85+
86+
for model_id in available_models:
87+
if can_model_chat(client, model_id):
88+
return model_id
89+
90+
print(colored("No available chat-capable models.", "red"))
91+
return None

0 commit comments

Comments
 (0)