https://www.boundaryml.com logo
🟑 Including PDFs and images conditionally in chat...
# questions
a
How can I add the pdf/image to the prompt only if they are present?
Copy code
class ChatMessage {
    role "user" | "assistant"
    content string
    user_uploaded_pdf pdf?
    user_uploaded_image image?
}

function GenerateChatResponse(messages: ChatMessage[]) -> string {
    client OpenRouterGemini
    prompt #"
    Answer the user's questions based on the chat history:
    {% for message in messages %}
        {{ _.role(message.role) }} 
        {{ message.content }}
        Here are the PDFs:
        {{ message.user_uploaded_pdf }}
        Here are the images:
        {{ message.user_uploaded_image }}
    {% endfor %}
    Answer:
    "#
}
v
That will do it πŸ™‚
a
this is the output I got:
Copy code
Hello! I'm ready. I see you haven't provided any PDFs or images yet. Please provide them so I can help you.
Would I have to fix this using the system prompt?
v
Oh yes. If you tell the model that you are giving it PDFs and you don’t actually have PDFs it will likely do that.
You can do a couple of things alternative. You can also put an if condition around the image injection
And an else clause that says β€œ the user did not provide any documents here”
Or something of that accord
a
I had something similar to what you were saying:
Copy code
function GenerateChatResponse(messages: ChatMessage[]) -> string {
    client OpenRouterGemini
    prompt #"
    Answer the user's questions based on the chat history:
    {% for message in messages %}
        {{ _.role(message.role) }} 
        {{ message.content }}
        {% if message.user_uploaded_pdf %}
            Here are the PDFs:
            {{ message.user_uploaded_pdf }}
        {% endif %}
        {% if message.user_uploaded_image %}
            Here are the images:
            {{ message.user_uploaded_image }}
        {% endif %}
    {% endfor %}
    Answer:
    "#
}
v
From bammy You can use Jinja's if statements to conditionally render the PDFs and images. Here's how to modify your prompt: prompt #" Answer the user's questions based on the chat history: {% for message in messages %} {{ _.role(message.role) }} {{ message.content }} {% if message.user_uploaded_pdf %} PDF Content: {{ message.user_uploaded_pdf }} {% endif %} {% if message.user_uploaded_image %} Image Content: {{ message.user_uploaded_image }} {% endif %} {% endfor %} Answer: "# This will only include the PDF and image sections when those fields are present in the message.
a
that if check never worked
what I mean by that is even if a pdf was present, for some reason it wasn't being passed to the llm
v
Are you using OpenAI-responses?
Hmm actually I’ll test when I’m back on my pc (in one hour)
a
no fully using baml
v
Could you send me your client definition as well
a
Copy code
client<llm> OpenRouterGemini {
  provider openai-generic
  options {
    base_url https://openrouter.ai/api/v1
    model env.MODEL
    api_key env.OPENROUTER_API_KEY
    temperature 0.7
  }
}
v
Ty! Going to try and repro now
a
any luck?
v
ah! I've confirmed this:
Copy code
{{ message.user_uploaded_image|bool }}
this prints false
even when its set
@baml-sam any chance we can get a patch out for this one! its a pretty big bug we should patch ASAP
thanks btw for helping us find and diagnose this @anvith !
a
ty for following up!
could this issue be caused by this bug as well? https://discord.com/channels/1119368998161752075/1410268576853790760
v
That was a separate issue where we just handled pdfs wrong in the new provider we created.
But if they used if conditions then it would have impacted them as well