Skip to content

Commit

Permalink
feat: update chat model and support DISCORD_IMAGE_PROXY (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvqq authored Jun 15, 2023
1 parent 650d300 commit 1e21674
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ All deployment configurations could be configured in the `.env` file or in **Env
| OPENAI_API_BASE_URL | api.openai.com | The default address of the requested api |
| DISCORD_SERVER_ID | - | Discord server id |
| DISCORD_CHANNEL_ID | - | Discord channel id |
| DISCORD_TOKEN | - | Discord token |
| DISCORD_TOKEN | - | Discord token |
| DISCORD_IMAGE_PROXY | - | Discord image proxy url |


### Global Configurations
Expand Down
3 changes: 2 additions & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@
| OPENAI_API_BASE_URL | api.openai.com | 请求 api 的默认地址 |
| DISCORD_SERVER_ID | - | Discord 服务器 id |
| DISCORD_CHANNEL_ID | - | Discord 频道 id |
| DISCORD_TOKEN | - | Discord token |
| DISCORD_TOKEN | - | Discord token |
| DISCORD_IMAGE_PROXY | - | Discord 返回图片代理地址 |


### 全局配置
Expand Down
8 changes: 5 additions & 3 deletions src/configs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ export const localConversationKey = 'LOCAL_CONVERSATION';
// From https://platform.openai.com/docs/models/model-endpoint-compatibility
export const supportedModels = [
'gpt-4',
'gpt-4-0314',
'gpt-4-0613',
'gpt-4-32k',
'gpt-4-32k-0314',
'gpt-4-32k-0613',
'gpt-3.5-turbo',
'gpt-3.5-turbo-0301',
'gpt-3.5-turbo-0613',
'gpt-3.5-turbo-16k',
'gpt-3.5-turbo-16k-0613',
] as const;

export type SupportedModel = (typeof supportedModels)[number];
Expand Down
2 changes: 2 additions & 0 deletions src/configs/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export const serverGlobalConfigs: { polling: number } = {
// load balancer polling step
polling: 0,
};

export const discordImageCdn = 'cdn.discordapp.com';
20 changes: 19 additions & 1 deletion src/pages/api/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
type MessageType,
type MessageTypeProps,
} from 'midjourney-fetch';
import { discordImageCdn } from '@configs/server';
import {
apiKeyStrategy,
apiKeys,
Expand All @@ -17,6 +18,7 @@ import {
dicordServerId,
discordChannelId,
discordToken,
discordImageProxy,
} from '.';

export { config };
Expand Down Expand Up @@ -88,7 +90,23 @@ export const get: APIRoute = async ({ request }) => {
const message = await midjourney.getMessage(prompt, options);

if (message) {
return new Response(JSON.stringify(message), { status: 200 });
return new Response(
JSON.stringify(
discordImageProxy
? {
...message,
attachments: message.attachments.map((attachment) => ({
...attachment,
url: attachment.url.replace(
discordImageCdn,
discordImageProxy
),
})),
}
: message
),
{ status: 200 }
);
}

return new Response(JSON.stringify({ msg: 'No content found' }), {
Expand Down
4 changes: 4 additions & 0 deletions src/pages/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export const discordChannelId =
export const discordToken =
import.meta.env.DISCORD_TOKEN || process.env.DISCORD_TOKEN;

export const discordImageProxy = (
import.meta.env.DISCORD_IMAGE_PROXY || process.env.DISCORD_IMAGE_PROXY
)?.replace(/^https?:\/\//i, '');

/**
* https://vercel.com/docs/concepts/edge-network/regions#region-list
* disable hkg1 HongKong
Expand Down

0 comments on commit 1e21674

Please sign in to comment.