Skip to content

Commit

Permalink
feat: 使用新版 openai 服务,新增环境变量OPENAI_MODEL(不填默认gpt-4o)
Browse files Browse the repository at this point in the history
* README.md文件更新

* openai使用老版本

* openai:使用官方示例代码修改

* 添加机器人配置输出

* 从dotenv引入环境变量

* README.md更新

* README.md更新

* 新增system消息,环境变量OPENAI_SYSTEM_MESSAGE

* 更新env.example

* 删除config.js
  • Loading branch information
lengsukq authored Jun 25, 2024
1 parent 058af23 commit 568713a
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 74 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# .env

# OpenAi 的api key, 去 https://beta.openai.com/account/api-keys 中生成一个即可
# OpenAi 的api key, 去 https://beta.openai.com/account/api-keys 中生成一个即可,OPENAI_MODEL不填则默认gpt-4o,OPENAI_SYSTEM_MESSAGE为默认角色设定
OPENAI_API_KEY=''
OPENAI_PROXY_URL='https://openai.xxxx.com/v1/'
OPENAI_MODEL=''
OPENAI_SYSTEM_MESSAGE=You are a personal assistant.

# Kimi 的api key, 去 https://platform.moonshot.cn/console/api-keys
KIMI_API_KEY=''
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ test.js
package-lock.json
yarn.lock
Chromium.app
config.js

.DS_Store
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
## 使用前需要配置的 AI 服务(目前支持 4 种,可任选其一)

- deepseek获取自己的 `api key`,地址戳这里 👉🏻 :[deepseek开放平台](https://platform.deepseek.com/usage)
将获取到的`api key`填入 `.evn` 文件中,``
将获取到的`api key`填入 `.evn` 文件中的 `DEEPSEEK_FREE_TOKEN` 中。

- 科大讯飞

新增科大讯飞,去这里申请一个 key:[科大讯飞](https://console.xfyun.cn/services/bm35),每个模型都有 200 万的免费 token ,感觉很难用完。
Expand All @@ -35,6 +36,9 @@
OPENAI_API_KEY='你的key'
```

- 其他
(待实践)理论上使用openAI格式的api,都可以使用,在env文件中修改对应的api_key、model、proxy_url即可。

## 赞助商

<p align="center">
Expand Down Expand Up @@ -153,7 +157,7 @@ ROOM_WHITELIST=XX群1,群2
![](https://raw.githubusercontent.com/wangrongding/image-house/master/202403231002859.png)

- 确保你的 openai key 有余额
- 配置好 config.js 和 .env 文件
- 配置好 .env 文件
- 执行 npm run test 能成功拿到 openai 的回复
- 执行 npm run dev 愉快的玩耍吧~ 🎉

Expand All @@ -168,7 +172,7 @@ ROOM_WHITELIST=XX群1,群2
```sh
$ docker build . -t wechat-bot

$ docker run -d --rm --name wechat-bot -v $(pwd)/config.js:/app/config.js -v $(pwd)/.env:/app/.env wechat-bot
$ docker run -d --rm --name wechat-bot -v $(pwd)/.env:/app/.env wechat-bot
```

## Star History Chart
Expand Down
13 changes: 0 additions & 13 deletions config.js

This file was deleted.

11 changes: 0 additions & 11 deletions config.js.example

This file was deleted.

4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import dotenv from 'dotenv'
import fs from 'fs'
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'
import { defaultMessage, shardingMessage } from './wechaty/sendMessage.js'
import { defaultMessage } from './wechaty/sendMessage.js'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
Expand Down Expand Up @@ -178,7 +178,7 @@ function init() {
handleStart(res.serviceType)
})
.catch((error) => {
console.log('❌ inquirer error:', error)
console.log('❌ inquirer error:', error)
})
}
}
Expand Down
56 changes: 14 additions & 42 deletions src/openai/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,25 @@ import stripMarkdown from 'strip-markdown'
import OpenAIApi from 'openai'
import dotenv from 'dotenv'
const env = dotenv.config().parsed // 环境参数

let config = {
apiKey: env.OPENAI_API_KEY,
organization: '',
}
if (process.env.OPENAI_PROXY_URL) {
config.baseURL = process.env.OPENAI_PROXY_URL
if (env.OPENAI_PROXY_URL) {
config.baseURL = env.OPENAI_PROXY_URL
}
const openai = new OpenAIApi(config)

const chosen_model = env.OPENAI_MODEL || 'gpt-4o'
export async function getGptReply(prompt) {
console.log('🚀🚀🚀 / prompt', prompt)
//let chosen_model = 'text-davinci-003'
let chosen_model = 'gpt-3.5-turbo'
let reply = ''
//'gpt-3.5-turbo',
if (chosen_model == 'text-davinci-003') {
console.log('🚀🚀🚀 / Using model', chosen_model)
const response = await openai.createCompletion({
model: chosen_model,
prompt: prompt,
temperature: 0.8, // 每次返回的答案的相似度0-1(0:每次都一样,1:每次都不一样)
max_tokens: 4_000,
top_p: 1,
frequency_penalty: 0.0,
presence_penalty: 0.6,
stop: [' Human:', ' AI:'],
})

reply = markdownToText(response.data.choices[0].text)
} else if (chosen_model == 'gpt-3.5-turbo') {
console.log('🚀🚀🚀 / Using model', chosen_model)
const response = await openai.chat.completions.create({
model: chosen_model,
messages: [
{ role: 'system', content: 'You are a personal assistant.' },
{ role: 'user', content: prompt },
],
})

reply = markdownToText(response.data.choices[0].message.content)
}
console.log('🚀🚀🚀 / reply', reply)
return `${reply}\nVia ${chosen_model}`
}

function markdownToText(markdown) {
return remark()
.use(stripMarkdown)
.processSync(markdown ?? '')
.toString()
const response = await openai.chat.completions.create({
messages: [
{ role: 'system', content: env.OPENAI_SYSTEM_MESSAGE },
{ role: 'user', content: prompt },
],
model: chosen_model,
})
console.log('🚀🚀🚀 / reply', response.choices[0].message.content)
return `${response.choices[0].message.content}\nVia ${chosen_model}`
}
15 changes: 14 additions & 1 deletion src/wechaty/sendMessage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { botName, roomWhiteList, aliasWhiteList } from '../../config.js'
import dotenv from 'dotenv'
// 加载环境变量
dotenv.config()
const env = dotenv.config().parsed // 环境参数

// 从环境变量中导入机器人的名称
const botName = env.BOT_NAME

// 从环境变量中导入联系人白名单
const aliasWhiteList = env.ALIAS_WHITELIST ? env.ALIAS_WHITELIST.split(',') : []

// 从环境变量中导入群聊白名单
const roomWhiteList = env.ROOM_WHITELIST ? env.ROOM_WHITELIST.split(',') : []

import { getServe } from './serve.js'

/**
Expand Down

0 comments on commit 568713a

Please sign in to comment.