Skip to content

Commit 89377cd

Browse files
authored
Merge pull request #48 from A-Baji/dev
3.0.2 add unit tests
2 parents 94a23eb + 983c710 commit 89377cd

19 files changed

+2666
-52
lines changed

.github/workflows/package.yml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,21 @@ jobs:
2121
- uses: actions/checkout@v3
2222
- uses: actions/setup-python@v4
2323
with:
24-
python-version: ${{matrix.py_ver}}
24+
python-version: ${{ matrix.py_ver }}
2525
- name: Install Package
2626
run: pip install .
2727
- name: Test Package
28-
run: discordai_modelizer -V
28+
env:
29+
PY_VER: ${{ matrix.py_ver }}
30+
DISCORD_BOT_TOKEN: ${{ secrets.DISCORD_BOT_TOKEN }}
31+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
32+
CHANNEL_ID: ${{ secrets.CHANNEL_ID }}
33+
USERNAME: ${{ secrets.USERNAME }}
34+
PYTHONUNBUFFERED: 1
35+
run: |
36+
touch .env.test
37+
export VERSION=$(cat discordai_modelizer/version.py | grep -oP '\d+\.\d+\.\d+')
38+
docker compose -f docker-compose-test.yaml up --build --exit-code-from tests
2939
release:
3040
if: |
3141
github.event_name == 'push' &&
@@ -41,10 +51,10 @@ jobs:
4151
- name: Create release
4252
uses: actions/create-release@v1
4353
env:
44-
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
54+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4555
with:
46-
tag_name: ${{steps.version.outputs.discordai_modelizer_version}}
47-
release_name: Release ${{steps.version.outputs.discordai_modelizer_version}}
48-
body: Release ${{steps.version.outputs.discordai_modelizer_version}}
56+
tag_name: ${{ steps.version.outputs.discordai_modelizer_version }}
57+
release_name: Release ${{ steps.version.outputs.discordai_modelizer_version }}
58+
body: Release ${{ steps.version.outputs.discordai_modelizer_version }}
4959
draft: false
5060
prerelease: false

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/.env
1+
/*.env*
22
/files/
33

44
config.json

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) convention.
44

5+
## [3.0.2] - 06-29-2024
6+
7+
### Added
8+
9+
- unit tests
10+
11+
### Changed
12+
13+
- model deletion returns valid JSON on permission error
14+
15+
### Fixed
16+
17+
- multiple bugs for dataset generation, model creation, and cli
18+
519
## [3.0.1] - 06-27-2024
620

721
### Changed
@@ -81,6 +95,7 @@ Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and
8195

8296
- switched to `pathlib` for file path parsing
8397

98+
[3.0.2]: https://github.com/A-Baji/discordAI-modelizer/compare/3.0.1...3.0.2
8499
[3.0.1]: https://github.com/A-Baji/discordAI-modelizer/compare/3.0.0...3.0.1
85100
[3.0.0]: https://github.com/A-Baji/discordAI-modelizer/compare/2.0.1...3.0.0
86101
[2.0.1]: https://github.com/A-Baji/discordAI-modelizer/compare/1.2.2...2.0.1

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
FROM python:3.12
1+
ARG PY_VER
2+
FROM python:${PY_VER}
23
WORKDIR /main
34
RUN apt-get install git -y
45
COPY ./requirements.txt ./setup.py ./README.md /main/

discordai_modelizer/command_line/command_line.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,18 @@ def discordai_modelizer():
3434
subparsers.setup_job_cancel(job_subcommand)
3535

3636
args = parser.parse_args()
37-
try:
38-
os.environ["OPENAI_API_KEY"] = args.openai_key or os.environ["OPENAI_API_KEY"]
39-
except KeyError:
40-
raise argparse.ArgumentError(
41-
None,
42-
"Your OpenaAI API key must either be passed in as an argument or set as an environment variable",
43-
)
37+
if hasattr(args, "openai_key"):
38+
openai_wrapper.set_openai_api_key(args.openai_key)
4439

4540
if args.command == "model":
4641
if args.subcommand == "list":
4742
display(openai_wrapper.list_models(os.environ["OPENAI_API_KEY"], args.full))
4843
elif args.subcommand == "create":
4944
customize.create_model(
50-
args.discord_token,
51-
os.environ["OPENAI_API_KEY"],
5245
args.channel,
5346
args.user,
47+
args.discord_token,
48+
os.environ["OPENAI_API_KEY"],
5449
thought_time=args.thought_time,
5550
thought_max=args.thought_max,
5651
thought_min=args.thought_min,
@@ -87,9 +82,6 @@ def discordai_modelizer():
8782
display(
8883
openai_wrapper.cancel_job(args.job_id, os.environ["OPENAI_API_KEY"])
8984
)
90-
display(
91-
openai_wrapper.delete_model(args.model_id, os.environ["OPENAI_API_KEY"])
92-
)
9385
else:
9486
raise argparse.ArgumentError(
9587
job_subcommand,

discordai_modelizer/customize.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,41 @@
77
from openai import OpenAI
88
from argparse import ArgumentError
99
from discordai_modelizer.gen_dataset import parse_logs, get_lines
10+
from discordai_modelizer.openai import set_openai_api_key
1011

1112
MODEL_MAP = {
1213
"davinci": "davinci-002",
1314
"babbage": "babbage-002",
1415
}
1516

1617

18+
def set_bot_token(token):
19+
if token:
20+
os.environ["DISCORD_BOT_TOKEN"] = token
21+
elif "DISCORD_BOT_TOKEN" not in os.environ:
22+
raise ValueError(
23+
"Your Discord bot token must either be passed in as an argument or set as an environment variable",
24+
)
25+
26+
1727
def create_model(
18-
bot_token: str,
19-
openai_key: str,
2028
channel_id: str,
2129
user_id: str,
30+
bot_token: str = None,
31+
openai_key: str = None,
2232
thought_time=10,
2333
thought_max: int = None,
2434
thought_min=4,
2535
max_entry_count=1000,
26-
offset="start",
36+
offset=0,
2737
select_mode="sequential",
2838
base_model="none",
2939
reverse=False,
3040
clean=False,
3141
redownload=False,
3242
use_existing=False,
3343
):
44+
set_openai_api_key(openai_key)
3445
client = OpenAI()
3546
channel_user = f"{channel_id[:4]}_{user_id}"
3647
files_path = pathlib.Path(appdirs.user_data_dir(appname="discordai"))
@@ -43,13 +54,7 @@ def create_model(
4354

4455
# Download logs
4556
if (not os.path.isfile(full_logs_path) or redownload) and not use_existing:
46-
try:
47-
bot_token = bot_token or os.environ["DISCORD_BOT_TOKEN"]
48-
except KeyError:
49-
raise ArgumentError(
50-
None,
51-
"Your Discord bot token must either be passed in as an argument or set as an environment variable",
52-
)
57+
set_bot_token(bot_token)
5358

5459
print("INFO: Exporting chat logs using DiscordChatExporter...")
5560
print(
@@ -71,7 +76,7 @@ def create_model(
7176
"-c",
7277
channel_id,
7378
"-t",
74-
bot_token,
79+
os.environ["DISCORD_BOT_TOKEN"],
7580
"-o",
7681
f"{channel_id}_logs.json",
7782
"-f",
@@ -140,9 +145,6 @@ def create_model(
140145

141146
# Clean up generated files
142147
if clean and not use_existing:
143-
try:
144-
os.remove(full_dataset_path)
145-
except FileNotFoundError:
146-
pass
148+
full_dataset_path.unlink()
147149

148150
client.close()

discordai_modelizer/gen_dataset.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def add_to_dataset(thought: str):
8181
thought = build_thought("", messages[0])
8282
for i, msg in enumerate(messages[1::]):
8383
if msg["content"]:
84-
prev_timestamp = parser.parse(messages[i - 1]["timestamp"])
84+
prev_timestamp = parser.parse(messages[i]["timestamp"])
8585
curr_timestamp = parser.parse(msg["timestamp"])
8686
differentiation = (curr_timestamp - prev_timestamp) / timedelta(
8787
milliseconds=1
@@ -107,8 +107,6 @@ def get_lines(
107107
f.close()
108108

109109
num_lines = len(lines)
110-
if N > num_lines:
111-
return
112110

113111
if select_mode == "sequential":
114112
step = 1
@@ -118,7 +116,7 @@ def get_lines(
118116
if reverse:
119117
lines = lines[::-1]
120118

121-
selected_lines = lines[offset:][::step][:N]
119+
selected_lines = lines[offset:][:: step or 1][:N]
122120

123121
with open(file_name, "w") as f:
124122
f.writelines(selected_lines)

discordai_modelizer/openai.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import json
21
import os
32

4-
from openai import OpenAI
3+
from openai import OpenAI, PermissionDeniedError
54
from datetime import datetime, timezone
65

76

@@ -17,11 +16,11 @@ def convert_in_place(obj, key: str):
1716

1817

1918
def set_openai_api_key(key: str):
20-
try:
21-
os.environ["OPENAI_API_KEY"] = key or os.environ["OPENAI_API_KEY"]
22-
except KeyError:
19+
if key:
20+
os.environ["OPENAI_API_KEY"] = key
21+
elif "OPENAI_API_KEY" not in os.environ:
2322
raise ValueError(
24-
"Your OpenaAI API key must either be passed in as an argument or set as an environment variable",
23+
"Your OpenAI API key must either be passed in as an argument or set as an environment variable",
2524
)
2625

2726

@@ -117,6 +116,11 @@ def delete_model(model_name: str, openai_key: str = None) -> dict:
117116
return
118117
set_openai_api_key(openai_key)
119118
client = OpenAI()
120-
deleted = client.models.delete(model_name).model_dump()
119+
try:
120+
deleted = client.models.delete(model_name).model_dump()
121+
except PermissionDeniedError:
122+
deleted = {
123+
"error": "You have insufficient permissions for this operation. Missing scopes: api.delete"
124+
}
121125
client.close()
122126
return deleted

discordai_modelizer/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.0.1"
1+
__version__ = "3.0.2"

docker-compose-test.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# PY_VER=3.12 VERSION=$(cat discordai_modelizer/version.py | grep -oP '\d+\.\d+\.\d+') docker compose -f docker-compose-test.yaml --env-file .env.test up --build
2+
# docker exec -it discordai-modelizer-tests-1 bash
3+
services:
4+
tests:
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
args:
9+
- PY_VER
10+
image: discord-ai/discordai_modelizer:py${PY_VER}-v${VERSION}
11+
privileged: true
12+
network_mode: host
13+
working_dir: /main
14+
environment:
15+
- DISCORD_BOT_TOKEN
16+
- OPENAI_API_KEY
17+
- CHANNEL_ID
18+
- USERNAME
19+
- PYTHONUNBUFFERED=1
20+
env_file: .env.test
21+
volumes:
22+
- ./discordai_modelizer:/usr/local/lib/python${PY_VER}/site-packages/discordai_modelizer
23+
- ./tests:/main/tests
24+
command:
25+
- sh
26+
- -c
27+
- |
28+
pip install pytest pytest-cov pytest-console-scripts --root-user-action ignore
29+
pytest --cov-report term-missing --cov=discordai_modelizer tests
30+
# tail -f /dev/null

0 commit comments

Comments
 (0)