Skip to content

Commit b411af8

Browse files
committed
fixed some comments
1 parent 977ee69 commit b411af8

File tree

6 files changed

+117
-142
lines changed

6 files changed

+117
-142
lines changed

src/monkey_patch/exception.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class MonkeyPatchException(Exception):
2+
pass

src/monkey_patch/function_modeler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def _load_function_config(self, func_hash, function_description):
153153
"""
154154

155155
config, default = self.data_worker._load_function_config(func_hash)
156-
if default and self.check_for_finetunes:
156+
if default and self.check_for_finetunes and default.get('finetune_support', True):
157157
finetuned, finetune_config = self._check_for_finetunes(function_description)
158158
if finetuned:
159159
config = finetune_config
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from monkey_patch.exception import MonkeyPatchException
2+
from monkey_patch.language_models.bedrock_api import Bedrock_API
3+
from monkey_patch.language_models.openai_api import Openai_API
4+
5+
6+
class ApiModelFactory:
7+
@classmethod
8+
def get_model(cls, api_model_name: str):
9+
if api_model_name == 'openai':
10+
return {"openai": Openai_API()}
11+
elif api_model_name == 'bedrock':
12+
return {"bedrock": Bedrock_API()}
13+
else:
14+
MonkeyPatchException(f"not support {api_model_name}")
15+
16+
@classmethod
17+
def get_all_model_info(cls, api_model_name: str, generation_length):
18+
if api_model_name == 'bedrock':
19+
return {
20+
"anthropic.claude-instant-v1": {
21+
"token_limit": 100000 - generation_length,
22+
"type": "bedrock"
23+
},
24+
"anthropic.claude-v2": {
25+
"token_limit": 100000 - generation_length,
26+
"type": "bedrock"
27+
}
28+
} # models and token counts
29+
elif api_model_name == 'openai':
30+
return {
31+
"gpt-4": {
32+
"token_limit": 8192 - generation_length,
33+
"type": "openai"
34+
},
35+
"gpt-4-32k": {
36+
"token_limit": 32768 - generation_length,
37+
"type": "openai"
38+
}
39+
} # models and token counts
40+
else:
41+
MonkeyPatchException(f"not support {api_model_name}")
42+
43+
@classmethod
44+
def get_teacher_model(cls, api_model_name: str):
45+
if api_model_name == 'bedrock':
46+
return [
47+
'anthropic.claude-v2'
48+
]
49+
elif api_model_name == 'openai':
50+
return [
51+
"gpt-4",
52+
"gpt-4-32k"
53+
]
54+
else:
55+
MonkeyPatchException(f"not support {api_model_name}")
56+
57+
@classmethod
58+
def is_finetune_support(cls, api_model_name: str):
59+
if api_model_name == 'bedrock':
60+
lo
61+
return False
62+
elif api_model_name == 'openai':
63+
return True
64+
else:
65+
MonkeyPatchException(f"not support {api_model_name}")
66+
67+
@classmethod
68+
def get_distilled_model(cls, api_model_name: str):
69+
if api_model_name == 'bedrock':
70+
return 'anthropic.claude-instant-v1'
71+
72+
elif api_model_name == 'openai':
73+
return ""
74+
else:
75+
MonkeyPatchException(f"not support {api_model_name}")

src/monkey_patch/language_models/bedrock_api.py

Lines changed: 20 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,15 @@
22
import json
33

44
import boto3 as boto3
5-
import openai
5+
import botocore
66
import time
7-
# import abstract base class
7+
8+
from monkey_patch.exception import MonkeyPatchException
89
from monkey_patch.language_models.llm_api_abc import LLM_Api
910
import os
1011

11-
OPENAI_URL = "https://api.openai.com/v1/chat/completions"
1212
import requests
1313

14-
AWS_REGION = os.environ.get("AWS_DEFAULT_REGION", "us-east-1")
15-
bedrock = boto3.client(
16-
service_name='bedrock',
17-
region_name=AWS_REGION,
18-
)
19-
bedrock_runtime = boto3.client(
20-
service_name='bedrock-runtime',
21-
region_name=AWS_REGION,
22-
23-
)
24-
2514

2615
class PromptTemplate:
2716

@@ -40,10 +29,15 @@ class Bedrock_API(LLM_Api):
4029
def __init__(self) -> None:
4130
# initialise the abstract base class
4231
super().__init__()
32+
self.bedrock_runtime = boto3.client(
33+
service_name='bedrock-runtime',
34+
region_name=os.environ.get("AWS_DEFAULT_REGION", "us-east-1")
35+
)
4336

4437
def generate(self, model, system_message, prompt, **kwargs):
4538
"""
46-
The main generation function, given the args, kwargs, function_modeler, function description and model type, generate a response and check if the datapoint can be saved to the finetune dataset
39+
The main generation function, given the args, kwargs, function_modeler, function description and model type,
40+
generate a response and check if the datapoint can be saved to the finetune dataset
4741
"""
4842

4943
# check if api key is not none
@@ -54,7 +48,8 @@ def generate(self, model, system_message, prompt, **kwargs):
5448
frequency_penalty = kwargs.get("frequency_penalty", 0)
5549
presence_penalty = kwargs.get("presence_penalty", 0)
5650
body = json.dumps({
57-
"prompt": AnthropicClaudePromptTemplate().prompt_gen(system_prompt=system_message, user_prompt=prompt),
51+
"prompt": AnthropicClaudePromptTemplate().prompt_gen(system_prompt=system_message,
52+
user_prompt=prompt),
5853
"max_tokens_to_sample": 4096,
5954
"temperature": temperature,
6055
"top_k": 250,
@@ -65,60 +60,20 @@ def generate(self, model, system_message, prompt, **kwargs):
6560
"anthropic_version": "bedrock-2023-05-31"
6661
})
6762

68-
response = bedrock_runtime.invoke_model(
69-
body=body,
70-
modelId=model,
71-
contentType="application/json",
72-
accept="application/json")
73-
return json.loads(response.get('body').read().decode())['completion']
74-
75-
params = {
76-
"model": model,
77-
"temperature": temperature,
78-
"max_tokens": 512,
79-
"top_p": top_p,
80-
"frequency_penalty": frequency_penalty,
81-
"presence_penalty": presence_penalty,
82-
}
83-
messages = [
84-
{
85-
"role": "system",
86-
"content": system_message
87-
},
88-
{
89-
"role": "user",
90-
"content": prompt
91-
}
92-
]
93-
params["messages"] = messages
94-
9563
counter = 0
96-
choice = None
97-
# initiate response so exception logic doesnt error out when checking for error in response
98-
response = {}
9964
while counter < 5:
10065
try:
101-
openai_headers = {
102-
"Authorization": f"Bearer {self.api_key}",
103-
"Content-Type": "application/json",
104-
}
105-
response = requests.post(
106-
OPENAI_URL, headers=openai_headers, json=params, timeout=50
107-
)
108-
response = response.json()
109-
choice = response["choices"][0]["message"]["content"].strip("'")
110-
break
66+
response = self.bedrock_runtime.invoke_model(body=body,
67+
modelId=model,
68+
contentType="application/json",
69+
accept="application/json")
70+
return json.loads(response.get('body').read().decode())['completion']
71+
72+
except botocore.exceptions.ClientError as error:
73+
raise MonkeyPatchException("boto3 ")
11174
except Exception:
112-
if ("error" in response and
113-
"code" in response["error"] and
114-
response["error"]["code"] == 'invalid_api_key'):
115-
raise Exception(f"The supplied OpenAI API key {self.api_key} is invalid")
116-
11775
time.sleep(1 + 3 * counter)
11876
counter += 1
11977
continue
12078

121-
if not choice:
122-
raise Exception("OpenAI API failed to generate a response")
123-
124-
return choice
79+
raise MonkeyPatchException(f"Bedrock: Model {model} API failed to generate a response")

src/monkey_patch/language_models/language_modeler.py

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,8 @@
1-
from monkey_patch.language_models.bedrock_api import Bedrock_API
2-
from monkey_patch.language_models.openai_api import Openai_API
1+
from monkey_patch.language_models.api_model_factory import ApiModelFactory
32
from monkey_patch.models.language_model_output import LanguageModelOutput
43
from monkey_patch.utils import approximate_token_count
54

65

7-
class ApiModelFactory:
8-
@classmethod
9-
def get_model(cls, api_model_name: str):
10-
if api_model_name == 'openai':
11-
return {"openai": Openai_API()}
12-
elif api_model_name == 'bedrock':
13-
return {"bedrock": Bedrock_API()}
14-
15-
@classmethod
16-
def get_all_model_info(cls, api_model_name: str, generation_length):
17-
if api_model_name == 'bedrock':
18-
return {
19-
"anthropic.claude-instant-v1": {
20-
"token_limit": 100000 - generation_length,
21-
"type": "bedrock"
22-
},
23-
"anthropic.claude-v2": {
24-
"token_limit": 100000 - generation_length,
25-
"type": "bedrock"
26-
}
27-
} # models and token counts
28-
if api_model_name == 'openai':
29-
return {
30-
"gpt-4": {
31-
"token_limit": 8192 - self.generation_length,
32-
"type": "openai"
33-
},
34-
"gpt-4-32k": {
35-
"token_limit": 32768 - self.generation_length,
36-
"type": "openai"
37-
}
38-
} # models and token counts
39-
40-
@classmethod
41-
def get_teacher_model(cls, api_model_name: str):
42-
if api_model_name == 'bedrock':
43-
return [
44-
'anthropic.claude-v2'
45-
]
46-
elif api_model_name == 'openai':
47-
return [
48-
"gpt-4",
49-
"gpt-4-32k"
50-
]
51-
else:
52-
return [
53-
"gpt-4",
54-
"gpt-4-32k"
55-
]
56-
57-
@classmethod
58-
def get_distilled_model(cls, api_model_name: str):
59-
if api_model_name == 'bedrock':
60-
return 'anthropic.claude-instant-v1'
61-
62-
elif api_model_name == 'openai':
63-
return ""
64-
else:
65-
return ""
66-
67-
686
class LanguageModel(object):
697
def __init__(self, generation_token_limit=512, api_model='openai') -> None:
708
self.instruction = "You are given below a function description and input data. The function description of what the function must carry out can be found in the Function section, with input and output type hints. The input data can be found in Input section. Using the function description, apply the function to the Input and return a valid output type, that is acceptable by the output_class_definition and output_class_hint. Return None if you can't apply the function to the input or if the output is optional and the correct output is None.\nINCREDIBLY IMPORTANT: Only output a JSON-compatible string in the correct response format."
@@ -74,9 +12,8 @@ def __init__(self, generation_token_limit=512, api_model='openai') -> None:
7412
self.api_models = ApiModelFactory.get_model(api_model)
7513
self.repair_instruction = "Below are an outputs of a function applied to inputs, which failed type validation. The input to the function is brought out in the INPUT section and function description is brought out in the FUNCTION DESCRIPTION section. Your task is to apply the function to the input and return a correct output in the right type. The FAILED EXAMPLES section will show previous outputs of this function applied to the data, which failed type validation and hence are wrong outputs. Using the input and function description output the accurate output following the output_class_definition and output_type_hint attributes of the function description, which define the output type. Make sure the output is an accurate function output and in the correct type. Return None if you can't apply the function to the input or if the output is optional and the correct output is None."
7614
self.generation_length = generation_token_limit
77-
self.models = ApiModelFactory.get_all_model_info(
78-
api_model,
79-
self.generation_length)
15+
self.models = ApiModelFactory.get_all_model_info(api_model,
16+
self.generation_length)
8017

8118
def generate(self, args, kwargs, function_modeler, function_description, llm_parameters={}):
8219
"""

