refactor: add chat model factory
このコミットが含まれているのは:
コミット
14eca0beb3
25
index.ts
25
index.ts
|
@ -1,39 +1,50 @@
|
||||||
import {You} from "./model/you";
|
|
||||||
import Koa from 'koa';
|
import Koa from 'koa';
|
||||||
import Router from 'koa-router'
|
import Router from 'koa-router'
|
||||||
import bodyParser from 'koa-bodyparser';
|
import bodyParser from 'koa-bodyparser';
|
||||||
|
import {ChatModelFactory, Model} from "./model";
|
||||||
|
|
||||||
const app = new Koa();
|
const app = new Koa();
|
||||||
const router = new Router();
|
const router = new Router();
|
||||||
app.use(bodyParser());
|
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 {
|
interface AskReq {
|
||||||
prompt: string;
|
prompt: string;
|
||||||
|
model: Model;
|
||||||
}
|
}
|
||||||
|
|
||||||
router.get('/ask', async (ctx) => {
|
router.get('/ask', async (ctx) => {
|
||||||
const {prompt} = ctx.query;
|
const {prompt, model = Model.You} = ctx.query as unknown as AskReq;
|
||||||
if (!prompt) {
|
if (!prompt) {
|
||||||
ctx.body = 'please input prompt';
|
ctx.body = 'please input prompt';
|
||||||
return;
|
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;
|
ctx.body = res.text;
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/ask/stream', async (ctx) => {
|
router.get('/ask/stream', async (ctx) => {
|
||||||
const {prompt} = ctx.query;
|
const {prompt, model = Model.You} = ctx.query as unknown as AskReq;
|
||||||
if (!prompt) {
|
if (!prompt) {
|
||||||
ctx.body = 'please input prompt';
|
ctx.body = 'please input prompt';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const chat = chatModel.get(model);
|
||||||
|
if (!chat) {
|
||||||
|
ctx.body = 'Unsupported model';
|
||||||
|
return;
|
||||||
|
}
|
||||||
ctx.set({
|
ctx.set({
|
||||||
"Content-Type": "text/event-stream",
|
"Content-Type": "text/event-stream",
|
||||||
"Cache-Control": "no-cache",
|
"Cache-Control": "no-cache",
|
||||||
"Connection": "keep-alive",
|
"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;
|
ctx.body = res.text;
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -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>
|
||||||
|
}
|
|
@ -1,39 +1,27 @@
|
||||||
import {Stream} from "stream";
|
import {Chat, ChatOptions} from "./base";
|
||||||
|
import {You} from "./you";
|
||||||
|
|
||||||
export interface ChatOptions {
|
export enum Model {
|
||||||
proxy?: string;
|
// define new model here
|
||||||
|
You = 'you',
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Response {
|
export class ChatModelFactory {
|
||||||
text: string | null;
|
private modelMap: Map<Model, Chat>;
|
||||||
other: any;
|
private readonly options: ChatOptions | undefined;
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import tlsClient from 'tls-client';
|
||||||
import {Session} from "tls-client/dist/esm/sessions";
|
import {Session} from "tls-client/dist/esm/sessions";
|
||||||
import {Params} from "tls-client/dist/esm/types";
|
import {Params} from "tls-client/dist/esm/types";
|
||||||
import {toEventCB, toEventStream} from "../../utils";
|
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();
|
const userAgent = new UserAgent();
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ interface SearchResult {
|
||||||
export class You extends Chat {
|
export class You extends Chat {
|
||||||
private session: Session;
|
private session: Session;
|
||||||
|
|
||||||
constructor(props: ChatOptions) {
|
constructor(props?: ChatOptions) {
|
||||||
super(props);
|
super(props);
|
||||||
this.session = new tlsClient.Session({clientIdentifier: 'chrome_108'});
|
this.session = new tlsClient.Session({clientIdentifier: 'chrome_108'});
|
||||||
this.session.headers = this.getHeaders();
|
this.session.headers = this.getHeaders();
|
||||||
|
|
読み込み中…
新しいイシューから参照