Skip to content

Commit

Permalink
πŸ› fix: fix OpenAI deployment restrictions (#3206)
Browse files Browse the repository at this point in the history
* ⚑ perf(route): Optimize `OpenAI` deployment restrictions

* πŸ› fix(route): incorrect use of `hkg1`

* πŸ› fix(route): Incorrect use of `hkg1`
  • Loading branch information
RubuJam authored Jul 17, 2024
1 parent b63209e commit 3d7a35d
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 83 deletions.
3 changes: 0 additions & 3 deletions src/app/api/chat/[provider]/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getPreferredRegion } from '@/app/api/config';
import { createErrorResponse } from '@/app/api/errorResponse';
import { AgentRuntime, ChatCompletionErrorPayload } from '@/libs/agent-runtime';
import { ChatErrorType } from '@/types/fetch';
Expand All @@ -10,8 +9,6 @@ import { createTraceOptions, initAgentRuntimeWithUserPayload } from '../agentRun

export const runtime = 'edge';

export const preferredRegion = getPreferredRegion();

export const POST = checkAuth(async (req: Request, { params, jwtPayload, createRuntime }) => {
const { provider } = params;

Expand Down
3 changes: 0 additions & 3 deletions src/app/api/chat/models/[provider]/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NextResponse } from 'next/server';

import { getPreferredRegion } from '@/app/api/config';
import { createErrorResponse } from '@/app/api/errorResponse';
import { ChatCompletionErrorPayload, ModelProvider } from '@/libs/agent-runtime';
import { ChatErrorType } from '@/types/fetch';
Expand All @@ -10,8 +9,6 @@ import { initAgentRuntimeWithUserPayload } from '../../agentRuntime';

export const runtime = 'edge';

export const preferredRegion = getPreferredRegion();

const noNeedAPIKey = (provider: string) =>
[ModelProvider.OpenRouter, ModelProvider.TogetherAI].includes(provider as any);

Expand Down
28 changes: 28 additions & 0 deletions src/app/api/chat/openai/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @vitest-environment edge-runtime
import { describe, expect, it, vi } from 'vitest';

import { POST as UniverseRoute } from '../[provider]/route';
import { POST, preferredRegion, runtime } from './route';

// ζ¨‘ζ‹Ÿ '../[provider]/route'
vi.mock('../[provider]/route', () => ({
POST: vi.fn().mockResolvedValue('mocked response'),
}));

describe('Configuration tests', () => {
it('should have runtime set to "edge"', () => {
expect(runtime).toBe('edge');
});

it('should contain specific regions in preferredRegion', () => {
expect(preferredRegion).not.contain(['hkg1']);
});
});

describe('OpenAI POST function tests', () => {
it('should call UniverseRoute with correct parameters', async () => {
const mockRequest = new Request('https://example.com', { method: 'POST' });
await POST(mockRequest);
expect(UniverseRoute).toHaveBeenCalledWith(mockRequest, { params: { provider: 'openai' } });
});
});
25 changes: 25 additions & 0 deletions src/app/api/chat/openai/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { POST as UniverseRoute } from '../[provider]/route';

export const runtime = 'edge';

export const preferredRegion = [
'arn1',
'bom1',
'cdg1',
'cle1',
'cpt1',
'dub1',
'fra1',
'gru1',
'hnd1',
'iad1',
'icn1',
'kix1',
'lhr1',
'pdx1',
'sfo1',
'sin1',
'syd1',
];

export const POST = async (req: Request) => UniverseRoute(req, { params: { provider: 'openai' } });
43 changes: 0 additions & 43 deletions src/app/api/config.test.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/app/api/config.ts

This file was deleted.

24 changes: 21 additions & 3 deletions src/app/api/openai/stt/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import { OpenAISTTPayload } from '@lobehub/tts';
import { createOpenaiAudioTranscriptions } from '@lobehub/tts/server';

import { getPreferredRegion } from '../../config';
import { createBizOpenAI } from '../createBizOpenAI';
import { createBizOpenAI } from '@/app/api/openai/createBizOpenAI';

export const runtime = 'edge';
export const preferredRegion = getPreferredRegion();

export const preferredRegion = [
'arn1',
'bom1',
'cdg1',
'cle1',
'cpt1',
'dub1',
'fra1',
'gru1',
'hnd1',
'iad1',
'icn1',
'kix1',
'lhr1',
'pdx1',
'sfo1',
'sin1',
'syd1',
];

export const POST = async (req: Request) => {
const formData = await req.formData();
Expand Down
24 changes: 21 additions & 3 deletions src/app/api/openai/tts/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import { OpenAITTSPayload } from '@lobehub/tts';
import { createOpenaiAudioSpeech } from '@lobehub/tts/server';

import { getPreferredRegion } from '../../config';
import { createBizOpenAI } from '../createBizOpenAI';
import { createBizOpenAI } from '@/app/api/openai/createBizOpenAI';

export const runtime = 'edge';
export const preferredRegion = getPreferredRegion();

export const preferredRegion = [
'arn1',
'bom1',
'cdg1',
'cle1',
'cpt1',
'dub1',
'fra1',
'gru1',
'hnd1',
'iad1',
'icn1',
'kix1',
'lhr1',
'pdx1',
'sfo1',
'sin1',
'syd1',
];

export const POST = async (req: Request) => {
const payload = (await req.json()) as OpenAITTSPayload;
Expand Down
26 changes: 21 additions & 5 deletions src/app/api/text-to-image/[provider]/route.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { NextResponse } from 'next/server';

import { getPreferredRegion } from '@/app/api/config';
import { initAgentRuntimeWithUserPayload } from '@/app/api/chat/agentRuntime';
import { createErrorResponse } from '@/app/api/errorResponse';
import { checkAuth } from '@/app/api/middleware/auth';
import { ChatCompletionErrorPayload } from '@/libs/agent-runtime';
import { TextToImagePayload } from '@/libs/agent-runtime/types';
import { ChatErrorType } from '@/types/fetch';

import { initAgentRuntimeWithUserPayload } from '../../chat/agentRuntime';
import { checkAuth } from '../../middleware/auth';

export const runtime = 'edge';

export const preferredRegion = getPreferredRegion();
export const preferredRegion = [
'arn1',
'bom1',
'cdg1',
'cle1',
'cpt1',
'dub1',
'fra1',
'gru1',
'hnd1',
'iad1',
'icn1',
'kix1',
'lhr1',
'pdx1',
'sfo1',
'sin1',
'syd1',
];

// return NextResponse.json(
// {
Expand Down
8 changes: 0 additions & 8 deletions src/config/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import { createEnv } from '@t3-oss/env-nextjs';
import { z } from 'zod';

export const getLLMConfig = () => {
// region format: iad1,sfo1
let regions: string[] = [];
if (process.env.OPENAI_FUNCTION_REGIONS) {
regions = process.env.OPENAI_FUNCTION_REGIONS.split(',');
}

return createEnv({
server: {
API_KEY_SELECT_MODE: z.string().optional(),
Expand All @@ -17,7 +11,6 @@ export const getLLMConfig = () => {
OPENAI_API_KEY: z.string().optional(),
OPENAI_PROXY_URL: z.string().optional(),
OPENAI_MODEL_LIST: z.string().optional(),
OPENAI_FUNCTION_REGIONS: z.array(z.string()),

ENABLED_AZURE_OPENAI: z.boolean(),
AZURE_API_KEY: z.string().optional(),
Expand Down Expand Up @@ -99,7 +92,6 @@ export const getLLMConfig = () => {
OPENAI_API_KEY: process.env.OPENAI_API_KEY,
OPENAI_PROXY_URL: process.env.OPENAI_PROXY_URL,
OPENAI_MODEL_LIST: process.env.OPENAI_MODEL_LIST,
OPENAI_FUNCTION_REGIONS: regions as any,

ENABLED_AZURE_OPENAI: !!process.env.AZURE_API_KEY,
AZURE_API_KEY: process.env.AZURE_API_KEY,
Expand Down
3 changes: 2 additions & 1 deletion src/features/Conversation/Messages/Tool/Inspector/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ export const useStyles = createStyles(({ css, token }) => ({
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 1;
font-size: 12px;
text-overflow: ellipsis;
-webkit-line-clamp: 1;
`,
container: css`
cursor: pointer;
Expand Down

0 comments on commit 3d7a35d

Please sign in to comment.