Skip to content

Commit ce31697

Browse files
authored
Merge pull request #4 from JawherKl/feature/4-update-project-structure
update all the structure of project with adding different layers module
2 parents 5936a7e + b0235ce commit ce31697

17 files changed

+120
-1
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"main": "index.js",
55
"scripts": {
66
"test": "echo \"Error: no test specified\" && exit 1",
7-
"start": "node server.mjs"
7+
"start": "node src/server.mjs"
88
},
99
"keywords": [],
1010
"author": "",
File renamed without changes.
File renamed without changes.

search.mjs simple_code/search.mjs

File renamed without changes.

server.mjs simple_code/server.mjs

File renamed without changes.

src/app.mjs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import express from "express";
2+
import analyzeRoutes from "./routes/analyzeRoutes.mjs";
3+
import searchRoutes from "./routes/searchRoutes.mjs";
4+
import { errorHandler } from "./middlewares/errorHandler.mjs";
5+
6+
const app = express();
7+
app.use(express.json());
8+
9+
app.use("/search", searchRoutes);
10+
app.use("/analyze", analyzeRoutes);
11+
12+
app.use(errorHandler);
13+
14+
export default app;

src/config/openAIConfig.mjs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import OpenAI from "openai";
2+
import { config } from "dotenv";
3+
4+
config();
5+
6+
const openai = new OpenAI({
7+
baseURL: process.env.BASE_URL,
8+
apiKey: process.env.API_KEY,
9+
});
10+
11+
export default openai;

src/controllers/analyzeController.mjs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { analyzeData } from "../services/analyzeService.mjs";
2+
3+
export async function analyzeText(req, res) {
4+
try {
5+
const { text } = req.body;
6+
const analysis = await analyzeData(text);
7+
res.json({ analysis });
8+
} catch (error) {
9+
res.status(500).json({ error: error.message });
10+
}
11+
}

src/controllers/searchController.mjs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { semanticSearch } from "../services/searchService.mjs";
2+
3+
export async function searchQuery(req, res) {
4+
try {
5+
const { query, model } = req.body;
6+
const result = await semanticSearch(query, model);
7+
res.json({ result });
8+
} catch (error) {
9+
res.status(500).json({ error: error.message });
10+
}
11+
}

src/middlewares/errorHandler.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function errorHandler(err, req, res, next) {
2+
console.error(err.stack);
3+
res.status(500).json({ error: "Internal Server Error" });
4+
}
5+

src/routes/analyzeRoutes.mjs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import express from "express";
2+
import { analyzeText } from "../controllers/analyzeController.mjs";
3+
4+
const router = express.Router();
5+
6+
router.post("/", analyzeText);
7+
8+
export default router;

src/routes/searchRoutes.mjs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import express from "express";
2+
import { searchQuery } from "../controllers/searchController.mjs";
3+
4+
const router = express.Router();
5+
6+
router.post("/", searchQuery);
7+
8+
export default router;

src/server.mjs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import app from "./app.mjs";
2+
3+
const PORT = process.env.PORT || 3000;
4+
5+
app.listen(PORT, () => {
6+
console.log(`Server running on http://localhost:${PORT}`);
7+
});

src/services/analyzeService.mjs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import openai from "../config/openAIConfig.mjs";
2+
3+
export async function analyzeData(text) {
4+
try {
5+
const completion = await openai.chat.completions.create({
6+
model: "gpt-4-turbo",
7+
messages: [
8+
{
9+
role: "system",
10+
content: "Analyze this text for sentiment, key topics, and named entities."
11+
},
12+
{ role: "user", content: text }
13+
],
14+
temperature: 0.1,
15+
max_tokens: 256
16+
});
17+
return completion.choices[0].message.content;
18+
} catch (error) {
19+
console.error("Analysis error:", error.message);
20+
throw error;
21+
}
22+
}

src/services/searchService.mjs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import openai from "../config/openAIConfig.mjs";
2+
3+
export async function semanticSearch(query, model) {
4+
try {
5+
const completion = await openai.chat.completions.create({
6+
model: model || "gpt-4-turbo",
7+
messages: [
8+
{
9+
role: "system",
10+
content: "You are a powerful search engine. Return concise, factual answers."
11+
},
12+
{ role: "user", content: query }
13+
],
14+
temperature: 0.3,
15+
max_tokens: 150
16+
});
17+
return completion.choices[0].message.content;
18+
} catch (error) {
19+
console.error("Search error:", error.message);
20+
throw error;
21+
}
22+
}

test/analyze.test.js

Whitespace-only changes.

test/search.test.js

Whitespace-only changes.

0 commit comments

Comments
 (0)