Skip to content

Commit

Permalink
Merge branch 'main' into docker-compose
Browse files Browse the repository at this point in the history
  • Loading branch information
ddiu8081 committed Mar 11, 2023
2 parents a43f726 + 0793b6c commit 9bb06f7
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# build output
dist/
.vercel/
.netlify/

# generated types
.astro/
Expand Down
121 changes: 91 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,91 @@
# ChatGPT-API Demo

A demo repo based on [OpenAI GPT-3.5 Turbo API](https://platform.openai.com/docs/guides/chat).
English | [简体中文](./README.zh-CN.md)

> Notice: Our API Key limit has been exhausted. So the demo site is not available now.
A demo repo based on [OpenAI GPT-3.5 Turbo API.](https://platform.openai.com/docs/guides/chat)

## Run Locally
**🍿 Live preview**: https://chatgpt.ddiu.me

1. Setup & Install dependencies
> ⚠️ Notice: Our API Key limit has been exhausted. So the demo site is not available now.
> First, you need [Node.js](https://nodejs.org/) installed.
![chat-logo](https://cdn.staticaly.com/gh/yzh990918/static@master/chat-logo.webp)


```shell
npm i
```

2. Make a copy of `.env.example`, then rename it to `.env`
3. Add your [OpenAI API key](https://platform.openai.com/account/api-keys) to `.env`
```
## Running Locally

### Pre environment
1. **Node**: Check that both your development environment and deployment environment are using `Node v18` or later. You can use [nvm](https://github.com/nvm-sh/nvm) to manage multiple `node` versions locally。
```bash
node -v
```
2. **PNPM**: We recommend using [pnpm](https://pnpm.io/) to manage dependencies. If you have never installed pnpm, you can install it with the following command:
```bash
npm i -g pnpm
```
3. **OPENAI_API_KEY**: Before running this application, you need to obtain the API key from OpenAI. You can register the API key at [https://beta.openai.com/signup](https://beta.openai.com/signup).

### Getting Started

1. Install dependencies
```bash
pnpm install
```
2. Copy the `.env.example` file, then rename it to `.env`, and add your [OpenAI API key](https://platform.openai.com/account/api-keys) to the `.env` file.
```bash
OPENAI_API_KEY=sk-xxx...
```
4. Run the app
```shell
npm run dev
```
```
3. Run the application, the local project runs on `http:https://localhost:3000/`
```bash
pnpm run dev
```

## Deploy With Vercel
## Deploy

### Deploy With Vercel

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fddiu8081%2Fchatgpt-demo&env=OPENAI_API_KEY&envDescription=OpenAI%20API%20Key&envLink=https%3A%2F%2Fplatform.openai.com%2Faccount%2Fapi-keys)

## Deploy With Netlify
![image](https://cdn.staticaly.com/gh/yzh990918/static@master/20230310/image.4wzfb79qt7k0.webp)


### Deploy With Netlify

[![Deploy with Netlify](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/ddiu8081/chatgpt-demo#OPENAI_API_KEY=&HTTPS_PROXY=&OPENAI_API_BASE_URL=&HEAD_SCRIPTS=&SECRET_KEY=)

**Step-by-step deployment tutorial:**

1. [Fork](https://github.com/ddiu8081/chatgpt-demo/fork) this project,Go to [https://app.netlify.com/start](https://app.netlify.com/start) new Site, select the project you `forked` done, and connect it with your `GitHub` account.

![image](https://cdn.staticaly.com/gh/yzh990918/static@master/20230310/image.3nlt4hgzb16o.webp)

![image](https://cdn.staticaly.com/gh/yzh990918/static@master/20230310/image.5fhfouap270g.webp)


2. Select the branch you want to deploy, then configure environment variables in the project settings.

![image](https://cdn.staticaly.com/gh/yzh990918/static@master/20230311/image.gfs9lx8c854.webp)

3. Select the default build command and output directory, Click the `Deploy Site` button to start deploying the site。

![image](https://cdn.staticaly.com/gh/yzh990918/static@master/20230311/image.4jky9e1wbojk.webp)


### Deploy with Docker
```bash
# build
docker-compose build .
# run
docker-compose up -d
# stop
docker-compose down
```


### Deploy on more servers

Please refer to the official deployment documentation:https://docs.astro.build/en/guides/deploy

## Environment Variables

You can control the website through environment variables.
Expand All @@ -44,19 +99,25 @@ You can control the website through environment variables.
| `SECRET_KEY` | Secret string for the project. Use for generating signatures for API calls | `null` |
| `SITE_PASSWORD` | Set password for site. If not set, site will be public | `null` |

## Deploy with Docker
```bash
# build
docker-compose build .
# run
docker-compose up -d
# stop
docker-compose down
```
## Creative Variant Versions
## Frequently Asked Questions

Q: TypeError: fetch failed (can't connect to OpenAI Api)

A: Configure environment variables `HTTPS_PROXY`,reference: https://github.com/ddiu8081/chatgpt-demo/issues/34

Q: throw new TypeError(${context} is not a ReadableStream.)

A: The Node version needs to be `v18` or later,reference: https://github.com/ddiu8081/chatgpt-demo/issues/65


## Contributing

This project exists thanks to all those who contributed.

Thank you to all our supporters!🙏

- [ourongxing/chatgpt-vercel](https://github.com/ourongxing/chatgpt-vercel)
[![img](https://contributors.nn.ci/api?repo=ddiu8081/chatgpt-demo)](https://github.com/ddiu8081/chatgpt-demo/graphs/contributors)

## License

MIT
MIT © [ddiu8081](https://github.com/ddiu8081/chatgpt-demo/blob/main/LICENSE)
112 changes: 112 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# ChatGPT-API Demo

[English](./README.md) | 简体中文

一个基于 [OpenAI GPT-3.5 Turbo API](https://platform.openai.com/docs/guides/chat) 的 demo。

**🍿 在线预览**: https://chatgpt.ddiu.me

> ⚠️ 注意: 我们的API密钥限制已用尽。所以演示站点现在不可用。
![chat-logo](https://cdn.staticaly.com/gh/yzh990918/static@master/chat-logo.webp)

## 本地运行

### 前置环境

1. **Node**: 检查您的开发环境和部署环境是否都使用 `Node v18` 或更高版本。你可以使用 [nvm](https://github.com/nvm-sh/nvm) 管理本地多个 `node` 版本。
```bash
node -v
```
2. **PNPM**: 我们推荐使用 [pnpm](https://pnpm.io/) 来管理依赖,如果你从来没有安装过 pnpm,可以使用下面的命令安装:
```bash
npm i -g pnpm
```
3. **OPENAI_API_KEY**: 在运行此应用程序之前,您需要从 OpenAI 获取 API 密钥。您可以在 [https://beta.openai.com/signup](https://beta.openai.com/signup) 注册 API 密钥。

### 起步运行

1. 安装依赖
```bash
pnpm install
```
2. 复制 `.env.example` 文件,重命名为 `.env`,并添加你的 [OpenAI API key](https://platform.openai.com/account/api-keys)`.env` 文件中
```bash
OPENAI_API_KEY=sk-xxx...
```
3. 运行应用,本地项目运行在 `http:https://localhost:3000/`
```bash
pnpm run dev
```

## 部署

### 部署在 Vercel

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fddiu8081%2Fchatgpt-demo&env=OPENAI_API_KEY&envDescription=OpenAI%20API%20Key&envLink=https%3A%2F%2Fplatform.openai.com%2Faccount%2Fapi-keys)

![image](https://cdn.staticaly.com/gh/yzh990918/static@master/20230310/image.4wzfb79qt7k0.webp)

### 部署在 Netlify

[![Deploy with Netlify](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/ddiu8081/chatgpt-demo#OPENAI_API_KEY=&HTTPS_PROXY=&OPENAI_API_BASE_URL=&HEAD_SCRIPTS=&SECRET_KEY=)

**分步部署教程:**

1. [Fork](https://github.com/ddiu8081/chatgpt-demo/fork) 此项目,前往 [https://app.netlify.com/start](https://app.netlify.com/start) 新建站点,选择你 `fork` 完成的项目,将其与 `GitHub` 帐户连接。

![image](https://cdn.staticaly.com/gh/yzh990918/static@master/20230310/image.3nlt4hgzb16o.webp)

![image](https://cdn.staticaly.com/gh/yzh990918/static@master/20230310/image.5fhfouap270g.webp)


2. 选择要部署的分支,选择 `main` 分支, 在项目设置中配置环境变量,环境变量配置参考下文。

![image](https://cdn.staticaly.com/gh/yzh990918/static@master/20230310/image.6dvtfmoijb7k.webp)

3. 选择默认的构建命令和输出目录,单击 `Deploy Site` 按钮开始部署站点。

![image](https://cdn.staticaly.com/gh/yzh990918/static@master/20230310/image.e0n7c0zaen4.webp)

### 部署在更多的服务器

请参考官方部署文档:https://docs.astro.build/en/guides/deploy

## 环境变量

配置本地或者部署的环境变量

| 名称 | 描述 | 默认 |
| --- | --- | --- |
| `OPENAI_API_KEY` | 你的 OpenAI API Key | `null` |
| `HTTPS_PROXY` | 为 OpenAI API 提供代理. e.g. `http:https://127.0.0.1:7890` | `null` |
| `OPENAI_API_BASE_URL` | 请求 OpenAI API 的自定义 Base URL. | `https://api.openai.com` |
| `HEAD_SCRIPTS` | 在页面的 `</head>` 之前注入分析或其他脚本 | `null` |
| `SECRET_KEY` | 项目的秘密字符串。用于生成 API 调用的签名 | `null` |
| `SITE_PASSWORD` | 为网站设置密码。如果未设置,则该网站将是公开的 | `null` |

## 常见问题

Q: TypeError: fetch failed (can't connect to OpenAI Api)

A: 配置环境变量 `HTTPS_PROXY`,参考: https://github.com/ddiu8081/chatgpt-demo/issues/34

Q: throw new TypeError(${context} is not a ReadableStream.)

A: Node 版本需要在 `v18` 或者更高,参考: https://github.com/ddiu8081/chatgpt-demo/issues/65

## 创意变体版本

- [ourongxing/chatgpt-vercel](https://github.com/ourongxing/chatgpt-vercel)

## 参与贡献

这个项目的存在要感谢所有做出贡献的人。

感谢我们所有的支持者!🙏

[![img](https://contributors.nn.ci/api?repo=ddiu8081/chatgpt-demo)](https://github.com/ddiu8081/chatgpt-demo/graphs/contributors)

## License

MIT © [ddiu8081](https://github.com/ddiu8081/chatgpt-demo/blob/main/LICENSE)
3 changes: 2 additions & 1 deletion astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import vercelDisableBlocks from './plugins/vercelDisableBlocks'

import node from '@astrojs/node'
import vercel from '@astrojs/vercel/edge'
import netlify from "@astrojs/netlify/functions";
import netlify from "@astrojs/netlify/edge-functions";

const envAdapter = () => {
if (process.env.OUTPUT == 'vercel') {
Expand All @@ -30,6 +30,7 @@ export default defineConfig({
vite: {
plugins: [
process.env.OUTPUT == 'vercel' && vercelDisableBlocks(),
process.env.OUTPUT == 'netlify' && vercelDisableBlocks(),
]
},
});
6 changes: 4 additions & 2 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
FROM node:alpine
RUN mkdir -p /usr/src
WORKDIR /usr/src
RUN npm install -g pnpm
COPY package.json pnpm-lock.yaml /usr/src/
RUN pnpm install
COPY . /usr/src
RUN npm install
RUN npm run build
RUN pnpm run build
EXPOSE 3000
ENV HOST=0.0.0.0
ENV PORT=3000
Expand Down
2 changes: 1 addition & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[build.environment]
NETLIFY_USE_PNPM = "true"
NODE_VERSION = "18"

[build]
command = "OUTPUT=netlify astro build"
functions = "netlify/functions"
publish = "dist"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"astro": "astro"
},
"dependencies": {
"@astrojs/netlify": "^2.2.0",
"@astrojs/netlify": "2.0.0",
"@astrojs/node": "^5.0.4",
"@astrojs/solid-js": "^2.0.2",
"@astrojs/vercel": "^3.1.3",
Expand Down
22 changes: 5 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9bb06f7

Please sign in to comment.