### Run Human-Agent Chat Examples (Bash) Source: https://github.com/jozsefszalma/agent_link/blob/main/README.md These bash commands show how to start the host AI agent and the human chat client for the smolagents example. They illustrate the setup for a two-machine human-agent interaction using the provided example scripts. ```bash # Start the AI agent python smolagents_host_example.py ``` ```bash # Start the human chat client python smolagents_client_example.py ``` -------------------------------- ### Install agent_link Python Package Source: https://github.com/jozsefszalma/agent_link/blob/main/README.md Installs the `ai-agent-link` Python library using pip, making it available for use in Python projects. ```bash pip install ai-agent-link ``` -------------------------------- ### Configure MQTT Broker Environment Variables Source: https://github.com/jozsefszalma/agent_link/blob/main/README.md Example `.env` file configuration for MQTT broker credentials and room/host identifiers. These variables are crucial for the `agent_link` library to establish secure connections and identify agents within a communication room. ```bash MQTT_BROKER=your-broker-instance.hivemq.cloud # Broker hostname MQTT_PORT=8883 # Usually 8883 for TLS connections MQTT_USER=your_username # Your broker username MQTT_PASS=your_password # Your broker password MQTT_USE_TLS="true" # Use TLS encryption (recommended) ROOM_ID=unique-room-identifier # Can be anything, UUIDs recommended HOST_ID=unique-host-identifier # Agent ID of host/server agent, ``` -------------------------------- ### Initialize AgentNode for Basic Communication in Python Source: https://github.com/jozsefszalma/agent_link/blob/main/README.md Demonstrates how to create a `ConnectionConfig` object using environment variables and then initialize an `AgentNode`. This sets up an agent to connect to the MQTT broker and participate in a specified communication room. ```python from agent_link import ConnectionConfig, AgentNode, Audience, Message from agent_link.config import AuthMethod import os # 1. Create connection configuration 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=True, auth_method=AuthMethod.USERPASS, ) # 2. Create and connect an agent node room_id = "my_test_room" agent_id = "agent_001" node = AgentNode(config=config, room_id=room_id, agent_id=agent_id) ``` -------------------------------- ### API Reference: ConnectionConfig Class Source: https://github.com/jozsefszalma/agent_link/blob/main/README.md This section details the ConnectionConfig class, used for configuring connection settings to an MQTT broker. It outlines the parameters required for initialization, including broker address, port, TLS usage, authentication method, and credentials. ```APIDOC ConnectionConfig: config = ConnectionConfig( broker="broker.example.com", # Hostname of MQTT broker port=8883, # Port number use_tls=True, # Whether to use TLS encryption auth_method=AuthMethod.USERPASS,# Authentication method; NONE, USERPASS, TOKEN, CERT, API_KEY username="user", # Username for auth password="pass", # Password for auth client_id="my_client" # Optional client ID ) ``` -------------------------------- ### API Reference: AgentNode Class Source: https://github.com/jozsefszalma/agent_link/blob/main/README.md This section describes the AgentNode class, the core component for agent communication within the agent_link library. It covers its initialization parameters, such as connection configuration, room and agent IDs, and message response settings, along with its key methods for joining/leaving rooms, adding message handlers, and sending messages. ```APIDOC AgentNode: node = AgentNode( config=config, # ConnectionConfig object room_id="room_id", # Room identifier agent_id="agent_id", # Unique agent identifier respond_to_group=True, # Whether to process group messages respond_to_direct=True, # Whether to process direct messages qos=QoSLevel.AT_LEAST_ONCE # Quality of Service level; AT_MOST_ONCE, AT_LEAST_ONCE or EXACTLY_ONCE ) Methods: - join(): Connect to broker and join the room - leave(): Leave the room and disconnect - add_message_handler(handler): Add a function to process incoming messages - send_message(content, audience, recipient_id, in_reply_to): Send a message ``` -------------------------------- ### Integrate Smolagents with AgentLink (Python) Source: https://github.com/jozsefszalma/agent_link/blob/main/README.md This Python snippet demonstrates how to integrate the smolagents library with agent_link using a decorator. It shows setting up a CodeAgent, initializing an AgentNode, and defining a message handler that processes and optionally modifies responses from the smolagent. ```python from smolagents.agents import CodeAgent from smolagents import DuckDuckGoSearchTool, HfApiModel from agent_link import ConnectionConfig, AgentNode from agent_link.decorators import smolagent_message_handler # Set up the smolagent model = HfApiModel() my_agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=model) # Set up the agent_link node node = AgentNode(config=config, room_id=room_id, agent_id=agent_id) node.join() # Define handler with the decorator @smolagent_message_handler(my_agent, node) def handle_incoming(message, agent_response): """ Process incoming messages and optionally modify responses. Args: message: The incoming message object agent_response: The response generated by the smolagent Returns: Modified response or None to keep the original response """ print(f"Processing message from {message.sender_id}") # Example of modifying responses based on content if "urgent" in str(message.content).lower(): return f"[URGENT] {agent_response}" # Return None to use the smolagent's response as-is return None ``` -------------------------------- ### Define Message Handler and AgentNode Operations (Python) Source: https://github.com/jozsefszalma/agent_link/blob/main/README.md This snippet demonstrates how to define a message handler function, join an AgentNode to a room, register the handler, and send a message. It shows the basic flow of an agent interacting within the agent_link framework. ```python def handle_message(message: Message) -> str: print(f"Received from {message.sender_id}: {message.content}") return f"I received your message: {message.content}" node.join() node.add_message_handler(handle_message) node.send_message("Hello everyone, I've joined the room!") # Keep the agent running try: while True: time.sleep(1) pass # Your main loop logic here except KeyboardInterrupt: # Clean exit when terminated node.leave() ``` -------------------------------- ### API Reference: Message Class Source: https://github.com/jozsefszalma/agent_link/blob/main/README.md This section defines the Message class, which represents a chat message exchanged between agents. It details the various attributes of a message, including sender, content, timestamps, message IDs, reply references, audience type, and recipient ID for direct messages. ```APIDOC Message: message = Message( sender_id="agent_001", # ID of message sender content="Hello world", # Message content (any serializable type) timestamp=1234567890, # Optional timestamp message_id="msg_12345", # Optional message ID in_reply_to="msg_12344", # Optional reference to previous message audience=Audience.EVERYONE, # EVERYONE or DIRECT recipient_id="agent_002" # Required for DIRECT messages ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.