Skip to content

Latest commit

 

History

History
93 lines (78 loc) · 4.29 KB

python.md

File metadata and controls

93 lines (78 loc) · 4.29 KB

GenAIFramework Gem API for Python usage

Available Python EBusses

AIAgentRequestBus

TWo EBus methods are available for communication with an Agent in Python:

  • GetHistory
  • SendPrompt

The GetHistory method returns the history of the agent. The history is a JSON string containing the prompts and responses. It consists of a list of objects with roles and content. The roles can be user, assistant, or system corresponding to the user, assistant, or system message respectively. The content is the message itself. It is a list of strings generated by the model or the user. You can use the GetHistory method as follows:

from azlmbr.ai import AIAgentRequestBus
from azlmbr.bus import bus

history = AIAgentRequestBus(bus.Event, "GetHistory", agentId)

The history variable will become a JSON string containing the history of the agent. For example:

[
    {
        "role": "system",
        "content": ["Only respond in one word."]
    },
    {
        "role": "user",
        "content": ["What is a dog?"]
    },
    {
        "role": "assistant",
        "content": ["Animal"]
    }
]

The SendPrompt method sends a prompt to the agent. The prompt is also a JSON string containing the roles and content. The format of the prompt is the same as the history. You can use the SendPrompt method as follows:

from azlmbr.ai import AIAgentRequestBus

prompt = '[{"role": "user", "content": ["What is a dog?"]}]'

AIAgentRequestBus(bus.Event, "SendPrompt", agentId, prompt) # Agent ID is usually passed as an argument

The agentId is an argument passed to a Python feature during its creation. It represents the ID of the agent.

AIAgentNotificationBusHandler

In Python, the AIAgentNotificationBusHandler is used to receive notifications from the agent. The notifications are sent when the agent has a response ready. The OnAIResponse method is called when the agent has a response ready. You can use the AIAgentNotificationBusHandler as follows:

from azlmbr.ai import AIAgentNotificationBusHandler

def OnAIResponse(message):
    # Handle the response

handler = AIAgentNotificationBusHandler()
handler.connect(agentId) # Agent ID is usually passed as an argument
handler.add_callback("OnAIResponse", OnAIResponse)

The message parameter is a JSON string containing the response. The format of the response is a single object with role and content. For example:

{
    "role": "assistant",
    "content": ["Animal"]
}

ConversationNotificationBus

In Python, the ConversationNotificationBus is used to receive and send notification. Receiving the notification is done using ConversationNotificationBusHandler and sending using ConversationNotificationBus. The behavior of this handler is similar to the AIAgentNotificationBusHandler. The OnNewMessage method is called when a new message from the UI is received. You can use the ConversationNotificationBusHandler as follows:

from azlmbr.ai import ConversationNotificationBus

def OnNewMessage(message):
    # Handle the message

handler = ConversationNotificationBusHandler()
handler.connect(conversationId) # Conversation ID is usually passed as an argument
handler.add_callback("OnNewMessage", OnNewMessage)

The message parameter is a list of length 1 containing the string message from the UI. For example:

["What is a dog?"]

The ConversationNotificationBus is used to send a message to the UI. The OnFeatureResponse method sends a response to the UI. This method needs three arguments: the conversation ID (usually passed as an argument), a string response, list of strings containing other information about the response (typically details). You can use the ConversationNotificationBus as follows:

from azlmbr.ai import ConversationNotificationBus
from azlmbr.bus import bus

response = "Animal"
summary = ["This is a summary"]

ConversationNotificationBus(bus.Event, "OnFeatureResponse", conversationId, response, summary) # Conversation ID is usually passed as an argument

The response parameter is the main response to the UI. It will be immediately shown to the user. The summary parameter is a list of strings containing additional information about the response. This can consist of steps taken by the Feature or other significant information the user might want to know.