Astreus

视觉

在 Astreus 文档中了解 视觉,获取用于构建智能体系统的设置指导、API 模式和实用示例。 了解构建可靠的 Astreus 智能体系统所需的设置模式、API 和实用示例。

用于多模态交互的图像分析与文档处理

概述

视觉(Vision)系统使代理能够处理和分析图像,为更丰富的交互提供多模态 AI 能力。它支持多种图像格式,提供多种分析模式,并与 OpenAI、Claude、Gemini 以及本地 Ollama 提供方无缝集成,提供灵活的部署选项。

启用视觉能力

vision 选项设置为 true 即可为代理启用视觉能力:

import { Agent } from '@astreus-ai/astreus';

const agent = await Agent.create({
  name: 'VisionAgent',
  model: 'gpt-4o',  // Vision-capable model
  vision: true      // Enable vision capabilities (default: false)
});

附件系统

Astreus 支持直观的附件系统,方便处理图像:

// Clean, modern attachment API
const response = await agent.ask("What do you see in this image?", {
  attachments: [
    { type: 'image', path: '/path/to/image.jpg', name: 'My Photo' }
  ]
});

附件系统会自动:

  • 检测文件类型并选择合适的工具
  • 用附件信息增强提示词
  • 在存在附件时启用工具使用

视觉能力

视觉系统通过内置工具提供三项核心能力:

1. 通用图像分析

使用自定义提示词和可配置的详细程度分析图像:

// Using attachments (recommended approach)
const response = await agent.ask("Please analyze this screenshot and describe the UI elements", {
  attachments: [
    { type: 'image', path: '/path/to/screenshot.png', name: 'UI Screenshot' }
  ]
});

// Using the analyze_image tool through conversation
const response2 = await agent.ask("Please analyze the image at /path/to/screenshot.png and describe the UI elements");

// Direct method call
const analysis = await agent.analyzeImage('/path/to/image.jpg', {
  prompt: 'What UI elements are visible in this interface?',
  detail: 'high',
  maxTokens: 1500
});

2. 图像描述

为不同使用场景生成结构化描述:

// Accessibility-friendly description
const description = await agent.describeImage('/path/to/image.jpg', 'accessibility');

// Available styles:
// - 'detailed': Comprehensive description of all visual elements
// - 'concise': Brief description of main elements  
// - 'accessibility': Screen reader-friendly descriptions
// - 'technical': Technical analysis including composition and lighting

3. 文本提取(OCR)

从图像中提取并转录文本:

// Extract text with language hint
const text = await agent.extractTextFromImage('/path/to/document.jpg', 'english');

// The system maintains original formatting and structure
console.log(text);

支持的格式

视觉系统支持以下图像格式:

  • JPEG(.jpg.jpeg)
  • PNG(.png)
  • GIF(.gif)
  • BMP(.bmp)
  • WebP(.webp)

输入来源

1

文件路径

从本地文件系统分析图像:

const result = await agent.analyzeImage('/path/to/image.jpg');
2

Base64 数据

分析 base64 编码的图像数据:

const base64Image = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...';
const result = await agent.analyzeImageFromBase64(base64Image);

配置

视觉模型配置

直接在代理配置中指定视觉模型:

const agent = await Agent.create({
  name: 'VisionAgent',
  model: 'gpt-4o',
  visionModel: 'gpt-4o',  // Specify vision model here
  vision: true
});

环境变量

# API keys (auto-detected based on model)
OPENAI_API_KEY=your_openai_key               # For OpenAI models
OPENAI_VISION_API_KEY=your_openai_key        # Dedicated vision API key (takes priority)
ANTHROPIC_API_KEY=your_anthropic_key         # For Claude models
ANTHROPIC_VISION_API_KEY=your_anthropic_key  # Dedicated vision API key (takes priority)
GEMINI_API_KEY=your_gemini_key               # For Gemini models
GEMINI_VISION_API_KEY=your_gemini_key        # Dedicated vision API key (takes priority)

# Ollama configuration (local)
OLLAMA_BASE_URL=http://localhost:11434       # Default if not set

视觉系统会根据代理配置中指定的 visionModel 自动选择合适的提供方。

分析选项

通过以下选项配置分析行为:

interface AnalysisOptions {
  prompt?: string;                    // Custom analysis prompt
  maxTokens?: number;                 // Response length limit (default: 1000)
  detail?: 'low' | 'high' | 'auto';   // Analysis detail level (OpenAI only)
}

使用示例

截图分析

const agent = await Agent.create({
  name: 'UIAnalyzer',
  model: 'gpt-4o',
  vision: true
});

// Analyze a UI screenshot
const analysis = await agent.analyzeImage('/path/to/app-screenshot.png', {
  prompt: 'Analyze this mobile app interface. Identify key UI components, layout structure, and potential usability issues.',
  detail: 'high'
});

console.log(analysis);

文档处理

// Extract text from scanned documents
const documentText = await agent.extractTextFromImage('/path/to/scanned-invoice.jpg', 'english');

// Generate accessible descriptions
const accessibleDesc = await agent.describeImage('/path/to/chart.png', 'accessibility');

