https://promptfoo.dev/ logo
Multiline text file not being serialized correctly...
# ❓|questions
m
I am referencing the examples found here https://www.promptfoo.dev/docs/configuration/chat/. And am running into an issue where if I specify a text file as a variable with multiple lines I hit an error saying its invalid json. Example to reproduce: config file:
Copy code
prompts: [lmn_chat.json]
providers:
  - "openai:gpt-3.5-turbo"
defaultTest:
  vars:
    system_message: file://prompt.txt
tests:
  - vars:
      response: "Miguel"
    assert: 
      - type: contains
        value: bar
prompt json file
Copy code
[
  {
    "role": "system",
    "content": "{{ system_message }}"
  },
  {% for completion in _conversation %}
    {
      "role": "user",
      "content": "{{ completion.prompt[completion.prompt.length - 1].content }}"
    },
    {
      "role": "assistant",
      "content": "{{ completion.output }}"
    },
  {% endfor %}
  {
    "role": "user",
    "content": "{{ response }}"
  }
]
Example txt file
Copy code
foo 

bar
Error I get is
Copy code
[FAIL] Error: Chat Completion prompt is not a valid JSON       │
│                                                                │ string: SyntaxError: Bad control character in string literal   │
│                                                                │ in JSON at position 47 (line 4 column 20)
Worked around this by writing a python function that escapes backslashes and double quotes, and replace new lines with literal \n
i
try changing it to
Copy code
"content": {{ system_message | dump }}
That will serialize it to json by wrapping it in quotes and escaping newlines, quotes etc
m
Ahh that worked. Tried that originallby but wrapped in quotes
3 Views