Open h-arnold opened 1 week ago
To address the issue of the ChatInput
component only processing one image, you should ensure that the files
attribute is correctly handled as a list of file paths. The FileInput
is already configured to accept multiple files with is_list=True
, so the problem might be in how the files are being processed or passed to the OpenAIModel
component.
Here are a few steps to troubleshoot and potentially resolve the issue:
Verify File Handling in message_response
: Ensure that the message_response
method in the ChatInput
component correctly processes the files
attribute as a list. The files
attribute should be passed as a list to the Message
object.
Check JSON Input Format: When passing the filenames directly or via a tweak, ensure that the JSON input correctly represents the list of files. The files
attribute should be a JSON array, not a string representation of an array. For example:
"files": ["816eec43-ef5a-4eea-bf67-00f1318ee145/2024-10-03_14-31-04_image1.png", "816eec43-ef5a-4eea-bf67-00f1318ee145/2024-10-03_14-31-07_image2.png"]
Error Handling: The error message "too many values to unpack (expected 2)" suggests there might be an issue with how the files are being unpacked or processed. Review the code where the files are being handled to ensure it correctly iterates over the list of files.
Playground Chat Window: Since the playground chat window works correctly, compare the request payloads and processing logic between the playground and your implementation to identify any discrepancies.
Component Configuration: Ensure that the OpenAIModel
component is configured to accept and process multiple files. If the component expects a single file, it might need adjustments to handle a list of files.
By following these steps, you should be able to identify and resolve the issue with processing multiple image files in the ChatInput
component.
To continue talking to Dosu, mention @dosu.
@dosu you can't add more than one file via the Chat Input GUI and you can only pass a list via the API by encapsulating it in a string first. There isn't any information in the swagger docs about how to correctly pass a file via the API and in any case, the request object from the playground suggests that a list is the appropriate format to pass file paths but I've not had any luck doing that.
To correctly pass multiple file paths via the API to the ChatInput
component, you should use a list format for the files
input. The FileInput
for files
is configured with is_list=True
, indicating that it accepts multiple files. Therefore, you should pass the file paths as a list, not as a string representation of a list.
Here's an example of how the JSON structure should look when making a request to the API:
{
"input_value": "Your message here",
"sender": "User",
"sender_name": "User",
"session_id": "your-session-id",
"files": ["path/to/file1.png", "path/to/file2.png"]
}
Ensure that the files
field contains a JSON array of file paths, not a string. This should align with the expected input format and allow the ChatInput
component to process multiple files correctly [1].
To continue talking to Dosu, mention @dosu.
Thanks @dosu . I can confirm the request structure you suggest does not work for me, even after I've re-uploaded the images via the API and used the path directly from the response object for each.
If the request structure with a JSON array for the files
input is not working, there might be an issue with how the ChatInput
component or the API is processing the file paths. Here are a few additional steps to consider:
Check API Endpoint: Ensure that you are using the correct API endpoint and that it is configured to accept multiple file paths. Double-check the endpoint documentation or code to confirm the expected input format.
Inspect Server Logs: Look at the server logs to see if there are any error messages or warnings when you send the request. This might provide clues about what is going wrong.
Debugging: Add logging or debugging statements in the ChatInput
component to verify that the files
array is being received and processed as expected. This can help identify where the issue might be occurring.
Middleware or Validation: Check if there is any middleware or validation logic that might be altering the request payload before it reaches the ChatInput
component. This could be affecting how the files are processed [1].
Compare with Playground: Since the playground chat window works correctly, compare the request payloads and processing logic between the playground and your API implementation to identify any discrepancies.
By following these steps, you should be able to pinpoint the issue and ensure that multiple file paths are processed correctly.
To continue talking to Dosu, mention @dosu.
Hi @h-arnold ,
Thank you for reporting this issue!
I've analyzed your problem and found that it's a frontend limitation when uploading multiple files. However, you can work around this by manually passing the files as a list-similar to how you've done it. Here's an example:
message = Message(
text=self.input_value,
sender=self.sender,
sender_name=self.sender_name,
session_id=self.session_id,
files=[self.files, self.second_file, ...],
)
To properly pass the files to OpenAI, you need to build a Message
object as the chat input does and how you did before. I'll report this limitation to the team for further investigation.
@joaoguilhermeS
Thanks so much for looking into this. :)
How would I do this via the API? I've not been able to figure out a way of structuring the request to send more than one image, or image path at a time and there doesn't seem to be any examples in the swagger or langflow docs.
Sure @h-arnold, I am glad to help! You can set the atributes using the tweaks flags, so you can add as many images as you configured on your Chat Input doing a curl similar to this one:
curl -X POST \
"http://127.0.0.1:7860/api/v1/run/354a4875-3efa-4086-883e-cc3f2af76052?stream=false" \
-H 'Content-Type: application/json' \
-d '{
"output_type": "chat",
"input_type": "chat",
"tweaks": {
"ChatInput-Zqr2N": {
"files": "img1.jpg",
"second_file": "img3.jpeg"
},
"OpenAIModel-9zcSN": {
"api_key": "OPENAI_API_KEY"
}
}
}'
You can check the lower right button on the canvas where you have an API button where you can see more information about structuring your Flow parameters enabling the tweaks flag ).
Here are the docs: https://docs.langflow.org/workspace-api
@joaoguilhermeS
Thanks so much for your help! I modified the ChatInput as you suggested for a quick workaround and I've posted the result below in case anyone else needs it until the frontend gets updated.
from langflow.base.data.utils import IMG_FILE_TYPES, TEXT_FILE_TYPES
from langflow.base.io.chat import ChatComponent
from langflow.inputs import BoolInput
from langflow.io import DropdownInput, FileInput, MessageTextInput, MultilineInput, Output
from langflow.memory import store_message
from langflow.schema.message import Message
from langflow.utils.constants import MESSAGE_SENDER_AI, MESSAGE_SENDER_NAME_USER, MESSAGE_SENDER_USER
class ChatInput(ChatComponent):
display_name = "Chat Input"
description = "Get chat inputs from the Playground."
icon = "ChatInput"
name = "ChatInput"
MAX_FILES = 3 # Define the maximum number of files allowed
inputs = [
MultilineInput(
name="input_value",
display_name="Text",
value="",
info="Message to be passed as input.",
),
BoolInput(
name="should_store_message",
display_name="Store Messages",
info="Store the message in the history.",
value=True,
advanced=True,
),
DropdownInput(
name="sender",
display_name="Sender Type",
options=[MESSAGE_SENDER_AI, MESSAGE_SENDER_USER],
value=MESSAGE_SENDER_USER,
info="Type of sender.",
advanced=True,
),
MessageTextInput(
name="sender_name",
display_name="Sender Name",
info="Name of the sender.",
value=MESSAGE_SENDER_NAME_USER,
advanced=True,
),
MessageTextInput(
name="session_id",
display_name="Session ID",
info="The session ID of the chat. If empty, the current session ID parameter will be used.",
advanced=True,
),
]
# Dynamically add multiple FileInput fields
for i in range(1, MAX_FILES + 1):
inputs.append(
FileInput(
name=f"file_{i}",
display_name=f"File {i}",
file_types=TEXT_FILE_TYPES + IMG_FILE_TYPES,
info=f"File {i} to be sent with the message.",
advanced=True,
is_list=False, # Each FileInput handles a single file
)
)
outputs = [
Output(display_name="Message", name="message", method="message_response"),
]
def message_response(self) -> Message:
# Collect all uploaded files into a list
files = []
for i in range(1, self.MAX_FILES + 1):
file = getattr(self, f"file_{i}", None)
if file:
files.append(file)
# Create the Message object with the list of files
message = Message(
text=self.input_value,
sender=self.sender,
sender_name=self.sender_name,
session_id=self.session_id,
files=files, # Assign the collected list of files
)
# Store the message if required
if (
self.session_id
and isinstance(message, Message)
and isinstance(message.text, str)
and self.should_store_message
):
store_message(
message,
flow_id=self.graph.flow_id,
)
self.message.value = message
# Update the component status
self.status = message
return message
Bug Description
I've tried uploading more than one image several different ways with no luck. The only way that works is through the playground ChatInput. Any other method seems to either fail completely or only acknowledge the first file in the list.
Reproduction
In all cases, upload the images you plan an using first and make a note of their URL. I have tried the following methods to get Langflow to look at both files:
Passing the filenames directly
Input
Output
Result: The LLM only sees the one image.
Passing the filenames via a ChatInput Tweak
Input
Output
Result: An error.
Trying to select more than one image at a time through the ChatInput component in the web interface
Result: Can only select one image.
Adding a 2nd file input box to the ChatInput Component
Code
Result: Only the first image is passed to the OpenAI Component
What does seem to work
Passing the two images via the Playground Chat Window.
Here's the request payload I pulled from the developer tools that seemed to give the correct result:
Expected behavior
That I should be able to pass a list of file paths and have the LLM recognise both, as seems to happen when I run the flow through the playground chat window.
Who can help?
No response
Operating System
Google Cloud Run - latest nightly docker image
Langflow Version
1.0.18 nightly as of 03/10/2024
Python Version
None
Screenshot
No response
Flow File
No response