Skip to content

Commit e19174d

Browse files
committed
fix: inference-mock code review changes
Signed-off-by: Emilio Garcia <i.am.emilio@gmail.com>
1 parent 42b46f5 commit e19174d

File tree

8 files changed

+26
-20
lines changed

8 files changed

+26
-20
lines changed

.github/workflows/inference-mock.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,25 @@ on:
1111

1212
jobs:
1313
inference-mock-unit-tests:
14-
runs-on: ubuntu:latest
14+
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
1717
python-version: ["3.11"]
1818
steps:
19+
- uses: actions/checkout@v4
20+
with:
21+
sparse-checkout: |
22+
actions/inference-mock
1923
- name: Set up Python
2024
uses: actions/setup-python@v5
2125
with:
2226
python-version: ${{ matrix.python-version }}
2327
- name: Install dependencies
28+
working-directory: actions/inference-mock
2429
run: |
2530
python -m pip install --upgrade pip
2631
pip install -r requirements.txt
2732
- name: Run Unit Tests
33+
working-directory: actions/inference-mock/test
2834
run: |
29-
python -m unittest test/test.py
35+
python -m unittest test.py

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ missing-member-max-choices=1
627627
mixin-class-rgx=.*[Mm]ixin
628628

629629
# List of decorators that change the signature of a decorated function.
630-
signature-mutators=unittest.mock.patch,unittest.mock.patch.object
630+
signature-mutators=unittest.mock.patch,unittest.mock.patch.object,click.decorators.option
631631

632632

633633
[VARIABLES]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Below is a list of the in-house GitHub actions stored in this repository:
2121
| [launch-ec2-runner-with-fallback](./actions/launch-ec2-runner-with-fallback/launch-ec2-runner-with-fallback.md) | Used launch an EC2 instance in AWS, either as a spot instance or a dedicated instance. If your preferred availability zone lacks availability for your instance type, "backup" availability zones will be tried. | <ul><li>Insufficient capacity in AWS (i.e., AWS lacks availablility for your desired EC2 instance type in your preferred availability zone)</li><li>Cost savings (i.e., You want to try launching your EC2 runner as a spot instance first)</li></ul> |
2222
| [validate-notebooks](./actions/launch-ec2-runner-with-fallback/launch-ec2-runner-with-fallback.md) | Used to validate `.ipynb` files | <ul><li>I maintain a collection of `.ipynb` files and run ci jobs to test them. I would like to quickly verify that the files are formatted correctly before spinning up more complex or expensive CI jobs to test those notebooks.</li></ul>
2323
| [update-constraints](./actions/update-constraints/update-constraints.md) | Used to update `constraints-dev.txt` file | <ul><li>I pin all project dependencies in CI using `constraints-dev.txt` file. I would like to monitor new dependency releases and periodically post PRs to move pins forward.</li></ul>
24-
| [inference-mock](./actions/inference-mock/README.md) | Used to mock LLM calls | <ul><li>I have a notebook that I want to test that makes an LLM call, but I dont need to rely heavily on an LLM and don't need to run a real inference server for this test.</li></ul> |
24+
| [inference-mock](./actions/inference-mock/README.md) | Used to mock LLM calls | <ul><li>I have a notebook that I want to test that makes an LLM call, but I don't need to rely heavily on an LLM and don't need to run a real inference server for this test.</li></ul> |
2525

2626
## ❓ How to Use One or More In-House GitHub Actions
2727

actions/inference-mock/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Overview
44

55
Inference Mock is a tool that creates a flask server that runs as a background process. OpenAI comptabile calls can be made to its completions API.
6-
Based on how the server is configured, it will send a set of programmed resposnes back.
6+
Based on how the server is configured, it will send a set of programmed responses back.
77

88
## When to Use it?
99

@@ -29,7 +29,7 @@ port: 11434
2929
# - response: passing only a response is an `Always` match strategy. If no other strategy has matched yet, this will always be a positive match.
3030
#
3131
# note: the strategies are executed in the order listed, and the first succesful match is accepted. If you start with an `Always` strategy, its
32-
# response will be the only resposne returned.
32+
# response will be the only response returned.
3333
matches:
3434

