Compare commits

...

4 Commits

Author SHA1 Message Date
Laisky.Cai
abffbeedaa Merge be0f63931f into 8df4a2670b 2025-03-26 10:23:50 +00:00
Laisky.Cai
be0f63931f feat: add ForceEmailTLSVerify configuration and improve email sending error handling
closes #50
2025-03-26 10:23:43 +00:00
Laisky.Cai
e6d70c8bf7 fix: extend model check in GetModelModalities function to include additional models 2025-03-26 04:00:36 +00:00
JustSong
8df4a2670b docs: update ByteDance Doubao model link in README
Some checks failed
CI / Unit tests (push) Has been cancelled
CI / commit_lint (push) Has been cancelled
2025-02-21 19:30:16 +08:00
5 changed files with 62 additions and 28 deletions

View File

@@ -72,7 +72,7 @@ _✨ 通过标准的 OpenAI API 格式访问所有的大模型,开箱即用
+ [x] [Anthropic Claude 系列模型](https://anthropic.com) (支持 AWS Claude)
+ [x] [Google PaLM2/Gemini 系列模型](https://developers.generativeai.google)
+ [x] [Mistral 系列模型](https://mistral.ai/)
+ [x] [字节跳动豆包大模型](https://console.volcengine.com/ark/region:ark+cn-beijing/model)
+ [x] [字节跳动豆包大模型(火山引擎)](https://www.volcengine.com/experience/ark?utm_term=202502dsinvite&ac=DSASUQY5&rc=2QXCA1VI)
+ [x] [百度文心一言系列模型](https://cloud.baidu.com/doc/WENXINWORKSHOP/index.html)
+ [x] [阿里通义千问系列模型](https://help.aliyun.com/document_detail/2400395.html)
+ [x] [讯飞星火认知大模型](https://www.xfyun.cn/doc/spark/Web.html)

View File

@@ -109,6 +109,9 @@ var RequestInterval = time.Duration(requestInterval) * time.Second
var SyncFrequency = env.Int("SYNC_FREQUENCY", 10*60) // unit is second
// ForceEmailTLSVerify is used to determine whether to force TLS verification for email
var ForceEmailTLSVerify = env.Bool("FORCE_EMAIL_TLS_VERIFY", false)
var BatchUpdateEnabled = false
var BatchUpdateInterval = env.Int("BATCH_UPDATE_INTERVAL", 5)

View File

@@ -3,13 +3,14 @@ package env
import (
"os"
"strconv"
"strings"
)
func Bool(env string, defaultValue bool) bool {
if env == "" || os.Getenv(env) == "" {
return defaultValue
}
return os.Getenv(env) == "true"
return strings.ToLower(os.Getenv(env)) == "true"
}
func Int(env string, defaultValue int) int {

View File

@@ -10,6 +10,7 @@ import (
"strings"
"time"
"github.com/pkg/errors"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/logger"
)
@@ -27,17 +28,17 @@ func SendEmail(subject string, receiver string, content string) error {
}
encodedSubject := fmt.Sprintf("=?UTF-8?B?%s?=", base64.StdEncoding.EncodeToString([]byte(subject)))
// Extract domain from SMTPFrom
// Extract domain from SMTPFrom with fallback
domain := "localhost"
parts := strings.Split(config.SMTPFrom, "@")
var domain string
if len(parts) > 1 {
if len(parts) > 1 && parts[1] != "" {
domain = parts[1]
}
// Generate a unique Message-ID
buf := make([]byte, 16)
_, err := rand.Read(buf)
if err != nil {
return err
if _, err := rand.Read(buf); err != nil {
return errors.Wrap(err, "failed to generate random bytes for Message-ID")
}
messageId := fmt.Sprintf("<%x@%s>", buf, domain)
@@ -50,59 +51,85 @@ func SendEmail(subject string, receiver string, content string) error {
receiver, config.SystemName, config.SMTPFrom, encodedSubject, messageId, time.Now().Format(time.RFC1123Z), content))
auth := smtp.PlainAuth("", config.SMTPAccount, config.SMTPToken, config.SMTPServer)
addr := fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort)
to := strings.Split(receiver, ";")
addr := net.JoinHostPort(config.SMTPServer, fmt.Sprintf("%d", config.SMTPPort))
// Clean up recipient addresses
receiverEmails := []string{}
for _, email := range strings.Split(receiver, ";") {
email = strings.TrimSpace(email)
if email != "" {
receiverEmails = append(receiverEmails, email)
}
}
if len(receiverEmails) == 0 {
return errors.New("no valid recipient email addresses")
}
if config.SMTPPort == 465 || !shouldAuth() {
// need advanced client
var conn net.Conn
var err error
// Add connection timeout
dialer := &net.Dialer{
Timeout: 30 * time.Second,
}
if config.SMTPPort == 465 {
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
InsecureSkipVerify: !config.ForceEmailTLSVerify,
ServerName: config.SMTPServer,
}
conn, err = tls.Dial("tcp", fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort), tlsConfig)
conn, err = tls.DialWithDialer(dialer, "tcp", addr, tlsConfig)
} else {
conn, err = net.Dial("tcp", fmt.Sprintf("%s:%d", config.SMTPServer, config.SMTPPort))
conn, err = dialer.Dial("tcp", addr)
}
if err != nil {
return err
return errors.Wrap(err, "failed to connect to SMTP server")
}
client, err := smtp.NewClient(conn, config.SMTPServer)
if err != nil {
return err
return errors.Wrap(err, "failed to create SMTP client")
}
defer client.Close()
if shouldAuth() {
if err = client.Auth(auth); err != nil {
return err
return errors.Wrap(err, "SMTP authentication failed")
}
}
if err = client.Mail(config.SMTPFrom); err != nil {
return err
return errors.Wrap(err, "failed to set MAIL FROM")
}
receiverEmails := strings.Split(receiver, ";")
for _, receiver := range receiverEmails {
if err = client.Rcpt(receiver); err != nil {
return err
return errors.Wrapf(err, "failed to add recipient: %s", receiver)
}
}
w, err := client.Data()
if err != nil {
return err
return errors.Wrap(err, "failed to create message data writer")
}
_, err = w.Write(mail)
if err != nil {
return err
if _, err = w.Write(mail); err != nil {
return errors.Wrap(err, "failed to write email content")
}
err = w.Close()
if err != nil {
return err
if err = w.Close(); err != nil {
return errors.Wrap(err, "failed to close message data writer")
}
return nil
}
err = smtp.SendMail(addr, auth, config.SMTPAccount, to, mail)
// Use the same sender address in the SMTP protocol as in the From header
err := smtp.SendMail(addr, auth, config.SMTPFrom, receiverEmails, mail)
if err != nil && strings.Contains(err.Error(), "short response") { // 部分提供商返回该错误,但实际上邮件已经发送成功
logger.SysWarnf("short response from SMTP server, return nil instead of error: %s", err.Error())
return nil

View File

@@ -31,7 +31,10 @@ func GetModelModalities(model string) []string {
}
// Until 2025-03-26, the following models do not accept the responseModalities field
if model == "gemini-2.5-pro-exp-03-25" {
if model == "gemini-2.5-pro-exp-03-25" ||
model == "aqa" ||
strings.HasPrefix(model, "gemma") ||
strings.HasPrefix(model, "text-embed") {
return nil
}