DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  • Reinforcement Learning for AI Agent Development: Implementing Multi-Agent Systems
  • Why GenAI Apps Could Fail Without Agentic Frameworks
  • Why Clean Data Is the Foundation of Successful AI Systems

Trending

  • Performance Optimization Techniques for Snowflake on AWS
  • How to Format Articles for DZone
  • Unlocking the Benefits of a Private API in AWS API Gateway
  • Docker Base Images Demystified: A Practical Guide
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Function Calling and Agents in Agentic AI

Function Calling and Agents in Agentic AI

Function calling in agentic AI allows autonomous agents to dynamically execute tasks by invoking specific functions based on reasoning and goals.

By 
Bhala Ranganathan user avatar
Bhala Ranganathan
DZone Core CORE ·
Apr. 01, 25 · Analysis
Likes (0)
Comment
Save
Tweet
Share
1.5K Views

Join the DZone community and get the full member experience.

Join For Free

One of the most exciting innovations in the rapidly advancing field of AI is the development of agentic AI, a new paradigm that focuses on creating intelligent systems capable of performing autonomous tasks with humanlike reasoning and actions. 

Among the key concepts driving this field are function calling and the development of AI agents. These elements are paving the way for more sophisticated AI systems that learn from data and actively perform tasks, solve problems, and interact with humans in meaningful ways.

Function Calling in AI

Function calling refers to the ability of an AI system to invoke predefined functions or subroutines to perform specific tasks or calculations. In traditional coding, function calls allow programs to reuse code, modularize complex systems, and achieve a high degree of flexibility. In the context of AI, function calling becomes particularly powerful when integrated with decision-making processes.

Role of Function Calling in Agentic AI

In agentic AI, function calling is not merely about executing static code but involves leveraging the system's internal knowledge and reasoning capabilities to decide when, how, and which function to call. This dynamic decision-making process enables AI agents to adapt to new tasks, learn from their experiences, and even develop new capabilities through interaction with their environment.

Function calling plays a pivotal role in the autonomy of AI agents. By allowing the agent to invoke specific functions based on its reasoning and goals, function calling enables the agent to perform tasks that range from simple actions, such as sending an email or querying a database, to complex sequences of operations, such as navigating a physical environment, coordinating with other agents, or performing multi-step decision-making.

Function Calls and Agents in Action

AutoGen is a programming framework for building multi-agent applications. Below is an example of how AI agents can be built in just a few steps with the help of a small language model and function-calling capabilities.

Prerequisites

  1. Install Ollama.
  2. Install Qwen. Run the following command:
    Shell
     
    ollama run qwen2.5:0.5b
  3. Install Autogen Agent Framework. Use the pip command:
    Shell
     
    pip install "autogen-agentchat” “autogen-ext[openai]"

Steps

Check if Qwen2 is running.

PowerShell
 
PS F:\code> ollama run qwen2.5:0.5b
pulling manifest
pulling c5396e06af29... 100% ▕████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏ 397 MB
pulling 66b9ea09bd5b... 100% ▕████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏   68 B
pulling eb4402837c78... 100% ▕████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏ 1.5 KB
pulling 832dd9e00a68... 100% ▕████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏  11 KB
pulling 005f95c74751... 100% ▕████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏  490 B
verifying sha256 digest
writing manifest
success


Check if the model is listed.

PowerShell
 
PS F:\code\agent> ollama ls
NAME                           ID              SIZE      MODIFIED
qwen2.5:0.5b                   a8b0c5157701    397 MB    57 minutes ago


Create two simple functions: sum and diff.

Python
 
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.messages import TextMessage
from autogen_core import CancellationToken
from autogen_ext.models.openai import OpenAIChatCompletionClient

async def sum(a: int, b: int) -> str:
    return f"Sum is {a+b}"

async def diff(a: int, b: int) -> str:
    return f"Diff is {a-b}"


Create a model client and agent that uses the Qwen model and can talk to ollama via the base_url.

Python
 
model_client = OpenAIChatCompletionClient(
    model="qwen2.5:0.5b",    
    base_url= "http://localhost:11434/v1", 
    api_key="placeholder",   
    model_info={
        "vision": False,
        "function_calling": True,
        "json_output": False,
        "family": "unknown",
    },
)
agent = AssistantAgent(
    name="assistant",
    model_client=model_client,
    tools=[sum, diff],
    system_message="Use tools to solve tasks.",
)


Add a simple test function that uses function calls. The prompt given is to calculate sum and diff of 2 and 3.

Python
 
async def assistant_run() -> None:
    response = await agent.on_messages(
        [TextMessage(content="Calculate sum and diff of two numbers 2 and 3?", source="user")],        
        cancellation_token=CancellationToken(),
    )
    print(response.inner_messages)
    print("----------------------")
    print(response.chat_message)    

asyncio.run(assistant_run())


Run the Python code. 

PowerShell
 
PS F:\code\agent> python .\agentchat.py
[ToolCallRequestEvent(source='assistant', models_usage=RequestUsage(prompt_tokens=213, completion_tokens=50), content=[FunctionCall(id='call_a7l6q2pc', arguments='{"a":2,"b":3}', name='sum'), FunctionCall(id='call_xsod2bd5', arguments='{"a":2,"b":3}', name='diff')], type='ToolCallRequestEvent'), ToolCallExecutionEvent(source='assistant', models_usage=None, content=[FunctionExecutionResult(content='Sum is 5', call_id='call_a7l6q2pc'), FunctionExecutionResult(content='Diff is -1', call_id='call_xsod2bd5')], type='ToolCallExecutionEvent')]
----------------------
source='assistant' models_usage=None content='Sum is 5\nDiff is -1' type='ToolCallSummaryMessage'


As we can see, the predefined functions sum and diff are used to generate output Sum is 5\nDiff is -1.

PowerShell
 
FunctionCall(id='call_a7l6q2pc', arguments='{"a":2,"b":3}', name='sum'),
FunctionCall(id='call_xsod2bd5', arguments='{"a":2,"b":3}', name='diff')


Conclusion

Function calling and agents represent two core components of Agentic AI. By combining the flexibility of dynamic function invocation with the autonomy of intelligent decision-making, these systems have the potential to revolutionize industries and transform the way humans interact with technology. 

As AI advances, the development of such intelligent agents will open up new possibilities for automation, collaboration, and problem-solving, making the world of agentic AI one of the most exciting frontiers of modern technology.

AI systems agentic AI

Opinions expressed by DZone contributors are their own.

Related

  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  • Reinforcement Learning for AI Agent Development: Implementing Multi-Agent Systems
  • Why GenAI Apps Could Fail Without Agentic Frameworks
  • Why Clean Data Is the Foundation of Successful AI Systems

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: