# Chat using Functions — OpenAI (ChatGPT)

> Chat with your models and allow them to invoke functions. Optionally, you can build and invoke workflows as functions. See the documentation

- Key: `openai-chat-using-functions`
- Type: Action (Write)
- Version: 0.0.11
- App: OpenAI (ChatGPT) (`openai`) — https://pipedream.com/apps/openai.md
- This page (HTML): https://pipedream.com/apps/openai/actions/chat-using-functions
- Hints: open-world
- Source: https://github.com/PipedreamHQ/pipedream/blob/master/components/openai/actions/chat-using-functions/chat-using-functions.mjs

## Description

Chat with your models and allow them to invoke functions. Optionally, you can build and invoke workflows as functions. [See the documentation](https://platform.openai.com/docs/guides/function-calling)

## Props

| Prop | Type | Required | Description |
|---|---|---|---|
| `modelId` | `string` | Yes | The ID of the model to use for chat completions Options are loaded from the connected account. |
| `input` | `string` | Yes | Text inputs to the model used to generate a response |
| `functions` | `string` | Yes | A valid JSON array of functions using OpenAI's function schema definition. See guide here. |
| `instructions` | `string` | No | Inserts a system (or developer) message as the first item in the model's context |
| `toolChoice` | `string` | No | auto: The model decides whether and how many functions to call. required: The model must call one or more functions. function_name: Enter a custom expression to force the model to call this specific function. |
| `parallelToolCalls` | `boolean` | No | Allow or prevent the model to call multiple functions in a single turn |
| `previousResponseId` | `string` | No | The unique ID of the previous response to the model. Use this to create multi-turn conversations |
| `truncation` | `string` | No | Specifies the truncation mode for the response if it's larger than the context window size |
| `responseFormat` | `string` | No | Text: Returns unstructured text output. JSON Schema: Enables you to define a specific structure for the model's output using a JSON schema. |
| `skipThisStep` | `boolean` | No | Pass in a boolean custom expression to skip this step's execution at runtime |

## Run it

**MCP**

```ts
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
import { PipedreamClient } from "@pipedream/sdk"

const pd = new PipedreamClient({
  projectId: process.env.PIPEDREAM_PROJECT_ID!,
  clientId: process.env.PIPEDREAM_CLIENT_ID!,
  clientSecret: process.env.PIPEDREAM_CLIENT_SECRET!,
  projectEnvironment: "production",
})

const accessToken = await pd.rawAccessToken

const transport = new StreamableHTTPClientTransport(
  new URL("https://remote.mcp.pipedream.net/v3"),
  {
    requestInit: {
      headers: {
        Authorization: `Bearer ${accessToken}`,
        "x-pd-project-id": process.env.PIPEDREAM_PROJECT_ID!,
        "x-pd-environment": "production",
        "x-pd-external-user-id": "{external_user_id}", // any stable ID for this user in your system
        "x-pd-app-slug": "openai",
      },
    },
  },
)

const mcp = new Client({ name: "my-agent", version: "1.0.0" })
await mcp.connect(transport)

const { tools } = await mcp.listTools()

// listTools() hands your model this tool's input schema, so it can
// fill the arguments itself:
const result = await mcp.callTool({
  name: "openai-chat-using-functions",
  arguments: {
    modelId: "Model",
    input: "Chat Input",
  },
})
```

**TypeScript**

```ts
import { PipedreamClient } from "@pipedream/sdk"

const pd = new PipedreamClient({
  projectId: process.env.PIPEDREAM_PROJECT_ID!,
  clientId: process.env.PIPEDREAM_CLIENT_ID!,
  clientSecret: process.env.PIPEDREAM_CLIENT_SECRET!,
  projectEnvironment: "production",
})

const result = await pd.actions.run({
  id: "openai-chat-using-functions",
  externalUserId: "{external_user_id}", // any stable ID for this user in your system
  configuredProps: {
    openai: { authProvisionId: "apn_xxxxxxx" },
    modelId: "Model",
    input: "Chat Input",
  },
})

console.log(result)
```

**cURL**

```bash
curl -X POST https://api.pipedream.com/v1/connect/{project_id}/actions/run \
  -H "Content-Type: application/json" \
  -H "X-PD-Environment: production" \
  -H "Authorization: Bearer {access_token}" \
  -d '{
    "external_user_id": "{external_user_id}",
    "id": "openai-chat-using-functions",
    "configured_props": {
      "openai": { "authProvisionId": "apn_xxxxxxx" },
      "modelId": "Model",
      "input": "Chat Input"
    }
  }'
```

---

- App: https://pipedream.com/apps/openai.md · All apps: https://pipedream.com/apps
