Skip to content

Commit 07b7c30

Browse files
Initial commit for Domain Modelling Copilot project
1 parent b9f5902 commit 07b7c30

File tree

14 files changed

+930
-1
lines changed

14 files changed

+930
-1
lines changed

.dockerignore

Whitespace-only changes.

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Python-related files
2+
*.pyc
3+
__pycache__/
4+
*.pyo
5+
*.pyd
6+
*.ipynb_checkpoints/
7+
8+
# Environment files
9+
.env
10+
11+
# IDE-specific files
12+
.vscode/
13+
.idea/
14+
15+
# OS-specific files
16+
.DS_Store
17+
Thumbs.db
18+
19+
# Docker-related files
20+
*.log

Adapters/GPT_v2/gpt2.py

Lines changed: 372 additions & 0 deletions
Large diffs are not rendered by default.

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Use a base image
2+
FROM python:3.10
3+
4+
# Set the working directory
5+
WORKDIR /app
6+
7+
# Copy project files into the container
8+
COPY . .
9+
10+
# Install dependencies
11+
RUN pip install -r requirements.txt
12+
13+
# Set the default command
14+
CMD ["python", "app.py"]

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
# domain-modelling-copilot

app.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from flask import Flask, render_template, request, jsonify
2+
from openai import AzureOpenAI
3+
from dotenv import load_dotenv
4+
import os
5+
6+
app = Flask(__name__)
7+
app.secret_key = "supersecretkey" # Required for session handling
8+
9+
# Initialize the OpenAI client
10+
load_dotenv(override=True)
11+
client = AzureOpenAI(
12+
api_version=os.getenv("GPT_API_VERSION"),
13+
api_key=os.getenv("API_KEY"),
14+
azure_endpoint=os.getenv("ENDPOINT"),
15+
)
16+
17+
# Function to get chatbot response from OpenAI API
18+
def chatbot_response(user_input):
19+
try:
20+
prompts = [
21+
{"role": "system", "content": "You are a helpful assistant."},
22+
{"role": "user", "content": user_input},
23+
]
24+
25+
response = client.chat.completions.create(
26+
model=os.getenv("GPT_MODEL"), messages=prompts
27+
)
28+
29+
return response.choices[0].message.content.strip()
30+
except Exception as e:
31+
print(f"Error with OpenAI API: {e}")
32+
return "Sorry, I couldn't process your request."
33+
34+
# Initialize chat history as a list of dictionaries
35+
chat_history = []
36+
37+
@app.route("/")
38+
def home():
39+
global chat_history
40+
chat_history = [] # Reset chat history on page load
41+
return render_template("index.html")
42+
43+
@app.route("/chat", methods=["POST"])
44+
def chat():
45+
global chat_history
46+
try:
47+
# Get user message from the request
48+
user_message = request.json.get("message", "").strip()
49+
if not user_message:
50+
return jsonify({"error": "Message is required"}), 400
51+
52+
# Add the user's message to the chat history
53+
chat_history.append({"role": "user", "content": user_message})
54+
55+
# Prepare the prompts with the chat history
56+
prompts = [{"role": "system", "content": "You are a helpful assistant."}] + chat_history
57+
58+
# Get response from chatbot
59+
response = client.chat.completions.create(
60+
model=os.getenv("GPT_MODEL"), messages=prompts
61+
)
62+
bot_reply = response.choices[0].message.content.strip()
63+
64+
# Add the bot's response to the chat history
65+
chat_history.append({"role": "assistant", "content": bot_reply})
66+
67+
# Example of a formatted response
68+
formatted_reply = f"""
69+
<p><b>Response:</b></p>
70+
<ul>
71+
<li><i>{bot_reply}</i></li>
72+
</ul>
73+
"""
74+
75+
return jsonify({"response": formatted_reply, "history": chat_history})
76+
except Exception as e:
77+
print(f"Error in /chat endpoint: {e}")
78+
return jsonify({"error": "An error occurred"}), 500
79+
80+
if __name__ == "__main__":
81+
app.run(debug=True)

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytest
2+
pytest-flask

