> ## Documentation Index
> Fetch the complete documentation index at: https://docs.portkey.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Function Calling

> Portkey's AI Gateway supports function calling capabilities that many foundational model providers offer. In the API call you can describe functions and the model can choose to output text or this function name with parameters.

## Functions Usage

Portkey supports the OpenAI signature to define functions as part of the API request. The `tools` parameter accepts functions which can be sent specifically for models that support function/tool calling.

<Tabs>
  <Tab title="NodeJS">
    ```js theme={"system"}
    import Portkey from 'portkey-ai';

    // Initialize the Portkey client
    const portkey = new Portkey({
        apiKey: "PORTKEY_API_KEY",  // Replace with your Portkey API key
        virtualKey: "VIRTUAL_KEY"   // Add your provider's virtual key
    });

    // Generate a chat completion with streaming
    async function getChatCompletionFunctions(){
      const messages = [{"role": "user", "content": "What's the weather like in Boston today?"}];
      const tools = [
          {
            "type": "function",
            "function": {
              "name": "get_current_weather",
              "description": "Get the current weather in a given location",
              "parameters": {
                "type": "object",
                "properties": {
                  "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA",
                  },
                  "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                },
                "required": ["location"],
              },
            }
          }
      ];

      const response = await portkey.chat.completions.create({
        model: "gpt-3.5-turbo",
        messages: messages,
        tools: tools,
        tool_choice: "auto",
      });

      console.log(response)

    }
    await getChatCompletionFunctions();
    ```
  </Tab>

  <Tab title="Python">
    ```py theme={"system"}
    from portkey_ai import Portkey

    # Initialize the Portkey client
    portkey = Portkey(
        api_key="PORTKEY_API_KEY",  # Replace with your Portkey API key
        virtual_key="VIRTUAL_KEY"   # Add your provider's virtual key
    )

    tools = [
      {
        "type": "function",
        "function": {
          "name": "get_current_weather",
          "description": "Get the current weather in a given location",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA",
              },
              "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
            },
            "required": ["location"],
          },
        }
      }
    ]

    messages = [{"role": "user", "content": "What's the weather like in Boston today?"}]

    completion = portkey.chat.completions.create(
      model="gpt-3.5-turbo",
      messages=messages,
      tools=tools,
      tool_choice="auto"
    )

    print(completion)
    ```
  </Tab>

  <Tab title="OpenAI NodeJS">
    ```js theme={"system"}
    import OpenAI from 'openai'; // We're using the v4 SDK
    import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'

    const openai = new OpenAI({
      apiKey: 'OPENAI_API_KEY', // defaults to process.env["OPENAI_API_KEY"],
      baseURL: PORTKEY_GATEWAY_URL,
      defaultHeaders: createHeaders({
        provider: "openai",
        apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"]
      })
    });

    // Generate a chat completion with streaming
    async function getChatCompletionFunctions(){
      const messages = [{"role": "user", "content": "What's the weather like in Boston today?"}];
      const tools = [
          {
            "type": "function",
            "function": {
              "name": "get_current_weather",
              "description": "Get the current weather in a given location",
              "parameters": {
                "type": "object",
                "properties": {
                  "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA",
                  },
                  "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                },
                "required": ["location"],
              },
            }
          }
      ];

      const response = await openai.chat.completions.create({
        model: "gpt-3.5-turbo",
        messages: messages,
        tools: tools,
        tool_choice: "auto",
      });

      console.log(response)

    }
    await getChatCompletionFunctions();
    ```
  </Tab>

  <Tab title="OpenAI Python">
    ```py theme={"system"}
    from openai import OpenAI
    from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders

    openai = OpenAI(
        api_key='OPENAI_API_KEY',
        base_url=PORTKEY_GATEWAY_URL,
        default_headers=createHeaders(
            provider="openai",
            api_key="PORTKEY_API_KEY"
        )
    )

    tools = [
      {
        "type": "function",
        "function": {
          "name": "get_current_weather",
          "description": "Get the current weather in a given location",
          "parameters": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA",
              },
              "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
            },
            "required": ["location"],
          },
        }
      }
    ]

    messages = [{"role": "user", "content": "What's the weather like in Boston today?"}]

    completion = openai.chat.completions.create(
      model="gpt-3.5-turbo",
      messages=messages,
      tools=tools,
      tool_choice="auto"
    )

    print(completion)
    ```
  </Tab>

  <Tab title="cURL">
    ```sh theme={"system"}
    curl "https://api.portkey.ai/v1/chat/completions" \
      -H "Content-Type: application/json" \
      -H "x-portkey-api-key: $PORTKEY_API_KEY" \
      -H "x-portkey-provider: openai" \
      -H "Authorization: Bearer $OPENAI_API_KEY" \
      -d '{
      "model": "gpt-3.5-turbo",
      "messages": [
        {
          "role": "user",
          "content": "What is the weather like in Boston?"
        }
      ],
      "tools": [
        {
          "type": "function",
          "function": {
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "parameters": {
              "type": "object",
              "properties": {
                "location": {
                  "type": "string",
                  "description": "The city and state, e.g. San Francisco, CA"
                },
                "unit": {
                  "type": "string",
                  "enum": ["celsius", "fahrenheit"]
                }
              },
              "required": ["location"]
            }
          }
        }
      ],
      "tool_choice": "auto"
    }'
    ```
  </Tab>
