Merge branch 'main' into chatsticker

This commit is contained in:
ASDF9999
2023-06-22 23:21:36 +09:00
committed by GitHub
15 changed files with 337 additions and 73 deletions

40
src/ts/process/deepai.ts Normal file
View File

@@ -0,0 +1,40 @@
import md5 from "blueimp-md5";
import { globalFetch } from "../storage/globalApi";
import type { OpenAIChat } from ".";
function randomBytes(size: number): Uint8Array {
const array = new Uint8Array(size);
return crypto.getRandomValues(array);
}
export async function createDeep(messages: OpenAIChat[]) {
const userAgent = navigator.userAgent;
const part1 = Math.floor(Math.random() * Math.pow(10, 11)).toString();
const md5Text = (text: string): string => {
return md5(text).split('').reverse().join('');
}
const part2 = md5Text(userAgent + md5Text(userAgent + md5Text(userAgent + part1 + "x")));
const apiKey = `tryit-${part1}-${part2}`;
const headers = {
"api-key": apiKey,
"user-agent": userAgent
};
const body = new URLSearchParams();
body.append("chat_style", "chat");
console.log(messages);
body.append("chatHistory", JSON.stringify(messages));
const response = await globalFetch("https://api.deepai.org/chat_response", {
method: 'POST',
headers: headers,
body: body,
rawResponse: true
});
return response;
}

View File

@@ -139,6 +139,11 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
maxContextTokens = 8000
}
}
if(db.aiModel === 'deepai'){
if(maxContextTokens > 3000){
maxContextTokens = 3000
}
}
let unformated = {
@@ -154,7 +159,7 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
}
if(!currentChar.utilityBot){
const mainp = currentChar.systemPrompt || db.mainPrompt
const mainp = currentChar.systemPrompt?.replaceAll('{{original}}', db.mainPrompt) || db.mainPrompt
function formatPrompt(data:string){
@@ -183,7 +188,7 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
unformated.jailbreak.push(...formatPrompt(replacePlaceholders(db.jailbreak, currentChar.name)))
}
unformated.globalNote.push(...formatPrompt(replacePlaceholders(currentChar.replaceGlobalNote || db.globalNote, currentChar.name)))
unformated.globalNote.push(...formatPrompt(replacePlaceholders(currentChar.replaceGlobalNote?.replaceAll('{{original}}', db.globalNote) || db.globalNote, currentChar.name)))
}
if(currentChat.note){

33
src/ts/process/lua.ts Normal file
View File

@@ -0,0 +1,33 @@
import { get } from "svelte/store";
import { DataBase, type character } from "../storage/database";
import type {LuaEngine} from 'wasmoon'
import { selectedCharID } from "../stores";
let lua: LuaEngine = null
export class CharacterLua{
char:character
constructor(char:character){
this.char = char
}
async init(){
if(!lua){
const factory = new (await import("wasmoon")).LuaFactory
lua = await factory.createEngine()
lua.global.set('getChat', () => {
const cha = get(DataBase).characters[get(selectedCharID)]
return cha.chats[cha.chatPage].message
})
lua.global.set('setChat', () => {
const cha = get(DataBase).characters[get(selectedCharID)]
return cha.chats[cha.chatPage].message
})
lua.global.set('doSend', (a:string) => {
console.log(a)
})
}
}
}

View File

@@ -6,6 +6,7 @@ import { language } from "../../lang";
import { stringlizeChat, unstringlizeChat } from "./stringlize";
import { globalFetch, isTauri } from "../storage/globalApi";
import { sleep } from "../util";
import { createDeep } from "./deepai";
interface requestDataArgument{
formated: OpenAIChat[]
@@ -183,6 +184,13 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
}
}
if(da.headers.get('Content-Type') !== 'text/event-stream'){
return {
type: "fail",
result: await da.text()
}
}
let dataUint = new Uint8Array([])
const transtream = new TransformStream<Uint8Array, string>( {
@@ -572,6 +580,40 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
'result': unstringlizeChat(result, formated, currentChar?.name ?? '')
}
}
case "deepai":{
for(let i=0;i<formated.length;i++){
delete formated[i].memo
delete formated[i].name
if(arg.isGroupChat && formated[i].name && formated[i].role === 'assistant'){
formated[i].content = formated[i].name + ": " + formated[i].content
}
if(formated[i].role !== 'assistant' && formated[i].role !== 'user'){
formated[i].content = formated[i].role + ": " + formated[i].content
formated[i].role = 'assistant'
}
formated[i].name = undefined
}
const response = await createDeep([{
role: 'user',
content: stringlizeChat(formated, currentChar?.name ?? '')
}])
if(!response.ok){
return {
type: 'fail',
result: response.data
}
}
const result = Buffer.from(response.data).toString('utf-8')
return {
'type': 'success',
'result': result
}
}
default:{
if(aiModel.startsWith('claude')){
for(let i=0;i<formated.length;i++){