static/domain_model.png

43.1 KB
Loading

static/script.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
document.addEventListener("DOMContentLoaded", () => {
2+
const userNameInput = document.getElementById("userName");
3+
4+
// Load saved name
5+
const savedName = sessionStorage.getItem("userData");
6+
if (savedName) {
7+
userNameInput.value = savedName;
8+
}
9+
10+
// Save username on blur
11+
userNameInput.addEventListener("blur", () => {
12+
sessionStorage.setItem("userData", userNameInput.value);
13+
});
14+
});
15+
16+
17+
document.addEventListener("DOMContentLoaded", () => {
18+
const userInput = document.getElementById("userInput");
19+
const sendButton = document.getElementById("sendButton");
20+
const chatBox = document.getElementById("chatBox");
21+
const loadingIndicator = document.getElementById("loadingIndicator");
22+
const actionButtons = document.getElementById("actionButtons");
23+
let isFirstResponse = true; // Track if it's the first response
24+
25+
// Function to render the chat history
26+
function renderChatHistory(history) {
27+
chatBox.innerHTML = ""; // Clear the chatbox
28+
history.forEach((message) => {
29+
const messageDiv = document.createElement("div");
30+
messageDiv.classList.add("chat-message");
31+
32+
if (message.role === "user") {
33+
messageDiv.classList.add("user-message");
34+
messageDiv.textContent = message.content;
35+
} else if (message.role === "assistant") {
36+
messageDiv.classList.add("bot-message");
37+
messageDiv.innerHTML = message.content; // Render HTML for bot messages
38+
}
39+
40+
chatBox.appendChild(messageDiv);
41+
});
42+
chatBox.scrollTop = chatBox.scrollHeight; // Auto-scroll to the latest message
43+
}
44+
45+
// Function to send a message
46+
function sendMessage() {
47+
const message = userInput.value.trim();
48+
if (message === "") return;
49+
50+
// Disable input and button while processing
51+
userInput.disabled = true;
52+
sendButton.disabled = true;
53+
54+
// Show the loading indicator
55+
loadingIndicator.classList.remove("d-none");
56+
57+
// Send the message to the server
58+
fetch("/chat", {
59+
method: "POST",
60+
headers: { "Content-Type": "application/json" },
61+
body: JSON.stringify({ message: message }),
62+
})
63+
.then((response) => response.json())
64+
.then((data) => {
65+
if (data.error) {
66+
alert(data.error);
67+
} else {
68+
renderChatHistory(data.history); // Render the updated chat history
69+
70+
// Show action buttons after the first response
71+
if (isFirstResponse) {
72+
actionButtons.style.visibility = "visible"; // Make visible
73+
actionButtons.classList.add("show"); // Add animation class
74+
isFirstResponse = false;
75+
}
76+
}
77+
})
78+
.catch((err) => {
79+
console.error("Error:", err);
80+
alert("An error occurred while processing your request.");
81+
})
82+
.finally(() => {
83+
// Hide the loading indicator
84+
loadingIndicator.classList.add("d-none");
85+
86+
// Re-enable input and button
87+
userInput.disabled = false;
88+
sendButton.disabled = false;
89+
userInput.value = ""; // Clear the input field
90+
userInput.focus(); // Focus back on the input field
91+
});
92+
}
93+
94+
// Add event listeners for action buttons
95+
actionButtons.addEventListener("click", (event) => {
96+
if (event.target.tagName === "BUTTON") {
97+
const action = event.target.textContent.trim();
98+
alert(`Action triggered: ${action}`);
99+
}
100+
});
101+
102+
// Event listeners for sending a message
103+
sendButton.addEventListener("click", sendMessage);
104+
userInput.addEventListener("keypress", (event) => {
105+
if (event.key === "Enter") {
106+
event.preventDefault();
107+
sendMessage();
108+
}
109+
});
110+
});

0 commit comments

Comments
 (0)