# -*- coding: utf-8 -*- # # @author: lyg # @date: 2025-5-7 # @version: 1 # @description:视觉识别文档内容 from langchain_openai.chat_models import ChatOpenAI from langchain_core.prompts import HumanMessagePromptTemplate, ChatPromptTemplate from langchain_core.messages import HumanMessage,SystemMessage from langchain_core.output_parsers import JsonOutputParser import json import base64 class VisionTest: def __init__(self,file): self.llm = ChatOpenAI(temperature=0, model="qwen2.5-72b-instruct", base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", api_key="sk-15ecf7e273ad4b729c7f7f42b542749e") image = base64.b64encode(open(file, 'rb').read()).decode() self.prompt = ChatPromptTemplate.from_messages([ SystemMessage("你是一个资深软件工程师,请分析图片回答问题。"), HumanMessage(content=[ {"type": "text", "text": "describe the weather in this image"}, { "type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image}"}, } ]) ]) def run(self,msg): chain = self.prompt | self.llm resp = chain.invoke({"msg": msg}) print(resp.content) if __name__ == '__main__': vision = VisionTest("image_path") vision.run("问题")