### Visual Understanding with Qwen VL Plus Source: https://github.com/eswulei/dashscope-go/blob/main/README.md Demonstrates using the Qwen VL Plus model for visual understanding tasks. It supports processing images from URLs or local files. The example shows how to construct a prompt with both text and image content. Ensure the DASHSCOPE_API_KEY environment variable is set. ```go func main() { model := qwen.QwenVLPlus token := os.Getenv("DASHSCOPE_API_KEY") if token == "" panic("token is empty") } cli := dashscopego.NewTongyiClient(model, token) sysContent := qwen.VLContentList{ { Text: "You are a helpful assistant.", }, } userContent := qwen.VLContentList{ { Text: "用唐诗体描述一下这张图片中的内容", }, { // 官方文档的例子, oss 下载 Image: "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg", // 使用 图片URL链接 // Image: "https://pic.ntimg.cn/20140113/8800276_184351657000_2.jpg", // 本地图片 // Image: "file:///Users/xxxx/xxxx.png", }, } input := dashscopego.VLInput{ Messages: []dashscopego.VLMessage{ {Role: "system", Content: &sysContent}, {Role: "user", Content: &userContent}, }, } // (可选 SSE开启)需要流式输出时 通过该 Callback Function 获取结果 streamCallbackFn := func(ctx context.Context, chunk []byte) error { fmt.Print(string(chunk)) return nil } req := &dashscopego.VLRequest{ Input: input, StreamingFn: streamCallbackFn, } ctx := context.TODO() resp, err := cli.CreateVLCompletion(ctx, req) if err != nil { panic(err) } fmt.Println("\nnon-stream result: ") fmt.Println(resp.Output.Choices[0].Message.Content.ToString()) } ``` -------------------------------- ### Paraformer: Real-time Speech Recognition Source: https://github.com/eswulei/dashscope-go/blob/main/README.md Perform real-time speech recognition using the Paraformer model. This example simulates a real-time audio stream from a local file. Ensure the DASHSCOPE_API_KEY environment variable is set and the audio sample rate matches the expected 16000 Hz. ```go package main import ( "bufio" "context" "fmt" "os" "os/user" "path/filepath" "time" "github.com/eswulei/dashscope-go" "github.com/eswulei/dashscope-go/paraformer" ) func main() { model := paraformer.ParaformerRealTimeV1 token := os.Getenv("DASHSCOPE_API_KEY") if token == "" { panic("token is empty") } cli := dashscopego.NewTongyiClient(model, token) streamCallbackFn := func(ctx context.Context, chunk []byte) error { fmt.Print(string(chunk)) return nil } headerPara := paraformer.ReqHeader{ Streaming: "duplex", TaskID: paraformer.GenerateTaskID(), Action: "run-task", } payload := paraformer.PayloadIn{ Parameters: paraformer.Parameters{ // seems like only support 16000 sample-rate. SampleRate: 16000, Format: "pcm", }, Input: map[string]interface{}{}, Task: "asr", TaskGroup: "audio", Function: "recognition", } req := ¶former.Request{ Header: headerPara, Payload: payload, StreamingFn: streamCallbackFn, } // 声音获取 实际使用时请替换成实时音频流. voiceReader := readAudioFromDesktop() reader := bufio.NewReader(voiceReader) cli.CreateSpeechToTextGeneration(context.TODO(), req, reader) // 等待语音识别结果输出 time.Sleep(5 * time.Second) } // 读取音频文件中的录音 模拟实时语音流. 这里下载的官方文档中的示例音频文件. // `https://dashscope.oss-cn-beijing.aliyuncs.com/samples/audio/paraformer/hello_world_male2.wav`. func readAudioFromDesktop() *bufio.Reader { usr, err := user.Current() if err != nil { panic(err) } voiceFilePath := filepath.Join(usr.HomeDir, "Desktop", "hello_world_female2.wav") f, err := os.OpenFile(voiceFilePath, os.O_RDONLY, 0640) if err != nil { panic(err) } if err != nil { panic(err) } reader := bufio.NewReader(f) return reader } ``` -------------------------------- ### Generate Text with Qwen Turbo Source: https://github.com/eswulei/dashscope-go/blob/main/README.md Demonstrates how to use the Qwen Turbo model for text generation. Supports both streaming and non-streaming responses. Ensure the DASHSCOPE_API_KEY environment variable is set. ```go import ( "context" "fmt" "os" "github.com/eswulei/dashscope-go" "github.com/eswulei/dashscope-go/qwen" ) func main() { model := qwen.QwenTurbo token := os.Getenv("DASHSCOPE_API_KEY") if token == "" panic("token is empty") } cli := dashscopego.NewTongyiClient(model, token) content := qwen.TextContent{Text: "讲个冷笑话"} input := dashscopego.TextInput{ Messages: []dashscopego.TextMessage{ {Role: "user", Content: &content}, }, } // (可选 SSE开启) 需要流式输出时 通过该 Callback Function 获取结果 streamCallbackFn := func(ctx context.Context, chunk []byte) error { fmt.Print(string(chunk)) return nil } req := &dashscopego.TextRequest{ Input: input, StreamingFn: streamCallbackFn, } ctx := context.TODO() resp, err := cli.CreateCompletion(ctx, req) if err != nil { panic(err) } fmt.Println("\nnon-stream result: ") fmt.Println(resp.Output.Choices[0].Message.Content.ToString()) } ``` -------------------------------- ### Generate Image with Wanx V1 Source: https://github.com/eswulei/dashscope-go/blob/main/README.md Demonstrates image generation using the Wanx V1 model. The `Download` parameter can be set to true to automatically download the generated image. Ensure the DASHSCOPE_API_KEY environment variable is set. ```go func main() { model := wanx.WanxV1 token := os.Getenv("DASHSCOPE_API_KEY") if token == "" panic("token is empty") } cli := dashscopego.NewTongyiClient(model, token) req := &wanx.ImageSynthesisRequest{ // Model: "wanx-v1", Model: model, Input: wanx.ImageSynthesisInput{ Prompt: "画一只松鼠", }, Params: wanx.ImageSynthesisParams{ N: 1, }, Download: true, // 从 URL 下载图片 } ctx := context.TODO() imgBlobs, err := cli.CreateImageGeneration(ctx, req) if err != nil { panic(err) } for _, blob := range imgBlobs { // blob.Data 会在 request 中设置了 Download: true 时下载 // 否则使用 blob.ImgURL saveImg2Desktop(blob.ImgType, blob.Data) } } func saveImg2Desktop(fileType string, data []byte) { buf := bytes.NewBuffer(data) img, _, err := image.Decode(buf) if err != nil { log.Fatal(err) } usr, err := user.Current() if err != nil { panic(err) } f, err := os.Create(filepath.Join(usr.HomeDir, "Desktop", "wanx_image.png")) if err != nil { panic(err) } defer f.Close() if err := png.Encode(f, img); err != nil { panic(err) } } ``` -------------------------------- ### QwenAudioTurbo: Transcribe and Analyze Audio Source: https://github.com/eswulei/dashscope-go/blob/main/README.md Use QwenAudioTurbo to transcribe audio and analyze the speaker's sentiment and tone. Local audio files are temporarily uploaded to OSS. Ensure the DASHSCOPE_API_KEY environment variable is set. ```go func main() { model := qwen.QwenAudioTurbo token := os.Getenv("DASHSCOPE_API_KEY") if token == "" { panic("token is empty") } cli := dashscopego.NewTongyiClient(model, token) sysContent := qwen.AudioContentList{ { Text: "You are a helpful assistant.", }, } userContent := qwen.AudioContentList{ { Text: "该段对话表达了什么观点? 详细分析该讲话者的语气,展现出什么样的情绪", //nolint:gosmopolitan }, { // 使用本地音频文件 // Audio: "file:///Users/xxx/Desktop/hello_world_female2.wav", // 官方文档中的例子 Audio: "https://dashscope.oss-cn-beijing.aliyuncs.com/audios/2channel_16K.wav", }, } input := dashscopego.AudioInput{ Messages: []dashscopego.AudioMessage{ {Role: "system", Content: &sysContent}, {Role: "user", Content: &userContent}, }, } // callback function: print stream result streamCallbackFn := func(ctx context.Context, chunk []byte) error { log.Print(string(chunk)) return nil } req := &dashscopego.AudioRequest{ Input: input, StreamingFn: streamCallbackFn, } ctx := context.TODO() resp, err := cli.CreateAudioCompletion(ctx, req) if err != nil { panic(err) } log.Println("\nnon-stream result: ") log.Println(resp.Output.Choices[0].Message.Content.ToString()) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.