allow services for anonymous users & gpt4 fix

main
Cogent Apps 2023-04-29 18:29:48 +00:00
parent 5bcd75f120
commit 8f9ec46f9e
8 changed files with 33 additions and 11 deletions

View File

@ -24,6 +24,7 @@ export interface User {
export class Backend extends EventEmitter {
public user: User | null = null;
public services: string[] = [];
private checkedSession = false;
private sessionInterval = new AsyncLoop(() => this.getSession(), 1000 * 30);
@ -70,8 +71,10 @@ export class Backend extends EventEmitter {
avatar: session.picture,
services: session.services,
};
this.services = session.services || [];
} else {
this.user = null;
this.services = session?.services || [];
}
this.checkedSession = true;

View File

@ -7,7 +7,7 @@ import { backend } from "../backend";
export const defaultModel = 'gpt-3.5-turbo';
export function isProxySupported() {
return !!backend.current?.user?.services?.includes('openai');
return !!backend.current?.services?.includes('openai');
}
function shouldUseProxy(apiKey: string | undefined | null) {
@ -141,5 +141,5 @@ export async function createStreamingChatCompletion(messages: OpenAIMessage[], p
export const maxTokensByModel = {
"chatgpt-3.5-turbo": 2048,
"gpt-4": 8096,
"gpt-4": 8192,
}

View File

@ -6,7 +6,7 @@ import { defaultElevenLabsVoiceID, defaultVoiceList } from "./elevenlabs-default
import { backend } from "../core/backend";
function isProxySupported() {
return !!backend.current?.user?.services?.includes('elevenlabs');
return !!backend.current?.services?.includes('elevenlabs');
}
function shouldUseProxy(apiKey: string | undefined | null) {

View File

@ -18,6 +18,7 @@ export interface Config {
// When provided, signed in users will be able to access OpenAI through the server
// without needing their own API key.
apiKey?: string;
loginRequired?: boolean;
};
elevenlabs?: {
@ -25,6 +26,7 @@ export interface Config {
// When provided, signed in users will be able to access ElevenLabs through the server
// without needing their own API key.
apiKey?: string;
loginRequired?: boolean;
};
};
@ -102,9 +104,18 @@ if (!fs.existsSync('./data')) {
fs.mkdirSync('./data');
}
const filename = process.env.CHATWITHGPT_CONFIG_FILENAME
? path.resolve(process.env.CHATWITHGPT_CONFIG_FILENAME)
: path.resolve(__dirname, '../data/config.yaml');
let filename = process.env.CHATWITHGPT_CONFIG_FILENAME as string;
// assume config.yaml if no filename is provided:
if (!filename) {
filename = path.resolve(__dirname, '../data/config.yaml')
// try config.yml if config.yaml doesn't exist:
const fallbackFilename = path.resolve(__dirname, '../data/config.yml');
if (!fs.existsSync(filename) && fs.existsSync(fallbackFilename)) {
filename = fallbackFilename;
}
}
if (fs.existsSync(filename)) {
config = {
@ -132,6 +143,7 @@ if (process.argv.includes('--self-signed')) {
}
if (config.publicSiteURL) {
// remove trailing slash:
config.publicSiteURL = config.publicSiteURL.replace(/\/$/, '');
}

View File

@ -23,6 +23,6 @@ export default class ElevenLabsTTSProxyRequestHandler extends RequestHandler {
}
public isProtected() {
return true;
return config.services?.elevenlabs?.loginRequired ?? true;
}
}

View File

@ -2,6 +2,7 @@ import express from 'express';
import RequestHandler from "../../base";
import axios from 'axios';
import { endpoint, apiKey } from './text-to-speech';
import { config } from '../../../config';
export default class ElevenLabsVoicesProxyRequestHandler extends RequestHandler {
async handler(req: express.Request, res: express.Response) {
@ -16,6 +17,6 @@ export default class ElevenLabsVoicesProxyRequestHandler extends RequestHandler
}
public isProtected() {
return true;
return config.services?.elevenlabs?.loginRequired ?? true;
}
}

View File

@ -17,6 +17,6 @@ export default class OpenAIProxyRequestHandler extends RequestHandler {
}
public isProtected() {
return true;
return config.services?.openai?.loginRequired ?? true;
}
}

View File

@ -7,7 +7,12 @@ export default class SessionRequestHandler extends RequestHandler {
const request = req as any;
const availableServiceNames = Object.keys(config.services || {})
.filter(key => (config.services as any)?.[key]?.apiKey);
.filter(key => {
const serviceConfig = (config.services as any)?.[key];
const apiKey = serviceConfig?.apiKey;
const loginRequired = serviceConfig?.loginRequired ?? true;
return apiKey && (!loginRequired || request.isAuthenticated());
});
if (request.oidc) {
const user = request.oidc.user;
@ -40,6 +45,7 @@ export default class SessionRequestHandler extends RequestHandler {
res.json({
authProvider: this.context.authProvider,
authenticated: false,
services: availableServiceNames,
});
}
}