-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsimple_agent_stream_example.dart
More file actions
58 lines (51 loc) · 1.61 KB
/
simple_agent_stream_example.dart
File metadata and controls
58 lines (51 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import 'dart:io';
import 'package:dart_agent_core/dart_agent_core.dart';
String getWeather(String location) {
if (location.toLowerCase().contains('tokyo')) return 'Sunny, 25C';
return 'Unknown';
}
void main() async {
final apiKey = Platform.environment['GEMINI_API_KEY'] ?? 'YOUR_API_KEY';
final client = GeminiClient(apiKey: apiKey);
final modelConfig = ModelConfig(model: 'gemini-2.5-flash');
final weatherTool = Tool(
name: 'get_weather',
description: 'Get the current weather for a location.',
executable: getWeather,
parameters: {
'type': 'object',
'properties': {
'location': {
'type': 'string',
'description': 'The city and state, e.g. Tokyo',
},
},
'required': ['location'],
},
);
final agent = StatefulAgent(
name: 'streaming_agent',
client: client,
tools: [weatherTool],
modelConfig: modelConfig,
state: AgentState.empty(),
systemPrompts: ['You are a helpful assistant.'],
);
print('Sending message to agent and streaming response...');
final stream = agent.runStream([
UserMessage.text(
'Tell me a short story about a robot traveling to Tokyo, and mention the weather there.',
),
]);
await for (final event in stream) {
if (event.eventType == StreamingEventType.modelChunkMessage) {
final chunk = event.data as ModelMessage;
if (chunk.textOutput != null) {
stdout.write(chunk.textOutput);
}
} else if (event.eventType == StreamingEventType.functionCallRequest) {
print('\n[Agent is calling a tool...]');
}
}
print('\nDone streaming.');
}