You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2025/03/10 15:59:58 UTC

(dubbo-go-samples) branch main updated: bug fix and rewirte image encode and decode logic (#807)

This is an automated email from the ASF dual-hosted git repository.

alexstocks pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/dubbo-go-samples.git


The following commit(s) were added to refs/heads/main by this push:
     new a150316c bug fix and rewirte image encode and decode logic (#807)
a150316c is described below

commit a150316c7837b1247070e050466af94cfcccd576
Author: Alan <m1...@163.com>
AuthorDate: Mon Mar 10 23:59:53 2025 +0800

    bug fix and rewirte image encode and decode logic (#807)
    
    * fix import
    
    * fix readme dir
    
    * rewrite image encode and decode logic to support image files robustness and fix out of bounds err
    
    * fix typo
    
    * limit upload file types and add clarification in the readme
    
    * limit upload file types and add clarification in the readme
    
    * Revert "limit upload file types and add clarification in the readme"
    
    This reverts commit 8c514464208b43e982258724fabd58d09bf2bcb6.
    
    * limit upload file types and add clarification in the readme
---
 llm/README.md                               |  7 ++++---
 llm/README_zh.md                            | 10 +++++-----
 llm/go-client/frontend/handlers/chat.go     | 18 +++++++++++++++---
 llm/go-client/frontend/static/script.js     |  3 +++
 llm/go-client/frontend/templates/index.html |  2 +-
 llm/go-server/cmd/server.go                 | 12 +++++++-----
 6 files changed, 35 insertions(+), 17 deletions(-)

diff --git a/llm/README.md b/llm/README.md
index ed909b81..a7472907 100644
--- a/llm/README.md
+++ b/llm/README.md
@@ -48,7 +48,7 @@ The server integrates the Ollama model and uses Dubbo-go's RPC service for invoc
 Run the server by executing:
 
 ```shell
-$ go run llm/go-server/cmd/server.go
+$ go run go-server/cmd/server.go
 ```
 
 ### **Run the Client**
@@ -58,7 +58,7 @@ The client invokes the server's RPC interface to retrieve the inference results
 Run the cli client by executing:
 
 ```shell
-$ go run llm/go-client/cmd/client.go
+$ go run go-client/cmd/client.go
 ```
 
 Cli client supports multi-turn conversations, command interact, context management.
@@ -66,7 +66,8 @@ Cli client supports multi-turn conversations, command interact, context manageme
 We also support a frontend using Gin framework for users to interact. If you want run the frontend client you can executing the following command and open it in ```localhost:8080``` by default:
 
 ```shell
-$ go run llm/go-client/frontend/main.go
+$ go run go-client/frontend/main.go
 ```
 
 Frontend client supports multi-turn conversations, binary file (image) support for LLM interactions.
+Currently the supported uploaded image types are limited to png, jpeg and gif, with plans to support more binary file types in the future.
diff --git a/llm/README_zh.md b/llm/README_zh.md
index 7f81488f..3000c1fc 100644
--- a/llm/README_zh.md
+++ b/llm/README_zh.md
@@ -48,7 +48,7 @@ $ cd llm
 在服务端目录下运行:
 
 ```shell
-$ go run llm/go-server/cmd/server.go
+$ go run go-server/cmd/server.go
 ```
 
 ### **客户端运行**
@@ -58,7 +58,7 @@ $ go run llm/go-server/cmd/server.go
 在客户端目录下运行:
 
 ```shell
-$ go run llm/go-client/cmd/client.go
+$ go run go-client/cmd/client.go
 ```
 
 命令行客户端支持多轮对话、命令交互、上下文管理功能。
@@ -66,8 +66,8 @@ $ go run llm/go-client/cmd/client.go
 我们也提供了包含前端页面的基于Gin框架的客户端进行交互,运行以下命令然后访问 ```localhost:8080``` 即可使用:
 
 ```shell
-$ go run llm/go-client/frontend/main.go
+$ go run go-client/frontend/main.go
 ```
 
-包含前端页面的客户端支持多轮对话,支持进行二进制文件(图片)传输并与大模型进行交互.
-
+包含前端页面的客户端支持多轮对话,支持进行二进制文件(图片)传输并与大模型进行交互。
+目前所支持上传的图片类型被限制为 png,jpeg 和 gif 类型,计划在将来支持更多的二进制文件类型。
\ No newline at end of file
diff --git a/llm/go-client/frontend/handlers/chat.go b/llm/go-client/frontend/handlers/chat.go
index c65ab5d9..e16197e2 100644
--- a/llm/go-client/frontend/handlers/chat.go
+++ b/llm/go-client/frontend/handlers/chat.go
@@ -22,8 +22,8 @@ import (
 	"io"
 	"log"
 	"net/http"
+	"regexp"
 	"runtime/debug"
-	"strings"
 	"time"
 )
 
@@ -82,13 +82,25 @@ func (h *ChatHandler) Chat(c *gin.Context) {
 		return
 	}
 
-	sepIndex := strings.Index(",", req.Bin)
+	var img string
+	if len(req.Bin) > 0 {
+		re := regexp.MustCompile(`^data:image/([a-zA-Z]+);base64,([^"]+)$`)
+		// this regex does not support file types like svg
+		matches := re.FindStringSubmatch(req.Bin)
+
+		if len(matches) != 3 {
+			c.JSON(http.StatusBadRequest, gin.H{"error": "invalid base64 data format"})
+			return
+		}
+
+		img = matches[2]
+	}
 
 	messages := h.ctxManager.GetHistory(ctxID)
 	messages = append(messages, &chat.ChatMessage{
 		Role:    "human",
 		Content: req.Message,
-		Bin:     []byte(req.Bin[sepIndex+1:]),
+		Bin:     []byte(img),
 	})
 
 	stream, err := h.svc.Chat(context.Background(), &chat.ChatRequest{
diff --git a/llm/go-client/frontend/static/script.js b/llm/go-client/frontend/static/script.js
index d09d1517..3b56d2cc 100644
--- a/llm/go-client/frontend/static/script.js
+++ b/llm/go-client/frontend/static/script.js
@@ -23,6 +23,7 @@ const createChatLi = (message, className) => {
 
 const handleChat = () => {
     userMessage = chatInput.value.trim(); // Get user entered message and remove extra whitespace
+    userBin = null
     if (fileBlobArr.length > 0) {
         userBin = fileBlobArr[0]
     }
@@ -204,6 +205,8 @@ function clear() {
     document.getElementById("drop").style.display = "none";
     addBtn.style.display = "flex";
     fileInput.value = "";
+    fileBlobArr = [];
+    fileArr = [];
 }
 
 document.getElementById("drop").addEventListener('click', clear);
\ No newline at end of file
diff --git a/llm/go-client/frontend/templates/index.html b/llm/go-client/frontend/templates/index.html
index 68eefdc5..35aef33a 100644
--- a/llm/go-client/frontend/templates/index.html
+++ b/llm/go-client/frontend/templates/index.html
@@ -41,7 +41,7 @@
 
     </div>
 
-    <input id="input" accept="image/*" type="file" style="display: none" >
+    <input id="input" accept="image/png, image/jpeg, image/gif" type="file" style="display: none" >
 
   </body>
 
diff --git a/llm/go-server/cmd/server.go b/llm/go-server/cmd/server.go
index c4e752fb..8f2b88ec 100644
--- a/llm/go-server/cmd/server.go
+++ b/llm/go-server/cmd/server.go
@@ -19,8 +19,10 @@ package main
 
 import (
 	"context"
+	"encoding/base64"
 	"fmt"
 	"log"
+	"net/http"
 	"runtime/debug"
 )
 
@@ -81,13 +83,13 @@ func (s *ChatServer) Chat(ctx context.Context, req *chat.ChatRequest, stream cha
 		}
 
 		if msg.Bin != nil && len(msg.Bin) != 0 {
-			img := string(msg.Bin)
+			decodeByte, err := base64.StdEncoding.DecodeString(string(msg.Bin))
 			if err != nil {
-				log.Println("Decode image error:", err)
-				return fmt.Errorf("decode image error: %s", err)
+				log.Println("GenerateContent failed: %v", err)
+				return fmt.Errorf("GenerateContent failed")
 			}
-
-			messageContent.Parts = append(messageContent.Parts, llms.BinaryPart("image/png", img))
+			imgType := http.DetectContentType(decodeByte)
+			messageContent.Parts = append(messageContent.Parts, llms.BinaryPart(imgType, decodeByte))
 		}
 
 		messages = append(messages, messageContent)