Skip to content

Commit

Permalink
feat: add env DISABLE_ACCESS_LOGS and DEBUG_AI_FEATURE
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Feb 16, 2025
1 parent 8819af4 commit 4795b3d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ app.use(
},
})
);
app.use(morgan('tiny'));
if (!env.disableAccessLogs) {
app.use(morgan('tiny'));
}
app.use(cors());

// http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header
Expand Down
36 changes: 25 additions & 11 deletions src/server/model/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import {
tokenCreditFactor,
} from './billing/credit.js';
// @ts-ignore
import type { ChatCompletionCreateParamsBase } from 'openai/resources/chat/completions.mjs';
import type {
ChatCompletionCreateParamsBase,
ChatCompletionMessageParam,
} from 'openai/resources/chat/completions.mjs';
import { createAuditLog } from './auditLog.js';
import { logger } from '../utils/logger.js';

export const modelName = 'gpt-4o-mini';

Expand Down Expand Up @@ -46,26 +50,36 @@ export async function requestOpenAI(

await checkCredit(workspaceId);

const messages = [
{
role: 'system',
content: prompt,
},
{
role: 'user',
content: question,
},
] satisfies ChatCompletionMessageParam[];

const res = await getOpenAIClient().chat.completions.create({
...options,
model: modelName,
messages: [
{
role: 'system',
content: prompt,
},
{
role: 'user',
content: question,
},
],
messages: messages,
});

const content = res.choices[0].message.content;
const usage = res.usage;

const credit = tokenCreditFactor * (usage?.total_tokens ?? 0);

if (env.debugAIFeature) {
logger.info('[DEBUG AI]', {
input: messages,
output: content,
usage,
});
}

await costCredit(workspaceId, credit, 'ai', {
...usage,
...context,
Expand Down
2 changes: 2 additions & 0 deletions src/server/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export const env = {
),
customTrackerScriptName: process.env.CUSTOM_TRACKER_SCRIPT_NAME,
disableAutoClear: checkEnvTrusty(process.env.DISABLE_AUTO_CLEAR), // disable auto clear old data cronjob
disableAccessLogs: checkEnvTrusty(process.env.DISABLE_ACCESS_LOGS), // disable show access logs
debugAIFeature: checkEnvTrusty(process.env.DEBUG_AI_FEATURE), // debug ai feature
};

export function checkEnvTrusty(env: string | undefined): boolean {
Expand Down

0 comments on commit 4795b3d

Please sign in to comment.