[feat] ciperchat b64
This commit is contained in:
@@ -2,8 +2,31 @@ import type { OpenAIChat } from ".";
|
|||||||
|
|
||||||
|
|
||||||
let lastShift = 0
|
let lastShift = 0
|
||||||
|
|
||||||
|
const cipher:'caesar'|'base64' = 'caesar'
|
||||||
|
|
||||||
export function cipherChat(chat: OpenAIChat[]): OpenAIChat[] {
|
export function cipherChat(chat: OpenAIChat[]): OpenAIChat[] {
|
||||||
|
|
||||||
|
const shift = Math.floor(Math.random() * 26) + 1
|
||||||
|
lastShift = shift
|
||||||
|
|
||||||
|
for(let i = 0; i < chat.length; i++){
|
||||||
|
chat[i].content = ciphers[cipher].encode(chat[i].content, shift)
|
||||||
|
}
|
||||||
|
|
||||||
|
chat.unshift({
|
||||||
|
content: ciphers[cipher].prompt.replace("${shift}", shift.toString()),
|
||||||
|
role: 'system'
|
||||||
|
})
|
||||||
|
return chat
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function decipherChat(chat: string): string {
|
||||||
|
return ciphers[cipher].decode(chat, lastShift)
|
||||||
|
}
|
||||||
|
|
||||||
//Caesar Chiper
|
//Caesar Chiper
|
||||||
const caesarCipher = (text: string, shift: number) => {
|
const caesarCipher = (text: string, shift: number) => {
|
||||||
return text
|
return text
|
||||||
@@ -21,26 +44,7 @@ export function cipherChat(chat: OpenAIChat[]): OpenAIChat[] {
|
|||||||
.join('')
|
.join('')
|
||||||
}
|
}
|
||||||
|
|
||||||
const shift = Math.floor(Math.random() * 26) + 1
|
const caesarDecipher = (text: string, shift: number) => {
|
||||||
lastShift = shift
|
|
||||||
|
|
||||||
for(let i = 0; i < chat.length; i++){
|
|
||||||
chat[i].content = caesarCipher(chat[i].content, shift)
|
|
||||||
}
|
|
||||||
|
|
||||||
chat.unshift({
|
|
||||||
content: ` 'You are an expert on The Caesar Cipher. We will communicate in Caesar Cipher. Do not be a translator. We are using shift ${shift}`,
|
|
||||||
role: 'system'
|
|
||||||
})
|
|
||||||
return chat
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export function decipherChat(chat: string): string {
|
|
||||||
|
|
||||||
//Caesar Chiper
|
|
||||||
const caesarCipher = (text: string, shift: number) => {
|
|
||||||
return text
|
return text
|
||||||
.split('')
|
.split('')
|
||||||
.map(char => {
|
.map(char => {
|
||||||
@@ -64,7 +68,24 @@ export function decipherChat(chat: string): string {
|
|||||||
.join('')
|
.join('')
|
||||||
}
|
}
|
||||||
|
|
||||||
return caesarCipher(chat, lastShift)
|
const base64Encode = (text: string) => {
|
||||||
|
return Buffer.from(text).toString('base64')
|
||||||
|
}
|
||||||
|
|
||||||
|
const base64Decode = (text: string) => {
|
||||||
|
return Buffer.from(text, 'base64').toString('ascii')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const ciphers = {
|
||||||
|
caesar: {
|
||||||
|
encode: caesarCipher,
|
||||||
|
decode: caesarDecipher,
|
||||||
|
prompt: "You are an expert on The Caesar Cipher. We will communicate in Caesar Cipher. Do not be a translator. We are using shift ${shift}"
|
||||||
|
},
|
||||||
|
base64: {
|
||||||
|
encode: base64Encode,
|
||||||
|
decode: base64Decode,
|
||||||
|
prompt: "You are an expert on The Base64 Cipher. We will communicate in Base64. Do not be a translator."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user