I hosted Phi-3-vision-128k-instruc through vllm. How can I pass Image in Phi-3-vision-128k-instruct with langchain community? #24020
Replies: 3 comments 4 replies
-
To pass an image to the "microsoft/Phi-3-vision-128k-instruct" model using the from langchain_community.llms import VLLMOpenAI
from PIL import Image
import base64
import io
# Initialize the VLLMOpenAI model
llm = VLLMOpenAI(
openai_api_key="EMPTY",
openai_api_base="http://150.0.2.236:8888/v1",
model_name="microsoft/Phi-3-vision-128k-instruct",
model_kwargs={"stop": ["."]},
)
# Open and encode the image as a base64 string
image_path = "invoice_data_images/PO - 042 (REVISED )_page_1.png"
with open(image_path, "rb") as image_file:
image_base64 = base64.b64encode(image_file.read()).decode("utf-8")
# Create the prompt and message
prompt_1 = "Give me invoice date from given image"
messages = {
"prompt": prompt_1,
"multi_modal_data": {
"image": image_base64
},
}
# Invoke the model with the message
response = llm.invoke(messages)
print(response) In this example:
This should allow you to pass the image to the "microsoft/Phi-3-vision-128k-instruct" model for inference using LangChain [1]. |
Beta Was this translation helpful? Give feedback.
-
@dosu This still giving error. from langchain_community.llms import VLLMOpenAI llm = VLLMOpenAI( with open(image_path, "rb") as image_file: image_prompt_value = ImagePromptValue(image_url={"url": f"data:image/png;base64,{image_base64}", "detail": "high"}) prompt_1 = "Give me invoice date from given image" response = llm.invoke(messages) Error{ File ~/SapidBlue/invoice_data_extraction/lightllm_xinf/venv/lib/python3.8/site-packages/langchain_core/language_models/llms.py:347, in BaseLLM.invoke(self, input, config, stop, **kwargs) File ~/SapidBlue/invoice_data_extraction/lightllm_xinf/venv/lib/python3.8/site-packages/langchain_core/language_models/llms.py:331, in BaseLLM._convert_input(self, input) ValueError: Invalid input type <class 'dict'>. Must be a PromptValue, str, or list of BaseMessages." |
Beta Was this translation helpful? Give feedback.
-
@dosu This still giving error. from langchain_community.llms import VLLMOpenAI llm = VLLMOpenAI( with open(image_path, "rb") as image_file: prompt_1 = "Give me invoice date from given image" response = llm.invoke(messages) Error{ File ~/SapidBlue/invoice_data_extraction/lightllm_xinf/venv/lib/python3.8/site-packages/langchain_core/language_models/llms.py:346, in BaseLLM.invoke(self, input, config, stop, **kwargs) File ~/SapidBlue/invoice_data_extraction/lightllm_xinf/venv/lib/python3.8/site-packages/langchain_core/language_models/llms.py:703, in BaseLLM.generate_prompt(self, prompts, stop, callbacks, **kwargs) File ~/SapidBlue/invoice_data_extraction/lightllm_xinf/venv/lib/python3.8/site-packages/langchain_core/language_models/llms.py:882, in BaseLLM.generate(self, prompts, stop, callbacks, tags, metadata, run_name, run_id, **kwargs) File ~/SapidBlue/invoice_data_extraction/lightllm_xinf/venv/lib/python3.8/site-packages/langchain_core/language_models/llms.py:740, in BaseLLM._generate_helper(self, prompts, stop, run_managers, new_arg_supported, **kwargs) File ~/SapidBlue/invoice_data_extraction/lightllm_xinf/venv/lib/python3.8/site-packages/langchain_core/language_models/llms.py:727, in BaseLLM._generate_helper(self, prompts, stop, run_managers, new_arg_supported, **kwargs) File ~/SapidBlue/invoice_data_extraction/lightllm_xinf/venv/lib/python3.8/site-packages/langchain_community/llms/openai.py:464, in BaseOpenAI._generate(self, prompts, stop, run_manager, **kwargs) File ~/SapidBlue/invoice_data_extraction/lightllm_xinf/venv/lib/python3.8/site-packages/langchain_community/llms/openai.py:119, in completion_with_retry(llm, run_manager, **kwargs) File ~/SapidBlue/invoice_data_extraction/lightllm_xinf/venv/lib/python3.8/site-packages/openai/_utils/_utils.py:277, in required_args..inner..wrapper(*args, **kwargs) File ~/SapidBlue/invoice_data_extraction/lightllm_xinf/venv/lib/python3.8/site-packages/openai/resources/completions.py:528, in Completions.create(self, model, prompt, best_of, echo, frequency_penalty, logit_bias, logprobs, max_tokens, n, presence_penalty, seed, stop, stream, stream_options, suffix, temperature, top_p, user, extra_headers, extra_query, extra_body, timeout) File ~/SapidBlue/invoice_data_extraction/lightllm_xinf/venv/lib/python3.8/site-packages/openai/_base_client.py:1261, in SyncAPIClient.post(self, path, cast_to, body, options, files, stream, stream_cls) File ~/SapidBlue/invoice_data_extraction/lightllm_xinf/venv/lib/python3.8/site-packages/openai/_base_client.py:942, in SyncAPIClient.request(self, cast_to, options, remaining_retries, stream, stream_cls) File ~/SapidBlue/invoice_data_extraction/lightllm_xinf/venv/lib/python3.8/site-packages/openai/_base_client.py:1041, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls) BadRequestError: Error code: 400 - {'object': 'error', 'message': "This model's maximum context length is 8192 tokens. However, you requested 374850 tokens (374594 in the messages, 256 in the completion). Please reduce the length of the messages or completion.", 'type': 'BadRequestError', 'param': None, 'code': 400}" |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
above code not working.
Description
I want to run inference of a microsoft/Phi-3-vision-128k-instruct in langchain for image.
How can I give image to model?
I hosted vllm in ec2 instance.
System Info
langchain==0.2.7
langchain-community==0.2.7
langchain-core==0.2.12
langchain-text-splitters==0.2.2
Beta Was this translation helpful? Give feedback.
All reactions