firebase / genkit

An open source framework for building AI-powered apps with familiar code-centric patterns. Genkit makes it easy to integrate, test, and deploy sophisticated AI features to Firebase or Google Cloud.
Apache License 2.0
393 stars 47 forks source link

[JS] Input array of objects not unwrapped in Dotprompt request #207

Closed traxes9k closed 1 month ago

traxes9k commented 1 month ago

Describe the bug Input array not unwrapped in Dotprompt request.

To Reproduce [dotprompt]

model: vertexai/gemini-1.5-pro-preview
config:
  temperature: 0.4
  topK: 32
  topP: 0.95
input:
  schema:
    user_input: string
    ai_backstory: string
    previous_choices(array): 
      choice_description: string
      consequence: string
{{role "user"}}
{
  "user_input": "{{user_input}}",
  "ai_backstory": "{{ai_backstory}}",
  "previous_choices":{{previous_choices}}
}

[INPUT]

{
  "user_input": "bla",
  "ai_backstory": "bla",
  "previous_choices": [
    {
      "choice_description": "bla",
      "consequence": "bla"
    },
    {
      "choice_description": "bla",
      "consequence": "bla"
    },
    {
      "choice_description": "bla",
      "consequence": "bla"
    }
  ]
}

[REQUEST TRACE]

"role": "user",
"content": [
  {
    "text": "\n{\n  \"user_input\": \"bla\",\n  \"ai_backstory\": \"bla\",\n  \"previous_choices\":\[object Object],[object Object],[object Object]\\n}\n"
  }
]

Expected behavior Would expect the previous_choices array to be unpacked and sent with the request. This feature works for unwrapping outputs.

Screenshots Screenshot 2024-05-21 at 20 46 46

Runtime (please complete the following information):

** Node version v21.7.2

MichaelDoyle commented 1 month ago

Hey, thanks for reporting! Dotprompt files are essentially handlebars templates. You can look into the #each helper for how to unpack an array. https://handlebarsjs.com/guide/builtin-helpers.html#each. You're looking for JSON here, but it could be that you want something else, csv, ordered list, and so on. So how you define the template is really up to you. Let us know if you have more questions.

traxes9k commented 1 month ago

Thanks @MichaelDoyle ,

As per your excellent suggestion made the following changes to the prompt :

{{role "user"}}
{
  "user_input": "{{user_input}}",
  "ai_backstory": "{{ai_backstory}}",
  "previous_choices":[{{#each previous_choices}}
    {
      "choice_description":{{this.choice_description}}
      "consequence":{{this.consequence}}
    }
  {{/each}}]
}

Maybe this helps somebody else and thanks for your time.