-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Bug Recommendation system using vector cosine-similarity #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,4 +12,7 @@ dist-ssr/ | |
| .DS_Store | ||
| Thumbs.db | ||
|
|
||
| .vscode/ | ||
| .vscode/ | ||
|
|
||
| __pycache__/ | ||
| *.pyc | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import os | ||
| from dotenv import load_dotenv | ||
| from flask import Flask, request, jsonify | ||
| from model import get_embedding | ||
| from utils import build_text | ||
|
|
||
| load_dotenv() | ||
| app = Flask(__name__) | ||
|
|
||
| @app.route("/embed", methods=["POST"]) | ||
| def embed(): | ||
| try: | ||
| data = request.json | ||
| if not data: | ||
| return jsonify({"error": "Invalid input"}), 400 | ||
| text = build_text(data) | ||
| embedding = get_embedding(text) | ||
| return jsonify({"embedding": embedding}), 200 | ||
| except Exception as e: | ||
| print("Error in /embed:", str(e)) | ||
| return jsonify({"error": "Internal server error"}), 500 | ||
|
|
||
| if __name__ == "__main__": | ||
| PORT = int(os.getenv("PORT", 3000)) | ||
| DEBUG = os.getenv("FLASK_DEBUG", "false").lower() in ["true", "1", "yes"] | ||
| app.run(port=PORT, debug=DEBUG) | ||
|
Comment on lines
+23
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling for If the 🛡️ Proposed fix to handle invalid PORT values if __name__ == "__main__":
- PORT = int(os.getenv("PORT", 3000))
+ try:
+ PORT = int(os.getenv("PORT", "3000"))
+ except ValueError:
+ PORT = 3000
DEBUG = os.getenv("FLASK_DEBUG", "false").lower() in ["true", "1", "yes"]
app.run(port=PORT, debug=DEBUG)🤖 Prompt for AI Agents |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from sentence_transformers import SentenceTransformer | ||
|
|
||
| model = SentenceTransformer('all-MiniLM-L6-v2') | ||
|
|
||
| def get_embedding(text): | ||
| return model.encode(text).tolist() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| flask | ||
| sentence-transformers | ||
| numpy | ||
| scikit-learn | ||
| python-dotenv |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| def safe_join(value): | ||
| if isinstance(value, list): | ||
| return " ".join(value) | ||
| if isinstance(value, str): | ||
| return value | ||
| return "" | ||
| def build_text(data): | ||
| title = data.get("title", "") | ||
| description = data.get("description", "") | ||
| tags_text = safe_join(data.get("tags")) | ||
| tech_text = safe_join(data.get("techStack")) | ||
| difficulty = data.get("difficulty", "") | ||
|
|
||
| text = f""" | ||
| BUG TITLE: {title}. {title}. | ||
| BUG DESCRIPTION: {description}. | ||
| BUG TAGS: {tags_text}. | ||
| TECHNOLOGIES: {tech_text}. | ||
| DIFFICULTY LEVEL: {difficulty}. | ||
| """ | ||
| return text.lower().strip() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| const Bug = require("../models/Bug"); | ||
| const Vector = require("../models/Vector"); | ||
| const { cosineSimilarity } = require("../utils/similarityScore"); | ||
| const mongoose = require("mongoose"); | ||
|
|
||
| exports.getSimilarBugs = async (req, res) => { | ||
| try { | ||
| const bugId = req.params.id; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| if (!mongoose.Types.ObjectId.isValid(bugId)) { | ||
| return res.status(400).json({ message: "Invalid bugId" }); | ||
| } | ||
|
|
||
| const currentVectorDoc = await Vector.findOne({ bugId }); | ||
|
|
||
| if (!currentVectorDoc) { | ||
| return res.status(404).json({ message: "Vector not found" }); | ||
| } | ||
| const currentVector = currentVectorDoc.vector; | ||
| const allVectors = await Vector.find({ bugId: { $ne: bugId } }); | ||
| const similarities = allVectors.map((item) => ({ | ||
| bugId: item.bugId, | ||
| score: cosineSimilarity(currentVector, item.vector) | ||
| })); | ||
| const top5 = similarities | ||
| .sort((a, b) => b.score - a.score) | ||
| .slice(0, 5); | ||
| const bugs = await Bug.find({ | ||
| _id: { $in: top5.map(i => i.bugId) } | ||
| }); | ||
| const bugMap = new Map(); | ||
| bugs.forEach(b => bugMap.set(b._id.toString(), b)); | ||
|
|
||
| const finalRecommendations = top5.map(item => { | ||
| const bug = bugMap.get(item.bugId.toString()); | ||
| if (!bug) return null; | ||
| return { | ||
| ...bug.toObject(), | ||
| similarityScore: item.score | ||
| }; | ||
| }).filter(Boolean); | ||
| res.json({ | ||
| recommendations: finalRecommendations | ||
| }); | ||
|
|
||
| } catch (err) { | ||
| console.error(err); | ||
| res.status(500).json({ message: "Error fetching recommendations" }); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| const mongoose = require("mongoose"); | ||
| const Bug = require("../models/Bug"); | ||
| const Vector = require("../models/Vector"); | ||
| const { getEmbedding } = require("../services/embedding.Service"); | ||
|
|
||
| exports.createVector = async (req, res) => { | ||
| try { | ||
| const { bugId } = req.body; | ||
|
|
||
| if (!bugId) { | ||
| return res.status(400).json({ message: "bugId is required" }); | ||
| } | ||
|
|
||
| if (!mongoose.Types.ObjectId.isValid(bugId)) { | ||
| return res.status(400).json({ message: "Invalid bugId format" }); | ||
| } | ||
|
|
||
| const bug = await Bug.findById(bugId); | ||
|
|
||
| if (!bug) { | ||
| return res.status(404).json({ message: "Bug not found" }); | ||
| } | ||
|
|
||
| const embedding = await getEmbedding(bug); | ||
|
|
||
| const vectorDoc = await Vector.findOneAndUpdate( | ||
| { bugId: bug._id }, | ||
| { | ||
| bugId: bug._id, | ||
| vector: embedding, | ||
| modelVersion: "all-MiniLM-L6-v2", | ||
| updatedAt: new Date(), | ||
| }, | ||
| { upsert: true, returnDocument: "after"} | ||
| ); | ||
|
|
||
| return res.status(200).json({ | ||
| message: "Vector created successfully", | ||
| data: vectorDoc, | ||
| }); | ||
|
|
||
| } catch (error) { | ||
| console.error("VECTOR ERROR:", error); | ||
| return res.status(500).json({ message: "Error creating vector" }); | ||
| } | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.