This repository was archived by the owner on Jan 30, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmolagents_client_example.py
More file actions
143 lines (116 loc) · 5.2 KB
/
smolagents_client_example.py
File metadata and controls
143 lines (116 loc) · 5.2 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Copyright 2025 Jozsef Szalma
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
import signal
import time
import logging
from threading import Event
from dotenv import load_dotenv
from agent_link import ConnectionConfig, AgentNode, Audience, Message
from agent_link.config import AuthMethod
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Exit flag for clean shutdown
exit_flag = Event()
load_dotenv()
def main():
# -------------------------------------------------------------------
# 1) Connection setup using environment variables
# -------------------------------------------------------------------
config = ConnectionConfig(
broker=os.getenv("MQTT_BROKER"),
port=int(os.getenv("MQTT_PORT", "1883")),
username=os.getenv("MQTT_USER"),
password=os.getenv("MQTT_PASS"),
use_tls=os.getenv("MQTT_USE_TLS", "true").lower() in ("true", "1", "yes"),
auth_method=AuthMethod.USERPASS,
)
# -------------------------------------------------------------------
# 2) Create and connect an AgentNode for the client
# -------------------------------------------------------------------
room_id = os.getenv("ROOM_ID")
client_id = f"human_client_{int(time.time())}" # Generate a unique ID
host_id = os.getenv("HOST_ID") # The ID of the agent we want to talk to
client_node = AgentNode(config=config, room_id=room_id, agent_id=client_id)
logger.info(f"Client ID: {client_id}")
logger.info(f"Room ID: {room_id}")
if host_id:
logger.info(f"Will send direct messages to host ID: {host_id}")
else:
logger.info("No HOST_ID specified, will send messages to everyone in the room")
# -------------------------------------------------------------------
# 3) Handle incoming messages
# -------------------------------------------------------------------
def handle_message(message: Message) -> None:
"""Print incoming messages"""
# Don't respond to our own messages
if message.sender_id == client_id:
return
# Print the message
print(f"\n[{message.sender_id}]: {message.content}")
print("\nYou: ", end="", flush=True) # Reset prompt
return None # No response needed
# -------------------------------------------------------------------
# 4) Set up exit handler
# -------------------------------------------------------------------
def signal_handler(sig, frame):
logger.info("Shutting down client...")
exit_flag.set()
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
try:
# Join the room and register our message handler
client_node.join()
client_node.add_message_handler(handle_message)
print("\n=== MQTT Chat Client ===")
print(f"Connected to room: {room_id}")
if host_id:
print(f"Sending direct messages to: {host_id}")
else:
print("Sending messages to everyone in the room")
print("Type your messages and press Enter to send")
print("Press Ctrl+C to exit")
print("======================\n")
# Send an initial notification
#client_node.send_message(f"Human client {client_id} has joined the chat")
# Main loop: Read user input and send messages
while not exit_flag.is_set():
try:
print("You: ", end="", flush=True)
user_input = input()
if not user_input.strip():
continue
if user_input.lower() in ('exit', 'quit'):
break
# Determine if this is a direct message or group message
if host_id:
# Send direct message to the specific agent
client_node.send_message(
content=user_input,
audience=Audience.DIRECT,
recipient_id=host_id
)
else:
# Send to everyone in the room
client_node.send_message(content=user_input)
except EOFError:
break # Handle Ctrl+D
except Exception as e:
logger.error(f"Error: {e}")
finally:
# Send a goodbye message and leave the room
#client_node.send_message("Human client has left the chat")
client_node.leave()
logger.info("Client disconnected")
if __name__ == "__main__":
main()