Hugging face API agent as a Function
Bookmark

Hugging face API agent as a Function

Introduction

This system shows how to integrate the Hugging Face API with an Agentverse Agent. The example consists of multiple layers of Primary and Secondary functions, with the Hugging Face system as the main Primary functions and Hugging Face request and model list as Secondary functions.

Guide

Supporting documentation

The agents

Hugging Face System Agent

agent.py
    # Here we demonstrate how we can create a hugging face system agent that is compatible with DeltaV.
 
    # After running this agent, it can be registered to DeltaV on Agentverse. For registration you will have to use the agent's address.
 
    # Importing required libraries.
    import requests
    from ai_engine import UAgentResponse, UAgentResponseType
 
    # Define a model class for the Hugging Face agent's expected message format.
    class HF(Model):
        response : str # This class has a single attribute 'response' that holds the string response from the subtask.
 
    # Create a protocol for the Hugging Face (HF) agent, specifying its communication protocol.
    hf_protocol = Protocol("Hugging Face")
 
    # Define a handler for the Hugging face protocol.
    @hf_protocol.on_message(model=HF, replies = UAgentResponse)
    async def on_hf_request(ctx: Context, sender: str, msg: HF):
 
        # Log the receipt of a response, including the sender and the message prompt.
        ctx.logger.info(f"Received hugging face request from {sender} with prompt: {msg.response}")
 
        # Format a response message incorporating the received message.
        message = f'Response to your query from model is \n {msg.response}'
        # Asynchronously send a response back to the sender with the processed message.
        await ctx.send(sender, UAgentResponse(message = message, type = UAgentResponseType.FINAL))
 
    # Include the Hugging Face protocol in your agent.
    agent.include(hf_protocol)

Hugging Face Request Agent

agent.py
    # Here we demonstrate how we can create a hugging face request agent that is compatible with DeltaV.
 
    # After running this agent, it can be registered to DeltaV on Agentverse. For registration you will have to use the agent's address.
 
    # Importing required libraries.
    import requests
    import json
    from ai_engine import UAgentResponse, UAgentResponseType
 
    # Define a model class for the Hugging Face Request agent's expected message format.
    class Search(Model):
        model_id : str
        query : str
 
    # Define a function to handle query from user using model_id and query provided by user.
    async def handle_query(model_id, query):
        Model_ID = model_id
        API_URL = f'https://api-inference.huggingface.co/models/{Model_ID}' # hugging face url
        API_TOKEN = 'YOUR TOKEN HERE' # hugging face API token
 
        headers = {"Authorization": f"Bearer {API_TOKEN}"}
 
        # Make request to hugging face API with model_id and query.
        response = requests.post(API_URL, headers=headers, json=query).json()
        return response
 
    # Create a protocol for the Hugging Face Request(HF) agent, specifying its communication protocol.
    hfprotocol = Protocol(name = 'Hugging Face protocol')
 
    # Define a handler for the Hugging face request protocol.
    @hfprotocol.on_message(model = Search, replies = UAgentResponse)
    async def handle_message(ctx: Context, sender: str, msg: Search):
 
        # Log the model_id and query provided by user.
        ctx.logger.info(f'Message sent from {sender} : {msg.model_id}')
        ctx.logger.info(f'Message sent from subtask : {msg.query}')
 
        # Calling handle_query function to get response from API.
        response = await handle_query(msg.model_id, msg.query)
        # sending response to hugging face agent
        await ctx.send(sender, UAgentResponse(message = str(response), type = UAgentResponseType.FINAL))
 
    # Include the Hugging Face protocol in your agent.
    agent.include(hfprotocol, publish_manifest = True)

Model List Agent

agent.py
    # Here we demonstrate how we can create a model list agent that is compatible with DeltaV.
 
    # After running this agent, it can be registered to DeltaV on Agentverse. For registration you will have to use the agent's address.
 
    # Importing required libraries.
    import requests
    from ai_engine import UAgentResponse, UAgentResponseType
    import json
 
    # Define a model class for the Model List agent's expected message format.
    class Search(Model):
        search : str # This is a keyword for which user wants to search model
 
    # Create a protocol for the Model List agent, specifying its communication protocol.
    model_list_protocol = Protocol(name = 'Model List protocol')
 
    # Define a function to handle query from user using search keyword provided by user.
    async def handle_query(search):
        url = "https://huggingface.co/api/models"
 
        params = {
            "search": search,
            "filter": "text-classification",
            "sort": "downloads",
            "direction": -1,
            "limit": 5
        } # Search parameters.
 
        models = [] # List of models.
 
        # Make the GET request.
        response = requests.get(url, params = params)
 
        # Append models in list.
        for model in response.json():
            models.append(model['id'])
 
        return models
 
    # Define a handler for the Model list protocol.
    @model_list_protocol.on_message(model = Search, replies = UAgentResponse)
    async def handle_message(ctx: Context, sender: str, msg: Search):
        # Log search keyword provided by user.
        ctx.logger.info(f'Message sent from {sender} : {msg.search}')
 
        # Call handle_query to get list of models.
        options = handle_query(msg.search)
        # Log model list responded by hugging face request.
        ctx.logger.info(f'Message sent from {sender} : {options}')
 
        # Format options in dictionary format to provide options to user.
        formatted_options = [{'key': i + 1, 'value': value} for i, value in enumerate(options)]
 
        # Send message to the user.
        await ctx.send(sender, UAgentResponse(message = str(formatted_options), type=UAgentResponseType.FINAL))
 
    # Include model_list protocol in agent.
    agent.include(model_list_protocol, publish_manifest = True)

Was this page helpful?

Bookmark