You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

custom_tool_direct.py 2.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """
  2. This example shows how to use custom function tools with a tool-enabled
  3. agent.
  4. """
  5. import asyncio
  6. import os
  7. import random
  8. import sys
  9. from typing import List
  10. from agnext.application import SingleThreadedAgentRuntime
  11. from agnext.components.models import (
  12. SystemMessage,
  13. )
  14. from agnext.components.tool_agent import ToolAgent
  15. from agnext.components.tools import FunctionTool, Tool
  16. from agnext.core import AgentInstantiationContext
  17. from typing_extensions import Annotated
  18. sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__))))
  19. sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
  20. from agnext.core import AgentId
  21. from coding_direct import Message, ToolUseAgent
  22. from common.utils import get_chat_completion_client_from_envs
  23. async def get_stock_price(ticker: str, date: Annotated[str, "The date in YYYY/MM/DD format."]) -> float:
  24. """Get the stock price of a company."""
  25. # This is a placeholder function that returns a random number.
  26. return random.uniform(10, 100)
  27. async def main() -> None:
  28. # Create the runtime.
  29. runtime = SingleThreadedAgentRuntime()
  30. tools: List[Tool] = [
  31. # A tool that gets the stock price.
  32. FunctionTool(
  33. get_stock_price,
  34. description="Get the stock price of a company given the ticker and date.",
  35. name="get_stock_price",
  36. )
  37. ]
  38. # Register agents.
  39. await runtime.register(
  40. "tool_executor_agent",
  41. lambda: ToolAgent(
  42. description="Tool Executor Agent",
  43. tools=tools,
  44. ),
  45. )
  46. await runtime.register(
  47. "tool_enabled_agent",
  48. lambda: ToolUseAgent(
  49. description="Tool Use Agent",
  50. system_messages=[SystemMessage("You are a helpful AI Assistant. Use your tools to solve problems.")],
  51. model_client=get_chat_completion_client_from_envs(model="gpt-4o-mini"),
  52. tool_schema=[tool.schema for tool in tools],
  53. tool_agent=AgentId("tool_executor_agent", AgentInstantiationContext.current_agent_id().key),
  54. ),
  55. )
  56. tool_use_agent = AgentId("tool_enabled_agent", "default")
  57. runtime.start()
  58. # Send a task to the tool user.
  59. response = await runtime.send_message(Message("What is the stock price of NVDA on 2024/06/01"), tool_use_agent)
  60. # Print the result.
  61. assert isinstance(response, Message)
  62. print(response.content)
  63. # Run the runtime until the task is completed.
  64. await runtime.stop()
  65. if __name__ == "__main__":
  66. import logging
  67. logging.basicConfig(level=logging.WARNING)
  68. logging.getLogger("agnext").setLevel(logging.DEBUG)
  69. asyncio.run(main())