3535
# this is an example of a `contains` strategy. If the prompt contains the substrings, it returns the response.

actions/inference-mock/app.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ def completions():
2323
if not data or "prompt" not in data:
2424
raise exceptions.BadRequest("prompt is empty or None")
2525

26-
prompt_debug_str = data["prompt"][:90] + "..."
26+
prompt = data.get('prompt')
27+
prompt_debug_str = prompt
28+
if len(prompt) > 90:
29+
prompt_debug_str = data["prompt"][:90] + "..."
30+
2731
app.logger.debug(
2832
f"{request.method} {request.url} {data['model']} {prompt_debug_str}"
2933
)
3034

31-
prompt = data.get("prompt")
3235
chat_response = strategies.find_match(
3336
prompt
3437
) # handle prompt and generate correct response
@@ -52,7 +55,7 @@ def start_server(config):
5255
# get config
5356
yaml_data = yaml.safe_load(config)
5457
if not isinstance(yaml_data, dict):
55-
raise ValueError("config file format is invalid")
58+
raise ValueError(f"config file {config} must be a set of key-value pairs")
5659

5760
conf = Config(**yaml_data)
5861

@@ -72,4 +75,4 @@ def start_server(config):
7275

7376

7477
if __name__ == "__main__":
75-
start_server() # pylint: disable=no-value-for-parameter
78+
start_server() # pylint: disable=no-value-for-parameter

actions/inference-mock/matching/matching.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Match(Protocol):
88
"""
9-
Match represnts a single prompt matching
9+
Match represents a single prompt matching
1010
strategy. When a match is successful,
1111
the response is what should be returned.
1212
"""
@@ -69,8 +69,8 @@ def to_match(pattern: dict) -> Match:
6969
)
7070
if "contains" in pattern:
7171
return Contains(**pattern)
72-
else:
73-
return Always(**pattern)
72+
73+
return Always(**pattern)
7474

7575

7676
class Matcher:

actions/inference-mock/test/__init__.py

Whitespace-only changes.

actions/inference-mock/test/test.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55

66
class TestAlways(unittest.TestCase):
7-
87
# match on any prompt
98
def test_always(self):
109
expect_response = "expected response"
@@ -23,23 +22,22 @@ def test_always_empty_prompt(self):
2322

2423

2524
class TestContains(unittest.TestCase):
26-
2725
def test_contains(self):
2826
expect_response = "expected response"
2927
prompt = "example prompt"
3028
match_on = ["example"]
3129
contains = Contains(match_on, expect_response)
3230
actual_response = contains.match(prompt)
3331
self.assertEqual(actual_response, expect_response)
34-
32+
3533
def test_contains_many(self):
3634
expect_response = "expected response"
3735
prompt = "a much longer example prompt so we can match on many substring elements of this string"
3836
match_on = ["example", "many substring elements", "match on"]
3937
contains = Contains(match_on, expect_response)
4038
actual_response = contains.match(prompt)
4139
self.assertEqual(actual_response, expect_response)
42-
40+
4341
# if any substrings don't match, return None
4442
def test_contains_mismatch(self):
4543
response = "expected response"
@@ -48,7 +46,7 @@ def test_contains_mismatch(self):
4846
contains = Contains(match_on, response)
4947
actual_response = contains.match(prompt)
5048
self.assertIsNone(actual_response)
51-
49+
5250
# reject empty prompts
5351
def test_contains_empty(self):
5452
response = "expected response"
@@ -60,7 +58,6 @@ def test_contains_empty(self):
6058

6159

6260
class TestMatcher(unittest.TestCase):
63-
6461
def test_to_contains(self):
6562
response = 'I am a response'
6663
substr = ["a", "b", "c"]
@@ -75,7 +72,7 @@ def test_to_always(self):
7572
always = to_match(always_pattern)
7673
self.assertIsInstance(always, Always)
7774
self.assertEqual(always.response, response)
78-
75+
7976
def test_to_invalid(self):
8077
response = 'I am a response'
8178
invalid_pattern = {'banana':'foo', 'response': response}

0 commit comments

Comments
 (0)