Skip to content

Commit

Permalink
Add basic test of Chatbot
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcarroll committed Oct 9, 2023
1 parent 85b3cf6 commit 33a05e4
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python application

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.venv
secrets.toml
__pycache__/
.DS_Store
secrets.toml
6 changes: 4 additions & 2 deletions Chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
for msg in st.session_state.messages:
st.chat_message(msg["role"]).write(msg["content"])

if prompt := st.chat_input():
if prompt := st.chat_input(key="chat"):
if not openai_api_key:
st.info("Please add your OpenAI API key to continue.")
st.stop()

openai.api_key = openai_api_key
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").write(prompt)
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=st.session_state.messages)
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=st.session_state.messages
)
msg = response.choices[0].message
st.session_state.messages.append(msg)
st.chat_message("assistant").write(msg.content)
33 changes: 33 additions & 0 deletions app_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from unittest.mock import patch
from streamlit.testing.v1 import AppTest
from openai.openai_object import OpenAIObject


# See https://github.com/openai/openai-python/issues/398
def create_openai_object_sync(response: str, role: str = "assistant") -> OpenAIObject:
obj = OpenAIObject()
message = OpenAIObject()
content = OpenAIObject()
content.content = response
content.role = role
message.message = content
obj.choices = [message]
return obj


@patch("openai.ChatCompletion.create")
def test_basic_chat(openai_create):
at = AppTest.from_file("Chatbot.py")
# Todo: Remove the key in app and fix this once we have chat_input support
at.session_state["chat"] = "Do you know any jokes?"
at.run()
assert at.get("alert")[0].proto.body == "Please add your OpenAI API key to continue."
assert not at.exception

JOKE = "Why did the chicken cross the road? To get to the other side."
openai_create.return_value = create_openai_object_sync(JOKE)
at.text_input(key="chatbot_api_key").set_value("sk-...").run()
print(at)
assert at.get("chat_message")[1].markdown[0].value == "Do you know any jokes?"
assert at.get("chat_message")[2].markdown[0].value == JOKE
assert not at.exception
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ black==23.3.0
mypy==1.4.1
pre-commit==3.3.3
watchdog
pytest
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
streamlit>=1.26.0
streamlit-nightly >= 1.26.1.dev20231006
langchain>=0.0.217
openai
duckduckgo-search
Expand Down

0 comments on commit 33a05e4

Please sign in to comment.