Function Calling

Note: This feature requires SDK version >= 3.2.0

Function calling allows users to connect our latest models to external tools, streamlining agentic use cases, and allowing users to integrate our models much more deeply into their workflows. Some examples of tools that users could pass to our model are:

  • Calculator: Connect our models to a calculator to eliminate all hallucinations, especially if working with complex computations.
  • Internal Databases Connect our models to your internal tools/databases to allow customers to ask questions about inventory and product availability.
  • REST APIs: Connect our models to any REST API, allowing our models to access to the particular real-time information that you need to drive your application-specific use case.

Models That Support Function Calling

Currently, only Reka Flash supports function calling. We are working hard to quickly enable support for our other classes of models.

Step By Step Example of How to Leverage Function Calling

Function Calling Workflow

In this section, we will go through step by step how to use Reka’s function calling capabilities with a concrete example. For this example, we will define two functions: get_product_availability, and get_similar_products. Both take in a product id, and get_similar_products additionally takes in the maximum number of results to return, allowing customers to ask questions about various products.

Step 1: Define The Tools

First, we must create a specification for each function. We pass these specs to the model to connect them to the tools.

1tools = [
2 {
3 "name": "get_product_availability",
4 "description": "Determine whether or not a product is currently in stock given a product id.",
5 "parameters": {
6 "type": "object",
7 "properties": {
8 "product_id": {
9 "type": "string",
10 "description": "The unique product id to retrieve availability information for"
11 },
12 },
13 "required": ["product_id"]
14 }
15 },
16 {
17 "name": "get_similar_products",
18 "description": "Get a list of similar products to a given product id.",
19 "parameters": {
20 "type": "object",
21 "properties": {
22 "product_id": {
23 "type": "string",
24 "description": "The unique product id to retrieve similar products for",
25 },
26 "max_results": {
27 "type": "int",
28 "description": "The maximum number of similar products to return",
29 "default": 3,
30 },
31 },
32 "required": ["product_id"]
33 }
34 }
35]

Step 2: Pass the Tools Along With a User Query to the Model

Now that we have defined the tools our model has access to, we can query the model by passing in these tools along with a user query. Assume we have the following user query:

1user_query = "Is product a-12345 in stock right now?"

We can then query the model using the usual client.chat.create:

1from reka.client import Reka
2
3# You can also set the API key using the REKA_API_KEY environment variable.
4client = Reka(api_key="YOUR_API_KEY")
5
6messages = [
7 {
8 "content": user_query,
9 "role": "user",
10 },
11]
12
13response = client.chat.create(
14 messages=messages,
15 tool_choice="auto",
16 tools=tools,
17 model="reka-flash",
18)
19tool_call = response.responses[0].message.tool_calls[0]
20print(tool_call)

This will print a response like:

1{
2 "id": "c25b10ed-22b6-4dee-8c22-48844d60d5e8",
3 "name": "get_product_availability",
4 "parameters": {
5 "product_id": "a-12345"
6 }
7}

Tool Choice

The tool_choice parameter controls how the model uses the tools you pass. There are three available options:

  • auto lets the model decide whether or not to invoke a tool. This is the recommended way to do function calling with our models
  • none disables tool calling. In this case, even if you pass tools to the model, the model will not invoke any tools
  • tool forces the model to invoke one or more of the tools it has been passed

Model Response Type

Tool Choice Flowchart

Note that if tool_choice is set to auto, the model will decide whether or not to invoke a function. If the model chooses to invoke a function, the invocations can be extracted according to the code snippet above. In this case, response.responses[0].message.content will be None. In the case that the model refrains from making a function call, the converse will be true: tool_calls will be None and content will contain the model’s natural language response.

Step 3: Execute the Function

In the case the model decides to invoke a tool, the next step is to execute the function to obtain the output from the function invocation.

1def get_product_availability(product_id: str):
2 """This function will actually check the availability of the product
3 (by querying a database, for instance)"""
4
5 return "AVAILABLE" # Mock response for the purposes of this example
6
7product_id = tool_call.parameters.get("product_id")
8tool_output = get_product_availability(product_id)

Step 4: Pass the Result Back to the Model

Finally, we can pass the result back to the model in order to receive a natural language response based on the result of the function execution.

1messages.extend([
2 {
3 "role": "assistant",
4 "tool_calls": [tool_call]
5 },
6 {
7 "role": "tool_output",
8 "content": [{
9 "tool_call_id": tool_call.id,
10 "output": json.dumps({"status": tool_output})
11 }]
12 },
13])
14
15response = client.chat.create(
16 messages=messages,
17 model="reka-flash"
18)
19
20print(response.responses[0].message.content)

This will print a response like

Yes, product a-12345 is currently available.