Run Agent Spec configurations with WayFlow#

This usage example showcases the creation of a simple Agent Spec Agent, subsequently serialized into JSON and converted into a WayFlow assistant. Also includes mapping of a ServerTool and execution of the conversation.

# Create a Agent Spec agent
from pyagentspec.agent import Agent
from pyagentspec.llms.openaicompatibleconfig import OpenAiCompatibleConfig
from pyagentspec.property import FloatProperty
from pyagentspec.tools import ServerTool

subtraction_tool = ServerTool(
    name="subtraction-tool",
    description="subtract two numbers together",
    inputs=[FloatProperty(title="a"), FloatProperty(title="b")],
    outputs=[FloatProperty(title="difference")],
)

agentspec_llm_config = OpenAiCompatibleConfig(
    name="llama-3.3-70b-instruct",
    model_id="/storage/models/Llama-3.3-70B-Instruct",
    url="url.to.my.llm",
)

agentspec_agent = Agent(
    name="agentspec_tools_test",
    description="agentspec_tools_test",
    llm_config=agentspec_llm_config,
    system_prompt="Perform subtraction with the given tool.",
    tools=[subtraction_tool],
)

# Export the Agent Spec configuration
from pyagentspec.serialization import AgentSpecSerializer

agentspec_config = AgentSpecSerializer().to_json(agentspec_agent)

# Load and run the Agent Spec configuration with WayFlow
from wayflowcore.agentspec import AgentSpecLoader

def subtract(a: float, b: float) -> float:
    return a - b

async def main():
    converter = AgentSpecLoader(tool_registry={"subtraction-tool": subtract})
    assistant = converter.load_json(agentspec_config)
    conversation = assistant.start_conversation()

    while True:
        user_input = input("USER >> ")
        if user_input == "exit":
            break
        conversation.append_user_message(user_input)
        await conversation.execute_async()
        last = conversation.get_last_message()
        print(f"AGENT >> {last.content}")

# anyio.run(main)
# USER >> Compute 987654321-123456789
# AGENT >> The result of the subtraction is 864197532.