Skip to content

Commit

Permalink
allow services for anonymous users & gpt4 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cogentapps committed Apr 29, 2023
1 parent 5bcd75f commit 8f9ec46
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 11 deletions.
3 changes: 3 additions & 0 deletions app/src/core/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions app/src/core/chat/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
}
2 changes: 1 addition & 1 deletion app/src/tts-plugins/elevenlabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 16 additions & 4 deletions server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ 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?: {
// The API key required to authenticate with the ElevenLabs service.
// When provided, signed in users will be able to access ElevenLabs through the server
// without needing their own API key.
apiKey?: string;
loginRequired?: boolean;
};
};

/*
Optional configuration for enabling Transport Layer Security (TLS) in the server.
Requires specifying the file paths for the key and cert files. Includes:
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -132,6 +143,7 @@ if (process.argv.includes('--self-signed')) {
}

if (config.publicSiteURL) {
// remove trailing slash:
config.publicSiteURL = config.publicSiteURL.replace(/\/$/, '');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ export default class ElevenLabsTTSProxyRequestHandler extends RequestHandler {
}

public isProtected() {
return true;
return config.services?.elevenlabs?.loginRequired ?? true;
}
}
3 changes: 2 additions & 1 deletion server/src/endpoints/service-proxies/elevenlabs/voices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -16,6 +17,6 @@ export default class ElevenLabsVoicesProxyRequestHandler extends RequestHandler
}

public isProtected() {
return true;
return config.services?.elevenlabs?.loginRequired ?? true;
}
}
2 changes: 1 addition & 1 deletion server/src/endpoints/service-proxies/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export default class OpenAIProxyRequestHandler extends RequestHandler {
}

public isProtected() {
return true;
return config.services?.openai?.loginRequired ?? true;
}
}
8 changes: 7 additions & 1 deletion server/src/endpoints/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,6 +45,7 @@ export default class SessionRequestHandler extends RequestHandler {
res.json({
authProvider: this.context.authProvider,
authenticated: false,
services: availableServiceNames,
});
}
}

0 comments on commit 8f9ec46

Please sign in to comment.