feat: migrate codes from previous project
This commit is contained in:
50
internal/silicon/client.go
Normal file
50
internal/silicon/client.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package silicon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"rss-to-feishu-next/internal/config"
|
||||
"rss-to-feishu-next/internal/net"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
netClient *net.Client
|
||||
}
|
||||
|
||||
func NewClient(client *net.Client) *Client {
|
||||
return &Client{client}
|
||||
}
|
||||
|
||||
func (c *Client) FetchModelResponse(ctx context.Context, message, model string) (*Response, error) {
|
||||
cfg, err := config.LoadConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
token := cfg.Silicon.Token
|
||||
|
||||
headers := map[string]string{
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bearer " + token,
|
||||
}
|
||||
|
||||
var messages []*Message
|
||||
messages = append(messages, &Message{
|
||||
Role: "user",
|
||||
Content: message,
|
||||
})
|
||||
|
||||
body := &RequestBody{
|
||||
Model: model,
|
||||
Messages: messages,
|
||||
Temperature: 0.7,
|
||||
MaxTokens: 8096,
|
||||
}
|
||||
|
||||
var resp *Response
|
||||
url := "https://api.siliconflow.cn/v1/messages"
|
||||
if err := c.netClient.Post(ctx, headers, url, body, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
21
internal/silicon/types.go
Normal file
21
internal/silicon/types.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package silicon
|
||||
|
||||
type Response struct {
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Content []struct {
|
||||
Text string `json:"text"`
|
||||
} `json:"content"`
|
||||
}
|
||||
|
||||
type RequestBody struct {
|
||||
Model string `json:"model"`
|
||||
Messages []*Message `json:"messages"`
|
||||
Temperature float64 `json:"temperature"`
|
||||
MaxTokens int `json:"max_tokens"`
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
Reference in New Issue
Block a user