update anonymous api context memory

This commit is contained in:
Zhang Minghan 2023-09-09 15:05:18 +08:00
parent d8c8c0395d
commit cf86ebf9fe
5 changed files with 53 additions and 17 deletions

View File

@ -15,6 +15,7 @@ import (
type AnonymousRequestBody struct {
Message string `json:"message" required:"true"`
Web bool `json:"web"`
History []types.ChatGPTMessage `json:"history"`
}
type AnonymousResponseCache struct {
@ -58,27 +59,45 @@ func TestKey(key string) bool {
return res.(map[string]interface{})["choices"] != nil
}
func GetAnonymousResponse(message string, web bool) (string, string, error) {
func GetAnonymousResponse(message []types.ChatGPTMessage, web bool) (string, string, error) {
if !web {
resp, err := GetChatGPTResponse([]types.ChatGPTMessage{{Role: "user", Content: message}}, 1000)
resp, err := GetChatGPTResponse(message, 1000)
return "", resp, err
}
keyword, source := ChatWithWeb([]types.ChatGPTMessage{{Role: "user", Content: message}}, false)
keyword, source := ChatWithWeb(message, false)
resp, err := GetChatGPTResponse(source, 1000)
return keyword, resp, err
}
func GetAnonymousResponseWithCache(c *gin.Context, message string, web bool) (string, string, error) {
func GetSegmentMessage(data []types.ChatGPTMessage, length int) []types.ChatGPTMessage {
if len(data) <= length {
return data
}
return data[len(data)-length:]
}
func GetAnonymousMessage(message string, history []types.ChatGPTMessage) []types.ChatGPTMessage {
return append(
GetSegmentMessage(history, 5),
types.ChatGPTMessage{
Role: "user",
Content: strings.TrimSpace(message),
})
}
func GetAnonymousResponseWithCache(c *gin.Context, message string, web bool, history []types.ChatGPTMessage) (string, string, error) {
segment := GetAnonymousMessage(message, history)
hash := utils.Md5Encrypt(utils.ToJson(segment))
cache := c.MustGet("cache").(*redis.Client)
res, err := cache.Get(c, fmt.Sprintf(":chatgpt-%v:%s", web, message)).Result()
res, err := cache.Get(c, fmt.Sprintf(":chatgpt-%v:%s", web, hash)).Result()
form := utils.UnmarshalJson[AnonymousResponseCache](res)
if err != nil || len(res) == 0 || res == "{}" || form.Message == "" {
key, res, err := GetAnonymousResponse(message, web)
key, res, err := GetAnonymousResponse(segment, web)
if err != nil {
return "", "There was something wrong...", err
}
cache.Set(c, fmt.Sprintf(":chatgpt-%v:%s", web, message), utils.ToJson(AnonymousResponseCache{
cache.Set(c, fmt.Sprintf(":chatgpt-%v:%s", web, hash), utils.ToJson(AnonymousResponseCache{
Keyword: key,
Message: res,
}), time.Hour*48)
@ -108,7 +127,8 @@ func AnonymousAPI(c *gin.Context) {
})
return
}
key, res, err := GetAnonymousResponseWithCache(c, message, body.Web)
key, res, err := GetAnonymousResponseWithCache(c, message, body.Web, body.History)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"status": false,

View File

@ -1,6 +1,7 @@
package api
import (
"chat/types"
"github.com/gin-gonic/gin"
"github.com/russross/blackfriday/v2"
"net/http"
@ -57,7 +58,7 @@ func CardAPI(c *gin.Context) {
return
}
key, res, err := GetAnonymousResponseWithCache(c, message, body.Web)
key, res, err := GetAnonymousResponseWithCache(c, message, body.Web, []types.ChatGPTMessage{})
if err != nil {
res = "There was something wrong..."
}

View File

@ -1,4 +1,5 @@
import axios from "axios";
import { Message } from "./types";
export type AnonymousResponse = {
status: boolean;
@ -14,9 +15,13 @@ export type AnonymousProps = {
export async function requestAnonymous(
t: any,
props: AnonymousProps,
history?: Message[],
): Promise<AnonymousResponse> {
try {
const response = await axios.post("/anonymous", props);
const response = await axios.post("/anonymous", {
history,
...props,
});
return response.data as AnonymousResponse;
} catch (error) {
console.debug(error);

View File

@ -110,9 +110,13 @@ export class Conversation {
};
}
public getSegmentData(length: number): Message[] {
return this.data.slice(this.data.length - length);
}
public sendAnonymous(t: any, props: AnonymousProps): void {
this.end = false;
requestAnonymous(t, props).then((response) => {
requestAnonymous(t, props, this.getSegmentData(5)).then((response) => {
this.addMessage({
content: response.message,
role: "assistant",
@ -133,15 +137,16 @@ export class Conversation {
public sendMessage(t: any, auth: boolean, props: SendMessageProps): boolean {
if (!this.end) return false;
this.addMessage({
content: props.message,
role: "user",
});
auth
? this.sendAuthenticated(t, props as AuthenticatedProps)
: this.sendAnonymous(t, props as AnonymousProps);
this.addMessage({
content: props.message,
role: "user",
});
return true;
}
}

View File

@ -14,6 +14,11 @@ func Sha2Encrypt(raw string) string {
return hex.EncodeToString(hash[:])
}
func Md5Encrypt(raw string) string {
hash := sha256.Sum256([]byte(raw))
return hex.EncodeToString(hash[:])
}
func AES256Encrypt(key string, data string) (string, error) {
text := []byte(data)
block, err := aes.NewCipher([]byte(key))