to:sync
This commit is contained in:
parent
07457c047c
commit
a50de57320
14
apps/ai.go
14
apps/ai.go
@ -43,4 +43,18 @@ func InitAiGroup(g *echo.Group) {
|
|||||||
return c.JSON(http.StatusOK, utils.Ok(result.Completions))
|
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))
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
12
infrastructure/remote_http/ai-api-impl_test.go
Normal file
12
infrastructure/remote_http/ai-api-impl_test.go
Normal 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)
|
||||||
|
}
|
@ -10,3 +10,4 @@ type aiApi interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var AiApi aiApi = new(aiApiImpl)
|
var AiApi aiApi = new(aiApiImpl)
|
||||||
|
var AiOpenRouter aiApi = new(aiRouterImpl)
|
||||||
|
67
infrastructure/remote_http/ai-router-impl.go
Normal file
67
infrastructure/remote_http/ai-router-impl.go
Normal 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"`
|
||||||
|
}
|
3
main.go
3
main.go
@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"ai-gateway/apps"
|
"ai-gateway/apps"
|
||||||
"ai-gateway/common/constant"
|
"ai-gateway/common/constant"
|
||||||
"ai-gateway/common/filter"
|
|
||||||
"ai-gateway/common/utils"
|
"ai-gateway/common/utils"
|
||||||
"ai-gateway/static"
|
"ai-gateway/static"
|
||||||
"context"
|
"context"
|
||||||
@ -74,7 +73,7 @@ func main() {
|
|||||||
c.Set(constant.BizId, constant.BizIdAI)
|
c.Set(constant.BizId, constant.BizIdAI)
|
||||||
return next(c)
|
return next(c)
|
||||||
}
|
}
|
||||||
}, filter.UserAuth, filter.UseLimit))
|
} /*filter.UserAuth,*/ /*filter.UseLimit*/))
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -43,16 +43,15 @@
|
|||||||
requests: {
|
requests: {
|
||||||
send: function (msg) {
|
send: function (msg) {
|
||||||
// alert(msg.content.text)
|
// alert(msg.content.text)
|
||||||
messages.push({role: "user", content: 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});
|
const body = JSON.stringify({ "prompt": msg.content.text});
|
||||||
// console.log(body);
|
// console.log(body);
|
||||||
if (msg.type === 'text') {
|
if (msg.type === 'text') {
|
||||||
return {
|
return {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: 'https://openrouter.ai/api/v1/chat/completions',
|
url: '/v1/ai/completionsTest',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': 'Bearer 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'
|
|
||||||
},
|
},
|
||||||
body: body
|
body: body
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user