diff --git a/apps/ai.go b/apps/ai.go index 7c9f7b0..815bf23 100644 --- a/apps/ai.go +++ b/apps/ai.go @@ -43,4 +43,18 @@ func InitAiGroup(g *echo.Group) { return c.JSON(http.StatusOK, utils.Ok(result.Completions)) }) + g.POST("completionsTest", func(c echo.Context) error { + req := new(dto.AiReq) + if err := c.Bind(req); err != nil { + return c.JSON(500, err) + } + + result, err := remote_http.AiOpenRouter.Completions(context.Background(), req.Prompt, "") + if err != nil || result == nil { + return c.JSON(http.StatusOK, utils.Failed(err.Error())) + } + + return c.JSON(http.StatusOK, utils.Ok(result.Completions)) + }) + } diff --git a/infrastructure/remote_http/ai-api-impl_test.go b/infrastructure/remote_http/ai-api-impl_test.go new file mode 100644 index 0000000..f4762c6 --- /dev/null +++ b/infrastructure/remote_http/ai-api-impl_test.go @@ -0,0 +1,12 @@ +package remote_http + +import ( + "context" + "fmt" + "testing" +) + +func TestOpenRouter(t *testing.T) { + res, err := AiOpenRouter.Completions(context.Background(), "你好", "test") + fmt.Println(res, err) +} diff --git a/infrastructure/remote_http/ai-api.go b/infrastructure/remote_http/ai-api.go index 5bea18d..baab6c7 100644 --- a/infrastructure/remote_http/ai-api.go +++ b/infrastructure/remote_http/ai-api.go @@ -10,3 +10,4 @@ type aiApi interface { } var AiApi aiApi = new(aiApiImpl) +var AiOpenRouter aiApi = new(aiRouterImpl) diff --git a/infrastructure/remote_http/ai-router-impl.go b/infrastructure/remote_http/ai-router-impl.go new file mode 100644 index 0000000..2030687 --- /dev/null +++ b/infrastructure/remote_http/ai-router-impl.go @@ -0,0 +1,67 @@ +package remote_http + +import ( + "ai-gateway/common/dto" + "ai-gateway/common/utils" + "context" + "errors" + "fmt" +) + +type aiRouterImpl struct { +} + +func (aiRouterImpl) Completions(ctx context.Context, prompt, groupId string) (*dto.AiRes, error) { + logger := utils.NewLog("") + + url := "https://openrouter.ai/api/v1/chat/completions" + + var aiResult Result[dto.AiRes] + + var result RouterResultVO + //const body = JSON.stringify({model: "google/gemma-7b-it:free", "stream": false, messages: messages}); + resp, err := httpClient.R(). + SetBody(map[string]interface{}{ + "model": "google/gemma-7b-it:free", + "stream": false, + "messages": []map[string]interface{}{{"role": "user", "content": prompt}}, + }).SetHeaders(map[string]string{ + "Content-Type": "application/json", + "Authorization": "Bearer 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", + }). + 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.Choices[0].Message.Content + + return &aiResult.Data, nil +} + +type RouterResultVO struct { + Id string `json:"id"` + Model string `json:"model"` + Object string `json:"object"` + Created int `json:"created"` + Choices []struct { + Index int `json:"index"` + Message struct { + Role string `json:"role"` + Content string `json:"content"` + } `json:"message"` + FinishReason string `json:"finish_reason"` + } `json:"choices"` + Usage struct { + PromptTokens int `json:"prompt_tokens"` + CompletionTokens int `json:"completion_tokens"` + TotalTokens int `json:"total_tokens"` + } `json:"usage"` +} diff --git a/main.go b/main.go index dda6d74..e58754e 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "ai-gateway/apps" "ai-gateway/common/constant" - "ai-gateway/common/filter" "ai-gateway/common/utils" "ai-gateway/static" "context" @@ -74,7 +73,7 @@ func main() { c.Set(constant.BizId, constant.BizIdAI) return next(c) } - }, filter.UserAuth, filter.UseLimit)) + } /*filter.UserAuth,*/ /*filter.UseLimit*/)) // Start server go func() { diff --git a/static/ai-chat2.html b/static/ai-chat2.html index c68d8e9..28b0adf 100644 --- a/static/ai-chat2.html +++ b/static/ai-chat2.html @@ -43,16 +43,15 @@ requests: { send: function (msg) { // alert(msg.content.text) - messages.push({role: "user", content: msg.content.text}); - const body = JSON.stringify({model: "google/gemma-7b-it:free", "stream": false, messages: messages}); + // messages.push({role: "user", content: msg.content.text}); + const body = JSON.stringify({ "prompt": msg.content.text}); // console.log(body); if (msg.type === 'text') { return { method: 'POST', - url: 'https://openrouter.ai/api/v1/chat/completions', + url: '/v1/ai/completionsTest', headers: { 'Content-Type': 'application/json', - 'Authorization': 'Bearer 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' }, body: body };