feat: add proxy config

このコミットが含まれているのは:
xiang 2023-05-04 22:01:55 +08:00
コミット 311197873b
3個のファイルの変更20行の追加8行の削除

ファイルの表示

@ -2,12 +2,11 @@ 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 {Readable} from "stream";
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(); const you = new You({proxy: process.env.https_proxy || process.env.http_proxy});
interface AskReq { interface AskReq {
prompt: string; prompt: string;
@ -23,7 +22,7 @@ router.get('/ask', async (ctx) => {
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} = ctx.query;
if (!prompt) { if (!prompt) {
ctx.body = 'please input prompt'; ctx.body = 'please input prompt';
@ -34,7 +33,7 @@ router.get('/ask/stream',async(ctx)=>{
"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 you.askStream({prompt: prompt as string});
ctx.body = res.text; ctx.body = res.text;
}) })

ファイルの表示

@ -1,5 +1,9 @@
import {Stream} from "stream"; import {Stream} from "stream";
export interface ChatOptions {
proxy?: string;
}
export interface Response { export interface Response {
text: string | null; text: string | null;
other: any; other: any;
@ -23,6 +27,13 @@ export interface HistoryItem {
export abstract class Chat { export abstract class Chat {
protected proxy: string | undefined;
constructor(options?: ChatOptions) {
this.proxy = options?.proxy;
}
public abstract ask(req: Request): Promise<Response> public abstract ask(req: Request): Promise<Response>
public abstract askStream(req: Request): Promise<ResponseStream> public abstract askStream(req: Request): Promise<ResponseStream>
} }

ファイルの表示

@ -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, Request, Response, ResponseStream} from "../index"; import {Chat, ChatOptions, Request, Response, ResponseStream} from "../index";
const userAgent = new UserAgent(); const userAgent = new UserAgent();
@ -65,11 +65,13 @@ interface SearchResult {
export class You extends Chat { export class You extends Chat {
private session: Session; private session: Session;
constructor() { constructor(props: ChatOptions) {
super(); 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();
this.session.proxy = "http://192.168.0.155:10811"; if (this.proxy) {
this.session.proxy = this.proxy;
}
} }
private async request(req: Request) { private async request(req: Request) {