File size: 1,773 Bytes
09321b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from modelscope.utils.constant import Tasks
from .pipeline_tool import ModelscopePipelineTool


class ImageChatTool(ModelscopePipelineTool):
    default_model = 'damo/multi-modal_mplug_owl_multimodal-dialogue_7b'
    description = '图文对话和图像描述服务,针对输入的图片和用户的文本输入,给出文本回复'
    name = 'modelscope_image-chat'
    parameters: list = [{
        'name': 'image',
        'description': '用户输入的图片',
        'required': True
    }, {
        'name': 'text',
        'description': '用户输入的文本',
        'required': True
    }]
    task = Tasks.multimodal_dialogue

    def construct_image_chat_input(self, **kwargs):
        image = kwargs.pop('image', '')
        text = kwargs.pop('text', '')

        system_prompt_1 = 'The following is a conversation between a curious human and AI assistant.'
        system_prompt_2 = "The assistant gives helpful, detailed, and polite answers to the user's questions."
        messages = {
            'messages': [
                {
                    'role': 'system',
                    'content': system_prompt_1 + ' ' + system_prompt_2
                },
                {
                    'role': 'user',
                    'content': [{
                        'image': image
                    }]
                },
                {
                    'role': 'user',
                    'content': text
                },
            ]
        }
        return messages

    def _remote_parse_input(self, *args, **kwargs):
        messages = self.construct_image_chat_input(**kwargs)
        return {'input': messages}

    def _local_parse_input(self, *args, **kwargs):
        return (self.construct_image_chat_input(**kwargs)), {}