Spaces:
Running
Running
File size: 3,603 Bytes
8a8fe1d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
import {Chat, ChatOptions, Request, Response, ResponseStream} from "../base";
import {CreateAxiosProxy} from "../../utils/proxyAgent";
import {AxiosInstance, AxiosRequestConfig, CreateAxiosDefaults} from "axios";
import {Stream} from "stream";
import es from "event-stream";
import {parseJSON} from "../../utils";
export interface AiDreamReq extends Request {
options: {
parentMessageId: string
systemMessage: string
temperature: number;
top_p: number
parse: boolean;
};
}
interface RealReq {
options: {
parentMessageId?: string;
};
prompt: string;
systemMessage: string;
temperature: number;
top_p: number;
}
interface RealRes {
role: string;
id: string;
parentMessageId: string;
text: string;
delta: string;
detail: {
id: string;
object: string;
created: number;
model: string;
choices: {
delta: {
content: string;
};
index: number;
finish_reason: any;
}[];
};
}
export class AiDream extends Chat {
private client: AxiosInstance;
constructor(options?: ChatOptions) {
super(options);
this.client = CreateAxiosProxy({
baseURL: 'http://aidream.cloud/api/',
headers: {
"Cache-Control": "no-cache",
"Proxy-Connection": "keep-alive"
}
} as CreateAxiosDefaults);
}
public async ask(req: AiDreamReq): Promise<Response> {
req.options.parse = false;
const res = await this.askStream(req)
const result: Response = {
text: '', other: {}
}
return new Promise(resolve => {
res.text.pipe(es.split(/\r?\n/)).pipe(es.map(async (chunk: any, cb: any) => {
const data = parseJSON(chunk, {}) as RealRes;
if (!data?.detail?.choices) {
cb(null, '');
return;
}
const [{delta: {content}}] = data.detail.choices;
result.other.parentMessageId = data.parentMessageId;
cb(null, content);
})).on('data', (data) => {
result.text += data;
}).on('close', () => {
resolve(result);
})
})
}
public async askStream(req: AiDreamReq): Promise<ResponseStream> {
const {prompt = ''} = req;
const {
systemMessage = 'You are ChatGPT, a large language model trained by OpenAI. Follow the user\'s instructions carefully. Respond using markdown.',
temperature = 1.0,
top_p = 1,
parentMessageId,
parse = true,
} = req.options;
const data: RealReq = {
options: {parentMessageId}, prompt, systemMessage, temperature, top_p
};
const res = await this.client.post('/chat-process', data, {
responseType: 'stream'
} as AxiosRequestConfig);
if (parse) {
return {
text: this.parseData(res.data)
}
}
return {text: res.data};
}
parseData(v: Stream): Stream {
return v.pipe(es.split(/\r?\n/)).pipe(es.map(async (chunk: any, cb: any) => {
const data = parseJSON(chunk, {}) as RealRes;
if (!data?.detail?.choices) {
cb(null, '');
return;
}
const [{delta: {content}}] = data.detail.choices;
cb(null, content);
}))
}
}
|