Skip to content

[ACTION] OpenAI Responses API #17475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
export default {
...common,
name: "Chat using File Search",
version: "0.0.5",
version: "0.0.6",
key: "openai-chat-using-file-search",
description: "Chat with your files knowledge base (vector stores). [See the documentation](https://platform.openai.com/docs/guides/tools-file-search)",
type: "action",
props: {
openai,
alert: {

Check warning on line 14 in components/openai/actions/chat-using-file-search/chat-using-file-search.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop alert must have a description. See https://pipedream.com/docs/components/guidelines/#props

Check warning on line 14 in components/openai/actions/chat-using-file-search/chat-using-file-search.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop alert must have a label. See https://pipedream.com/docs/components/guidelines/#props
type: "alert",
alertType: "info",
content: "To use this action, you need to have set up a knowledge base in a vector store and uploaded files to it. [More infomation here](https://platform.openai.com/docs/guides/tools-file-search?lang=javascript#overview).",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
export default {
...common,
name: "Chat using Functions",
version: "0.0.6",
version: "0.0.7",
key: "openai-chat-using-functions",
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)",
type: "action",
props: {
openai,
alert: {

Check warning on line 14 in components/openai/actions/chat-using-functions/chat-using-functions.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop alert must have a description. See https://pipedream.com/docs/components/guidelines/#props

Check warning on line 14 in components/openai/actions/chat-using-functions/chat-using-functions.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop alert must have a label. See https://pipedream.com/docs/components/guidelines/#props
type: "alert",
alertType: "info",
content: "Provide function names and parameters, and the model will either answer the question directly or decide to invoke one of the functions, returning a function call that adheres to your specified schema. Add a custom code step that includes all available functions which can be invoked based on the model's response - [you can even build an entire workflow as a function](https://pipedream.com/docs/workflows/building-workflows/code/nodejs/#invoke-another-workflow)! Once the appropriate function or workflow is executed, continue the overall execution or pass the result back to the model for further analysis. For more details, [see this guide](https://platform.openai.com/docs/guides/function-calling?api-mode=responses#overview) and this [walkthrough](https://pipedream.com/blog/introducing-enhanced-openai-chat-actions-for-pipedream/#using-pipedream-workflows-as-functions).",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import constants from "../../common/constants.mjs";
export default {
...common,
name: "Chat using Web Search",
version: "0.0.5",
version: "0.0.6",
key: "openai-chat-using-web-search",
description: "Chat using the web search tool. [See the documentation](https://platform.openai.com/docs/guides/tools-web-search)",
type: "action",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import openai from "../../openai.app.mjs";
import common from "../common/common.mjs";
import constants from "../../common/constants.mjs";
import { parseArray } from "../../common/helpers.mjs";

export default {
...common,
key: "openai-chat-with-responses-api",
name: "Chat With Responses API",
version: "0.0.1",
description: "Send a chat via the Responses API, mixing built-in tools and MCP server tools. [See the documentation](https://platform.openai.com/docs/guides/tools?api-mode=responses).",
type: "action",
props: {
openai,
modelId: {
description: "Model used to generate the response",
propDefinition: [
openai,
"chatCompletionModelId",
],
},
input: {
description: "Text input to the model used to generate a response",
propDefinition: [
openai,
"input",
],
},
instructions: {
description: "Inserts a system (or developer) message as the first item in the model's context",
propDefinition: [
openai,
"instructions",
],
},
previousResponseId: {
type: "string",
label: "Previous Response ID",
description: "The unique ID of the previous response to the model. Use this to create multi-turn conversations",
optional: true,
},
truncation: {
type: "string",
label: "Truncation",
description:
"Specifies the truncation mode for the response if it exceeds the context window",
default: "auto",
options: [
"auto",
"disabled",
],
optional: true,
},
responseFormat: {
type: "string",
label: "Response Format",
description: "- `text`: Returns unstructured text output.\n- `json_schema`: Enforces a specific structure using a JSON schema.",
options: [
"text",
"json_schema",
],
default: "text",
optional: true,
reloadProps: true,
},
builtInTools: {
type: "string[]",
label: "Built-In Tools",
description: "Which of OpenAI's first-party tools to enable (web search, file search, code interpreter).",
options: [
{
label: "Web Search",
value: "web_search_preview",
},
{
label: "File Search",
value: "file_search",
},
{
label: "Code Interpreter",
value: "code_interpreter",
},
],
default: [],
},
otherTools: {
type: "string[]",
label: "Other Tools",
description: "Other tools to include in the chat. [See the documentation](https://platform.openai.com/docs/guides/tools-remote-mcp). Example: `{ type: \"mcp\", server_label: \"gmail\", server_url: \"https://remote.mcp.pipedream.net\", headers: {}, require_approval: \"never\" }`",
optional: true,
},
},
additionalProps() {
const props = {};
if (this.responseFormat === constants.CHAT_RESPONSE_FORMAT.JSON_SCHEMA.value) {
props.jsonSchema = {
type: "string",
label: "JSON Schema",
description:
"Define the schema that the model's output must adhere to.",
};
}
return props;
},
async run({ $ }) {
const {
builtInTools,
otherTools,
modelId,
input,
instructions,
previousResponseId,
truncation,
responseFormat,
jsonSchema,
} = this;

const tools = builtInTools.map((tool) => ({
type: tool,
}));

if (otherTools) {
tools.push(...parseArray(otherTools));
}

const data = {
model: modelId,
input,
instructions,
previous_response_id: previousResponseId,
truncation,
tools,
};

if (responseFormat === constants.CHAT_RESPONSE_FORMAT.JSON_SCHEMA.value) {
try {
data.text = {
format: {
type: responseFormat,
...JSON.parse(jsonSchema),
},
};
} catch {
throw new Error("Invalid JSON format in the provided JSON Schema");
}
}

const response = await this.openai.responses({
$,
data,
debug: true,
});

if (response) {
$.export("$summary", `Successfully sent chat to OpenAI Responses API with ID \`${response.id}\`.`);
$.export("chat_responses", response.output);
}

return response;
},
};
2 changes: 1 addition & 1 deletion components/openai/actions/chat/chat.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
export default {
...common,
name: "Chat",
version: "0.3.1",
version: "0.3.2",
key: "openai-chat",
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)",
type: "action",
props: {
openai,
alert: {

Check warning on line 14 in components/openai/actions/chat/chat.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop alert must have a description. See https://pipedream.com/docs/components/guidelines/#props

Check warning on line 14 in components/openai/actions/chat/chat.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop alert must have a label. See https://pipedream.com/docs/components/guidelines/#props
type: "alert",
alertType: "info",
content: "Looking to chat with your tools? Check out our individual actions: [Chat using Web Search](https://pipedream.com/apps/openai/actions/chat-using-web-search), [Chat using File Search](https://pipedream.com/apps/openai/actions/chat-using-file-search), and [Chat using Functions](https://pipedream.com/apps/openai/actions/chat-using-functions).",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
export default {
...common,
name: "Classify Items into Categories",
version: "0.1.7",
version: "0.1.8",
key: "openai-classify-items-into-categories",
description: "Classify items into specific categories using the Chat API. [See the documentation](https://platform.openai.com/docs/api-reference/chat)",
type: "action",
props: {
...common.props,
info: {

Check warning on line 12 in components/openai/actions/classify-items-into-categories/classify-items-into-categories.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop info must have a description. See https://pipedream.com/docs/components/guidelines/#props

Check warning on line 12 in components/openai/actions/classify-items-into-categories/classify-items-into-categories.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop info must have a label. See https://pipedream.com/docs/components/guidelines/#props
type: "alert",
alertType: "info",
content: `Provide a list of **items** and a list of **categories**. The output will contain an array of objects, each with properties \`item\` and \`category\`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import common from "../common/common.mjs";

export default {
name: "Create Embeddings",
version: "0.0.19",
version: "0.0.20",
key: "openai-create-embeddings",
description: "Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms. [See the documentation](https://platform.openai.com/docs/api-reference/embeddings)",
type: "action",
Expand Down
2 changes: 1 addition & 1 deletion components/openai/actions/send-prompt/send-prompt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
export default {
...common,
name: "Create Completion (Send Prompt)",
version: "0.1.18",
version: "0.1.19",
key: "openai-send-prompt",
description: "OpenAI recommends using the **Chat** action for the latest `gpt-3.5-turbo` API, since it's faster and 10x cheaper. This action creates a completion for the provided prompt and parameters using the older `/completions` API. [See the documentation](https://beta.openai.com/docs/api-reference/completions/create)",
type: "action",
props: {
openai,
alert: {

Check warning on line 13 in components/openai/actions/send-prompt/send-prompt.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop alert must have a description. See https://pipedream.com/docs/components/guidelines/#props

Check warning on line 13 in components/openai/actions/send-prompt/send-prompt.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop alert must have a label. See https://pipedream.com/docs/components/guidelines/#props
type: "alert",
alertType: "warning",
content: "We recommend using the Pipedream **Chat** action instead of this one. It supports the latest `gpt-3.5-turbo` API, which is faster and 10x cheaper. This action, **Create Completion (Send Prompt)**, creates a completion for the provided prompt and parameters using the older `/completions` API.",
Expand Down
2 changes: 1 addition & 1 deletion components/openai/actions/summarize/summarize.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import constants from "../../common/constants.mjs";
export default {
...common,
name: "Summarize Text",
version: "0.1.7",
version: "0.1.8",
key: "openai-summarize",
description: "Summarizes text using the Chat API. [See the documentation](https://platform.openai.com/docs/api-reference/chat)",
type: "action",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const langOptions = lang.LANGUAGES.map((l) => ({
export default {
...common,
name: "Translate Text (Whisper)",
version: "0.1.7",
version: "0.1.8",
key: "openai-translate-text",
description: "Translate text from one language to another using the Chat API. [See the documentation](https://platform.openai.com/docs/api-reference/chat)",
type: "action",
Expand Down
47 changes: 47 additions & 0 deletions components/openai/common/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,50 @@ export function parse(value) {
throw new ConfigurationError("Make sure the schema contains a valid JSON object.");
}
}

const parseJson = (input, maxDepth = 100) => {
const seen = new WeakSet();
const parse = (value) => {
if (maxDepth <= 0) {
return value;
}
if (typeof(value) === "string") {
// Only parse if the string looks like a JSON object or array
const trimmed = value.trim();
if (
(trimmed.startsWith("{") && trimmed.endsWith("}")) ||
(trimmed.startsWith("[") && trimmed.endsWith("]"))
) {
try {
return parseJson(JSON.parse(value), maxDepth - 1);
} catch (e) {
return value;
}
}
return value;
} else if (typeof(value) === "object" && value !== null) {
if (seen.has(value)) {
return value;
}
seen.add(value);
return Object.entries(value)
.reduce((acc, [
key,
val,
]) => Object.assign(acc, {
[key]: parse(val),
}), {});
}
return value;
};

return parse(input);
};

export function parseArray (input, maxDepth = 100) {
if (!Array.isArray(input)) {
return input;
}

return input.map((item) => parseJson(item, maxDepth));
}
4 changes: 2 additions & 2 deletions components/openai/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/openai",
"version": "1.0.2",
"version": "1.1.0",
"description": "Pipedream OpenAI Components",
"main": "openai.app.mjs",
"keywords": [
Expand All @@ -15,8 +15,8 @@
},
"dependencies": {
"@pipedream/platform": "^3.1.0",
"@pipedream/sdk": "^1.7.0",
"@pipedream/types": "^0.1.4",
"axios": "^1.6.2",
"bottleneck": "^2.19.5",
"form-data": "^4.0.0",
"got": "^12.6.0",
Expand Down
Loading
Loading