多模态对话

// Using attachments for cleaner API
const response = await agent.ask("I'm getting an error. Can you analyze this screenshot and help me fix it?", {
  attachments: [
    { type: 'image', path: '/Users/john/Desktop/error.png', name: 'Error Screenshot' }
  ]
});

// Multiple attachments
const response2 = await agent.ask("Compare these UI mockups and suggest improvements", {
  attachments: [
    { type: 'image', path: '/designs/mockup1.png', name: 'Design A' },
    { type: 'image', path: '/designs/mockup2.png', name: 'Design B' }
  ]
});

// Traditional approach (still works)
const response3 = await agent.ask(
  "Please analyze the error screenshot at /Users/john/Desktop/error.png and suggest how to fix the issue"
);

提供方对比

特性OpenAI (gpt-4o)Claude (claude-3-5-sonnet)Gemini (gemini-1.5-pro)Ollama (llava)
分析质量优秀优秀优秀良好
处理速度可变
成本按用量付费按用量付费按用量付费免费(本地)
隐私云端处理云端处理云端处理本地处理
详细程度低/高/自动标准标准标准
语言支持广泛广泛广泛良好

OpenAI 提供方

  • 最适合:需要高精度的生产环境应用
  • 默认模型:gpt-4o
  • 特性:可控制详细程度,文本识别效果出色

Claude 提供方

  • 最适合:细致分析和详细描述
  • 默认模型:claude-3-5-sonnet-20241022
  • 特性:推理能力强,上下文理解出色

Gemini 提供方

  • 最适合:多模态任务和文档分析
  • 默认模型:gemini-1.5-pro
  • 特性:支持长上下文,适合处理复杂图像

Ollama 提供方(本地)

  • 最适合:注重隐私的应用或开发场景
  • 默认模型:llava
  • 特性:本地处理,无 API 费用,支持离线使用

批量处理

高效处理多张图像:

const images = [
  '/path/to/image1.jpg',
  '/path/to/image2.png',
  '/path/to/image3.gif'
];

// Process all images in parallel
const results = await Promise.all(
  images.map(imagePath => 
    agent.describeImage(imagePath, 'concise')
  )
);

console.log('Analysis results:', results);

// Or use task attachments for batch processing
const batchTask = await agent.createTask({
  prompt: 'Analyze all these images and provide a comparative report',
  attachments: images.map(path => ({
    type: 'image',
    path,
    name: path.split('/').pop()
  }))
});

const batchResult = await agent.executeTask(batchTask.id);

内置视觉工具

启用视觉功能后,以下工具会自动可用:

analyze_image

  • 参数:
    • image_path(string,必填):图像文件路径
    • prompt(string,可选):自定义分析提示词
    • detail(string,可选):'low' 或 'high' 详细程度

describe_image

  • 参数:
    • image_path(string,必填):图像文件路径
    • style(string,可选):描述风格('detailed'、'concise'、'accessibility'、'technical')

extract_text_from_image

  • 参数:
    • image_path(string,必填):图像文件路径
    • language(string,可选):语言提示,以提高 OCR 准确度

响应类型

视觉方法返回包含分析结果的字符串响应。

图像分析响应

图像分析会根据你的提示词返回一段描述性字符串:

const analysis = await agent.analyzeImage('/path/to/office.jpg', {
  prompt: "What objects are in this image and how is the space organized?",
  detail: "high"
});

// Response: string
"The image shows a modern office workspace with a MacBook Pro laptop, wireless keyboard, and mouse on a wooden desk. To the left is a coffee mug and a notebook. The desk is positioned near a window with natural lighting. The space features a minimalist organization with cable management and a small potted plant."

图像描述响应

describeImage 返回一段格式化的描述字符串:

const description = await agent.describeImage('/path/to/product.jpg');

// Response: string
"A professional product photograph featuring a stainless steel water bottle with a matte black finish. The bottle has a wide mouth opening and is photographed against a white background with soft studio lighting creating subtle highlights along the curved surfaces."

图像文本提取响应

OCR 会以字符串形式返回提取的文本:

const text = await agent.extractTextFromImage('/path/to/document.png', {
  language: 'en'
});

// Response: string
"INVOICE\nDate: January 15, 2024\nInvoice #: INV-2024-001\n\nBill To:\nAcme Corporation\n123 Main Street\nNew York, NY 10001\n\nDescription          Quantity    Price    Total\nProfessional Services    8 hrs    $150    $1,200\nConsulting Fee           1        $500    $500\n\nSubtotal: $1,700\nTax (8%): $136\nTotal: $1,836"

从 Base64 分析图像响应

Base64 图像分析同样返回一个字符串:

const base64Image = "data:image/png;base64,iVBORw0KG...";
const result = await agent.analyzeImageFromBase64(base64Image, {
  prompt: "Identify the main subject and mood of this image"
});

// Response: string
"The main subject is a sunset landscape with mountains in the background. The mood is serene and peaceful, with warm orange and pink tones dominating the sky. The composition creates a sense of tranquility and natural beauty."

最后更新时间:2026年7月6日