</Tabs>

### [API Reference](/provider-endpoints/chat)

On completion, the request will get logged in the logs UI where the tools and functions can be viewed. Portkey will automatically format the JSON blocks in the input and output which makes a great debugging experience.

<Frame>
  <img src="https://mintcdn.com/portkey-docs/VWP2Y8zxPP5N4jE6/images/product/ai-gateway/ai-15.webp?fit=max&auto=format&n=VWP2Y8zxPP5N4jE6&q=85&s=0b2b72327452d12a019999d086922d0b" width="2304" height="2202" data-path="images/product/ai-gateway/ai-15.webp" />
</Frame>

## Managing Functions and Tools in Prompts

Portkey's Prompt Library supports creating prompt templates with function/tool definitions, as well as letting you set the `tool choice` param. Portkey will also validate your tool definition on the fly, eliminating syntax errors.

<Frame>
  <img src="https://mintcdn.com/portkey-docs/VWP2Y8zxPP5N4jE6/images/product/ai-gateway/ai-16.avif?fit=max&auto=format&n=VWP2Y8zxPP5N4jE6&q=85&s=7ec1cef509f11c322c3a91c2dd77f3cc" width="700" height="500" data-path="images/product/ai-gateway/ai-16.avif" />
</Frame>

## Supported Providers and Models

The following providers are supported for function calling with more providers getting added soon. Please raise a [request](/integrations/llms/suggest-a-new-integration) or a [PR](https://github.com/Portkey-AI/gateway/pulls) to add model or provider to the AI gateway.

| Provider                                                                               | Models                                                                                                          |
| -------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
| [OpenAI](/integrations/llms/openai)                                                    | gpt-4 series of modelsgpt-3.5-turbo series of models                                                            |
| [Azure OpenAI](/integrations/llms/azure-openai)                                        | gpt-4 series of modelsgpt-3.5-turbo series of models                                                            |
| [Anyscale](/integrations/llms/anyscale-llama2-mistral-zephyr)                          | mistralai/Mistral-7B-Instruct-v0.1 mistralai/Mixtral-8x7B-Instruct-v0.1                                         |
| [Together AI](/integrations/llms/together-ai)                                          | mistralai/Mixtral-8x7B-Instruct-v0.1 mistralai/Mistral-7B-Instruct-v0.1 togethercomputer/CodeLlama-34b-Instruct |
| [Fireworks AI](/integrations/llms/fireworks)                                           | firefunction-v1fw-function-call-34b-v0                                                                          |
| [Google Gemini](/integrations/llms/gemini) / [Vertex AI](/integrations/llms/vertex-ai) | gemini-1.0-progemini-1.0-pro-001gemini-1.5-pro-latest                                                           |

## Cookbook

[**Here's a detailed cookbook on function calling using Portkey.**](/guides/getting-started/function-calling)
