Skip to content

Commit 33a05e4

Browse files
Add basic test of Chatbot
1 parent 85b3cf6 commit 33a05e4

File tree

6 files changed

+80
-4
lines changed

6 files changed

+80
-4
lines changed

.github/workflows/python-app.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This workflow will install Python dependencies, run tests and lint with a single version of Python
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
3+
4+
name: Python application
5+
6+
on:
7+
push:
8+
branches: [ "main" ]
9+
pull_request:
10+
branches: [ "main" ]
11+
12+
permissions:
13+
contents: read
14+
15+
jobs:
16+
build:
17+
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- uses: actions/checkout@v3
22+
- name: Set up Python 3.10
23+
uses: actions/setup-python@v3
24+
with:
25+
python-version: "3.10"
26+
- name: Install dependencies
27+
run: |
28+
python -m pip install --upgrade pip
29+
pip install flake8 pytest
30+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
31+
- name: Lint with flake8
32+
run: |
33+
# stop the build if there are Python syntax errors or undefined names
34+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
35+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
36+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
37+
- name: Test with pytest
38+
run: |
39+
pytest

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.venv
2-
secrets.toml
32
__pycache__/
3+
.DS_Store
4+
secrets.toml

Chatbot.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
for msg in st.session_state.messages:
1616
st.chat_message(msg["role"]).write(msg["content"])
1717

18-
if prompt := st.chat_input():
18+
if prompt := st.chat_input(key="chat"):
1919
if not openai_api_key:
2020
st.info("Please add your OpenAI API key to continue.")
2121
st.stop()
2222

2323
openai.api_key = openai_api_key
2424
st.session_state.messages.append({"role": "user", "content": prompt})
2525
st.chat_message("user").write(prompt)
26-
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=st.session_state.messages)
26+
response = openai.ChatCompletion.create(
27+
model="gpt-3.5-turbo", messages=st.session_state.messages
28+
)
2729
msg = response.choices[0].message
2830
st.session_state.messages.append(msg)
2931
st.chat_message("assistant").write(msg.content)

app_test.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from unittest.mock import patch
2+
from streamlit.testing.v1 import AppTest
3+
from openai.openai_object import OpenAIObject
4+
5+
6+
# See https://github.com/openai/openai-python/issues/398
7+
def create_openai_object_sync(response: str, role: str = "assistant") -> OpenAIObject:
8+
obj = OpenAIObject()
9+
message = OpenAIObject()
10+
content = OpenAIObject()
11+
content.content = response
12+
content.role = role
13+
message.message = content
14+
obj.choices = [message]
15+
return obj
16+
17+
18+
@patch("openai.ChatCompletion.create")
19+
def test_basic_chat(openai_create):
20+
at = AppTest.from_file("Chatbot.py")
21+
# Todo: Remove the key in app and fix this once we have chat_input support
22+
at.session_state["chat"] = "Do you know any jokes?"
23+
at.run()
24+
assert at.get("alert")[0].proto.body == "Please add your OpenAI API key to continue."
25+
assert not at.exception
26+
27+
JOKE = "Why did the chicken cross the road? To get to the other side."
28+
openai_create.return_value = create_openai_object_sync(JOKE)
29+
at.text_input(key="chatbot_api_key").set_value("sk-...").run()
30+
print(at)
31+
assert at.get("chat_message")[1].markdown[0].value == "Do you know any jokes?"
32+
assert at.get("chat_message")[2].markdown[0].value == JOKE
33+
assert not at.exception

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ black==23.3.0
22
mypy==1.4.1
33
pre-commit==3.3.3
44
watchdog
5+
pytest

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
streamlit>=1.26.0
1+
streamlit-nightly >= 1.26.1.dev20231006
22
langchain>=0.0.217
33
openai
44
duckduckgo-search

0 commit comments

Comments
 (0)