This commit is contained in:
shaoyongjun 2024-05-22 17:39:32 +08:00
parent 07457c047c
commit a50de57320
6 changed files with 98 additions and 6 deletions

View File

@ -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))
})
}

View File

@ -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)
}

View File

@ -10,3 +10,4 @@ type aiApi interface {
}
var AiApi aiApi = new(aiApiImpl)
var AiOpenRouter aiApi = new(aiRouterImpl)

View File

@ -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"`
}

View File

@ -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() {

View File

@ -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
};