yao/tools/image/generate.go
Max af0d4edd74 feat(image): add image generation and enhanced reading capabilities
- Introduced `image_generate` tool for generating images from text prompts, with options for specifying output file paths and image dimensions.
- Updated `image_read` functionality to allow optional provider specification for enhanced image analysis.
- Implemented new `GenerateImage` method in the LLM API for seamless integration of image generation capabilities.
- Enhanced documentation to include detailed usage examples for both image reading and generation tools.
- Updated tests to validate new image generation features and ensure robust functionality across image tools.
2026-05-06 10:59:35 +08:00

54 lines
1.4 KiB
Go

package image
import (
_ "embed"
"fmt"
"github.com/yaoapp/gou/process"
agentLLM "github.com/yaoapp/yao/agent/llm"
"github.com/yaoapp/yao/openapi/oauth/authorized"
)
//go:embed generate_schema.json
var GenerateSchemaJSON []byte
// GenerateHandler is the tools.image_generate process handler.
func GenerateHandler(proc *process.Process) interface{} {
prompt := proc.ArgsString(0)
if prompt == "" {
return map[string]interface{}{"error": "prompt is required"}
}
provider := proc.ArgsString(1)
size := proc.ArgsString(2, "1024x1024")
authInfo := authorized.ProcessAuthInfo(proc)
if authInfo == nil {
return map[string]interface{}{"error": "unauthorized: no auth info in request"}
}
connectorID := provider
if connectorID == "" {
connectorID = findFirstImageGenConnector(authInfo)
if connectorID == "" {
return map[string]interface{}{"error": "no image generation provider available; configure one or specify a provider"}
}
}
conn, _, err := agentLLM.ResolveConnector(connectorID, authInfo)
if err != nil {
return map[string]interface{}{"error": fmt.Sprintf("resolve connector: %v", err)}
}
options := map[string]interface{}{"size": size}
resp, err := agentLLM.GenerateImage(conn, prompt, options)
if err != nil {
return map[string]interface{}{"error": fmt.Sprintf("image generation failed: %v", err)}
}
return map[string]interface{}{
"image": resp.Image,
"format": resp.Format,
"size": size,
}
}