-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #674 from Bob17293729/zongbo-dev
code exection class and test cases
- Loading branch information
Showing
4 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import os | ||
import argparse | ||
from lmflow.args import InferencerArguments | ||
from lmflow.args import ModelArguments | ||
from lmflow.args import DatasetArguments | ||
from lmflow.models import hf_decoder_model | ||
from lmflow.pipeline.inferencer import ToolInferencer | ||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--gpu', type=str, default='0', | ||
help='gpu id, currently speculative inference only support single gpu') | ||
parser.add_argument('--model', type=str, default='codellama/CodeLlama-7b-instruct-hf', | ||
help='target code generation model name or path you \ | ||
currently only supports huggingface decoder only models') | ||
params = parser.parse_args() | ||
os.environ["CUDA_VISIBLE_DEVICES"] = params.gpu | ||
|
||
model_args = ModelArguments(model_name_or_path=params.model) | ||
model = hf_decoder_model.HFDecoderModel(model_args) | ||
inferencer_args = InferencerArguments() | ||
data_args = DatasetArguments() | ||
|
||
toolinf = ToolInferencer(model_args, data_args, inferencer_args) | ||
|
||
while True: | ||
try: | ||
text = input("Tool Inference: ") | ||
toolinf_res = toolinf.inference(model, text) | ||
toolinf_res = toolinf_res.replace("<s>","") | ||
toolinf_res = toolinf_res.replace("</s>","") | ||
print('\n\nResult:') | ||
print(toolinf_res) | ||
print('\n\n') | ||
run_code = input("Run code? (y/n): ") | ||
if run_code == 'y': | ||
toolinf.code_exec(toolinf_res) | ||
if run_code == 'n': | ||
continue | ||
|
||
|
||
except EOFError: | ||
break | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
model="gorilla-llm/gorilla-7b-hf-delta-v1" | ||
python examples/tool_inference.py \ | ||
--model ${model} \ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from lmflow.pipeline.inferencer import ToolInferencer | ||
import unittest | ||
from lmflow.args import InferencerArguments | ||
from lmflow.args import ModelArguments | ||
from lmflow.args import DatasetArguments | ||
from lmflow.models import hf_decoder_model | ||
|
||
CODE_1 = "print(\"hello world\")" | ||
RES_1 = "hello world\n" | ||
CODE_2 = "b=a+1\nprint(b)" | ||
RES_2 = """Traceback (most recent call last): | ||
File "<string>", line 1, in <module> | ||
NameError: name 'a' is not defined | ||
""" | ||
|
||
class ToolInferencerTest(unittest.TestCase): | ||
def set_up(self): | ||
model_args = ModelArguments(model_name_or_path="codellama/CodeLlama-7b-instruct-hf") | ||
model = hf_decoder_model.HFDecoderModel(model_args) | ||
inferencer_args = InferencerArguments() | ||
data_args = DatasetArguments() | ||
self.toolinf = ToolInferencer(model_args, data_args, inferencer_args) | ||
|
||
def test_code_exec_1(self,code=CODE_1, expected_output=RES_1): | ||
|
||
toolinf_res = self.toolinf.code_exec(code) | ||
self.assertEqual(toolinf_res, expected_output) | ||
|
||
def test_code_exec_2(self,code=CODE_2): | ||
toolinf_res = self.toolinf.code_exec(code) | ||
self.assertNotEqual(toolinf_res.returncode, 0) | ||
|
||
unittest.main() | ||
|
||
|
||
|