-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace gemini with openai api
- Loading branch information
1 parent
4cb1be6
commit 9b76b22
Showing
13 changed files
with
139 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,6 @@ | ||
[ | ||
{ | ||
"role": "user", | ||
"parts": "這是一場角色扮演,但請您不要向我再次透漏這是一場扮演,並以一般交談方式溝通。溝通請使用正體中文交談,您的正體中文名字是「寧芙」,英文名字是「Nymph」。你來自於「臺灣網際網路技術推廣組織」,該組織英文名稱為「Taiwan Web Technology Promotion Organization」,該組織主旨為:在臺灣推廣網際網路技術,如JavaScript、資訊安全素養、網路通訊觀念......等。溝通方式將以 Discord 通訊軟體的方式進行,你可以任意使用Markdown語法。若有人使用「@」標注任何人,請勿不要再次重複標注。" | ||
}, | ||
{ | ||
"role": "model", | ||
"parts": "好的,這是一場扮演遊戲,且我不會再次對你透漏這是一場扮演,並會以一般交談方式溝通。現在開始我的名字是「寧芙」,來自於「臺灣網際網路技術推廣組織」。並在通訊軟體中,將聽從你的指示回覆訊息。" | ||
"role": "system", | ||
"content": "這是一場角色扮演,但請您不要向我再次透漏這是一場扮演,並以一般交談方式溝通。溝通請使用正體中文交談,您的正體中文名字是「寧芙」,英文名字是「Nymph」。你來自於「臺灣網際網路技術推廣組織」,該組織英文名稱為「Taiwan Web Technology Promotion Organization」,該組織主旨為:在臺灣推廣網際網路技術,如JavaScript、資訊安全素養、網路通訊觀念......等。溝通方式將以 Discord 通訊軟體的方式進行,你可以任意使用Markdown語法。若有人使用「@」標注任何人,請勿不要再次重複標注。" | ||
} | ||
] |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"use strict"; | ||
// openai is a client for the OpenAI API | ||
|
||
const {getMust} = require("../config"); | ||
|
||
const OpenAI = require("openai"); | ||
|
||
const baseUrl = getMust("OPENAI_BASE_URL"); | ||
const apiKey = getMust("OPENAI_API_KEY"); | ||
const chatModel = getMust("OPENAI_CHAT_MODEL"); | ||
|
||
const prependPrompts = require("../../prompts.json"); | ||
|
||
const client = new OpenAI({baseUrl, apiKey}); | ||
|
||
const chatHistoryMapper = new Map(); | ||
|
||
/** | ||
* Randomly choose an element from an array. | ||
* @param {Array<object>} choices The array of choices. | ||
* @return {object} The randomly chosen element. | ||
*/ | ||
function choose(choices) { | ||
const seed = Math.random(); | ||
const index = Math.floor(seed * choices.length); | ||
return choices[index]; | ||
} | ||
|
||
/** | ||
* Chat with the AI. | ||
* @param {string} chatId The chat ID to chat with the AI. | ||
* @param {string} prompt The prompt to chat with the AI. | ||
* @return {Promise<string>} The response from the AI. | ||
*/ | ||
async function chatWithAI(chatId, prompt) { | ||
if (!chatHistoryMapper.has(chatId)) { | ||
chatHistoryMapper.set(chatId, []); | ||
} | ||
const chatHistory = chatHistoryMapper.get(chatId); | ||
|
||
const messages = [ | ||
...chatHistory, | ||
...prependPrompts, | ||
{ | ||
role: "user", | ||
content: prompt, | ||
}, | ||
]; | ||
|
||
// Debug | ||
console.log(chatId); | ||
console.log(prompt); | ||
console.log(messages); | ||
|
||
const response = await client.chat.completions.create({ | ||
model: chatModel, | ||
messages, | ||
}); | ||
|
||
const choice = choose(response.choices); | ||
const content = choice.message.content; | ||
chatHistory.push({ | ||
role: "assistant", | ||
content, | ||
}); | ||
|
||
// Debug | ||
console.log(response); | ||
console.log(content); | ||
|
||
return choice.message.content; | ||
} | ||
|
||
exports.useClient = () => client; | ||
exports.chatWithAI = chatWithAI; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters