Skip to content

Commit 697e755

Browse files
authored
fix: update inference/agent example scripts to be more robust (#14)
* update agent example * improve test
1 parent 77ad9c8 commit 697e755

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

examples/agents.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('RAG Integration Tests', () => {
1313
it('should create an agent and handle conversation successfully', async () => {
1414
// Get available models
1515
const models = await client.models.list();
16-
const llmModel = models.find(model => model.model_type === 'llm');
16+
const llmModel = models.find(model => model.model_type === 'llm' && !model.identifier.includes('guard') && !model.identifier.includes('405'));
1717
expect(llmModel).toBeTruthy();
1818

1919
// Create agent with configuration

examples/agents.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ const client = new LlamaStackClient({ baseURL: 'http://localhost:8321' });
77

88
async function main() {
99
const availableModels = (await client.models.list())
10-
.filter((model: any) => model.model_type === 'llm')
10+
.filter((model: any) =>
11+
model.model_type === 'llm' &&
12+
!model.identifier.includes('guard') &&
13+
!model.identifier.includes('405')
14+
)
1115
.map((model: any) => model.identifier);
1216

1317
if (availableModels.length === 0) {
@@ -31,12 +35,13 @@ async function main() {
3135
},
3236
toolgroups: process.env["TAVILY_SEARCH_API_KEY"] ? ['builtin::websearch'] : [],
3337
tool_choice: 'auto',
34-
tool_prompt_format: 'json',
38+
tool_prompt_format: 'python_list',
3539
input_shields: [],
3640
output_shields: [],
3741
enable_session_persistence: false,
3842
max_infer_iters: 10,
3943
};
44+
console.log('Agent Configuration:', JSON.stringify(agentConfig, null, 2));
4045

4146
const agentic_system_create_response = await client.agents.create({agent_config: agentConfig});
4247
const agent_id = agentic_system_create_response.agent_id;

examples/inference.test.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@ import LlamaStackClient from 'llama-stack-client';
22

33
describe('LlamaStack Client Integration Tests', () => {
44
let client: LlamaStackClient;
5+
let availableModels: string[];
56

6-
beforeAll(() => {
7+
beforeAll(async () => {
78
client = new LlamaStackClient({ baseURL: 'http://localhost:8321' });
9+
10+
// Fetch available models once
11+
const models = await client.models.list();
12+
availableModels = models
13+
.filter((model: any) =>
14+
model.model_type === 'llm' &&
15+
!model.identifier.includes('guard') &&
16+
!model.identifier.includes('405')
17+
)
18+
.map((model: any) => model.identifier);
819
});
920

1021
test('should list available models', async () => {
@@ -14,18 +25,28 @@ describe('LlamaStack Client Integration Tests', () => {
1425
});
1526

1627
test('should perform non-streaming chat completion', async () => {
28+
// Skip test if no models available
29+
if (availableModels.length === 0) {
30+
console.warn('Skipping test: No available models');
31+
return;
32+
}
1733
const chatCompletion = await client.inference.chatCompletion({
1834
messages: [{ content: 'Hello, how are you?', role: 'user' }],
19-
model_id: 'meta-llama/Llama-3.2-3B-Instruct',
35+
model_id: availableModels[0] as string,
2036
});
2137
expect(chatCompletion).toBeDefined();
2238
expect(chatCompletion.completion_message).toBeDefined();
2339
}, 30000);
2440

2541
test('should perform streaming chat completion', async () => {
42+
// Skip test if no models available
43+
if (availableModels.length === 0) {
44+
console.warn('Skipping test: No available models');
45+
return;
46+
}
2647
const stream = await client.inference.chatCompletion({
2748
messages: [{ content: 'Hello, how are you?', role: 'user' }],
28-
model_id: 'meta-llama/Llama-3.2-3B-Instruct',
49+
model_id: availableModels[0] as string,
2950
stream: true,
3051
});
3152

examples/inference.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,34 @@ const client = new LlamaStackClient({ baseURL: 'http://localhost:8321' });
55

66
async function main() {
77
// list models
8-
const models = await client.models.list();
9-
console.log(models);
8+
const availableModels = (await client.models.list())
9+
.filter((model: any) =>
10+
model.model_type === 'llm' &&
11+
!model.identifier.includes('guard') &&
12+
!model.identifier.includes('405')
13+
)
14+
.map((model: any) => model.identifier);
15+
16+
console.log(availableModels);
17+
18+
if (availableModels.length === 0) {
19+
console.log('No available models. Exiting.');
20+
return;
21+
}
22+
const selectedModel = availableModels[0];
23+
console.log(`Using model: ${selectedModel}`);
1024

1125
// non-streaming chat-completion
1226
const chatCompletion = await client.inference.chatCompletion({
1327
messages: [{ content: 'Hello, how are you?', role: 'user' }],
14-
model_id: 'meta-llama/Llama-3.2-3B-Instruct',
28+
model_id: selectedModel,
1529
});
1630
console.log(chatCompletion);
1731

1832
// streaming chat-completion
1933
const stream = await client.inference.chatCompletion({
2034
messages: [{ content: 'Hello, how are you?', role: 'user' }],
21-
model_id: 'meta-llama/Llama-3.2-3B-Instruct',
35+
model_id: selectedModel,
2236
stream: true,
2337
});
2438
for await (const chunk of stream) {

0 commit comments

Comments
 (0)