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

@ -13,8 +13,9 @@ import (
) )
type AnonymousRequestBody struct { type AnonymousRequestBody struct {
Message string `json:"message" required:"true"` Message string `json:"message" required:"true"`
Web bool `json:"web"` Web bool `json:"web"`
History []types.ChatGPTMessage `json:"history"`
} }
type AnonymousResponseCache struct { type AnonymousResponseCache struct {
@ -58,27 +59,45 @@ func TestKey(key string) bool {
return res.(map[string]interface{})["choices"] != nil 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 { if !web {
resp, err := GetChatGPTResponse([]types.ChatGPTMessage{{Role: "user", Content: message}}, 1000) resp, err := GetChatGPTResponse(message, 1000)
return "", resp, err return "", resp, err
} }
keyword, source := ChatWithWeb([]types.ChatGPTMessage{{Role: "user", Content: message}}, false) keyword, source := ChatWithWeb(message, false)
resp, err := GetChatGPTResponse(source, 1000) resp, err := GetChatGPTResponse(source, 1000)
return keyword, resp, err 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) 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) form := utils.UnmarshalJson[AnonymousResponseCache](res)
if err != nil || len(res) == 0 || res == "{}" || form.Message == "" { if err != nil || len(res) == 0 || res == "{}" || form.Message == "" {
key, res, err := GetAnonymousResponse(message, web) key, res, err := GetAnonymousResponse(segment, web)
if err != nil { if err != nil {
return "", "There was something wrong...", err 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, Keyword: key,
Message: res, Message: res,
}), time.Hour*48) }), time.Hour*48)
@ -108,7 +127,8 @@ func AnonymousAPI(c *gin.Context) {
}) })
return return
} }
key, res, err := GetAnonymousResponseWithCache(c, message, body.Web)
key, res, err := GetAnonymousResponseWithCache(c, message, body.Web, body.History)
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"status": false, "status": false,

View File

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

View File

@ -1,4 +1,5 @@
import axios from "axios"; import axios from "axios";
import { Message } from "./types";
export type AnonymousResponse = { export type AnonymousResponse = {
status: boolean; status: boolean;
@ -14,9 +15,13 @@ export type AnonymousProps = {
export async function requestAnonymous( export async function requestAnonymous(
t: any, t: any,
props: AnonymousProps, props: AnonymousProps,
history?: Message[],
): Promise<AnonymousResponse> { ): Promise<AnonymousResponse> {
try { try {
const response = await axios.post("/anonymous", props); const response = await axios.post("/anonymous", {
history,
...props,
});
return response.data as AnonymousResponse; return response.data as AnonymousResponse;
} catch (error) { } catch (error) {
console.debug(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 { public sendAnonymous(t: any, props: AnonymousProps): void {
this.end = false; this.end = false;
requestAnonymous(t, props).then((response) => { requestAnonymous(t, props, this.getSegmentData(5)).then((response) => {
this.addMessage({ this.addMessage({
content: response.message, content: response.message,
role: "assistant", role: "assistant",
@ -133,15 +137,16 @@ export class Conversation {
public sendMessage(t: any, auth: boolean, props: SendMessageProps): boolean { public sendMessage(t: any, auth: boolean, props: SendMessageProps): boolean {
if (!this.end) return false; if (!this.end) return false;
this.addMessage({
content: props.message,
role: "user",
});
auth auth
? this.sendAuthenticated(t, props as AuthenticatedProps) ? this.sendAuthenticated(t, props as AuthenticatedProps)
: this.sendAnonymous(t, props as AnonymousProps); : this.sendAnonymous(t, props as AnonymousProps);
this.addMessage({
content: props.message,
role: "user",
});
return true; return true;
} }
} }

View File

@ -14,6 +14,11 @@ func Sha2Encrypt(raw string) string {
return hex.EncodeToString(hash[:]) 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) { func AES256Encrypt(key string, data string) (string, error) {
text := []byte(data) text := []byte(data)
block, err := aes.NewCipher([]byte(key)) block, err := aes.NewCipher([]byte(key))