I have a function tool that returns json string. l...
# ask-ai
w
I have a function tool that returns json string. llm is expected to invoke this tool and consume the response to be used when interacting with user. while the tool gets invoked, llm is unable to consume the response and gives error. give me an example of how to do achieve this
t
Thanks for reaching out, Sunil. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To ensure an LLM can invoke a function tool that returns a JSON string and then consume the response, follow this structured approach: • Tool Implementation: Your function tool should return a Python dictionary or list, which will be automatically converted to a JSON string for the LLM to consume. • Response Handling: The system converts the tool's return value to a string. For complex types (like dict or list), it uses
json.dumps()
to serialize the response. If the response is a simple type (str, int, float, bool), it is converted to a string directly. • *Example Tool:*`@llm.ai_callable() async def get_weather(self, location: str): # ... fetch weather data ... return {"weather": "sunny", "temperature_f": 70}` • LLM Consumption: The LLM receives the JSON string and can use it in its reply to the user. Common Errors and Solutions: • JSON Parsing Errors: Ensure the tool returns serializable data. If the tool returns a non-serializable object, use
json.dumps()
or convert it to a string. • Tool Execution Errors: Handle exceptions within the tool and return error messages as strings if needed. • Response Format Issues: Always return a dictionary, list, or string. Avoid returning custom objects unless they are serializable. By following these practices, the LLM will be able to invoke your function tool, receive the JSON response, and use it in user interactions without errors. Sources: Tool definition and use | LiveKit Docs | Function calling with Voice Agents | LiveKit Docs | mcp_client/util.py | common/chat.cpp
w
how to frame the prompt to use json response from tool to communicate to user?
t
Hey there! Let me share some friendly tips on how to help an LLM communicate smoothly using tool responses. Think of it like giving clear directions to a helpful buddy. • Be crystal clear about the format: Just tell the LLM exactly how to turn that JSON into a warm, human-friendly message. • Show a real-life example: It's like showing a friend how to translate tech-speak into something anyone can understand. • Walk through the response process: Guide the LLM on picking out the important bits from the JSON and presenting them in a way that makes sense. Example prompt:
Copy code
You've got this awesome tool that gives you a JSON response. When it comes in, your job is to take those details and turn them into a friendly, easy-to-understand message for the user. Like if the tool tells you {"weather": "sunny", "temperature_f": 70}, you'd say: "Great news! It's a beautiful sunny day with a perfect 70°F temperature."
Just remember: be specific about the JSON structure, give a clear example, and show the LLM how to make those tool responses feel personal and helpful. Sources: models/templates/NousResearch-Hermes-3-Llama-3.1-8B-tool_use.jinja | Tool definition and use | LiveKit Docs