This commit is contained in:
Liang Ding 2023-03-05 10:27:46 +08:00
parent 5eae66a35e
commit d85568a003
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
4 changed files with 55 additions and 45 deletions

View file

@ -5,7 +5,7 @@
"Properties": { "Properties": {
"id": "20230304000547-ibldj1z", "id": "20230304000547-ibldj1z",
"title": "Artificial Intelligence", "title": "Artificial Intelligence",
"updated": "20230304171533" "updated": "20230305102525"
}, },
"Children": [ "Children": [
{ {
@ -70,7 +70,7 @@
}, },
"Properties": { "Properties": {
"id": "20230304000646-a5e7srt", "id": "20230304000646-a5e7srt",
"updated": "20230304171533" "updated": "20230305102525"
}, },
"Children": [ "Children": [
{ {
@ -114,7 +114,7 @@
}, },
"Properties": { "Properties": {
"id": "20230304000646-6207b6r", "id": "20230304000646-6207b6r",
"updated": "20230304171533" "updated": "20230305102525"
}, },
"Children": [ "Children": [
{ {
@ -137,7 +137,7 @@
"ListData": {}, "ListData": {},
"Properties": { "Properties": {
"id": "20230304000646-aj5szch", "id": "20230304000646-aj5szch",
"updated": "20230304171533" "updated": "20230305102525"
}, },
"Children": [ "Children": [
{ {
@ -186,7 +186,7 @@
}, },
"Properties": { "Properties": {
"id": "20230304000646-78w2ck5", "id": "20230304000646-78w2ck5",
"updated": "20230304000646" "updated": "20230305102525"
}, },
"Children": [ "Children": [
{ {
@ -194,7 +194,7 @@
"Type": "NodeParagraph", "Type": "NodeParagraph",
"Properties": { "Properties": {
"id": "20230304000646-2blt9e0", "id": "20230304000646-2blt9e0",
"updated": "20230304000646" "updated": "20230305102525"
}, },
"Children": [ "Children": [
{ {
@ -208,7 +208,7 @@
}, },
{ {
"Type": "NodeText", "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)"
} }
] ]
} }

View file

@ -5,7 +5,7 @@
"Properties": { "Properties": {
"id": "20230303235619-ex5l63e", "id": "20230303235619-ex5l63e",
"title": "人工智能", "title": "人工智能",
"updated": "20230304171349" "updated": "20230305102554"
}, },
"Children": [ "Children": [
{ {
@ -70,7 +70,7 @@
}, },
"Properties": { "Properties": {
"id": "20230303235749-kurfjky", "id": "20230303235749-kurfjky",
"updated": "20230304171349" "updated": "20230305102554"
}, },
"Children": [ "Children": [
{ {
@ -114,7 +114,7 @@
}, },
"Properties": { "Properties": {
"id": "20230303235819-0o648f9", "id": "20230303235819-0o648f9",
"updated": "20230304171349" "updated": "20230305102554"
}, },
"Children": [ "Children": [
{ {
@ -137,7 +137,7 @@
"ListData": {}, "ListData": {},
"Properties": { "Properties": {
"id": "20230303235950-z00ou0r", "id": "20230303235950-z00ou0r",
"updated": "20230304171349" "updated": "20230305102554"
}, },
"Children": [ "Children": [
{ {
@ -186,7 +186,7 @@
}, },
"Properties": { "Properties": {
"id": "20230303235950-l6o3a5a", "id": "20230303235950-l6o3a5a",
"updated": "20230303235950" "updated": "20230305102554"
}, },
"Children": [ "Children": [
{ {
@ -194,7 +194,7 @@
"Type": "NodeParagraph", "Type": "NodeParagraph",
"Properties": { "Properties": {
"id": "20230303235950-zfhft7e", "id": "20230303235950-zfhft7e",
"updated": "20230303235950" "updated": "20230305102554"
}, },
"Children": [ "Children": [
{ {
@ -208,7 +208,7 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": " 指定超时时间(秒),默认为 15 秒(可选)" "Data": " 指定超时时间(秒),默认为 30 秒(可选)"
} }
] ]
} }

View file

@ -5,7 +5,7 @@
"Properties": { "Properties": {
"id": "20230304000829-9jwu3po", "id": "20230304000829-9jwu3po",
"title": "人工智能", "title": "人工智能",
"updated": "20230304171553" "updated": "20230305102546"
}, },
"Children": [ "Children": [
{ {
@ -70,7 +70,7 @@
}, },
"Properties": { "Properties": {
"id": "20230304000837-t0swnf6", "id": "20230304000837-t0swnf6",
"updated": "20230304171553" "updated": "20230305102546"
}, },
"Children": [ "Children": [
{ {
@ -114,7 +114,7 @@
}, },
"Properties": { "Properties": {
"id": "20230304000837-ktv65zx", "id": "20230304000837-ktv65zx",
"updated": "20230304171553" "updated": "20230305102546"
}, },
"Children": [ "Children": [
{ {
@ -137,7 +137,7 @@
"ListData": {}, "ListData": {},
"Properties": { "Properties": {
"id": "20230304000837-8z180b1", "id": "20230304000837-8z180b1",
"updated": "20230304171553" "updated": "20230305102546"
}, },
"Children": [ "Children": [
{ {
@ -186,7 +186,7 @@
}, },
"Properties": { "Properties": {
"id": "20230304000837-71slt0q", "id": "20230304000837-71slt0q",
"updated": "20230304000837" "updated": "20230305102546"
}, },
"Children": [ "Children": [
{ {
@ -194,7 +194,7 @@
"Type": "NodeParagraph", "Type": "NodeParagraph",
"Properties": { "Properties": {
"id": "20230304000837-t2um8y2", "id": "20230304000837-t2um8y2",
"updated": "20230304000837" "updated": "20230305102546"
}, },
"Children": [ "Children": [
{ {
@ -208,7 +208,7 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": " 指定超時時間(秒),默認為 15 秒(可選)" "Data": " 指定超時時間(秒),默認為 30 秒(可選)"
} }
] ]
} }

View file

@ -19,9 +19,6 @@ package util
import ( import (
"bytes" "bytes"
"context" "context"
"errors"
"github.com/88250/lute/html"
"io"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -35,7 +32,7 @@ import (
var ( var (
OpenAIAPIKey = "" OpenAIAPIKey = ""
OpenAIAPITimeout = 15 * time.Second OpenAIAPITimeout = 30 * time.Second
OpenAIAPIProxy = "" OpenAIAPIProxy = ""
OpenAIAPIMaxTokens = 0 OpenAIAPIMaxTokens = 0
) )
@ -57,7 +54,25 @@ func ChatGPTContinueWrite(msg string, contextMsgs []string) (ret string, retCont
defer ClearPushProgress(100) defer ClearPushProgress(100)
c := newOpenAIClient() 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 var reqMsgs []gogpt.ChatCompletionMessage
if 7 < len(contextMsgs) { if 7 < len(contextMsgs) {
contextMsgs = contextMsgs[len(contextMsgs)-7:] 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) ctx, cancel := context.WithTimeout(context.Background(), OpenAIAPITimeout)
defer cancel() defer cancel()
stream, err := c.CreateChatCompletionStream(ctx, req) resp, err := c.CreateChatCompletion(ctx, req)
if nil != err { 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 return
} }
defer stream.Close()
buf := bytes.Buffer{} if 1 > len(resp.Choices) {
for { stop = true
resp, recvErr := stream.Recv() return
if errors.Is(recvErr, io.EOF) {
break
} }
if nil != recvErr { buf := &strings.Builder{}
logging.LogErrorf("create chat completion stream recv failed: %s", recvErr) choice := resp.Choices[0]
break buf.WriteString(choice.Message.Content)
} if "length" == choice.FinishReason {
stop = false
for _, choice := range resp.Choices { } else {
content := choice.Delta.Content stop = true
buf.WriteString(content)
PushEndlessProgress(html.EscapeHTMLStr(buf.String()))
}
} }
ret = buf.String() ret = buf.String()
ret = strings.TrimSpace(ret) ret = strings.TrimSpace(ret)
retContextMsgs = append(retContextMsgs, msg, ret)
return return
} }