Lab: More Project Setup
Due: Wednesday, November 4 at 11:59pm (one week after it is assigned) · Worth: 8 points
Repeatable tests let us check whether a chatbot still works after we change its code. You’ll test a conversation without calling the API, then package your program so someone can install it and run chat from the terminal.
Start
Continue in the docchat repository from the reading. Download the packaging files and copy the files you need into that repository. Merge .gitignore entries into your existing file. If you already have pyproject.toml, compare it with the template instead of replacing it.
With the repository’s Python environment active, run:
python -m pip install -r requirements.txt
python -m pip install pytest coverage build setuptoolsRecord dependencies in requirements.txt. For offline tests that construct the client, put a nonempty placeholder such as GROQ_API_KEY=offline-test in your ignored .env.
Test and package chat
In the docstrings in chat.py, test a two-turn conversation and both ways of exiting the input loop. Once those tests pass, check their coverage and try installing the command.
To check that the tests catch lost history, temporarily insert these lines at the start of send_message, after its docstring:
self.messages[:] = [message for message in self.messages
if message['role'] != 'assistant']Run the unchanged tests, confirm that at least one fails, then remove the inserted lines and confirm that they pass again.
Test the conversation
Use the reading’s scripted input and API patch. Test two replies, retained history on the second request, and the two exit conditions separately. Capture request messages when each call happens, before later turns change the list.
-
Put the input loop in
repl(temperature=0.8)and call it from the main guard. Include a doctest in its docstring that callsrepl(temperature=0). -
Send each input to the model and print the returned reply. Pass the chosen temperature to the request.
-
Keep earlier user messages and assistant replies in the conversation.
-
End the loop cleanly on both
EOFErrorandKeyboardInterrupt. -
Write passing doctests with scripted input and API replies. Patch
client.chat.completions.createso the realllm,send_message, andreplcode runs without a live provider. Use the synchronous Groq/OpenAI client. -
Cover more than 90% of
chat.pywith your doctests. -
Make the unchanged doctests fail when earlier assistant messages are deliberately removed, then pass after restoring the code.
Package the command
The download contains an incomplete metadata file. Fill in the command entry point and other TODOs using the reading’s installation example. Keep chat.py in your existing repository.
-
Complete
pyproject.tomlwith a package name, version, README, dependencies, setuptools build backend, thechatmodule, and achatconsole entry point connected torepl. -
Build and install the package so installation creates the
chatcommand. -
Make the installed command handle inputs, replies, retained history, and clean exits.
-
Keep secrets, virtual environments, caches, coverage output, and build artifacts out of the repository. Record dependencies and include a short README.
Check and submit
Run from the repository root:
python -m pytest chat.py --doctest-modules
python -m coverage run --include="./chat.py" -m pytest chat.py --doctest-modules
python -m coverage report -m
python -m build
python -m pip install .
chatAim for at least 91% line coverage. The report lists the lines your tests missed. Add a test that exercises the missing behavior and checks its result. See the coverage example.
Check the installed command with two messages and both exit conditions. Ctrl+C interrupts; end-of-input is Ctrl+D in Bash/macOS/Linux or Ctrl+Z then Enter in PowerShell. Live replies require a real API key, but the submitted doctests must work offline. Restore the correct implementation before submitting.
Commit and sync chat.py, pyproject.toml, README.md, dependencies, and .gitignore. On Gradescope, choose GitHub and submit docchat and the branch containing your work. The submission guide explains resubmission.