Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

14
Function calling using Claude 3 family of models
Post Flair (click to view more posts with a particular flair)
Post Body

As language models continue to advance, one of the most exciting features to emerge is the ability to call functions within prompts. This capability was first introduced with GPT-3.5 and later expanded upon in GPT-4, allowing for more structured and interactive conversations with these powerful AI systems.

Function calling enables users to define custom functions with specific parameters, which the language model can then understand and execute. This opens up a world of possibilities, from performing calculations and data analysis to automating tasks and generating code.

However, replicating this functionality in open-source models has been a significant challenge. While many OSS models have come close, it has been difficult to fully replicate the function-calling capabilities of GPT-3.5 and GPT-4. This has led to a gap between the commercial and open-source offerings in the language model space.

That is, until now.

With the recent launch of the Claude 3 family of models by Anthropic, it has become quite evident that this family of models is very good at logical reasoning and generating structured data. With the capability of generating structured data, we can now use these models for function calling.

As the field of language models continues to evolve, it will be fascinating to see how function calling and other cutting-edge features are implemented and democratized, bringing us closer to truly intelligent and capable AI systems.

One project worth checking out in this space is claudetools, a Python library that enables function calling with the Claude 3 family of language models. The library is available at https://github.com/vatsalsaglani/claudetools. The following example shows how easy is it to use it and get the right function calls.

```py import asyncio from claudetools.tools.tool import Tool from pydantic import BaseModel, Field from typing import List, Dict import json from configs import ANTHROPIC_API_KEY

create a tool instance with your Anthropic API Key

tool = Tool(ANTHROPIC_API_KEY)

define your function parameters

class AddTodo(BaseModel): text: str = Field(..., description="Text to add for the TODO to remember.")

class MarkCompleted(BaseModel): text: str = Field(..., description="Text of the completed TODO.")

class ReOpen(BaseModel): text: str = Field(..., description="Text of the TODO to reopen.")

specify the functions you want to use

functions = [{ "name": "AddTodo", "description": "Add a TODO with text to remember.", "parameters": AddTodo.model_json_schema() }, { "name": "MarkCompleted", "description": "Get text of the todo mark it complete", "parameters": MarkCompleted.model_json_schema() }, { "name": "ReOpen", "description": "Get text of the todo reopen it.", "parameters": ReOpen.model_json_schema() }]

set up the user messages

user_messages = [{ "role": "user", "content": """I have to pick up my daughter from school. After which I've to do the laundary. And now I need to cook lunch.""" }]

dependency prompt to attach to the main system prompt

DEPENDENCY_PROMPT = """You are a helpful assistant that helps a user with their tasks and todos. The user can add a todos, mark todos as completed, or reopen certain todos. The user can provide multiple actions at once so you've to break those down and call the appropriate functions in the correct sequence."""

call the tool with the required parameters

output = tool(model="claude-3-sonnet-20240229", messages=user_messages, tools=functions, tool_choice=None, multiple_tools=True, attach_system=DEPENDENCY_PROMPT, max_tokens=3000)

if output: print(json.dumps(output, indent=4)) else: print("Unable to find a function!")

output

[

{

"name": "AddTodo",

"parameters": {

"text": "Pick up daughter from school"

}

},

{

"name": "AddTodo",

"parameters": {

"text": "Do laundry"

}

},

{

"name": "AddTodo",

"parameters": {

"text": "Cook lunch"

}

}

]

```

Author
Account Strength
90%
Account Age
6 years
Verified Email
Yes
Verified Flair
No
Total Karma
1,206
Link Karma
1,068
Comment Karma
105
Profile updated: 4 days ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
8 months ago