Trulience Docs
Functions

Customisation

Functions

Extending avatar capabilities with custom Python functions

What are Functions?

Functions enable your avatars to interact with external systems and perform real-world actions through function calling - also known as “tool use” or “tool calling”. This powerful LLM capability allows your avatar to intelligently detect when it needs additional information or should trigger specific actions before generating responses.

When a user asks, “What’s the weather like in London?”, your avatar can automatically call your weather function to get current weather forecast data. Similarly, functions can trigger frontend actions like displaying menus, querying user location or integrating with your existing APIs and services.

Adding functions to your avatar enables:

  • Real-time data access - Fetch current information from APIs, databases or external services
  • Dynamic interactions - Trigger UI components, forms or other frontend actions
  • Business integration - Connect to CRMs, inventory systems or custom applications
  • Enhanced capabilities - Perform calculations or complex operations beyond text generation

Function Development

PYTHON FUNCTIONS

Write custom Python functions using our built-in templates or start from scratch. Full async support with access to popular libraries.

SMART DETECTION

The LLM automatically determines when to call your functions based on user intent and your function descriptions.

MODERATED APPROVAL

All functions are reviewed by our moderation team before approval to ensure security and compliance.

REUSABLE

Define a function once, reuse it on multiple avatars. Changes to function code will need to be approved and reattached.

Example Function

Here’s a weather forecast function that demonstrates the key concepts:

import aiohttp
import logging
 
logger=logging.getLogger("weather_function")
 
@tool
async def get_weather(location:str):
	'''
   	Called when the user asks about the weather. This function will return the weather for the given location.
    '''
    logger.info(f"getting weather for {location}")
    url = f"https://wttr.in/{location}?format=%C+%t"
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            if response.status == 200:
                weather_data = await response.text()
                # response from the function call is returned to the LLM
                # as a tool response. The LLM's response will include this data
                return f"The weather in {location} is {weather_data}."
            else:
                raise Exception(f"Failed to get weather data, status code: {response.status}")

Development Best Practices

Follow these guidelines to create robust, efficient functions:

  • Handle errors gracefully: Include proper error handling with meaningful error messages. Failed function calls should provide helpful feedback to users.
  • Keep functions focused: Each function should have a single, clear purpose. Break complex operations into multiple functions rather than creating monolithic ones.
  • Use async when needed: For API calls or I/O operations, use async functions to maintain performance and responsiveness.
  • Log appropriately: Use Python's logging API to log important events and debug information.
  • Return structured data: Return clear, formatted responses that the LLM can easily incorporate into natural language responses.
  • Test thoroughly: Validate your functions with various inputs before submitting for approval.

Common Use Cases

Functions excel in these scenarios:

  • Data Retrieval: Fetch real-time information from APIs, databases or external services like weather data, stock prices or inventory levels.
  • Business Integration: Connect with your existing systems such as CRM platforms, order management or customer support tools.
  • User Interactions: Trigger frontend actions like displaying forms, opening modal overlays or navigating to specific pages.
  • Calculations: Perform complex computations, validations or data processing that goes beyond an LLM’s typical capabilities.
  • Authentication: Verify user credentials, check permissions or manage session states.

Getting Started

Ready to build your first function? Visit the Functions page to get started:

  1. Choose a template → Start with one of our pre-built templates for common use cases
  2. Write your function → Use Python with full async support and popular libraries
  3. Test locally → Validate your function logic and error handling
  4. Submit for approval → Our moderation team will review your function for security and compliance
  5. Add to avatars → Once approved, assign your function to avatars via the Avatar Creator

In the Avatar Creator, navigate to the BRAIN tab, then the FUNCTIONS sub-tab to assign approved functions to your avatars. Your avatars will then intelligently call these functions when appropriate, seamlessly integrating external capabilities into natural conversations.

Functions are shared resources - you can assign the same function to multiple avatars. Note that changes to functions will mark them as ‘unapproved’, requiring subsequent moderator approval and reassignment to your avatars once approved.