Python Web Applications with GenAI Workshop: ChatGPT Clone (SLIDES)
- Install Docker: https://docs.docker.com/get-docker/
- Open the application and follow necessary setup instructions.
- Check installation by running
docker --version
in your Terminal.
- Clone the repo or download the zip file.
- Create a
.env
at the project's root containing the following (replacesk-...
with your OpenAI API key):
DB_PATH=.volumes/db/db.local
OPENAI_API_KEY=sk-...
- Run
docker compose up
(from the project's root) to start the server. - When you something like
core-1 | Done in XXXms.
, open your browser at http://localhost:8000. - You can edit code and it will automatically rebuild the server. You will see the change when refreshing the page.
Checkout branch ex1
.
In this exercise, you will complete the implementation of the user login functionality in the provided code. The login process involves retrieving the user from the database, checking the password, setting a cookie, and redirecting the user to the home page.
Tasks:
-
In the
login
function ofauth_router.py
:- Retrieve the user from the database using the provided
app_service.login
method. - Set a cookie with the key "python-htmx-workshop" and the value of the user's ID.
- Set the "HX-Redirect" header to redirect the user to the home page ("/").
- Retrieve the user from the database using the provided
-
In the
login
method ofservice.py
:- Retrieve the user from the database using the provided username.
- If the user is not found, raise an
HTTPException
with a status code of 404 and the detail "User not found".
-
In
login.html
:- Add the appropriate HTMX attribute to the form to trigger the login request.
Note: The password check and other necessary code are already provided.
Good luck!
Checkout branch ex2
.
In this exercise, you will complete the implementation of the chat deletion functionality in the provided code. The deletion process involves using the application service to delete the chat and returning an appropriate response to HTMX.
Tasks:
-
In the
delete_chat
function ofchat_router.py
:- Use the
app_service.delete_chat
method to delete the chat, passing thechat_id
anduser
as arguments. - Return an appropriate response to HTMX to handle the deletion.
- Use the
-
In the
delete_chat
method ofservice.py
:- Retrieve the chat from the database using the provided
chat_id
anduser.id
. - Delete the chat using the session's
delete
method.
- Retrieve the chat from the database using the provided
-
In
chat-base.html
:- Add the appropriate HTMX attributes to the delete button to trigger the chat deletion request.
Note: The necessary error handling and other code are already provided.
Good luck!
Checkout branch ex3
.
In this exercise, you will complete the implementation of the chat generation functionality using Server-Sent Events (SSE) in the provided code. The generation process involves retrieving the chat, building the message list for AI completion, and sending the generated response back to the client using SSE.
Tasks:
-
In the
generate
function ofchat_router.py
:- Build the
EventSourceResponse
using theapp_service.generate
method, passing thechat_id
as an argument.
- Build the
-
In the
generate
method ofservice.py
:- Retrieve the chat from the database using the provided
chat_id
. - If the chat is not found, raise an
HTTPException
with a status code of 404 and the detail "Chat not found". - Build the message list for AI completion by iterating over the chat messages and appending them to the
messages
list. - Build the response that will be swapped in the template by HTMX. Use the
markdown
function to render the generated content and wrap it in appropriate HTML tags. - Yield the response as a dictionary with the keys "event", "id", and "data".
- After the generation is complete, send the final response that will be swapped in the template by HTMX. Combine both writing the final message and removing the SSE connection with an out-of-band swap.
- Retrieve the chat from the database using the provided
-
In
chat-id.html
:- Add the appropriate HTMX attributes to the
<div>
element to establish the SSE connection, specify the target for message swapping, and handle the swap behavior.
- Add the appropriate HTMX attributes to the
Note: The necessary AI completion code and other parts are already provided.
Good luck!