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 04455d8b Make chat history count configurable (#826)
04455d8b is described below

commit 04455d8b548ec54350f0c86c49b05e5524286cdd
Author: Thisara Weerakoon <[email protected]>
AuthorDate: Mon Apr 14 01:57:12 2025 +0530

    Make chat history count configurable (#826)
    
    * feat(llm): make max history length configurable (refs 
apache/dubbo-go-samples#824)
    
    Added support for configuring the maximum number of retained history 
records per session in the LLM example. Default remains 3 for backward 
compatibility.
    
    * Fix Go fmt ci error
    
    * Refactor config loading: reduce nesting and add default constant for 
MaxContextCount
    
    * Update llm/go-client/cmd/client.go
    
    Co-authored-by: Alan <[email protected]>
    
    * fix: Declare defaultMaxContextCount as a constant instead of an inline 
variable.
    
    * fix:  add a constant to represent the default TIME_OUT_SECOND value
    
    ---------
    
    Co-authored-by: Alan <[email protected]>
---
 llm/.env.example            |  3 ++-
 llm/config/config.go        | 21 ++++++++++++++++++---
 llm/go-client/cmd/client.go |  8 +++++---
 3 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/llm/.env.example b/llm/.env.example
index e484c71d..e77245c0 100644
--- a/llm/.env.example
+++ b/llm/.env.example
@@ -20,4 +20,5 @@ OLLAMA_MODELS = llava:7b, qwen2.5:7b
 OLLAMA_URL = http://localhost:11434
 TIME_OUT_SECOND = 300
 
-NACOS_URL = localhost:8848
\ No newline at end of file
+NACOS_URL = localhost:8848
+MAX_CONTEXT_COUNT = 3
diff --git a/llm/config/config.go b/llm/config/config.go
index eeaaf05c..475b6c01 100644
--- a/llm/config/config.go
+++ b/llm/config/config.go
@@ -33,8 +33,9 @@ type Config struct {
        OllamaModels []string
        OllamaURL    string
 
-       TimeoutSeconds int
-       NacosURL       string
+       TimeoutSeconds  int
+       NacosURL        string
+       MaxContextCount int
 }
 
 var (
@@ -43,6 +44,9 @@ var (
        configErr  error
 )
 
+const defaultMaxContextCount = 3 // Default to 3 for backward compatibility
+const defaultTimeoutSeconds = 300
+
 func Load(envFile string) (*Config, error) {
        configOnce.Do(func() {
                config = &Config{}
@@ -78,7 +82,7 @@ func Load(envFile string) (*Config, error) {
 
                timeoutStr := os.Getenv("TIME_OUT_SECOND")
                if timeoutStr == "" {
-                       config.TimeoutSeconds = 300
+                       config.TimeoutSeconds = defaultTimeoutSeconds
                } else {
                        timeout, err := strconv.Atoi(timeoutStr)
                        if err != nil {
@@ -94,6 +98,17 @@ func Load(envFile string) (*Config, error) {
                        return
                }
                config.NacosURL = nacosURL
+               maxContextStr := os.Getenv("MAX_CONTEXT_COUNT")
+               if maxContextStr == "" {
+                       config.MaxContextCount = defaultMaxContextCount
+               } else {
+                       maxContext, err := strconv.Atoi(maxContextStr)
+                       if err != nil {
+                               configErr = fmt.Errorf("invalid 
MAX_CONTEXT_COUNT value: %v", err)
+                               return
+                       }
+                       config.MaxContextCount = maxContext
+               }
        })
 
        return config, configErr
diff --git a/llm/go-client/cmd/client.go b/llm/go-client/cmd/client.go
index 5935b856..31b13c38 100644
--- a/llm/go-client/cmd/client.go
+++ b/llm/go-client/cmd/client.go
@@ -48,6 +48,7 @@ var (
        maxID           uint8 = 0
        availableModels []string
        currentModel    string
+       maxContextCount int
 )
 
 func handleCommand(cmd string) (resp string) {
@@ -64,7 +65,7 @@ func handleCommand(cmd string) (resp string) {
                resp += "/model <name>  - Switch to specified model"
                return resp
        case cmd == "/list":
-               fmt.Println("Stored contexts (max 3):")
+               fmt.Printf("Stored contexts (max %d):\n", maxContextCount)
                for _, ctxID := range contextOrder {
                        resp += fmt.Sprintf("- %s\n", ctxID)
                }
@@ -124,8 +125,8 @@ func createContext() string {
        }
        contextOrder = append(contextOrder, id)
 
-       // up to 3 context
-       if len(contextOrder) > 3 {
+       // Use configurable max context count
+       if len(contextOrder) > maxContextCount {
                delete(contexts, contextOrder[0])
                contextOrder = contextOrder[1:]
        }
@@ -141,6 +142,7 @@ func main() {
 
        availableModels = cfg.OllamaModels
        currentModel = cfg.DefaultModel()
+       maxContextCount = cfg.MaxContextCount
 
        currentCtxID = createContext()
 

Reply via email to