68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package remote_http
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"mylomen_server/common/dto"
|
|
"mylomen_server/common/utils"
|
|
)
|
|
|
|
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": "qwen/qwen-2-7b-instruct: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"`
|
|
}
|