Skip to content

Commit b187922

Browse files
jcorteslcaresia
authored andcommitted
[Improvement]: OpenAI Function Calling in Chat completion API (#14561)
1 parent 2200fc5 commit b187922

File tree

8 files changed

+132
-42
lines changed

8 files changed

+132
-42
lines changed

components/openai/actions/analyze-image-content/analyze-image-content.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
key: "openai-analyze-image-content",
99
name: "Analyze Image Content",
1010
description: "Send a message or question about an image and receive a response. [See the documentation](https://platform.openai.com/docs/api-reference/runs/createThreadAndRun)",
11-
version: "0.1.1",
11+
version: "0.1.2",
1212
type: "action",
1313
props: {
1414
openai,

components/openai/actions/chat-with-assistant/chat-with-assistant.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "openai-chat-with-assistant",
77
name: "Chat with Assistant",
88
description: "Sends a message and generates a response, storing the message history for a continuous conversation. [See the documentation](https://platform.openai.com/docs/api-reference/runs/createThreadAndRun)",
9-
version: "0.0.6",
9+
version: "0.0.7",
1010
type: "action",
1111
props: {
1212
openai,

components/openai/actions/chat/chat.mjs

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ConfigurationError } from "@pipedream/platform";
66
export default {
77
...common,
88
name: "Chat",
9-
version: "0.2.2",
9+
version: "0.2.3",
1010
key: "openai-chat",
1111
description: "The Chat API, using the `gpt-3.5-turbo` or `gpt-4` model. [See the documentation](https://platform.openai.com/docs/api-reference/chat)",
1212
type: "action",
@@ -57,19 +57,87 @@ export default {
5757
optional: true,
5858
reloadProps: true,
5959
},
60+
toolTypes: {
61+
type: "string[]",
62+
label: "Tool Types",
63+
description: "The types of tools to enable on the assistant",
64+
options: constants.TOOL_TYPES.filter((toolType) => toolType === "function"),
65+
optional: true,
66+
reloadProps: true,
67+
},
6068
},
6169
additionalProps() {
62-
const { responseFormat } = this;
63-
if (responseFormat !== constants.CHAT_RESPONSE_FORMAT.JSON_SCHEMA.value) {
64-
return {};
65-
}
66-
return {
67-
jsonSchema: {
70+
const {
71+
responseFormat,
72+
toolTypes,
73+
numberOfFunctions,
74+
} = this;
75+
const props = {};
76+
77+
if (responseFormat === constants.CHAT_RESPONSE_FORMAT.JSON_SCHEMA.value) {
78+
props.jsonSchema = {
6879
type: "string",
6980
label: "JSON Schema",
7081
description: "Define the schema that the model's output must adhere to. [See the documentation here](https://platform.openai.com/docs/guides/structured-outputs/supported-schemas).",
71-
},
72-
};
82+
};
83+
}
84+
85+
if (toolTypes?.includes("function")) {
86+
props.numberOfFunctions = {
87+
type: "integer",
88+
label: "Number of Functions",
89+
description: "The number of functions to define",
90+
optional: true,
91+
reloadProps: true,
92+
default: 1,
93+
};
94+
95+
for (let i = 0; i < (numberOfFunctions || 1); i++) {
96+
props[`functionName_${i}`] = {
97+
type: "string",
98+
label: `Function Name ${i + 1}`,
99+
description: "The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.",
100+
};
101+
props[`functionDescription_${i}`] = {
102+
type: "string",
103+
label: `Function Description ${i + 1}`,
104+
description: "A description of what the function does, used by the model to choose when and how to call the function.",
105+
optional: true,
106+
};
107+
props[`functionParameters_${i}`] = {
108+
type: "object",
109+
label: `Function Parameters ${i + 1}`,
110+
description: "The parameters the functions accepts, described as a JSON Schema object. See the [guide](https://platform.openai.com/docs/guides/text-generation/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format.",
111+
optional: true,
112+
};
113+
}
114+
}
115+
116+
return props;
117+
},
118+
methods: {
119+
...common.methods,
120+
_buildTools() {
121+
const tools = this.toolTypes?.filter((toolType) => toolType !== "function")?.map((toolType) => ({
122+
type: toolType,
123+
})) || [];
124+
if (this.toolTypes?.includes("function")) {
125+
const numberOfFunctions = this.numberOfFunctions || 1;
126+
for (let i = 0; i < numberOfFunctions; i++) {
127+
tools.push({
128+
type: "function",
129+
function: {
130+
name: this[`functionName_${i}`],
131+
description: this[`functionDescription_${i}`],
132+
parameters: this[`functionParameters_${i}`],
133+
},
134+
});
135+
}
136+
}
137+
return tools.length
138+
? tools
139+
: undefined;
140+
},
73141
},
74142
async run({ $ }) {
75143
if (this.audio && !this.modelId.includes("gpt-4o-audio-preview")) {
@@ -80,7 +148,10 @@ export default {
80148

81149
const response = await this.openai.createChatCompletion({
82150
$,
83-
data: args,
151+
data: {
152+
...args,
153+
tools: this._buildTools(),
154+
},
84155
});
85156

86157
if (response) {

components/openai/actions/common/common-assistants.mjs

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,20 @@ export default {
1616
if (!this.toolTypes?.length) {
1717
return props;
1818
}
19-
return this.getToolProps();
19+
if (this.toolTypes.includes("function")) {
20+
props.numberOfFunctions = {
21+
type: "integer",
22+
label: "Number of Functions",
23+
description: "The number of functions to define.",
24+
optional: true,
25+
reloadProps: true,
26+
default: 1,
27+
};
28+
}
29+
return {
30+
...props,
31+
...(await this.getToolProps()),
32+
};
2033
},
2134
methods: {
2235
async getToolProps() {
@@ -58,23 +71,26 @@ export default {
5871
};
5972
}
6073
if (this.toolTypes.includes("function")) {
61-
props.functionName = {
62-
type: "string",
63-
label: "Function Name",
64-
description: "The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.",
65-
};
66-
props.functionDescription = {
67-
type: "string",
68-
label: "Function Description",
69-
description: "A description of what the function does, used by the model to choose when and how to call the function.",
70-
optional: true,
71-
};
72-
props.functionParameters = {
73-
type: "object",
74-
label: "Function Parameters",
75-
description: "The parameters the functions accepts, described as a JSON Schema object. See the [guide](https://platform.openai.com/docs/guides/text-generation/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format.",
76-
optional: true,
77-
};
74+
const numberOfFunctions = this.numberOfFunctions || 1;
75+
for (let i = 0; i < numberOfFunctions; i++) {
76+
props[`functionName_${i}`] = {
77+
type: "string",
78+
label: `Function Name ${i + 1}`,
79+
description: "The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.",
80+
};
81+
props[`functionDescription_${i}`] = {
82+
type: "string",
83+
label: `Function Description ${i + 1}`,
84+
description: "A description of what the function does, used by the model to choose when and how to call the function.",
85+
optional: true,
86+
};
87+
props[`functionParameters_${i}`] = {
88+
type: "object",
89+
label: `Function Parameters ${i + 1}`,
90+
description: "The parameters the functions accepts, described as a JSON Schema object. See the [guide](https://platform.openai.com/docs/guides/text-generation/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format.",
91+
optional: true,
92+
};
93+
}
7894
}
7995
return props;
8096
},
@@ -83,14 +99,17 @@ export default {
8399
type: toolType,
84100
})) || [];
85101
if (this.toolTypes?.includes("function")) {
86-
tools.push({
87-
type: "function",
88-
function: {
89-
name: this.functionName,
90-
description: this.functionDescription,
91-
parameters: this.functionParameters,
92-
},
93-
});
102+
const numberOfFunctions = this.numberOfFunctions || 1;
103+
for (let i = 0; i < numberOfFunctions; i++) {
104+
tools.push({
105+
type: "function",
106+
function: {
107+
name: this[`functionName_${i}`],
108+
description: this[`functionDescription_${i}`],
109+
parameters: this[`functionParameters_${i}`],
110+
},
111+
});
112+
}
94113
}
95114
return tools.length
96115
? tools

components/openai/actions/create-assistant/create-assistant.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "openai-create-assistant",
77
name: "Create Assistant",
88
description: "Creates an assistant with a model and instructions. [See the documentation](https://platform.openai.com/docs/api-reference/assistants/createAssistant)",
9-
version: "0.1.8",
9+
version: "0.1.9",
1010
type: "action",
1111
props: {
1212
openai,

components/openai/actions/create-thread/create-thread.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "openai-create-thread",
77
name: "Create Thread (Assistants)",
88
description: "Creates a thread with optional messages and metadata, and optionally runs the thread using the specified assistant. [See the documentation](https://platform.openai.com/docs/api-reference/threads/createThread)",
9-
version: "0.0.10",
9+
version: "0.0.11",
1010
type: "action",
1111
props: {
1212
openai,

components/openai/actions/modify-assistant/modify-assistant.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "openai-modify-assistant",
77
name: "Modify an Assistant",
88
description: "Modifies an existing OpenAI assistant. [See the documentation](https://platform.openai.com/docs/api-reference/assistants/modifyAssistant)",
9-
version: "0.1.8",
9+
version: "0.1.9",
1010
type: "action",
1111
props: {
1212
openai,

components/openai/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/openai",
3-
"version": "0.7.0",
3+
"version": "0.7.1",
44
"description": "Pipedream OpenAI Components",
55
"main": "openai.app.mjs",
66
"keywords": [

0 commit comments

Comments
 (0)