mirror of
https://github.com/coaidev/coai.git
synced 2025-05-31 10:50:21 +09:00
add transshipment and update scrollable offset
This commit is contained in:
parent
607df16cf8
commit
d084e544e6
@ -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 {
|
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",
|
Role: "user",
|
||||||
Content: fmt.Sprintf("%s\n%s", prompt, title),
|
Content: fmt.Sprintf("%s\n%s", prompt, title),
|
||||||
}}, enableWeb)
|
}}, enableWeb)
|
||||||
|
@ -64,13 +64,13 @@ func HandlerAPI(c *gin.Context) {
|
|||||||
return
|
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},
|
{Role: "user", Content: message},
|
||||||
}, body.Web)
|
}, body.Web)
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": MarkdownConvert(response),
|
"message": MarkdownConvert(response),
|
||||||
"keyword": keyword,
|
"keyword": "",
|
||||||
"quota": quota,
|
"quota": quota,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,8 @@ import (
|
|||||||
|
|
||||||
type Hook func(message []globals.Message, token int) (string, error)
|
type Hook func(message []globals.Message, token int) (string, error)
|
||||||
|
|
||||||
func ChatWithWeb(hook Hook, message []globals.Message, long bool) (string, []globals.Message) {
|
func ChatWithWeb(hook Hook, message []globals.Message, long bool) []globals.Message {
|
||||||
keyword := strings.TrimSpace(GetKeywordPoint(hook, message))
|
// keyword := strings.TrimSpace(GetKeywordPoint(hook, message))
|
||||||
data := SearchBing(GetPointByLatestMessage(message))
|
data := SearchBing(GetPointByLatestMessage(message))
|
||||||
|
|
||||||
if long {
|
if long {
|
||||||
@ -19,7 +19,7 @@ func ChatWithWeb(hook Hook, message []globals.Message, long bool) (string, []glo
|
|||||||
} else {
|
} else {
|
||||||
data = utils.GetSegmentString(data, 3000)
|
data = utils.GetSegmentString(data, 3000)
|
||||||
}
|
}
|
||||||
return keyword, utils.Insert(message, 0, globals.Message{
|
return utils.Insert(message, 0, globals.Message{
|
||||||
Role: "system",
|
Role: "system",
|
||||||
Content: fmt.Sprintf("你将扮演AI问答助手,你的知识库不是离线的,而是可以实时联网的,你可以提供实时联网的信息。"+
|
Content: fmt.Sprintf("你将扮演AI问答助手,你的知识库不是离线的,而是可以实时联网的,你可以提供实时联网的信息。"+
|
||||||
"当前时间: %s, 实时联网搜索结果:%s",
|
"当前时间: %s, 实时联网搜索结果:%s",
|
||||||
|
@ -6,25 +6,23 @@ import (
|
|||||||
"chat/manager/conversation"
|
"chat/manager/conversation"
|
||||||
)
|
)
|
||||||
|
|
||||||
func UsingWebSegment(instance *conversation.Conversation) (string, []globals.Message) {
|
func UsingWebSegment(instance *conversation.Conversation) []globals.Message {
|
||||||
var keyword string
|
segment := conversation.CopyMessage(instance.GetMessageSegment(12))
|
||||||
var segment []globals.Message
|
|
||||||
|
|
||||||
if instance.IsEnableWeb() {
|
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{
|
return chatgpt.NewChatInstanceFromConfig("gpt3").CreateChatRequest(&chatgpt.ChatProps{
|
||||||
Model: globals.GPT3TurboInstruct,
|
Model: globals.GPT3TurboInstruct,
|
||||||
Message: message,
|
Message: message,
|
||||||
Token: token,
|
Token: token,
|
||||||
})
|
})
|
||||||
}, conversation.CopyMessage(instance.GetMessageSegment(12)), globals.IsLongContextModel(instance.GetModel()))
|
}, segment, globals.IsLongContextModel(instance.GetModel()))
|
||||||
} else {
|
|
||||||
segment = conversation.CopyMessage(instance.GetMessageSegment(12))
|
|
||||||
}
|
}
|
||||||
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 {
|
if enable {
|
||||||
return ChatWithWeb(func(message []globals.Message, token int) (string, error) {
|
return ChatWithWeb(func(message []globals.Message, token int) (string, error) {
|
||||||
return chatgpt.NewChatInstanceFromConfig("gpt3").CreateChatRequest(&chatgpt.ChatProps{
|
return chatgpt.NewChatInstanceFromConfig("gpt3").CreateChatRequest(&chatgpt.ChatProps{
|
||||||
@ -34,6 +32,6 @@ func UsingWebNativeSegment(enable bool, message []globals.Message) (string, []gl
|
|||||||
})
|
})
|
||||||
}, message, false)
|
}, message, false)
|
||||||
} else {
|
} else {
|
||||||
return "", message
|
return message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,13 +33,13 @@ function ChatInterface({ setTarget }: ChatInterfaceProps) {
|
|||||||
|
|
||||||
const event = () => {
|
const event = () => {
|
||||||
setScrollable(
|
setScrollable(
|
||||||
el.scrollTop + el.clientHeight + 100 >= el.scrollHeight,
|
el.scrollTop + el.clientHeight + 20 >= el.scrollHeight, // at bottom
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return addEventListeners(el, [
|
return addEventListeners(el, [
|
||||||
"scroll", "resize", "touchmove",
|
"scroll", "scrollend",
|
||||||
"touchend", "touchcancel",
|
"resize", "touchend",
|
||||||
], event);
|
], event);
|
||||||
}, [ref]);
|
}, [ref]);
|
||||||
|
|
||||||
|
@ -15,7 +15,6 @@ type CacheProps struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CacheData struct {
|
type CacheData struct {
|
||||||
Keyword string `json:"keyword"`
|
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,8 +61,7 @@ func ChatHandler(conn *Connection, user *auth.User, instance *conversation.Conve
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
keyword, segment := web.UsingWebSegment(instance)
|
segment := web.UsingWebSegment(instance)
|
||||||
conn.Send(globals.ChatSegmentResponse{Keyword: keyword, End: false})
|
|
||||||
|
|
||||||
model := instance.GetModel()
|
model := instance.GetModel()
|
||||||
db := conn.GetDB()
|
db := conn.GetDB()
|
||||||
@ -139,7 +138,6 @@ func ChatHandler(conn *Connection, user *auth.User, instance *conversation.Conve
|
|||||||
Model: model,
|
Model: model,
|
||||||
Reversible: plan,
|
Reversible: plan,
|
||||||
}, &CacheData{
|
}, &CacheData{
|
||||||
Keyword: keyword,
|
|
||||||
Message: result,
|
Message: result,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"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() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
globals.Warn(fmt.Sprintf("caught panic from chat handler: %s (instance: %s, client: %s)",
|
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)
|
db := utils.GetDBFromContext(c)
|
||||||
cache := utils.GetCacheFromContext(c)
|
cache := utils.GetCacheFromContext(c)
|
||||||
check, plan := auth.CanEnableModelWithSubscription(db, cache, user, model)
|
check, plan := auth.CanEnableModelWithSubscription(db, cache, user, model)
|
||||||
|
|
||||||
if !check {
|
if !check {
|
||||||
return keyword, defaultQuotaMessage, 0
|
return defaultQuotaMessage, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
if form := ExtractCacheData(c, &CacheProps{
|
if form := ExtractCacheData(c, &CacheProps{
|
||||||
@ -35,7 +35,7 @@ func NativeChatHandler(c *gin.Context, user *auth.User, model string, message []
|
|||||||
Model: model,
|
Model: model,
|
||||||
Reversible: plan,
|
Reversible: plan,
|
||||||
}); form != nil {
|
}); form != nil {
|
||||||
return form.Keyword, form.Message, 0
|
return form.Message, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer := utils.NewBuffer(model, segment)
|
buffer := utils.NewBuffer(model, segment)
|
||||||
@ -52,7 +52,7 @@ func NativeChatHandler(c *gin.Context, user *auth.User, model string, message []
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
auth.RevertSubscriptionUsage(cache, user, model, plan)
|
auth.RevertSubscriptionUsage(cache, user, model, plan)
|
||||||
CollectQuota(c, user, buffer, plan)
|
CollectQuota(c, user, buffer, plan)
|
||||||
return keyword, err.Error(), GetErrorQuota(model)
|
return err.Error(), GetErrorQuota(model)
|
||||||
}
|
}
|
||||||
|
|
||||||
CollectQuota(c, user, buffer, plan)
|
CollectQuota(c, user, buffer, plan)
|
||||||
@ -62,9 +62,8 @@ func NativeChatHandler(c *gin.Context, user *auth.User, model string, message []
|
|||||||
Model: model,
|
Model: model,
|
||||||
Reversible: plan,
|
Reversible: plan,
|
||||||
}, &CacheData{
|
}, &CacheData{
|
||||||
Keyword: keyword,
|
|
||||||
Message: buffer.ReadWithDefault(defaultMessage),
|
Message: buffer.ReadWithDefault(defaultMessage),
|
||||||
})
|
})
|
||||||
|
|
||||||
return keyword, buffer.ReadWithDefault(defaultMessage), buffer.GetQuota()
|
return buffer.ReadWithDefault(defaultMessage), buffer.GetQuota()
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package manager
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"chat/adapter"
|
"chat/adapter"
|
||||||
|
"chat/addition/web"
|
||||||
"chat/admin"
|
"chat/admin"
|
||||||
"chat/auth"
|
"chat/auth"
|
||||||
"chat/globals"
|
"chat/globals"
|
||||||
@ -10,6 +11,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -108,6 +110,14 @@ func TranshipmentAPI(c *gin.Context) {
|
|||||||
return
|
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 {
|
if form.Stream {
|
||||||
sendStreamTranshipmentResponse(c, form, id, created, user, plan)
|
sendStreamTranshipmentResponse(c, form, id, created, user, plan)
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user