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?
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
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:
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.