Replies: 1 comment 1 reply
-
To send an image or a base64 encoded image to the Llava model using the First, ensure you have the necessary imports and helper functions to handle the image conversion: import base64
from io import BytesIO
from PIL import Image
def convert_to_base64(pil_image):
"""
Convert PIL images to Base64 encoded strings
:param pil_image: PIL image
:return: Base64 string
"""
buffered = BytesIO()
pil_image.save(buffered, format="JPEG") # You can change the format if needed
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
return img_str Next, use the from langchain_community.chat_models import ChatOllama
from langchain_core.messages import HumanMessage
from langchain_core.output_parsers import StrOutputParser
# Initialize the ChatOllama model
llm = ChatOllama(base_url="URL", model="model")
# Function to create the prompt with text and image
def prompt_func(data):
text = data["text"]
image = data["image"]
image_part = {
"type": "image_url",
"image_url": f"data:image/jpeg;base64,{image}",
}
content_parts = []
text_part = {"type": "text", "text": text}
content_parts.append(image_part)
content_parts.append(text_part)
return [HumanMessage(content=content_parts)]
# Create the chain with the prompt function, model, and output parser
chain = prompt_func | llm | StrOutputParser()
# Load and convert the image to base64
file_path = "path_to_your_image.jpg"
pil_image = Image.open(file_path)
image_b64 = convert_to_base64(pil_image)
# Invoke the chain with the text and image data
query_chain = chain.invoke(
{"text": "What is the Dollar-based gross retention rate?", "image": image_b64}
)
print(query_chain) This code will send the image and text prompt to the Llava model hosted on your server and print the response [1]. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
I am hosting llm on a server and it is a Llava model that is able to see the images. I want to send an image or a base64 code to it. How can I do it?
System Info
langchain==0.1.0
langchain-anthropic==0.1.4
langchain-community==0.0.20
langchain-core==0.1.50
langchain-experimental==0.0.57
langchain-google-genai==1.0.1
langchain-groq==0.1.3
langchain-openai==0.0.2.post1
langchain-text-splitters==0.0.1
langchainhub==0.1.15
Beta Was this translation helpful? Give feedback.
All reactions