[feat] add deepai
This commit is contained in:
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++){
|
||||
|
||||
Reference in New Issue
Block a user