mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 07:00:12 +01:00
✨ 桌面端接入 OpenAI Chat API https://github.com/siyuan-note/siyuan/issues/7560
This commit is contained in:
parent
5eae66a35e
commit
d85568a003
4 changed files with 55 additions and 45 deletions
|
|
@ -5,7 +5,7 @@
|
|||
"Properties": {
|
||||
"id": "20230304000547-ibldj1z",
|
||||
"title": "Artificial Intelligence",
|
||||
"updated": "20230304171533"
|
||||
"updated": "20230305102525"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -70,7 +70,7 @@
|
|||
},
|
||||
"Properties": {
|
||||
"id": "20230304000646-a5e7srt",
|
||||
"updated": "20230304171533"
|
||||
"updated": "20230305102525"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -114,7 +114,7 @@
|
|||
},
|
||||
"Properties": {
|
||||
"id": "20230304000646-6207b6r",
|
||||
"updated": "20230304171533"
|
||||
"updated": "20230305102525"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -137,7 +137,7 @@
|
|||
"ListData": {},
|
||||
"Properties": {
|
||||
"id": "20230304000646-aj5szch",
|
||||
"updated": "20230304171533"
|
||||
"updated": "20230305102525"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -186,7 +186,7 @@
|
|||
},
|
||||
"Properties": {
|
||||
"id": "20230304000646-78w2ck5",
|
||||
"updated": "20230304000646"
|
||||
"updated": "20230305102525"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -194,7 +194,7 @@
|
|||
"Type": "NodeParagraph",
|
||||
"Properties": {
|
||||
"id": "20230304000646-2blt9e0",
|
||||
"updated": "20230304000646"
|
||||
"updated": "20230305102525"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -208,7 +208,7 @@
|
|||
},
|
||||
{
|
||||
"Type": "NodeText",
|
||||
"Data": " specifies the timeout time (seconds), the default is 15 seconds (optional)"
|
||||
"Data": " specifies the timeout time (seconds), the default is 30 seconds (optional)"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
"Properties": {
|
||||
"id": "20230303235619-ex5l63e",
|
||||
"title": "人工智能",
|
||||
"updated": "20230304171349"
|
||||
"updated": "20230305102554"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -70,7 +70,7 @@
|
|||
},
|
||||
"Properties": {
|
||||
"id": "20230303235749-kurfjky",
|
||||
"updated": "20230304171349"
|
||||
"updated": "20230305102554"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -114,7 +114,7 @@
|
|||
},
|
||||
"Properties": {
|
||||
"id": "20230303235819-0o648f9",
|
||||
"updated": "20230304171349"
|
||||
"updated": "20230305102554"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -137,7 +137,7 @@
|
|||
"ListData": {},
|
||||
"Properties": {
|
||||
"id": "20230303235950-z00ou0r",
|
||||
"updated": "20230304171349"
|
||||
"updated": "20230305102554"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -186,7 +186,7 @@
|
|||
},
|
||||
"Properties": {
|
||||
"id": "20230303235950-l6o3a5a",
|
||||
"updated": "20230303235950"
|
||||
"updated": "20230305102554"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -194,7 +194,7 @@
|
|||
"Type": "NodeParagraph",
|
||||
"Properties": {
|
||||
"id": "20230303235950-zfhft7e",
|
||||
"updated": "20230303235950"
|
||||
"updated": "20230305102554"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -208,7 +208,7 @@
|
|||
},
|
||||
{
|
||||
"Type": "NodeText",
|
||||
"Data": " 指定超时时间(秒),默认为 15 秒(可选)"
|
||||
"Data": " 指定超时时间(秒),默认为 30 秒(可选)"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
"Properties": {
|
||||
"id": "20230304000829-9jwu3po",
|
||||
"title": "人工智能",
|
||||
"updated": "20230304171553"
|
||||
"updated": "20230305102546"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -70,7 +70,7 @@
|
|||
},
|
||||
"Properties": {
|
||||
"id": "20230304000837-t0swnf6",
|
||||
"updated": "20230304171553"
|
||||
"updated": "20230305102546"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -114,7 +114,7 @@
|
|||
},
|
||||
"Properties": {
|
||||
"id": "20230304000837-ktv65zx",
|
||||
"updated": "20230304171553"
|
||||
"updated": "20230305102546"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -137,7 +137,7 @@
|
|||
"ListData": {},
|
||||
"Properties": {
|
||||
"id": "20230304000837-8z180b1",
|
||||
"updated": "20230304171553"
|
||||
"updated": "20230305102546"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -186,7 +186,7 @@
|
|||
},
|
||||
"Properties": {
|
||||
"id": "20230304000837-71slt0q",
|
||||
"updated": "20230304000837"
|
||||
"updated": "20230305102546"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -194,7 +194,7 @@
|
|||
"Type": "NodeParagraph",
|
||||
"Properties": {
|
||||
"id": "20230304000837-t2um8y2",
|
||||
"updated": "20230304000837"
|
||||
"updated": "20230305102546"
|
||||
},
|
||||
"Children": [
|
||||
{
|
||||
|
|
@ -208,7 +208,7 @@
|
|||
},
|
||||
{
|
||||
"Type": "NodeText",
|
||||
"Data": " 指定超時時間(秒),默認為 15 秒(可選)"
|
||||
"Data": " 指定超時時間(秒),默認為 30 秒(可選)"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,9 +19,6 @@ package util
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/88250/lute/html"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
|
@ -35,7 +32,7 @@ import (
|
|||
|
||||
var (
|
||||
OpenAIAPIKey = ""
|
||||
OpenAIAPITimeout = 15 * time.Second
|
||||
OpenAIAPITimeout = 30 * time.Second
|
||||
OpenAIAPIProxy = ""
|
||||
OpenAIAPIMaxTokens = 0
|
||||
)
|
||||
|
|
@ -57,7 +54,25 @@ func ChatGPTContinueWrite(msg string, contextMsgs []string) (ret string, retCont
|
|||
defer ClearPushProgress(100)
|
||||
|
||||
c := newOpenAIClient()
|
||||
buf := &bytes.Buffer{}
|
||||
for i := 0; i < 7; i++ {
|
||||
part, stop := chatGPT(msg, contextMsgs, c)
|
||||
buf.WriteString(part)
|
||||
|
||||
if stop {
|
||||
break
|
||||
}
|
||||
|
||||
PushEndlessProgress("Continue writing...")
|
||||
}
|
||||
|
||||
ret = buf.String()
|
||||
ret = strings.TrimSpace(ret)
|
||||
retContextMsgs = append(retContextMsgs, msg, ret)
|
||||
return
|
||||
}
|
||||
|
||||
func chatGPT(msg string, contextMsgs []string, c *gogpt.Client) (ret string, stop bool) {
|
||||
var reqMsgs []gogpt.ChatCompletionMessage
|
||||
if 7 < len(contextMsgs) {
|
||||
contextMsgs = contextMsgs[len(contextMsgs)-7:]
|
||||
|
|
@ -81,35 +96,30 @@ func ChatGPTContinueWrite(msg string, contextMsgs []string) (ret string, retCont
|
|||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), OpenAIAPITimeout)
|
||||
defer cancel()
|
||||
stream, err := c.CreateChatCompletionStream(ctx, req)
|
||||
resp, err := c.CreateChatCompletion(ctx, req)
|
||||
if nil != err {
|
||||
logging.LogErrorf("create chat completion stream failed: %s", err)
|
||||
PushErrMsg("Requesting failed, please check kernel log for more details", 3000)
|
||||
logging.LogErrorf("create chat completion failed: %s", err)
|
||||
stop = true
|
||||
return
|
||||
}
|
||||
defer stream.Close()
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
for {
|
||||
resp, recvErr := stream.Recv()
|
||||
if errors.Is(recvErr, io.EOF) {
|
||||
break
|
||||
if 1 > len(resp.Choices) {
|
||||
stop = true
|
||||
return
|
||||
}
|
||||
|
||||
if nil != recvErr {
|
||||
logging.LogErrorf("create chat completion stream recv failed: %s", recvErr)
|
||||
break
|
||||
}
|
||||
|
||||
for _, choice := range resp.Choices {
|
||||
content := choice.Delta.Content
|
||||
buf.WriteString(content)
|
||||
PushEndlessProgress(html.EscapeHTMLStr(buf.String()))
|
||||
}
|
||||
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)
|
||||
retContextMsgs = append(retContextMsgs, msg, ret)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue