Skip to content

Commit f4dcd5e

Browse files
committed
fix: additional linting fixes for inference-mock
Signed-off-by: Emilio Garcia <[email protected]>
1 parent c9b5111 commit f4dcd5e

File tree

4 files changed

+77
-60
lines changed

4 files changed

+77
-60
lines changed

actions/inference-mock/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ debug: True
2222

2323
# port: the port the server will listen on
2424
# optional: this defaults to 11434
25-
port: 11343
25+
port: 11434
2626

2727
# matches: a list of matching strategies for expected sets of prompt response pairs. The following strategies are available:
2828
# - contains: accepts a list of substrings. All incoming prompts will need to contain all listed substrings for this match to be positive

actions/inference-mock/app.py

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
1-
from flask import Flask, request # type: ignore[import-not-found]
2-
from werkzeug import exceptions # type: ignore[import-not-found]
3-
4-
from completions.completion import create_chat_completion
1+
# Standard
52
from dataclasses import dataclass
6-
from matching.matching import Matcher
7-
8-
import click # type: ignore[import-not-found]
93
import logging
104
import pprint
11-
import yaml
125

6+
# Third Party
7+
from completions.completion import create_chat_completion
8+
from flask import Flask, request # type: ignore[import-not-found]
9+
from matching.matching import Matcher
10+
from werkzeug import exceptions # type: ignore[import-not-found]
11+
import click # type: ignore[import-not-found]
12+
import yaml
1313

1414
# Globals
1515
app = Flask(__name__)
16+
strategies: Matcher # a read only list of matching strategies
1617

1718

1819
# Routes
19-
@app.route('/v1/completions', methods=['POST'])
20+
@app.route("/v1/completions", methods=["POST"])
2021
def completions():
2122
data = request.get_json()
22-
if not data or 'prompt' not in data:
23+
if not data or "prompt" not in data:
2324
raise exceptions.BadRequest("prompt is empty or None")
2425

25-
prompt_debug_str = data['prompt'][:90] + "..."
26-
logger.debug(f"{request.method} {request.url} {data['model']} {prompt_debug_str}")
26+
prompt_debug_str = data["prompt"][:90] + "..."
27+
app.logger.debug(
28+
f"{request.method} {request.url} {data['model']} {prompt_debug_str}"
29+
)
2730

28-
prompt = data.get('prompt')
29-
chat_response = strategies.find_match(prompt) # handle prompt and generate correct response
31+
prompt = data.get("prompt")
32+
chat_response = strategies.find_match(
33+
prompt
34+
) # handle prompt and generate correct response
3035

31-
response = create_chat_completion(chat_response, model=data.get('model'))
32-
logger.debug(f"response: {pprint.pformat(response, compact=True)}")
36+
response = create_chat_completion(chat_response, model=data.get("model"))
37+
app.logger.debug(f"response: {pprint.pformat(response, compact=True)}")
3338
return response
3439

3540

@@ -42,31 +47,35 @@ class Config:
4247

4348

4449
@click.command()
45-
@click.option("--config", type=click.Path(), required=True, help='path to a YAML config file containing detailed configuration and model response options')
50+
@click.option(
51+
"--config",
52+
type=click.Path(),
53+
required=True,
54+
help="path to a YAML config file containing detailed configuration and model response options",
55+
)
4656
def start_server(config):
47-
# globals
48-
global logger
49-
global strategies
50-
5157
# get config
52-
with open(config, 'r', encoding="utf-8") as file:
58+
with open(config, "r", encoding="utf-8") as file:
5359
yaml_data = yaml.safe_load(file)
5460
if not isinstance(yaml_data, dict):
5561
raise ValueError("config file format is invalid")
5662

5763
conf = Config(**yaml_data)
5864

59-
# set globals
60-
strategies = Matcher(conf.matches)
61-
logger = logging.getLogger(__name__)
65+
# configure logger
6266
if conf.debug:
63-
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
67+
app.logger.setLevel(logging.DEBUG)
68+
app.logger.debug("debug mode enabled")
6469
else:
65-
logging.basicConfig(level=logging.INFO, format='%(message)s')
70+
app.logger.setLevel(logging.INFO)
71+
72+
# create match strategy object
73+
global strategies # pylint: disable=global-statement
74+
strategies = Matcher(conf.matches)
6675

6776
# init server
6877
app.run(debug=conf.debug, port=conf.port)
6978

7079

71-
if __name__ == '__main__':
72-
start_server()
80+
if __name__ == "__main__":
81+
start_server() # pylint: disable=no-value-for-parameter
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# mock openAI completion responses
22
# credit: https://github.com/openai/openai-python/issues/715#issuecomment-1809203346
33
# License: MIT
4+
5+
# Standard
46
import random
57

