Announcements
None today. Hurray!
You can use this Codespace template to quickly set up your development environment.
canvas-chatbot/
├── .env
├── app.py
├── chatbot.py
├── requirements.txt
├── templates/
│ └── index.html
└── .devcontainer/
└── devcontainer.json
chatbot.py
:
import openai, os
system_prompt = "You are a helpful teaching assistant for an Intro to Python course..."
openai.api_key = os.getenv("OPENAI_API_KEY")
def ask_chatbot(prompt, system_role=system_prompt):
response = openai.ChatCompletion.create(
model="gpt-4o",
messages=[
{"role": "system", "content": system_role},
{"role": "user", "content": prompt}
]
)
return response['choices'][0]['message']['content']
app.py
:
templates/index.html
:
<textarea id="question"></textarea>
<button onclick="askBot()">Ask</button>
<div id="response"></div>
<script>
async function askBot() {
const res = await fetch("/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: document.getElementById("question").value })
});
const data = await res.json();
document.getElementById("response").innerText = data.response;
}
</script>
Open in browser:
Ask questions like: - “How do I print Hello World 100 times?”
You can embed this tool in a Canvas Page using an <iframe>
if hosted publicly. We will need to investigate the best method to do this or look at Canvas LTI integrations.
You created a Codespaces-ready project for deployment or Canvas use.