forked from microsoft/UFO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.py
144 lines (116 loc) · 4.12 KB
/
basic.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import os
from abc import ABC, abstractmethod
from typing import Dict, List, Union
import yaml
from ufo.utils import print_with_color
class BasicPrompter(ABC):
"""
The BasicPrompter class is the abstract class for the prompter.
"""
def __init__(
self, is_visual: bool, prompt_template: str, example_prompt_template: str
):
"""
Initialize the BasicPrompter.
:param is_visual: Whether the request is for visual model.
:param prompt_template: The path of the prompt template.
:param example_prompt_template: The path of the example prompt template.
"""
self.is_visual = is_visual
if prompt_template:
self.prompt_template = self.load_prompt_template(prompt_template, is_visual)
else:
self.prompt_template = ""
if example_prompt_template:
self.example_prompt_template = self.load_prompt_template(
example_prompt_template, is_visual
)
else:
self.example_prompt_template = ""
@staticmethod
def load_prompt_template(template_path: str, is_visual=None) -> Dict[str, str]:
"""
Load the prompt template.
:return: The prompt template.
"""
if is_visual == None:
path = template_path
else:
path = template_path.format(
mode="visual" if is_visual == True else "nonvisual"
)
if not path:
return {}
if os.path.exists(path):
try:
prompt = yaml.safe_load(open(path, "r", encoding="utf-8"))
except yaml.YAMLError as exc:
print_with_color(f"Error loading prompt template: {exc}", "yellow")
else:
raise FileNotFoundError(f"Prompt template not found at {path}")
return prompt
@staticmethod
def prompt_construction(
system_prompt: str, user_content: List[Dict[str, str]]
) -> List:
"""
Construct the prompt for summarizing the experience into an example.
:param user_content: The user content.
return: The prompt for summarizing the experience into an example.
"""
system_message = {"role": "system", "content": system_prompt}
user_message = {"role": "user", "content": user_content}
prompt_message = [system_message, user_message]
return prompt_message
@staticmethod
def retrived_documents_prompt_helper(
header: str, separator: str, documents: List[str]
) -> str:
"""
Construct the prompt for retrieved documents.
:param header: The header of the prompt.
:param separator: The separator of the prompt.
:param documents: The retrieved documents.
return: The prompt for retrieved documents.
"""
if header:
prompt = "\n<{header}:>\n".format(header=header)
else:
prompt = ""
for i, document in enumerate(documents):
if separator:
prompt += "[{separator} {i}:]".format(separator=separator, i=i + 1)
prompt += "\n"
prompt += document
prompt += "\n\n"
return prompt
@abstractmethod
def system_prompt_construction(self) -> str:
"""
Construct the system prompt for LLM.
"""
pass
@abstractmethod
def user_prompt_construction(self) -> str:
"""
Construct the textual user prompt for LLM based on the `user` field in the prompt template.
"""
pass
@abstractmethod
def user_content_construction(self) -> str:
"""
Construct the full user content for LLM, including the user prompt and images.
"""
pass
def examples_prompt_helper(self) -> str:
"""
A helper function to construct the examples prompt for in-context learning.
"""
pass
def api_prompt_helper(self) -> str:
"""
A helper function to construct the API list and descriptions for the prompt.
"""
pass