* init * update xpu to cpu * update * update readme * update example * update * add refer * add guide to train different datasets * update readme * update
		
			
				
	
	
		
			81 lines
		
	
	
	
		
			2.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			81 lines
		
	
	
	
		
			2.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
#
 | 
						|
# Copyright 2016 The BigDL Authors.
 | 
						|
#
 | 
						|
# Licensed under the Apache License, Version 2.0 (the "License");
 | 
						|
# you may not use this file except in compliance with the License.
 | 
						|
# You may obtain a copy of the License at
 | 
						|
#
 | 
						|
#     http://www.apache.org/licenses/LICENSE-2.0
 | 
						|
#
 | 
						|
# Unless required by applicable law or agreed to in writing, software
 | 
						|
# distributed under the License is distributed on an "AS IS" BASIS,
 | 
						|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
						|
# See the License for the specific language governing permissions and
 | 
						|
# limitations under the License.
 | 
						|
#
 | 
						|
# Some parts of this file is adapted from
 | 
						|
# https://github.com/tloen/alpaca-lora/blob/main/utils/prompter.py
 | 
						|
#
 | 
						|
# Copyright 2023 Rohan Taori, Ishaan Gulrajani, Tianyi Zhang, Yann Dubois, Xuechen Li
 | 
						|
 | 
						|
# Licensed under the Apache License, Version 2.0 (the "License");
 | 
						|
# you may not use this file except in compliance with the License.
 | 
						|
# You may obtain a copy of the License at
 | 
						|
#
 | 
						|
# http://www.apache.org/licenses/LICENSE-2.0
 | 
						|
#
 | 
						|
# Unless required by applicable law or agreed to in writing, software
 | 
						|
# distributed under the License is distributed on an "AS IS" BASIS,
 | 
						|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
						|
# See the License for the specific language governing permissions and
 | 
						|
# limitations under the License.
 | 
						|
 | 
						|
 | 
						|
import json
 | 
						|
import os.path as osp
 | 
						|
from typing import Union
 | 
						|
from bigdl.llm.utils.common import invalidInputError
 | 
						|
 | 
						|
 | 
						|
class Prompter(object):
 | 
						|
    __slots__ = ("template", "_verbose")
 | 
						|
 | 
						|
    def __init__(self, template_name: str = "", verbose: bool = False):
 | 
						|
        self._verbose = verbose
 | 
						|
        if not template_name:
 | 
						|
            # Enforce the default here, so the constructor can be called with '' and will not break.
 | 
						|
            template_name = "alpaca"
 | 
						|
        file_name = osp.join("templates", f"{template_name}.json")
 | 
						|
        if not osp.exists(file_name):
 | 
						|
            invalidInputError(False, f"Can't read {file_name}")
 | 
						|
        with open(file_name) as fp:
 | 
						|
            self.template = json.load(fp)
 | 
						|
        if self._verbose:
 | 
						|
            print(
 | 
						|
                f"Using prompt template {template_name}: {self.template['description']}"
 | 
						|
            )
 | 
						|
 | 
						|
    def generate_prompt(
 | 
						|
        self,
 | 
						|
        instruction: str,
 | 
						|
        input: Union[None, str]=None,
 | 
						|
        label: Union[None, str]=None,
 | 
						|
    ) -> str:
 | 
						|
        # returns the full prompt from instruction and optional input
 | 
						|
        # if a label (=response, =output) is provided, it's also appended.
 | 
						|
        if input:
 | 
						|
            res = self.template["prompt_input"].format(
 | 
						|
                instruction=instruction, input=input
 | 
						|
            )
 | 
						|
        else:
 | 
						|
            res = self.template["prompt_no_input"].format(
 | 
						|
                instruction=instruction
 | 
						|
            )
 | 
						|
        if label:
 | 
						|
            res = f"{res}{label}"
 | 
						|
        if self._verbose:
 | 
						|
            print(res)
 | 
						|
        return res
 | 
						|
 | 
						|
    def get_response(self, output: str) -> str:
 | 
						|
        return output.split(self.template["response_split"])[1].strip()
 |