Files
siyuan/kernel/util/openai.go
T
2026-06-13 12:09:59 +08:00

146 lines
4.0 KiB
Go

// SiYuan - Refactor your thinking
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package util
import (
"context"
"net/http"
"strings"
"time"
"github.com/sashabaranov/go-openai"
"github.com/siyuan-note/logging"
)
func ChatGPT(msg string, contextMsgs []string, c *openai.Client, model string, maxTokens int, temperature float64, timeout int) (ret string, stop bool, err error) {
var reqMsgs []openai.ChatCompletionMessage
for _, ctxMsg := range contextMsgs {
if "" == ctxMsg {
continue
}
reqMsgs = append(reqMsgs, openai.ChatCompletionMessage{
Role: "user",
Content: ctxMsg,
})
}
if "" != msg {
reqMsgs = append(reqMsgs, openai.ChatCompletionMessage{
Role: "user",
Content: msg,
})
}
if 1 > len(reqMsgs) {
stop = true
return
}
req := openai.ChatCompletionRequest{
Model: model,
MaxCompletionTokens: maxTokens,
Temperature: float32(temperature),
Messages: reqMsgs,
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
resp, err := c.CreateChatCompletion(ctx, req)
if err != nil {
PushErrMsg("Requesting failed, please check kernel log for more details", 3000)
logging.LogErrorf("create chat completion failed: %s", err)
stop = true
return
}
if 1 > len(resp.Choices) {
stop = true
return
}
buf := &strings.Builder{}
choice := resp.Choices[0]
buf.WriteString(choice.Message.Content)
if "length" == choice.FinishReason {
stop = false
} else {
stop = true
}
ret = buf.String()
ret = strings.TrimSpace(ret)
return
}
func NewOpenAIClient(apiKey, apiBaseURL string) *openai.Client {
config := openai.DefaultConfig(apiKey)
config.BaseURL = apiBaseURL
return openai.NewClientWithConfig(config)
}
func IsNetworkError(err error) bool {
msg := strings.ToLower(err.Error())
return strings.Contains(msg, "actively refused") ||
strings.Contains(msg, "connection refused") ||
strings.Contains(msg, "no such host") ||
strings.Contains(msg, "connection failed") ||
strings.Contains(msg, "hostname resolution") ||
strings.Contains(msg, "no address associated with hostname") ||
strings.Contains(msg, "request canceled while waiting for connection") ||
strings.Contains(msg, "exceeded while awaiting") ||
strings.Contains(msg, "context deadline exceeded") ||
strings.Contains(msg, "timeout") ||
strings.Contains(msg, "connection") ||
strings.Contains(msg, "refused") ||
strings.Contains(msg, "socket") ||
strings.Contains(msg, "eof") ||
strings.Contains(msg, "closed") ||
strings.Contains(msg, "network")
}
func BatchGetEmbeddings(texts []string, apiKey, baseURL, model string, timeout int) (ret [][]float32, err error) {
if 1 > len(texts) {
return
}
config := openai.DefaultConfig(apiKey)
config.BaseURL = baseURL
config.HTTPClient = &http.Client{
Timeout: time.Duration(timeout) * time.Second,
Transport: &http.Transport{},
}
client := openai.NewClientWithConfig(config)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
resp, err := client.CreateEmbeddings(ctx, openai.EmbeddingRequestStrings{
Input: texts,
Model: openai.EmbeddingModel(model),
})
if err != nil {
logging.LogErrorf("create embeddings failed: %s", err)
return
}
for _, data := range resp.Data {
ret = append(ret, data.Embedding)
}
return
}