gulixin0922 commited on
Commit
94c0081
1 Parent(s): aeb3796

update backend api

Browse files
Files changed (1) hide show
  1. conversation.py +46 -0
conversation.py CHANGED
@@ -89,6 +89,51 @@ class Conversation:
89
  else:
90
  raise ValueError(f"Invalid role: {message['role']}")
91
  return send_messages
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  def append_message(
94
  self,
@@ -257,3 +302,4 @@ class Conversation:
257
  image.save(filename)
258
 
259
  return filename
 
 
89
  else:
90
  raise ValueError(f"Invalid role: {message['role']}")
91
  return send_messages
92
+
93
+ def get_prompt_v2(self, inlude_image=False, max_dynamic_patch=12):
94
+ send_messages = [
95
+ {
96
+ "role": "system",
97
+ "content": self.get_system_message(),
98
+ }
99
+ ]
100
+ for message in self.messages:
101
+ if message["role"] == self.USER:
102
+ user_message = {
103
+ "role": self.USER,
104
+ "content": message["content"],
105
+ }
106
+ if inlude_image and "image" in message:
107
+ user_message["image"] = []
108
+ for image in message["image"]:
109
+ user_message["image"].append(pil2base64(image))
110
+
111
+ content = [{"type": "text", "text": message["content"]}]
112
+ for image_base64 in user_message["image"]:
113
+ content.append({
114
+ "type": "image_url",
115
+ "image_url": {
116
+ "url": f"data:image/jpeg;base64,{image_base64}",
117
+ "max_dynamic_patch": max_dynamic_patch
118
+ }
119
+ })
120
+ send_messages.append({'role': self.USER, 'content': content})
121
+ else:
122
+ send_messages.append(user_message)
123
+ elif message["role"] == self.ASSISTANT:
124
+ send_messages.append(
125
+ {"role": self.ASSISTANT, "content": message["content"]}
126
+ )
127
+ elif message["role"] == self.SYSTEM:
128
+ send_messages.append(
129
+ {
130
+ "role": self.SYSTEM,
131
+ "content": message["content"],
132
+ }
133
+ )
134
+ else:
135
+ raise ValueError(f"Invalid role: {message['role']}")
136
+ return send_messages
137
 
138
  def append_message(
139
  self,
 
302
  image.save(filename)
303
 
304
  return filename
305
+