68

7-
def create_chat_completion(content: str, model: str="gpt-3.5") -> dict:
9+
def create_chat_completion(content: str, model: str = "gpt-3.5") -> dict:
810
response = {
9-
"id": f"chatcmpl-2nYZXNHxx1PeK1u8xXcE1Fqr1U6Ve",
11+
"id": "chatcmpl-2nYZXNHxx1PeK1u8xXcE1Fqr1U6Ve",
1012
"object": "chat.completion",
1113
"created": "12345678",
1214
"model": model,
@@ -17,14 +19,14 @@ def create_chat_completion(content: str, model: str="gpt-3.5") -> dict:
1719
"content": content,
1820
"index": 0,
1921
"logprobs": None,
20-
"finish_reason": "length"
22+
"finish_reason": "length",
2123
},
2224
],
2325
"usage": {
2426
"prompt_tokens": random.randint(10, 500),
2527
"completion_tokens": random.randint(10, 500),
26-
"total_tokens":random.randint(10, 500),
27-
}
28+
"total_tokens": random.randint(10, 500),
29+
},
2830
}
2931

3032
return response

actions/inference-mock/matching/matching.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
1-
from typing import Protocol
1+
# Standard
22
from abc import abstractmethod
3-
3+
from typing import Protocol
44
import pprint
55

66

77
class Match(Protocol):
8-
'''
8+
"""
99
Match represnts a single prompt matching
1010
strategy. When a match is successful,
1111
the response is what should be returned.
12-
'''
12+
"""
1313

1414
response: str
1515

1616
@abstractmethod
17-
def match(self, prompt:str) -> str|None:
17+
def match(self, prompt: str) -> str | None:
1818
raise NotImplementedError
1919

2020

2121
class Always:
22-
'''
22+
"""
2323
Always is a matching strategy that always
2424
is a positive match on a given prompt.
2525
2626
This is best used when only one prompt response
2727
is expected.
28-
'''
28+
"""
2929

3030
def __init__(self, response: str):
3131
self.response = response
3232

33-
def match(self, prompt:str) -> str|None:
34-
return self.response
33+
def match(self, prompt: str) -> str | None:
34+
if prompt:
35+
return self.response
36+
return None
3537

3638

3739
class Contains:
38-
'''
40+
"""
3941
Contains is a matching strategy that checks
4042
if the prompt string contains all of
4143
the substrings in the `contains` attribute.
42-
'''
44+
"""
4345

4446
contains: list[str]
4547

@@ -49,7 +51,9 @@ def __init__(self, contains: list[str], response: str):
4951
self.response = response
5052
self.contains = contains
5153

52-
def match(self, prompt:str) -> str|None:
54+
def match(self, prompt: str) -> str | None:
55+
if not prompt:
56+
return None
5357
for context in self.contains:
5458
if context not in prompt:
5559
return None
@@ -58,30 +62,32 @@ def match(self, prompt:str) -> str|None:
5862

5963

6064
class Matcher:
61-
'''
62-
Matcher matches prompt context and then
65+
"""
66+
Matcher matches prompt context and then
6367
selects a user provided reply.
64-
'''
68+
"""
6569

6670
strategies: list[Match]
6771

68-
def __init__(self, matching_patterns:list[dict]):
72+
def __init__(self, matching_patterns: list[dict]):
6973
if not matching_patterns:
70-
raise ValueError("matching strategies must contain at least one Match strategy")
74+
raise ValueError(
75+
"matching strategies must contain at least one Match strategy"
76+
)
7177

7278
self.strategies: list[Match] = []
7379
for matching_pattern in matching_patterns:
74-
response = matching_pattern.get('response')
80+
response = matching_pattern.get("response")
7581
if not response:
76-
raise ValueError(f"matching strategy must have a response: {pprint.pformat(matching_pattern, compact=True)}")
77-
if 'contains' in matching_pattern:
78-
pattern = Contains(**matching_pattern)
82+
raise ValueError(
83+
f"matching strategy must have a response: {pprint.pformat(matching_pattern, compact=True)}"
84+
)
85+
if "contains" in matching_pattern:
86+
self.strategies.append(Contains(**matching_pattern))
7987
else:
80-
pattern = Always(response)
81-
self.strategies.append(pattern)
88+
self.strategies.append(Always(response))
8289

83-
84-
def find_match(self, prompt:str) -> str:
90+
def find_match(self, prompt: str) -> str:
8591
for strategy in self.strategies:
8692
response = strategy.match(prompt)
8793
if response:

0 commit comments

Comments
 (0)