add transshipment and update scrollable offset

This commit is contained in:
Zhang Minghan 2023-11-12 17:40:18 +08:00
parent 607df16cf8
commit d084e544e6
9 changed files with 34 additions and 30 deletions

View File

@ -22,7 +22,7 @@ type Response struct {
}
func GenerateArticle(c *gin.Context, user *auth.User, model string, hash string, title string, prompt string, enableWeb bool) Response {
_, message, quota := manager.NativeChatHandler(c, user, model, []globals.Message{{
message, quota := manager.NativeChatHandler(c, user, model, []globals.Message{{
Role: "user",
Content: fmt.Sprintf("%s\n%s", prompt, title),
}}, enableWeb)

View File

@ -64,13 +64,13 @@ func HandlerAPI(c *gin.Context) {
return
}
keyword, response, quota := manager.NativeChatHandler(c, nil, globals.GPT3Turbo0613, []globals.Message{
response, quota := manager.NativeChatHandler(c, nil, globals.GPT3Turbo0613, []globals.Message{
{Role: "user", Content: message},
}, body.Web)
c.JSON(http.StatusOK, gin.H{
"message": MarkdownConvert(response),
"keyword": keyword,
"keyword": "",
"quota": quota,
})
}

View File

@ -10,8 +10,8 @@ import (
type Hook func(message []globals.Message, token int) (string, error)
func ChatWithWeb(hook Hook, message []globals.Message, long bool) (string, []globals.Message) {
keyword := strings.TrimSpace(GetKeywordPoint(hook, message))
func ChatWithWeb(hook Hook, message []globals.Message, long bool) []globals.Message {
// keyword := strings.TrimSpace(GetKeywordPoint(hook, message))
data := SearchBing(GetPointByLatestMessage(message))
if long {
@ -19,7 +19,7 @@ func ChatWithWeb(hook Hook, message []globals.Message, long bool) (string, []glo
} else {
data = utils.GetSegmentString(data, 3000)
}
return keyword, utils.Insert(message, 0, globals.Message{
return utils.Insert(message, 0, globals.Message{
Role: "system",
Content: fmt.Sprintf("你将扮演AI问答助手你的知识库不是离线的而是可以实时联网的你可以提供实时联网的信息。"+
"当前时间: %s, 实时联网搜索结果:%s",

View File

@ -6,25 +6,23 @@ import (
"chat/manager/conversation"
)
func UsingWebSegment(instance *conversation.Conversation) (string, []globals.Message) {
var keyword string
var segment []globals.Message
func UsingWebSegment(instance *conversation.Conversation) []globals.Message {
segment := conversation.CopyMessage(instance.GetMessageSegment(12))
if instance.IsEnableWeb() {
keyword, segment = ChatWithWeb(func(message []globals.Message, token int) (string, error) {
segment = ChatWithWeb(func(message []globals.Message, token int) (string, error) {
return chatgpt.NewChatInstanceFromConfig("gpt3").CreateChatRequest(&chatgpt.ChatProps{
Model: globals.GPT3TurboInstruct,
Message: message,
Token: token,
})
}, conversation.CopyMessage(instance.GetMessageSegment(12)), globals.IsLongContextModel(instance.GetModel()))
} else {
segment = conversation.CopyMessage(instance.GetMessageSegment(12))
}, segment, globals.IsLongContextModel(instance.GetModel()))
}
return keyword, segment
return segment
}
func UsingWebNativeSegment(enable bool, message []globals.Message) (string, []globals.Message) {
func UsingWebNativeSegment(enable bool, message []globals.Message) []globals.Message {
if enable {
return ChatWithWeb(func(message []globals.Message, token int) (string, error) {
return chatgpt.NewChatInstanceFromConfig("gpt3").CreateChatRequest(&chatgpt.ChatProps{
@ -34,6 +32,6 @@ func UsingWebNativeSegment(enable bool, message []globals.Message) (string, []gl
})
}, message, false)
} else {
return "", message
return message
}
}

View File

@ -33,13 +33,13 @@ function ChatInterface({ setTarget }: ChatInterfaceProps) {
const event = () => {
setScrollable(
el.scrollTop + el.clientHeight + 100 >= el.scrollHeight,
el.scrollTop + el.clientHeight + 20 >= el.scrollHeight, // at bottom
);
}
return addEventListeners(el, [
"scroll", "resize", "touchmove",
"touchend", "touchcancel",
"scroll", "scrollend",
"resize", "touchend",
], event);
}, [ref]);

View File

@ -15,7 +15,6 @@ type CacheProps struct {
}
type CacheData struct {
Keyword string `json:"keyword"`
Message string `json:"message"`
}

View File

@ -61,8 +61,7 @@ func ChatHandler(conn *Connection, user *auth.User, instance *conversation.Conve
}
}()
keyword, segment := web.UsingWebSegment(instance)
conn.Send(globals.ChatSegmentResponse{Keyword: keyword, End: false})
segment := web.UsingWebSegment(instance)
model := instance.GetModel()
db := conn.GetDB()
@ -139,7 +138,6 @@ func ChatHandler(conn *Connection, user *auth.User, instance *conversation.Conve
Model: model,
Reversible: plan,
}, &CacheData{
Keyword: keyword,
Message: result,
})
}

View File

@ -11,7 +11,7 @@ import (
"github.com/gin-gonic/gin"
)
func NativeChatHandler(c *gin.Context, user *auth.User, model string, message []globals.Message, enableWeb bool) (string, string, float32) {
func NativeChatHandler(c *gin.Context, user *auth.User, model string, message []globals.Message, enableWeb bool) (string, float32) {
defer func() {
if err := recover(); err != nil {
globals.Warn(fmt.Sprintf("caught panic from chat handler: %s (instance: %s, client: %s)",
@ -20,14 +20,14 @@ func NativeChatHandler(c *gin.Context, user *auth.User, model string, message []
}
}()
keyword, segment := web.UsingWebNativeSegment(enableWeb, message)
segment := web.UsingWebNativeSegment(enableWeb, message)
db := utils.GetDBFromContext(c)
cache := utils.GetCacheFromContext(c)
check, plan := auth.CanEnableModelWithSubscription(db, cache, user, model)
if !check {
return keyword, defaultQuotaMessage, 0
return defaultQuotaMessage, 0
}
if form := ExtractCacheData(c, &CacheProps{
@ -35,7 +35,7 @@ func NativeChatHandler(c *gin.Context, user *auth.User, model string, message []
Model: model,
Reversible: plan,
}); form != nil {
return form.Keyword, form.Message, 0
return form.Message, 0
}
buffer := utils.NewBuffer(model, segment)
@ -52,7 +52,7 @@ func NativeChatHandler(c *gin.Context, user *auth.User, model string, message []
if err != nil {
auth.RevertSubscriptionUsage(cache, user, model, plan)
CollectQuota(c, user, buffer, plan)
return keyword, err.Error(), GetErrorQuota(model)
return err.Error(), GetErrorQuota(model)
}
CollectQuota(c, user, buffer, plan)
@ -62,9 +62,8 @@ func NativeChatHandler(c *gin.Context, user *auth.User, model string, message []
Model: model,
Reversible: plan,
}, &CacheData{
Keyword: keyword,
Message: buffer.ReadWithDefault(defaultMessage),
})
return keyword, buffer.ReadWithDefault(defaultMessage), buffer.GetQuota()
return buffer.ReadWithDefault(defaultMessage), buffer.GetQuota()
}

View File

@ -2,6 +2,7 @@ package manager
import (
"chat/adapter"
"chat/addition/web"
"chat/admin"
"chat/auth"
"chat/globals"
@ -10,6 +11,7 @@ import (
"github.com/gin-gonic/gin"
"io"
"net/http"
"strings"
"time"
)
@ -108,6 +110,14 @@ func TranshipmentAPI(c *gin.Context) {
return
}
if strings.HasPrefix(form.Model, "web-") {
suffix := strings.TrimPrefix(form.Model, "web-")
if utils.Contains[string](suffix, globals.AllModels) {
form.Model = suffix
form.Messages = web.UsingWebNativeSegment(true, form.Messages)
}
}
if form.Stream {
sendStreamTranshipmentResponse(c, form, id, created, user, plan)
} else {