From 81ca64183620bcf6c73b132ae67e097842f22488 Mon Sep 17 00:00:00 2001 From: Xicheng Guo Date: Wed, 10 Apr 2024 15:38:12 +0800 Subject: [PATCH] add ai --- src/.vitepress/config.mts | 3 +- src/.vitepress/sidebar.yaml | 3 ++ src/artificial-intelligence/index.md | 0 src/artificial-intelligence/openai-api.md | 32 +++++++++++++++++++++ src/frontend/index.md | 0 src/index.md | 2 ++ sync.mjs | 34 ++++++++++++++--------- 7 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 src/artificial-intelligence/index.md create mode 100644 src/artificial-intelligence/openai-api.md create mode 100644 src/frontend/index.md diff --git a/src/.vitepress/config.mts b/src/.vitepress/config.mts index a75501c..cca9121 100644 --- a/src/.vitepress/config.mts +++ b/src/.vitepress/config.mts @@ -20,8 +20,9 @@ export default defineConfig( nav: [ { text: 'Home', link: '/' }, { text: 'JavaScript', link: '/javascript/index' }, - { text: '前端', link: '/frontend/react/jsx' }, + { text: '前端', link: '/frontend/index' }, { text: '后端', link: '/backend/nodejs/index' }, + { text: 'AI', link: '/artificial-intelligence/index'}, { text: '参考', link: '/reference' } ], diff --git a/src/.vitepress/sidebar.yaml b/src/.vitepress/sidebar.yaml index 5c01501..19f5b12 100644 --- a/src/.vitepress/sidebar.yaml +++ b/src/.vitepress/sidebar.yaml @@ -117,3 +117,6 @@ link: /backend/nodejs/event-emitter - text: File System link: /backend/nodejs/file-system +/artificial-intelligence: + - text: OpenAI API + link: /artificial-intelligence/openai-api diff --git a/src/artificial-intelligence/index.md b/src/artificial-intelligence/index.md new file mode 100644 index 0000000..e69de29 diff --git a/src/artificial-intelligence/openai-api.md b/src/artificial-intelligence/openai-api.md new file mode 100644 index 0000000..4e8a727 --- /dev/null +++ b/src/artificial-intelligence/openai-api.md @@ -0,0 +1,32 @@ +# OpenAI API + +## 基本用法 + +可以使用POST请求向OpenAI API提交内容,其中**content**字段包含提问与回答的内容。 + +**temperature**用于控制生成文本的创造性,值越高,生成的文本越有创造性。 + +::: code-group + +<<< @/../projects/javascript-sandbox/src/openai/basic-usage.ts [Request] + +<<< @/../projects/javascript-sandbox/src/openai/basic-usage-output.json [Response] + +::: + +## 连续对话 + +连续对话需要把每一次的提问和回答都追加到`messages`数组中,以便OpenAI能够理解对话的上下文。 + +**role**可用于指定对话的角色: +- `user`:表示用户的提问 +- `assistant`:表示助手的回答 +- `system`:用于定义整个对话的行为,通常放在`messages`数组的第一个元素 + +::: code-group + +<<< @/../projects/javascript-sandbox/src/openai/chat-usage.ts [Request] + +<<< @/../projects/javascript-sandbox/src/openai/chat-usage-output.json [Response] + +::: \ No newline at end of file diff --git a/src/frontend/index.md b/src/frontend/index.md new file mode 100644 index 0000000..e69de29 diff --git a/src/index.md b/src/index.md index 0cf8181..91e5a76 100644 --- a/src/index.md +++ b/src/index.md @@ -65,4 +65,6 @@ import MarkMap from './MarkMap.vue'; - [NodeJS](backend/nodejs/index) - [EventEmitter](backend/nodejs/event-emitter) - [File System](backend/nodejs/file-system) + - AI + - [OpenAI API](artificial-intelligence/openai-api) "/> diff --git a/sync.mjs b/sync.mjs index 59f29c6..dc71673 100644 --- a/sync.mjs +++ b/sync.mjs @@ -3,22 +3,30 @@ import fs from "fs"; (async () => { const projectsDir = './projects'; - const repoURL = 'https://github.com/GuoXiCheng/react-sandbox.git'; - let cloneCommand; - + // 使用数组来支持多个repoURL + const repoURLs = [ + 'https://github.com/GuoXiCheng/react-sandbox.git', + 'https://github.com/GuoXiCheng/javascript-sandbox.git', + ]; + if (!fs.existsSync(projectsDir)) { fs.mkdirSync(projectsDir); } + + repoURLs.forEach(repoURL => { + let cloneCommand; - if (process.env.CI) { - // 在CI环境中 - const token = process.env.GITHUB_TOKEN; // 确保在CI环境中已设置GITHUB_TOKEN - cloneCommand = `git clone https://${token}:x-oauth-basic@${repoURL.replace('https://', '')}`; - } else { - // 在本地环境中 - cloneCommand = `git clone ${repoURL}`; - } + if (process.env.CI) { + // 在CI环境中 + const token = process.env.GITHUB_TOKEN; // 确保在CI环境中已设置GITHUB_TOKEN + cloneCommand = `git clone https://${token}:x-oauth-basic@${repoURL.replace('https://', '')}`; + } else { + // 在本地环境中 + cloneCommand = `git clone ${repoURL}`; + } - // 在projects目录中克隆仓库 - execSync(cloneCommand, { cwd: projectsDir }); + // 在projects目录中克隆仓库 + execSync(cloneCommand, { cwd: projectsDir }); + }); })(); +