forked from fuergaosi233/wechat-chatgpt
-
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.
- Loading branch information
Showing
8 changed files
with
467 additions
and
166 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* 使用内存作为数据库 | ||
*/ | ||
type session = { | ||
userMsg?: string, | ||
assistantMsg?: string | ||
} | ||
type user = { | ||
username: string, | ||
prompt: string, | ||
session: session[] | ||
} | ||
type data = user[] | ||
// Initialize data | ||
const data: data = [] | ||
|
||
/** | ||
* Add user | ||
* @param username | ||
* @param prompt default: "" | ||
*/ | ||
function addUser(username: string, prompt: string = ""): user { | ||
const user = { | ||
username: username, | ||
prompt: prompt, | ||
session: [{ | ||
userMsg: "", | ||
assistantMsg: "" | ||
}] | ||
} | ||
data.push(user) | ||
return data.find(user => user.username === username) as user; | ||
} | ||
|
||
function addSessionByUsername( | ||
username: string, | ||
{userMsg = "", assistantMsg = ""}: session | ||
): void { | ||
const user = getUserByUsername(username) | ||
if (user) { | ||
user.session.push({ | ||
userMsg: userMsg, | ||
assistantMsg: assistantMsg | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* Get user by username | ||
* @param username | ||
*/ | ||
function getUserByUsername(username: string): user | undefined { | ||
let user = data.find(user => user.username === username); | ||
return user | ||
} | ||
function getSessionByUsername(username: string): session[] | undefined { | ||
const user = getUserByUsername(username) | ||
if (user) { | ||
return user.session | ||
} | ||
} | ||
function getAllData(): data { | ||
return data | ||
} | ||
function setPromptByUsername(username: string, prompt: string): void { | ||
const user = getUserByUsername(username) | ||
if (user) { | ||
user.prompt = prompt | ||
}else{ | ||
addUser(username,prompt).prompt= prompt | ||
} | ||
} | ||
export {addUser, addSessionByUsername,getUserByUsername, getSessionByUsername,getAllData,setPromptByUsername} |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import {Configuration, OpenAIApi, ChatCompletionRequestMessageRoleEnum} from "openai"; | ||
import {addSessionByUsername, getUserByUsername} from "./data.js"; | ||
import {ChatCompletionRequestMessage} from "openai/api"; | ||
|
||
|
||
const configuration = new Configuration({ | ||
apiKey: process.env.OPENAI_API_KEY, | ||
}); | ||
const openai = new OpenAIApi(configuration); | ||
|
||
/** | ||
* Get completion from OpenAI | ||
* @param username | ||
* @param message | ||
*/ | ||
async function getCompletion(username:string,message: string): Promise<string> { | ||
// 先将用户输入的消息添加到数据库中 | ||
let userData = getUserByUsername(username) | ||
console.log("数据库返回: ", userData) | ||
const messages:ChatCompletionRequestMessage[] = []; | ||
if (userData) { | ||
// 添加用户输入的消息 | ||
console.log(`${username}的session:`, userData.session) | ||
addSessionByUsername(username, {userMsg: message}) | ||
console.log("Database: ", getUserByUsername(username)) | ||
// 填充prompt | ||
if(userData.prompt!==""){ | ||
messages.push({ | ||
role: ChatCompletionRequestMessageRoleEnum.System, | ||
content: userData.prompt | ||
}) | ||
} | ||
// 填充messages | ||
userData.session.map((item) => { | ||
if (item.userMsg!=="") { | ||
messages.push({ | ||
role: ChatCompletionRequestMessageRoleEnum.User, | ||
content: item.userMsg as string | ||
}) | ||
} | ||
if (item.assistantMsg!=="") { | ||
messages.push({ | ||
role: ChatCompletionRequestMessageRoleEnum.Assistant, | ||
content: item.assistantMsg as string | ||
}) | ||
} | ||
}) | ||
}else{ | ||
return "请先执行/cmd prompt命令. \n EXAMPLE: /cmd prompt 你的prompt" | ||
} | ||
console.log("ChatGPT MESSages: ", messages) | ||
const response = await openai.createChatCompletion({ | ||
model: "gpt-3.5-turbo", | ||
messages: messages, | ||
temperature: 0.6 | ||
}).then((res) => res.data); | ||
if (response.choices[0].message) { | ||
return response.choices[0].message.content.replace(/^\n+|\n+$/g, ""); | ||
} else { | ||
return "Something went wrong" | ||
} | ||
} | ||
|
||
export {getCompletion}; |
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,34 @@ | ||
import {config} from "./config.js"; | ||
// const fs = require('fs'); | ||
import fs from 'fs'; | ||
|
||
let api = config.api; | ||
let apiKey = config.openai_api_key; | ||
const voiceToText = async (path:string) => { | ||
const formData = new FormData(); | ||
formData.append("model", "whisper-1"); | ||
// 根据文件路径读取文件 | ||
const fileContent = fs.readFileSync(path) | ||
// @ts-ignore | ||
formData.append("file",fileContent); | ||
try { | ||
const response = await fetch(`${api}/v1/audio/transcriptions`, { | ||
method: "POST", | ||
headers: { | ||
Authorization: `Bearer ${apiKey}`, | ||
"Content-Type": "multipart/form-data", | ||
}, | ||
body: formData | ||
}).then((res) => res.json()); | ||
if (response.error?.message) { | ||
console.log("OpenAI API ERROR: ",response.error.message) | ||
// throw new Error(`OpenAI API ${response.error.message}`); | ||
} | ||
return response.text; | ||
} catch (e) { | ||
console.error(e) | ||
return "Something went wrong" | ||
} | ||
} | ||
|
||
export {voiceToText}; |