src/monkey_patch/trackers/buffered_logger.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from appdirs import user_data_dir
55

66
from monkey_patch.bloom_filter import BloomFilter, optimal_bloom_filter_params
7-
from monkey_patch.language_models.language_modeler import ApiModelFactory
7+
from monkey_patch.language_models.api_model_factory import ApiModelFactory
88
from monkey_patch.trackers.dataset_worker import DatasetWorker
99
import json
1010

@@ -37,15 +37,21 @@ def __init__(self, name, level=15):
3737

3838
super().__init__(name, level)
3939

40-
self.default_function_config = {"distilled_model": ApiModelFactory.get_distilled_model(os.getenv('API_MODEL')),
41-
"current_model_stats": {
42-
"trained_on_datapoints": 0,
43-
"running_faults": []},
44-
"last_training_run": {"trained_on_datapoints": 0},
45-
"current_training_run": {},
46-
"teacher_models": ApiModelFactory.get_teacher_model(os.getenv('API_MODEL')),
47-
# currently supported teacher models
48-
"nr_of_training_runs": 0}
40+
self.default_function_config = {
41+
"distilled_model": ApiModelFactory.get_distilled_model(os.getenv('API_MODEL')),
42+
"current_model_stats": {
43+
"trained_on_datapoints": 0,
44+
"running_faults": []
45+
},
46+
"last_training_run": {
47+
"trained_on_datapoints": 0
48+
},
49+
"current_training_run": {},
50+
"teacher_models": ApiModelFactory.get_teacher_model(os.getenv('API_MODEL')),
51+
# currently supported teacher models
52+
"nr_of_training_runs": 0,
53+
"finetune_support": ApiModelFactory.is_finetune_support(os.getenv('API_MODEL')),
54+
}
4955

5056
def _get_log_directory(self):
5157

0 commit comments

Comments
 (0)