Skip to content

Commit

Permalink
refactor: add chat model factory
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangsx committed May 5, 2023
1 parent 33b40e2 commit 14eca0b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 39 deletions.
25 changes: 18 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
import {You} from "./model/you";
import Koa from 'koa';
import Router from 'koa-router'
import bodyParser from 'koa-bodyparser';
import {ChatModelFactory, Model} from "./model";

const app = new Koa();
const router = new Router();
app.use(bodyParser());
const you = new You({proxy: process.env.https_proxy || process.env.http_proxy});
const chatModel = new ChatModelFactory({proxy: process.env.https_proxy || process.env.http_proxy});

interface AskReq {
prompt: string;
model: Model;
}

router.get('/ask', async (ctx) => {
const {prompt} = ctx.query;
const {prompt, model = Model.You} = ctx.query as unknown as AskReq;
if (!prompt) {
ctx.body = 'please input prompt';
return;
}
const res = await you.ask({prompt: prompt as string});
const chat = chatModel.get(model);
if (!chat) {
ctx.body = 'Unsupported model';
return;
}
const res = await chat.ask({prompt: prompt as string});
ctx.body = res.text;
});

router.get('/ask/stream', async (ctx) => {
const {prompt} = ctx.query;
const {prompt, model = Model.You} = ctx.query as unknown as AskReq;
if (!prompt) {
ctx.body = 'please input prompt';
return;
}
const chat = chatModel.get(model);
if (!chat) {
ctx.body = 'Unsupported model';
return;
}
ctx.set({
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
})
const res = await you.askStream({prompt: prompt as string});
});
const res = await chat.askStream({prompt: prompt as string});
ctx.body = res.text;
})

Expand Down
39 changes: 39 additions & 0 deletions model/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {Stream} from "stream";

export interface ChatOptions {
proxy?: string;
}

export interface Response {
text: string | null;
other: any;
}

export interface ResponseStream {
text: Stream;
other: any;
}

export interface Request {
prompt: string;
history?: HistoryItem[];
options?: any;
}

export interface HistoryItem {
question?: string;
answer?: string;
}


export abstract class Chat {
protected proxy: string | undefined;

constructor(options?: ChatOptions) {
this.proxy = options?.proxy;
}

public abstract ask(req: Request): Promise<Response>

public abstract askStream(req: Request): Promise<ResponseStream>
}
48 changes: 18 additions & 30 deletions model/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
import {Stream} from "stream";
import {Chat, ChatOptions} from "./base";
import {You} from "./you";

export interface ChatOptions {
proxy?: string;
export enum Model {
// define new model here
You = 'you',
}

export interface Response {
text: string | null;
other: any;
}

export interface ResponseStream {
text: Stream;
other: any;
}

export interface Request {
prompt: string;
history?: HistoryItem[];
options?: any;
}

export interface HistoryItem {
question?: string;
answer?: string;
}


export abstract class Chat {
protected proxy: string | undefined;
export class ChatModelFactory {
private modelMap: Map<Model, Chat>;
private readonly options: ChatOptions | undefined;

constructor(options?: ChatOptions) {
this.proxy = options?.proxy;
this.modelMap = new Map();
this.options = options;
this.init();
}

public abstract ask(req: Request): Promise<Response>
init() {
// register new model here
this.modelMap.set(Model.You, new You(this.options))
}

public abstract askStream(req: Request): Promise<ResponseStream>
get(model: Model): Chat | undefined {
return this.modelMap.get(model);
}
}
4 changes: 2 additions & 2 deletions model/you/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tlsClient from 'tls-client';
import {Session} from "tls-client/dist/esm/sessions";
import {Params} from "tls-client/dist/esm/types";
import {toEventCB, toEventStream} from "../../utils";
import {Chat, ChatOptions, Request, Response, ResponseStream} from "../index";
import {Chat, ChatOptions, Request, Response, ResponseStream} from "../base";

const userAgent = new UserAgent();

Expand Down Expand Up @@ -65,7 +65,7 @@ interface SearchResult {
export class You extends Chat {
private session: Session;

constructor(props: ChatOptions) {
constructor(props?: ChatOptions) {
super(props);
this.session = new tlsClient.Session({clientIdentifier: 'chrome_108'});
this.session.headers = this.getHeaders();
Expand Down

0 comments on commit 14eca0b

Please sign in to comment.