How to Use Pipedream to Create a Workflow with OpenAI API for Each Element in an Array Using Python?

This topic was automatically generated from Slack. You can find the original thread here.

Hi there. I am new to Pipedream. I am trying to create a workflow that receives a webhook payload which contains an array. I want to iterate over the elements of the list and call the OpenAI API for each element. The goal is to return an enriched array containing the same number of elements as in the input array but with additional elements obtained from the AI response. What is the recommended pattern to do this? I am using Python (my javascript is super limited). Thanks

You can use a Python code step, and loop over your array.

And you might want to pass your OpenAI app as a prop to your code step.

: How to pass an OpenAI app as a prop in a code step.

To pass an OpenAI app as a prop in a Pipedream code step, you’ll define it within the props object of your component. Here’s an example that demonstrates how to do this: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { openai: { type: "app", app: "openai", }, }, async run({ steps, $ }) { return await axios($, { url: ``, headers: { Authorization: `Bearer ${this.openai.$auth.api_key}`, }, }); }, }); In this example, openai is defined as a prop of type app, which allows you to connect your OpenAI (ChatGPT) account to the step. This connection authorizes requests to the OpenAI (ChatGPT) API by using the API key associated with your OpenAI account. The axios call within the run method demonstrates how to make a GET request to the OpenAI API, utilizing the API key for authorization.

: This should be all you need! :point_up_2:

: Could you show me how to do that in a python code step?

Based on your request, it seems you’re looking to understand how to use Python in a Pipedream code step. Here’s a basic structure for a Python code step in Pipedream, which demonstrates how to reference data from previous steps and return data for use in future steps: def handler(pd: "pipedream"): # Reference data from previous steps print(pd.steps["trigger"]["context"]["id"]) # Return data for use in future steps return {"foo": {"test": True}} This example shows the fundamental components of a Python code step in Pipedream: - Accessing previous step data: pd.steps["trigger"]["context"]["id"] demonstrates how to access data from the trigger step or any previous step in your workflow. - Returning data: The return statement at the end of the function allows you to pass data to subsequent steps in your workflow. In this example, we’re returning a dictionary with a nested dictionary as its value. Remember, you can perform more complex operations within your Python code step, including making authenticated API requests, accessing Data Stores, and installing third-party packages from PyPI. For more detailed information and examples, please refer to the Pipedream Python documentation.

: How to pass an OpenAI app as a prop in a python code step.

Currently, Pipedream workflows support Python code steps, but the ability to pass apps like OpenAI as a prop directly within a Python code step is not supported in the same way it is with Node.js. The integration of apps through the props system and the direct use of $auth tokens within the code is a feature specific to Node.js components in Pipedream. For Python code steps, you would typically interact with external APIs by making HTTP requests using Python’s HTTP client libraries, such as requests or httpx, and manually handling the authentication by passing the necessary API keys or tokens as part of the request headers or parameters. This means you would need to securely manage your API keys, possibly using environment variables or Pipedream’s built-in secrets manager to store and reference these keys in your Python code. If your use case requires integrating OpenAI with Python in a Pipedream workflow, you would do this by making HTTP requests to the OpenAI API, including the necessary authentication headers. Here’s a conceptual example using the requests library (note that this is a hypothetical example for illustrative purposes): import requests def handler(pd: "pipedream"): api_key = "your_openai_api_key_here" # Consider securely storing this value headers = { "Authorization": f"Bearer {api_key}" } response = requests.get("", headers=headers) return response.json() In this example, replace "your_openai_api_key_here" with your actual OpenAI API key. Remember, hardcoding your API keys directly in your code, especially in shared or public environments, is not recommended due to security concerns. Use Pipedream’s environment variables or secrets manager to securely store and reference sensitive values like API keys. For specific functionality or further assistance, consider reaching out to Pipedream’s support or community forums for more tailored guidance.

Well… I guess you could use an env var for your API key.

Thanks. Will give it a try