Use a dynamic list as prompts in OpenAI API

Describe the issue/error/question

I have a dynamic list of messages that I have to enter in the Open AI Chat API (either GPT 3.5 or GPT 4). For example:

[
  { "role": "system", "content": "You are a helpful assistant." },
  { "role": "user", "content": "Who won the soccer world cup in 2018?" },
  {
    "role": "assistant",
    "content": "The 2018 FIFA World Cup was won by the French national football team."
  },
  { "role": "user", "content": "Where was it played?" }
]

The number of messages is not fixed, it may vary over time.

Is there a way to add it using an expression?

Screenshot 2023-03-21 at 21.47.26

Information on your n8n setup

  • n8n version: 0.220
  • Database you’re using (default: SQLite): MySQL
  • Running n8n with the execution process [own(default), main]: main
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: npm

I guess I can do this, but is there any other way?

That approach doesn’t seem to work when there are fewer fields in the list:

Hi @LuisHerranz, if the number of items in your array field can change it would make sense to split them up into individual items and then have the OpenAI node run for each of them. The “splitting up” part can be done using the Item Lists node.

Assuming your field is called data, the below workflow would do the job:

This way it doesn’t matter how many elements your array has as the node will process each one individually:

Hope this helps!

Thanks for your answer, @MutedJam.

This way it doesn’t matter how many elements your array has as the node will process each one individually

The problem is that I don’t want to process them individually, they need to be sent in the same request.

So there’s no way to do that?

Hey @LuisHerranz,

With the way it works you would need to know how many fields you are sending, As a possible feature request for the future we could look at allowing raw json data so it could be dynamic.

Thanks. I’ll start a feature request for that.

Done! Allow to use a dynamic list as user/assistant prompts for chats in OpenAI

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.