diff --git a/README.md b/README.md index 75dd7fd..f7240b9 100644 --- a/README.md +++ b/README.md @@ -2,21 +2,24 @@ This is a replication project for the typescript version of [gpt4free](https://g ## Demo [GPTGOD](http://gptgod.site) -In the next two weeks, I will open source all the code for GPTGOD. If you need, Please watch this project or follow me to receive notifications. +In the next two weeks, I will open source all the code for GPTGOD. If you need, Please watch this project or follow me +to receive notifications. Why now? because there are stil some secret config shoule be removed from that project. ## Reverse target + Still striving to keep updating. Have implemented models here: -- [x] [you.com](you.com) GPT-3.5 / Internet / good search **not active because of new auth method** -- [x] [forefront.ai](forefront.ai) GPT-4/3.5 ***It still work perfect after 2023-05-07*** -- [ ] [bing.com/chat](bing.com/chat) GPT-4/3.5 -- [ ] [poe.com](poe.com) GPT-4/3.5 -- [ ] [writesonic.com](writesonic.com) GPT-3.5 / Internet -- [ ] [t3nsor.com](t3nsor.com) GPT-3.5 +- [x] [aidream](http://aidream.cloud) GPT-3.5 ***Still active after 2023-05-11*** +- [x] [you.com](you.com) GPT-3.5 / Internet / good search **not active because of new auth method** +- [x] [forefront.ai](forefront.ai) GPT-4/3.5 ***Not valid from 2023-05-10, but it will be right back*** +- [ ] [bing.com/chat](bing.com/chat) GPT-4/3.5 +- [ ] [poe.com](poe.com) GPT-4/3.5 +- [ ] [writesonic.com](writesonic.com) GPT-3.5 / Internet +- [ ] [t3nsor.com](t3nsor.com) GPT-3.5 ## Run local @@ -28,7 +31,9 @@ yarn start ``` ## Run with docker + first, you should create file .env + ```env http_proxy=http://host:port # you should config this if you use forefront api, this apikey is used for receive register email @@ -45,6 +50,7 @@ docker run --env-file .env xiangsx/gpt4free-ts:latest first, you should create file .env; Follow step "Run with docker deploy + ``` docker-compose up --build -d ``` @@ -57,7 +63,20 @@ docker-compose up --build -d prompt: string; // required ``` +aidread options + +```typescript +interface options { + parentMessageId: string // if you need context try this + systemMessage: string // default: You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown. + temperature: number; // default: 1 + top_p: number // default:1 + parse: boolean; // default:true only valid in stream;if set false,return source data contains parentMessageId... +} +``` + forefront options + ``` chatId?: string; actionType?: Action; // 'new' or 'continue' @@ -72,7 +91,11 @@ resignup?: number; // default 0 if set 1, auto sign up when gpt4 times use up ### test now! common request + ```shell +# test default model aidream +curl "http://127.0.0.1:3000/ask?prompt=hello&model=aidream" + # test default model chat.forefront.at curl "http://127.0.0.1:3000/ask?prompt=hello&model=forefront&gptmodel=gpt-4&resignup=1" @@ -80,9 +103,13 @@ curl "http://127.0.0.1:3000/ask?prompt=hello&model=forefront&gptmodel=gpt-4&resi curl "http://127.0.0.1:3000/ask?prompt=hello&model=you" ``` -request event-stream +request event-stream + ```shell -# test default model chat.forefront.at +# test default model aidream +curl "http://127.0.0.1:3000/ask/stream?prompt=hello&model=aidream" + +# test chat.forefront.at curl "http://127.0.0.1:3000/ask/stream?prompt=hello&model=forefront&gptmodel=gpt-4&resignup=1" # test you @@ -100,20 +127,38 @@ curl "http://127.0.0.1:3000/ask/stream?prompt=hello&model=you" ## Legal Notice -This repository is _not_ associated with or endorsed by providers of the APIs contained in this GitHub repository. This project is intended **for educational purposes only**. This is just a little personal project. Sites may contact me to improve their security or request the removal of their site from this repository. +This repository is _not_ associated with or endorsed by providers of the APIs contained in this GitHub repository. This +project is intended **for educational purposes only**. This is just a little personal project. Sites may contact me to +improve their security or request the removal of their site from this repository. Please note the following: -1. **Disclaimer**: The APIs, services, and trademarks mentioned in this repository belong to their respective owners. This project is _not_ claiming any right over them nor is it affiliated with or endorsed by any of the providers mentioned. +1. **Disclaimer**: The APIs, services, and trademarks mentioned in this repository belong to their respective owners. + This project is _not_ claiming any right over them nor is it affiliated with or endorsed by any of the providers + mentioned. -2. **Responsibility**: The author of this repository is _not_ responsible for any consequences, damages, or losses arising from the use or misuse of this repository or the content provided by the third-party APIs. Users are solely responsible for their actions and any repercussions that may follow. We strongly recommend the users to follow the TOS of the each Website. +2. **Responsibility**: The author of this repository is _not_ responsible for any consequences, damages, or losses + arising from the use or misuse of this repository or the content provided by the third-party APIs. Users are solely + responsible for their actions and any repercussions that may follow. We strongly recommend the users to follow the + TOS of the each Website. -3. **Educational Purposes Only**: This repository and its content are provided strictly for educational purposes. By using the information and code provided, users acknowledge that they are using the APIs and models at their own risk and agree to comply with any applicable laws and regulations. +3. **Educational Purposes Only**: This repository and its content are provided strictly for educational purposes. By + using the information and code provided, users acknowledge that they are using the APIs and models at their own risk + and agree to comply with any applicable laws and regulations. -4. **Copyright**: All content in this repository, including but not limited to code, images, and documentation, is the intellectual property of the repository author, unless otherwise stated. Unauthorized copying, distribution, or use of any content in this repository is strictly prohibited without the express written consent of the repository author. +4. **Copyright**: All content in this repository, including but not limited to code, images, and documentation, is the + intellectual property of the repository author, unless otherwise stated. Unauthorized copying, distribution, or use + of any content in this repository is strictly prohibited without the express written consent of the repository + author. -5. **Indemnification**: Users agree to indemnify, defend, and hold harmless the author of this repository from and against any and all claims, liabilities, damages, losses, or expenses, including legal fees and costs, arising out of or in any way connected with their use or misuse of this repository, its content, or related third-party APIs. +5. **Indemnification**: Users agree to indemnify, defend, and hold harmless the author of this repository from and + against any and all claims, liabilities, damages, losses, or expenses, including legal fees and costs, arising out of + or in any way connected with their use or misuse of this repository, its content, or related third-party APIs. -6. **Updates and Changes**: The author reserves the right to modify, update, or remove any content, information, or features in this repository at any time without prior notice. Users are responsible for regularly reviewing the content and any changes made to this repository. +6. **Updates and Changes**: The author reserves the right to modify, update, or remove any content, information, or + features in this repository at any time without prior notice. Users are responsible for regularly reviewing the + content and any changes made to this repository. -By using this repository or any code related to it, you agree to these terms. The author is not responsible for any copies, forks, or reuploads made by other users. This is the author's only account and repository. To prevent impersonation or irresponsible actions, you may comply with the GNU GPL license this Repository uses. +By using this repository or any code related to it, you agree to these terms. The author is not responsible for any +copies, forks, or reuploads made by other users. This is the author's only account and repository. To prevent +impersonation or irresponsible actions, you may comply with the GNU GPL license this Repository uses. diff --git a/index.ts b/index.ts index b2d52b9..98a90fc 100644 --- a/index.ts +++ b/index.ts @@ -35,7 +35,7 @@ router.get('/ask', async (ctx) => { return; } const res = await chat.ask({prompt: prompt as string, options}); - ctx.body = res?.text; + ctx.body = res; }); router.get('/ask/stream', async (ctx) => { diff --git a/model/aidream/index.ts b/model/aidream/index.ts new file mode 100644 index 0000000..dcd549c --- /dev/null +++ b/model/aidream/index.ts @@ -0,0 +1,122 @@ +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 { + 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 { + 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); + })) + } +} diff --git a/model/index.ts b/model/index.ts index 0e1c65d..4c71dea 100644 --- a/model/index.ts +++ b/model/index.ts @@ -1,11 +1,13 @@ import {Chat, ChatOptions} from "./base"; import {You} from "./you"; import {Forefront} from "./forefront"; +import {AiDream} from "./aidream"; export enum Model { // define new model here You = 'you', Forefront = 'forefront', + AiDream = 'aidream', } export class ChatModelFactory { @@ -22,6 +24,7 @@ export class ChatModelFactory { // register new model here this.modelMap.set(Model.You, new You(this.options)) this.modelMap.set(Model.Forefront, new Forefront(this.options)) + this.modelMap.set(Model.AiDream, new AiDream(this.options)) } get(model: Model): Chat | undefined {