-
-
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.
Merge pull request #15 from langx:xuelink/issue14
Migrate to ChatGPT from Gemini API
- Loading branch information
Showing
11 changed files
with
337 additions
and
290 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
DISCORD_BOT_TOKEN=your_discord_token | ||
DISCORD_CLIENT_ID=your_discord_client_id | ||
GEMINI_API_KEY=your_gemini_api_key | ||
|
||
OPENAI_API_KEY=your_openai_api_key | ||
OPENAI_ASSISTANT_ID=your_openai_assistant_id |
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,24 +1,23 @@ | ||
{ | ||
"projectId": "650750d21e4a6a589be3", | ||
"projectName": "LangX", | ||
"functions": [ | ||
{ | ||
"$id": "copilot", | ||
"name": "copilot", | ||
"runtime": "node-20.0", | ||
"execute": [], | ||
"events": [], | ||
"schedule": "", | ||
"timeout": 15, | ||
"enabled": true, | ||
"logging": true, | ||
"entrypoint": "index.js", | ||
"commands": "npm install --omit=dev", | ||
"ignore": [ | ||
"node_modules", | ||
".npm" | ||
], | ||
"path": "./" | ||
} | ||
] | ||
} | ||
"projectId": "650750d21e4a6a589be3", | ||
"projectName": "LangX", | ||
"functions": [ | ||
{ | ||
"$id": "copilot", | ||
"name": "copilot", | ||
"runtime": "node-20.0", | ||
"execute": [], | ||
"events": [ | ||
"databases.650750f16cd0c482bb83.collections.65075108a4025a4f5bd7.documents.*.create" | ||
], | ||
"schedule": "", | ||
"timeout": 15, | ||
"enabled": true, | ||
"logging": true, | ||
"entrypoint": "index.js", | ||
"commands": "npm install --omit=dev", | ||
"ignore": ["node_modules", ".npm"], | ||
"path": "./" | ||
} | ||
] | ||
} |
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,55 +1,66 @@ | ||
import OpenAI from "openai"; | ||
import dotenv from "dotenv"; | ||
|
||
import { genAI, safetySettings } from "../utils/common.js"; | ||
|
||
// Load environment variables from .env file | ||
dotenv.config(); | ||
|
||
const systemInstruction = decodeURIComponent(process.env.SYSTEM_INSTRUCTION); | ||
const chatHistory = JSON.parse(process.env.CHAT_HISTORY) || []; | ||
// Verify that the environment variable is loaded correctly | ||
if (!process.env.OPENAI_API_KEY) { | ||
throw new Error( | ||
"The OPENAI_API_KEY environment variable is missing or empty." | ||
); | ||
} | ||
|
||
const model = genAI.getGenerativeModel({ | ||
model: "gemini-1.5-pro-latest", | ||
systemInstruction: systemInstruction, | ||
const openai = new OpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY, | ||
}); | ||
|
||
const generationConfig = { | ||
temperature: 1, | ||
topP: 0.95, | ||
topK: 64, | ||
maxOutputTokens: 512, | ||
responseMimeType: "application/json", | ||
}; | ||
const assistant_id = process.env.OPENAI_ASSISTANT_ID; | ||
|
||
async function handleInteraction(userMessage) { | ||
try { | ||
if ( | ||
!userMessage || | ||
typeof userMessage !== "string" || | ||
userMessage.trim() === "" | ||
) { | ||
console.error("Invalid user message:", userMessage); | ||
throw new Error("Invalid user message"); | ||
} | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
// Create the thread | ||
const thread = await openai.beta.threads.create({ | ||
messages: [ | ||
{ | ||
role: "user", | ||
content: userMessage, | ||
}, | ||
], | ||
}); | ||
|
||
const chatSession = model.startChat({ | ||
generationConfig, | ||
safetySettings, | ||
history: chatHistory, | ||
}); | ||
|
||
// Send user message | ||
const result = await chatSession.sendMessage(userMessage); | ||
if (!result || !result.response) { | ||
console.error("Invalid response from generative model:", result); | ||
throw new Error("Invalid response from generative model"); | ||
} | ||
if (!thread || !thread.id) { | ||
throw new Error("Thread creation failed"); | ||
} | ||
|
||
// Return the corrected message | ||
return result.response; | ||
} catch (error) { | ||
console.error("Error in handleInteraction:", error); | ||
throw error; | ||
} | ||
console.log("Thread created:", thread); | ||
|
||
let output = ""; | ||
|
||
openai.beta.threads.runs | ||
.stream(thread.id, { | ||
assistant_id: assistant_id, | ||
}) | ||
.on("textDelta", (textDelta) => { | ||
output += textDelta.value; | ||
}) | ||
.on("end", async () => { | ||
console.log(output); | ||
|
||
// Delete Thread | ||
const response = await openai.beta.threads.del(thread.id); | ||
if (response.deleted) { | ||
console.log("Thread deleted"); | ||
} | ||
// You can now use the 'output' variable for further processing | ||
resolve(output); | ||
}); | ||
} catch (error) { | ||
console.error("Error creating thread or run:", error); | ||
reject(error); | ||
} | ||
}); | ||
} | ||
|
||
export { handleInteraction }; |
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
Oops, something went wrong.