[feat] add deepai
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
"@msgpack/msgpack": "3.0.0-beta2",
|
||||
"@tauri-apps/api": "1.3.0",
|
||||
"@xenova/transformers": "^2.1.1",
|
||||
"blueimp-md5": "^2.19.0",
|
||||
"body-parser": "^1.20.2",
|
||||
"buffer": "^6.0.3",
|
||||
"core-js": "^3.30.2",
|
||||
@@ -50,6 +51,7 @@
|
||||
"@tailwindcss/typography": "^0.5.9",
|
||||
"@tauri-apps/cli": "1.3.1",
|
||||
"@tsconfig/svelte": "^3.0.0",
|
||||
"@types/blueimp-md5": "^2.18.0",
|
||||
"@types/dompurify": "^3.0.1",
|
||||
"@types/lodash": "^4.14.194",
|
||||
"@types/lodash.clonedeep": "^4.5.7",
|
||||
|
||||
14
pnpm-lock.yaml
generated
14
pnpm-lock.yaml
generated
@@ -16,6 +16,9 @@ dependencies:
|
||||
'@xenova/transformers':
|
||||
specifier: ^2.1.1
|
||||
version: 2.1.1
|
||||
blueimp-md5:
|
||||
specifier: ^2.19.0
|
||||
version: 2.19.0
|
||||
body-parser:
|
||||
specifier: ^1.20.2
|
||||
version: 1.20.2
|
||||
@@ -105,6 +108,9 @@ devDependencies:
|
||||
'@tsconfig/svelte':
|
||||
specifier: ^3.0.0
|
||||
version: 3.0.0
|
||||
'@types/blueimp-md5':
|
||||
specifier: ^2.18.0
|
||||
version: 2.18.0
|
||||
'@types/dompurify':
|
||||
specifier: ^3.0.1
|
||||
version: 3.0.1
|
||||
@@ -731,6 +737,10 @@ packages:
|
||||
resolution: {integrity: sha512-pYrtLtOwku/7r1i9AMONsJMVYAtk3hzOfiGNekhtq5tYBGA7unMve8RvUclKLMT3PrihvJqUmzsRGh0RP84hKg==}
|
||||
dev: true
|
||||
|
||||
/@types/blueimp-md5@2.18.0:
|
||||
resolution: {integrity: sha512-f4A+++lGZGJvVSgeyMkqA7BEf2BVQli6F+qEykKb49c5ieWQBkfpn6CP5c1IZr2Yi2Ofl6Fj+v0e1fN18Z8Cnw==}
|
||||
dev: true
|
||||
|
||||
/@types/dompurify@3.0.1:
|
||||
resolution: {integrity: sha512-ubq8VKmf8W+U48jUOiZO4BoSGS7NnbITPMvrF+7HgMN4L+eezCKv8QBPB8p3o4YPicLMmNeTyDkE5X4c2ViHJQ==}
|
||||
dependencies:
|
||||
@@ -912,6 +922,10 @@ packages:
|
||||
readable-stream: 3.6.2
|
||||
dev: false
|
||||
|
||||
/blueimp-md5@2.19.0:
|
||||
resolution: {integrity: sha512-DRQrD6gJyy8FbiE4s+bDoXS9hiW3Vbx5uCdwvcCf3zLHL+Iv7LtGHLpr+GZV8rHG8tK766FGYBwRbu8pELTt+w==}
|
||||
dev: false
|
||||
|
||||
/body-parser@1.20.1:
|
||||
resolution: {integrity: sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==}
|
||||
engines: {node: '>= 0.8', npm: 1.2.8000 || >= 1.4.16}
|
||||
|
||||
40
src/ts/process/deepai.ts
Normal file
40
src/ts/process/deepai.ts
Normal 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;
|
||||
}
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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[]
|
||||
@@ -579,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++){
|
||||
|
||||
@@ -498,8 +498,17 @@ export async function globalFetch(url:string, arg:{body?:any,headers?:{[key:stri
|
||||
if(db.requester === 'new'){
|
||||
try {
|
||||
let preHeader = arg.headers ?? {}
|
||||
preHeader["Content-Type"] = `application/json`
|
||||
const body = JSON.stringify(arg.body)
|
||||
let body:any
|
||||
if(arg.body instanceof URLSearchParams){
|
||||
const argBody = arg.body as URLSearchParams
|
||||
body = argBody.toString()
|
||||
preHeader["Content-Type"] = `application/x-www-form-urlencoded`
|
||||
}
|
||||
else{
|
||||
body = JSON.stringify(arg.body)
|
||||
preHeader["Content-Type"] = `application/json`
|
||||
}
|
||||
console.log(body)
|
||||
const header = JSON.stringify(preHeader)
|
||||
const res:string = await invoke('native_request', {url:url, body:body, header:header, method: method})
|
||||
const d:{
|
||||
@@ -576,15 +585,27 @@ export async function globalFetch(url:string, arg:{body?:any,headers?:{[key:stri
|
||||
}
|
||||
else{
|
||||
try {
|
||||
let headers = arg.headers ?? {}
|
||||
if(!headers["Content-Type"]){
|
||||
headers["Content-Type"] = `application/json`
|
||||
let body:any
|
||||
if(arg.body instanceof URLSearchParams){
|
||||
const argBody = arg.body as URLSearchParams
|
||||
body = argBody.toString()
|
||||
let headers = arg.headers ?? {}
|
||||
if(!headers["Content-Type"]){
|
||||
headers["Content-Type"] = `application/x-www-form-urlencoded`
|
||||
}
|
||||
}
|
||||
else{
|
||||
body = JSON.stringify(arg.body)
|
||||
let headers = arg.headers ?? {}
|
||||
if(!headers["Content-Type"]){
|
||||
headers["Content-Type"] = `application/json`
|
||||
}
|
||||
}
|
||||
if(arg.rawResponse){
|
||||
const furl = `/proxy?url=${encodeURIComponent(url)}`
|
||||
|
||||
const da = await fetch(furl, {
|
||||
body: JSON.stringify(arg.body),
|
||||
body: body,
|
||||
headers: {
|
||||
"risu-header": encodeURIComponent(JSON.stringify(arg.headers)),
|
||||
"Content-Type": "application/json"
|
||||
@@ -604,20 +625,29 @@ export async function globalFetch(url:string, arg:{body?:any,headers?:{[key:stri
|
||||
const furl = `/proxy?url=${encodeURIComponent(url)}`
|
||||
|
||||
const da = await fetch(furl, {
|
||||
body: JSON.stringify(arg.body),
|
||||
body: body,
|
||||
headers: {
|
||||
"risu-header": encodeURIComponent(JSON.stringify(arg.headers)),
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
method: method
|
||||
})
|
||||
|
||||
const dat = await da.json()
|
||||
addFetchLog(dat, da.ok && da.status >= 200 && da.status < 300)
|
||||
return {
|
||||
ok: da.ok && da.status >= 200 && da.status < 300,
|
||||
data: dat,
|
||||
headers: Object.fromEntries(da.headers)
|
||||
const daText = await da.text()
|
||||
try {
|
||||
const dat = JSON.parse(daText)
|
||||
addFetchLog(dat, da.ok && da.status >= 200 && da.status < 300)
|
||||
return {
|
||||
ok: da.ok && da.status >= 200 && da.status < 300,
|
||||
data: dat,
|
||||
headers: Object.fromEntries(da.headers)
|
||||
}
|
||||
} catch (error) {
|
||||
addFetchLog(daText, false)
|
||||
return {
|
||||
ok:false,
|
||||
data: daText,
|
||||
headers: Object.fromEntries(da.headers)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -854,3 +884,13 @@ export function openURL(url:string){
|
||||
window.open(url, "_blank")
|
||||
}
|
||||
}
|
||||
|
||||
function formDataToString(formData: FormData): string {
|
||||
const params: string[] = [];
|
||||
|
||||
for (const [name, value] of formData.entries()) {
|
||||
params.push(`${encodeURIComponent(name)}=${encodeURIComponent(value.toString())}`);
|
||||
}
|
||||
|
||||
return params.join('&');
|
||||
}
|
||||
Reference in New Issue
Block a user