Alanxtl commented on code in PR #823:
URL: https://github.com/apache/dubbo-go-samples/pull/823#discussion_r2030051062


##########
llm/go-server/cmd/server.go:
##########
@@ -23,38 +23,54 @@ import (
        "fmt"
        "log"
        "net/http"
-       "os"
        "runtime/debug"
+       "strings"
 )
 
 import (
        _ "dubbo.apache.org/dubbo-go/v3/imports"
        "dubbo.apache.org/dubbo-go/v3/protocol"
        "dubbo.apache.org/dubbo-go/v3/server"
 
-       "github.com/joho/godotenv"
-
        "github.com/tmc/langchaingo/llms"
        "github.com/tmc/langchaingo/llms/ollama"
 )
 
 import (
+       "github.com/apache/dubbo-go-samples/llm/config"
        chat "github.com/apache/dubbo-go-samples/llm/proto"
 )
 
 type ChatServer struct {
-       llm *ollama.LLM
+       llms map[string]*ollama.LLM
 }
 
 func NewChatServer() (*ChatServer, error) {
-       llm, err := ollama.New(
-               ollama.WithModel(os.Getenv("OLLAMA_MODEL")),
-               ollama.WithServerURL(os.Getenv("OLLAMA_URL")),
-       )
+       cfg, err := config.GetConfig()
        if err != nil {
-               return nil, err
+               return nil, fmt.Errorf("Error loading config: %v\n", err)
        }
-       return &ChatServer{llm: llm}, nil
+
+       llmMap := make(map[string]*ollama.LLM)
+
+       for _, model := range cfg.OllamaModels {
+               model = strings.TrimSpace(model)

Review Comment:
   here



##########
llm/config/config.go:
##########
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package config
+
+import (
+       "fmt"
+       "os"
+       "strconv"
+       "strings"
+       "sync"
+)
+
+import (
+       "github.com/joho/godotenv"
+)
+
+type Config struct {
+       OllamaModels []string
+       OllamaURL    string
+
+       TimeoutSeconds int
+}
+
+var (
+       config     *Config
+       configOnce sync.Once
+       configErr  error
+)
+
+func Load(envFile string) (*Config, error) {
+       configOnce.Do(func() {
+               config = &Config{}
+               err := godotenv.Load(envFile)
+               if err != nil {
+                       configErr = fmt.Errorf("error loading .env file: %v", 
err)
+                       return
+               }
+
+               modelsEnv := os.Getenv("OLLAMA_MODELS")
+               if modelsEnv == "" {
+                       configErr = fmt.Errorf("error: OLLAMA_MODELS 
environment variable is not set")
+                       return
+               }
+
+               modelsList := strings.Split(modelsEnv, ",")
+               for i, model := range modelsList {
+                       modelsList[i] = strings.TrimSpace(model)

Review Comment:
   ```suggestion
                        modelsList[i] = strings.TrimSpace(model)
   ```
   config 里面已经 TrimSpace 过了 调用cfg.OllamaModels 的时候为什么还要再 TrimSpace 一遍



##########
llm/go-client/frontend/main.go:
##########
@@ -29,36 +30,26 @@ import (
 
        "github.com/gin-contrib/sessions"
        "github.com/gin-contrib/sessions/cookie"
-
        "github.com/gin-gonic/gin"
-
-       "github.com/joho/godotenv"
 )
 
 import (
+       "github.com/apache/dubbo-go-samples/llm/config"
        "github.com/apache/dubbo-go-samples/llm/go-client/frontend/handlers"
        "github.com/apache/dubbo-go-samples/llm/go-client/frontend/service"
        chat "github.com/apache/dubbo-go-samples/llm/proto"
 )
 
 func main() {
-       err := godotenv.Load(".env")
+       cfg, err := config.GetConfig()
        if err != nil {
-               panic(fmt.Sprintf("Error loading .env file: %v", err))
-       }
-
-       _, exist := os.LookupEnv("TIME_OUT_SECOND")
-
-       if !exist {
-               fmt.Println("TIME_OUT_SECOND is not set")
+               fmt.Printf("Error loading config: %v\n", err)
                return
        }
 
-       _, exist = os.LookupEnv("OLLAMA_MODEL")
-
-       if !exist {
-               fmt.Println("OLLAMA_MODEL is not set")
-               return
+       models := cfg.OllamaModels
+       for i, model := range models {
+               models[i] = strings.TrimSpace(model)

Review Comment:
   here



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org
For additional commands, e-mail: notifications-h...@dubbo.apache.org

Reply via email to