From 342fbf9a612beb97bc577ecf3ea6b02ddba180cd Mon Sep 17 00:00:00 2001 From: shaoyongjun Date: Tue, 8 Oct 2024 22:06:28 +0800 Subject: [PATCH] to:sync --- apps/ai.go | 2 +- common/config/config.dev.yaml | 2 + common/config/config.prod.yaml | 2 + common/config/init.go | 1 + infrastructure/remote_http/ai-api.go | 1 + infrastructure/remote_http/ai_ollama_imp.go | 77 +++++++++++++++++++ .../{ai-router-impl.go => ai_router_impl.go} | 0 .../{ai-api-impl.go => ai_service_imp.go} | 0 ...pi-impl_test.go => ai_service_imp_test.go} | 5 ++ static/mylomen.html | 4 +- 10 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 infrastructure/remote_http/ai_ollama_imp.go rename infrastructure/remote_http/{ai-router-impl.go => ai_router_impl.go} (100%) rename infrastructure/remote_http/{ai-api-impl.go => ai_service_imp.go} (100%) rename infrastructure/remote_http/{ai-api-impl_test.go => ai_service_imp_test.go} (57%) diff --git a/apps/ai.go b/apps/ai.go index 84ca1af..dc2a897 100644 --- a/apps/ai.go +++ b/apps/ai.go @@ -49,7 +49,7 @@ func InitAiGroup(g *echo.Group) { return c.JSON(500, err) } - result, err := remote_http.AiOpenRouter.Completions(context.Background(), req.Prompt, "") + result, err := remote_http.AiOllama.Completions(context.Background(), req.Prompt, "") if err != nil || result == nil { return c.JSON(http.StatusOK, utils.Failed(err.Error())) } diff --git a/common/config/config.dev.yaml b/common/config/config.dev.yaml index 8d7f290..931823d 100644 --- a/common/config/config.dev.yaml +++ b/common/config/config.dev.yaml @@ -12,6 +12,8 @@ fbConsul: # rpc rpc: api-golang: http://api-golang.consul.service:8080 + wss: http://127.0.0.1:8080 + ollama: https://localaiapi.mylomen.com # pgSql pgSql: diff --git a/common/config/config.prod.yaml b/common/config/config.prod.yaml index b60b5a4..1c60d16 100644 --- a/common/config/config.prod.yaml +++ b/common/config/config.prod.yaml @@ -12,6 +12,8 @@ fbConsul: # rpc rpc: api-golang: http://api-golang.consul.service:8080 + wss: http://127.0.0.1:8080 + ollama: http://10.0.12.8:11434 # pgSql pgSql: diff --git a/common/config/init.go b/common/config/init.go index 3bfa07d..09d15f7 100644 --- a/common/config/init.go +++ b/common/config/init.go @@ -21,6 +21,7 @@ type FbConsul struct { type Rpc struct { ApiGolangAddress string `mapstructure:"api-golang" json:"api-golang" yaml:"api-golang"` WssAddress string `mapstructure:"wss" json:"wss" yaml:"wss"` + OllamaAddress string `mapstructure:"ollama" json:"ollama" yaml:"ollama"` } // PgSql mysql配置 diff --git a/infrastructure/remote_http/ai-api.go b/infrastructure/remote_http/ai-api.go index 313bd0b..5805b11 100644 --- a/infrastructure/remote_http/ai-api.go +++ b/infrastructure/remote_http/ai-api.go @@ -11,3 +11,4 @@ type aiApi interface { var AiApi aiApi = new(aiApiImpl) var AiOpenRouter aiApi = new(aiRouterImpl) +var AiOllama aiApi = new(aiOllamaImpl) diff --git a/infrastructure/remote_http/ai_ollama_imp.go b/infrastructure/remote_http/ai_ollama_imp.go new file mode 100644 index 0000000..86db94e --- /dev/null +++ b/infrastructure/remote_http/ai_ollama_imp.go @@ -0,0 +1,77 @@ +package remote_http + +import ( + "context" + "errors" + "fmt" + "mylomen_server/common/config" + "mylomen_server/common/dto" + "mylomen_server/common/utils" + "time" +) + +type aiOllamaImpl struct { +} + +func (aiOllamaImpl) Completions(ctx context.Context, prompt, groupId string) (*dto.AiRes, error) { + logger := utils.NewLog("") + + url := config.Instance.Rpc.OllamaAddress + "/api/chat" + + var aiResult Result[dto.AiRes] + + var result chatVO + resp, err := httpClient.R(). + SetBody(map[string]interface{}{ + "model": "qwen2.5:0.5b", + "stream": false, + "messages": []map[string]interface{}{{"role": "user", "content": prompt}}, + }).SetHeaders(map[string]string{ + "Content-Type": "application/json", + }).SetSuccessResult(&result).Post(url) + if err != nil { + logger.Error(fmt.Sprintf("remote_http_wx_SendMsg error: %s", err.Error())) + return nil, err + } + + if !resp.IsSuccessState() { + logger.Error(fmt.Sprintf("remote_http_wx_SendMsg resp:%+v", resp)) + return nil, errors.New("接口异常") + } + + aiResult.Data.Completions = result.Message.Content + + return &aiResult.Data, nil +} + +type generateVO struct { + Model string `json:"model"` + CreatedAt time.Time `json:"created_at"` + Response string `json:"response"` + Done bool `json:"done"` + DoneReason string `json:"done_reason"` + Context []int `json:"context"` + TotalDuration int64 `json:"total_duration"` + LoadDuration int64 `json:"load_duration"` + PromptEvalCount int `json:"prompt_eval_count"` + PromptEvalDuration int `json:"prompt_eval_duration"` + EvalCount int `json:"eval_count"` + EvalDuration int64 `json:"eval_duration"` +} + +type chatVO struct { + Model string `json:"model"` + CreatedAt time.Time `json:"created_at"` + Message struct { + Role string `json:"role"` + Content string `json:"content"` + } `json:"message"` + DoneReason string `json:"done_reason"` + Done bool `json:"done"` + TotalDuration int64 `json:"total_duration"` + LoadDuration int `json:"load_duration"` + PromptEvalCount int `json:"prompt_eval_count"` + PromptEvalDuration int `json:"prompt_eval_duration"` + EvalCount int `json:"eval_count"` + EvalDuration int `json:"eval_duration"` +} diff --git a/infrastructure/remote_http/ai-router-impl.go b/infrastructure/remote_http/ai_router_impl.go similarity index 100% rename from infrastructure/remote_http/ai-router-impl.go rename to infrastructure/remote_http/ai_router_impl.go diff --git a/infrastructure/remote_http/ai-api-impl.go b/infrastructure/remote_http/ai_service_imp.go similarity index 100% rename from infrastructure/remote_http/ai-api-impl.go rename to infrastructure/remote_http/ai_service_imp.go diff --git a/infrastructure/remote_http/ai-api-impl_test.go b/infrastructure/remote_http/ai_service_imp_test.go similarity index 57% rename from infrastructure/remote_http/ai-api-impl_test.go rename to infrastructure/remote_http/ai_service_imp_test.go index f4762c6..a0e84b2 100644 --- a/infrastructure/remote_http/ai-api-impl_test.go +++ b/infrastructure/remote_http/ai_service_imp_test.go @@ -10,3 +10,8 @@ func TestOpenRouter(t *testing.T) { res, err := AiOpenRouter.Completions(context.Background(), "你好", "test") fmt.Println(res, err) } + +func TestOllamaRouter(t *testing.T) { + res, err := AiOllama.Completions(context.Background(), "红烧肉怎么做", "test") + fmt.Println(res, err) +} diff --git a/static/mylomen.html b/static/mylomen.html index f6d55fa..a504b59 100644 --- a/static/mylomen.html +++ b/static/mylomen.html @@ -23,8 +23,8 @@ height: 100%; width: 100%; - top: 0px; - left: 0px; + top: 0; + left: 0; } .myPopWindow4Login {