Skip to content

Commit

Permalink
Add Gemini API key and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Lujia-Cheng committed Feb 16, 2024
1 parent 2af8827 commit 36f48ec
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 19 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
REACT_APP_API_URL=
OPENAI_API_KEY=
GEMINI_API_KEY=
DB_HOST=
DB_USER=
DB_PASS=
Expand Down
11 changes: 10 additions & 1 deletion backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"start": "node server.js"
},
"dependencies": {
"@google/generative-ai": "^0.2.1",
"bcrypt": "^5.1.1",
"cors": "^2.8.5",
"express": ">5.0.0-alpha",
"faker": "^6.6.6",
"jsonwebtoken": "^9.0.2",
"mongodb": "^5.9.2",
"mongoose": "^8.1.1",
Expand Down
79 changes: 62 additions & 17 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
const express = require("express");
const cors = require("cors");
const { set } = require("mongoose");
// const mongoose = require("mongoose");
const mongoose = require("mongoose");
// const bcrypt = require("bcrypt");
// const validator = require("validator");
// const jwt = require("jsonwebtoken");
const {
GoogleGenerativeAI,
HarmCategory,
HarmBlockThreshold,
} = require("@google/generative-ai");

const app = express();

// connect to DB
// const mongoDB =
// "mongodb+srv://" +
// process.env.USERNAME +
// ":" +
// process.env.PASSWORD +
// "@" +
// process.env.HOST +
// "/" +
// process.env.DATABASE;
// mongoose.connect(mongoDB);
const mongoDB =
"mongodb+srv://" +
process.env.DB_USER +
":" +
process.env.DB_PASS +
"@" +
process.env.DB_HOST +
"/" +
process.env.DB_NAME;
mongoose.connect(mongoDB);

app.use(cors());
app.use(express.json());
Expand All @@ -31,12 +35,53 @@ app.post("/api/chat", async (req, res) => {
if (!req.body.message) {
return res.status(400).json({ message: "No message provided" });
}
// add a delay for testing
await new Promise((resolve) => setTimeout(resolve, 10000));

res.status(200).json({
message: `[backend] under construction. You sent: ${req.body.message}`,
});
// node --version # Should be >= 18
// npm install @google/generative-ai
const MODEL_NAME = "gemini-1.0-pro";
const AI_API_KEY = process.env.GEMINI_API_KEY;

async function runChat() {
const genAI = new GoogleGenerativeAI(AI_API_KEY);
const model = genAI.getGenerativeModel({ model: MODEL_NAME });

const generationConfig = {
temperature: 0.9,
topK: 1,
topP: 1,
maxOutputTokens: 2048,
};

const safetySettings = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
{
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
];

const chat = model.startChat({
generationConfig,
safetySettings,
history: req.body.history || [],
});

const result = await chat.sendMessage(req.body.message);
const response = result.response;
res.status(400).json({ message: response });
}

// todo api request via node @link https://platform.openai.com/docs/quickstart?context=node
});

Expand Down

0 comments on commit 36f48ec

Please sign in to comment.