forked from xiangsx/gpt4free-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import {Chat, ChatOptions, ChatRequest, ChatResponse, ModelType} from "../base"; | ||
import {AxiosInstance, AxiosRequestConfig, CreateAxiosDefaults} from "axios"; | ||
import {CreateAxiosProxy} from "../../utils/proxyAgent"; | ||
import es from "event-stream"; | ||
import {ErrorData, Event, EventStream, MessageData, parseJSON} from "../../utils"; | ||
import {randomUUID} from "crypto"; | ||
import {v4} from "uuid"; | ||
import moment from "moment"; | ||
|
||
interface RealReq { | ||
question: string; | ||
chat_id: string; | ||
timestamp: number; | ||
} | ||
|
||
export class ChatDemo extends Chat { | ||
private client: AxiosInstance; | ||
|
||
constructor(options?: ChatOptions) { | ||
super(options); | ||
this.client = CreateAxiosProxy({ | ||
baseURL: 'https://chat.chatgptdemo.net', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
"accept": "text/event-stream", | ||
"Cache-Control": "no-cache", | ||
"Proxy-Connection": "keep-alive" | ||
} | ||
} as CreateAxiosDefaults); | ||
} | ||
|
||
support(model: ModelType): number { | ||
switch (model) { | ||
case ModelType.GPT3p5Turbo: | ||
return 2000; | ||
default: | ||
return 0; | ||
} | ||
} | ||
|
||
public async ask(req: ChatRequest): Promise<ChatResponse> { | ||
const stream = new EventStream(); | ||
const res = await this.askStream(req, stream); | ||
const result: ChatResponse = { | ||
content: '', | ||
} | ||
return new Promise(resolve => { | ||
stream.read((event, data) => { | ||
switch (event) { | ||
case Event.done: | ||
break; | ||
case Event.message: | ||
result.content += (data as MessageData).content | ||
break; | ||
case Event.error: | ||
result.error = (data as ErrorData).error | ||
break; | ||
} | ||
}, () => { | ||
resolve(result); | ||
}) | ||
}) | ||
|
||
} | ||
|
||
public async askStream(req: ChatRequest, stream: EventStream) { | ||
const data: RealReq = { | ||
question: req.prompt, | ||
chat_id: v4(), | ||
timestamp: moment().valueOf(), | ||
}; | ||
try { | ||
const res = await this.client.post('/chat_api_stream', data, { | ||
responseType: 'stream', | ||
} as AxiosRequestConfig); | ||
res.data.pipe(es.split(/\r?\n\r?\n/)).pipe(es.map(async (chunk: any, cb: any) => { | ||
const dataStr = chunk.replace('data: ', ''); | ||
if (!dataStr) { | ||
stream.end(); | ||
return; | ||
} | ||
const data = parseJSON(dataStr, {} as any); | ||
if (!data?.choices) { | ||
stream.write(Event.error, {error: 'not found data.choices'}) | ||
stream.end(); | ||
return; | ||
} | ||
const [{delta: {content = ""}}] = data.choices; | ||
stream.write(Event.message, {content}); | ||
})) | ||
} catch (e: any) { | ||
console.error(e); | ||
stream.write(Event.error, {error: e.message}) | ||
stream.end(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters