sending images as input to a react agent #26032
Replies: 3 comments 2 replies
-
To send an image as input to a React agent using LangChain, you can use the import base64
import httpx
from langchain_core.messages import HumanMessage
from langchain_openai import ChatOpenAI
# Load and encode the image
image_path = "path/to/your/invoice.jpg"
with open(image_path, "rb") as image_file:
image_data = base64.b64encode(image_file.read()).decode("utf-8")
# Define the message with an image and text
message = HumanMessage(
content=[
{"type": "text", "text": "Please send this invoice to example@example.com."},
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{image_data}"},
},
],
)
# Initialize the model
model = ChatOpenAI(model="gpt-4o")
# Invoke the model with the message
response = model.invoke([message])
print(response.content) This code sets up a message that includes an image and a user message asking to send the invoice to a specified email address. The |
Beta Was this translation helpful? Give feedback.
-
I'm trying to solve the same thing. I tried sending the image as a base64 encoded string similar to @dosu 's example, but that just returns nonsense. The Messages formatting doesn't seem to work with react agent. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
I have created a working react agent using langchain. I was wondering how I would be able to send input to the agent based on a local image. For example, upload an image of an invoice. I would then add instructions to prompt on mailing it to a specific email address. When you define a custom ChatGPT it comes out of the box, but I can't find clear documentation on how to do it using langchain and langraph.
System Info
langchain==0.2.15
langchain-chroma==0.1.1
langchain-community==0.2.14
langchain-core==0.2.36
langchain-experimental==0.0.57
langchain-openai==0.1.3
langchain-pinecone==0.1.1
langchain-text-splitters==0.2.2
langchainhub==0.1.15
Beta Was this translation helpful? Give feedback.
All reactions