Skip to content

Add optional OpenAI configuration options (OPENAI_BASE_URL, SMART_LLM, FAST_LLM) #864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ MASTODON_CLIENT_SECRET=""

# Misc Settings
OPENAI_API_KEY=""
OPENAI_BASE_URL=""
SMART_LLM=""
FAST_LLM=""
NEXT_PUBLIC_DISCORD_SUPPORT=""
NEXT_PUBLIC_POLOTNO=""
# NOT_SECURED=false
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/api/routes/copilot.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class CopilotController {
// @ts-ignore
req?.body?.variables?.data?.metadata?.requestType ===
'TextareaCompletion'
? 'gpt-4o-mini'
: 'gpt-4.1',
? process.env.FAST_LLM || 'gpt-4o-mini'
: process.env.SMART_LLM || 'gpt-4.1',
}),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import { PostsService } from '@gitroom/nestjs-libraries/database/prisma/posts/po

const model = new ChatOpenAI({
apiKey: process.env.OPENAI_API_KEY || 'sk-proj-',
model: 'gpt-4o-2024-08-06',
model: process.env.SMART_LLM || 'gpt-4o-2024-08-06',
temperature: 0,
...(process.env.OPENAI_BASE_URL && { openAIApiKey: process.env.OPENAI_API_KEY, configuration: { baseURL: process.env.OPENAI_BASE_URL } }),
});

interface WorkflowChannelsState {
Expand Down
25 changes: 17 additions & 8 deletions libraries/nestjs-libraries/src/openai/openai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { z } from 'zod';

const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY || 'sk-proj-',
baseURL: process.env.OPENAI_BASE_URL,
});

const PicturePrompt = z.object({
Expand All @@ -18,6 +19,14 @@ const VoicePrompt = z.object({

@Injectable()
export class OpenaiService {
private getSmartModel(): string {
return process.env.SMART_LLM || 'gpt-4.1';
}

private getFastModel(): string {
return process.env.FAST_LLM || 'gpt-4o-mini';
}

async generateImage(prompt: string, isUrl: boolean, isVertical = false) {
const generate = (
await openai.images.generate({
Expand All @@ -35,7 +44,7 @@ export class OpenaiService {
return (
(
await openai.beta.chat.completions.parse({
model: 'gpt-4.1',
model: this.getSmartModel(),
messages: [
{
role: 'system',
Expand All @@ -56,7 +65,7 @@ export class OpenaiService {
return (
(
await openai.beta.chat.completions.parse({
model: 'gpt-4.1',
model: this.getSmartModel(),
messages: [
{
role: 'system',
Expand Down Expand Up @@ -90,7 +99,7 @@ export class OpenaiService {
],
n: 5,
temperature: 1,
model: 'gpt-4.1',
model: this.getFastModel(),
}),
openai.chat.completions.create({
messages: [
Expand All @@ -106,7 +115,7 @@ export class OpenaiService {
],
n: 5,
temperature: 1,
model: 'gpt-4.1',
model: this.getFastModel(),
}),
])
).flatMap((p) => p.choices);
Expand Down Expand Up @@ -144,7 +153,7 @@ export class OpenaiService {
content,
},
],
model: 'gpt-4.1',
model: this.getFastModel(),
});

const { content: articleContent } = websiteContent.choices[0].message;
Expand All @@ -164,7 +173,7 @@ export class OpenaiService {
const posts =
(
await openai.beta.chat.completions.parse({
model: 'gpt-4.1',
model: this.getSmartModel(),
messages: [
{
role: 'system',
Expand Down Expand Up @@ -197,7 +206,7 @@ export class OpenaiService {
return (
(
await openai.beta.chat.completions.parse({
model: 'gpt-4.1',
model: this.getSmartModel(),
messages: [
{
role: 'system',
Expand Down Expand Up @@ -231,7 +240,7 @@ export class OpenaiService {
return (
(
await openai.beta.chat.completions.parse({
model: 'gpt-4.1',
model: this.getSmartModel(),
messages: [
{
role: 'system',
Expand Down