| 30 | |
| 31 | |
| 32 | class FuncCallEvalution(ToolEvalution): |
| 33 | def __init__( |
| 34 | self, |
| 35 | model: ToolModel, |
| 36 | dataset: FuncCallDataset, |
| 37 | base_prompt: str = '', |
| 38 | template: str = 'default', |
| 39 | generate_configs: GenerateConfigs = None, |
| 40 | ): |
| 41 | self.model = model |
| 42 | self.dataset = dataset |
| 43 | self.base_prompt = base_prompt |
| 44 | self.template = template |
| 45 | self.generate_configs = generate_configs |
| 46 | |
| 47 | if not isinstance(model, ToolModel): |
| 48 | raise BaseException(f"must be ToolModel Class! not {model}") |
| 49 | |
| 50 | def calc(self): |
| 51 | '''开始计算结果''' |
| 52 | self.predicts = [] |
| 53 | func_call_train_datas = self.create_prompts(self.dataset) |
| 54 | |
| 55 | for idx, data in enumerate(func_call_train_datas): |
| 56 | print(f"总共 {len(func_call_train_datas)} 条prompt,当前运行到第 {idx} 条prompt", end="\r") |
| 57 | prompt = data["instruction"] |
| 58 | history = data["history"] |
| 59 | answer = data["output"] |
| 60 | functions = data["functions"] |
| 61 | predict = self.generate(prompt, self.template, self.generate_configs, history) |
| 62 | |
| 63 | if "arguments" in answer: |
| 64 | answer = {"content": answer["content"], "function_call": {"name": answer["name"], "arguments": answer["arguments"]}} |
| 65 | |
| 66 | if "#function" in predict: |
| 67 | try: |
| 68 | predict_param = json.loads(predict.split("#function")[-1]) |
| 69 | if "arguments" in predict_param: |
| 70 | predict_param = { |
| 71 | "content": predict_param["content"], |
| 72 | "function_call": {"name": predict_param["name"], "arguments": predict_param["arguments"]} |
| 73 | } |
| 74 | predict = {**predict_param, **{"role": "assistant"}} |
| 75 | except Exception as e: |
| 76 | logger.error("content: {content}") |
| 77 | predict = {**{"content": predict_param}, **{"role": "assistant"}} |
| 78 | else: |
| 79 | predict = { |
| 80 | "role": "assistant", |
| 81 | "content": predict |
| 82 | } |
| 83 | |
| 84 | self.predicts.append({ |
| 85 | "prompt": prompt, "history": history, |
| 86 | "predict": predict, "answer": answer, |
| 87 | "functions": functions |
| 88 | }) |
| 89 | |