> ## Content Index
> Fetch the complete content index at: https://www.hubwiz.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# DSPy.Image：视觉模型支持
- URL: https://www.hubwiz.com/blog/dspy-image-vlm-support/
- Published: 2024-12-30T03:48:18.000Z
- Updated: 2024-12-30T03:48:18.000Z
- Description: DSPy 最近在测试版中增加了对 VLM 的支持。本文介绍使用 DSPy 从图像中提取属性。
- Author: admin
- Tags: LIBRARY, DSPY, VLM, MULTIMODAL, #Import 2026-10-14 14:41

DSPy 最近在测试版中增加了对 VLM 的支持。本文介绍使用 DSPy 从图像中提取属性。对于此示例，我们将了解如何从网站屏幕截图中提取有用的属性

## 1、定义签名

定义DSPy签名。注意 `dspy.Image` 输入字段：

```
import dspy
class WebsiteDataExtractionSignature(dspy.Signature):
    """Website data extraction"""
    website_screenshot: dspy.Image = dspy.InputField(
        desc="A screenshot of the website"
    )
    hero_text: str = dspy.OutputField(
        desc="The hero text of the website"
    )
    website_description: str = dspy.OutputField(
        desc="A description of the website"
    )
    call_to_action: str = dspy.OutputField(
        desc="The call to action of the website"
    )
    color_palette: list[str] = dspy.OutputField(
        desc="The color palette of the website"
    )
    font_palette: list[str] = dspy.OutputField(
        desc="The font palette of the website"
    )
```

## 2、定义模块

接下来使用 `ChainOfThought` 优化器和上一步中的签名定义一个简单的程序：

```
class WebsiteDataExtraction(dspy.Module):
    """Module for extracting structured data from website screenshots."""
    def __init__(self):
        self.website_data_extraction = dspy.ChainOfThought(
            WebsiteDataExtractionSignature
        )
        
    # pylint: disable=missing-function-docstring
    def forward(self, website_screenshot: str):
        website_data = self.website_data_extraction(website_screenshot)
        return website_data
```

## 3、最终代码

最后，编写一个函数来读取图像并通过调用上一步中的程序来提取属性：

```
def extract_website_data(website_screenshot_path: str):
    """Extract data from a website screenshot.
    
    Args:
        website_screenshot_path (str): Path to the website screenshot image
    
    Returns:
        dict: Extracted website data
    """
    # Load the image
    with open(website_screenshot_path, "rb") as image_file:
        base64_data = base64.b64encode(image_file.read()).decode('utf-8').replace('\n', '')
        image_data_uri = f"data:image/png;base64,{base64_data}"
    website_data_extraction = WebsiteDataExtraction()
    website_data = website_data_extraction(image_data_uri)
    return website_data

if __name__ == "__main__":
    dspy_lm = dspy.LM(model="openai/gpt-4o-mini")
    dspy.config( lm=dspy_lm)
    result = extract_website_data(
        "src/vision_lm/data/langtrace-screenshot.png"
    )
    print(result)
```

## 4、可观察性

就是这样！如果您的开发需要可观察性，只需添加 `langtrace.init()` 即可从跟踪中获得更深入的见解。

![](https://www.hubwiz.com/blog/content/images/2024/12/image-841.png)

## 5、源代码

你可以在此处找到此示例的[完整源代码](https://github.com/Scale3-Labs/dspy-examples/tree/main/src/vision%5Flm?ref=hubwiz.com)。

---

原文链接：[Attribute Extraction from Images using DSPy](https://www.linkedin.com/pulse/attribute-extraction-from-images-using-dspy-karthik-kalyanaraman-vn2zf/?ref=hubwiz.com)

汇智网翻译整理，转载请标明出处