Skip to content

Commit ac94b69

Browse files
committed
opt ai-custom support image
1 parent 13e98e7 commit ac94b69

2 files changed

Lines changed: 26 additions & 29 deletions

File tree

dtable_events/automations/actions.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3449,43 +3449,33 @@ def get_classify_content(self, row_data):
34493449
content = f'Available options: {target_content}\nOption type: {target_column.get("type")}\nContent to classify: {classify_content}\n'
34503450
return content
34513451

3452-
def _get_image_ocr_text(self, image_list, repo_id):
3452+
def _collect_image(self, image_list, repo_id):
3453+
"""Collect binary content of the first image from image column"""
34533454
if not image_list or not repo_id:
3454-
return ''
3455+
return None
34553456

34563457
try:
34573458
file_name, download_token = self.get_file_download_info(image_list, repo_id)
34583459
if not file_name or not download_token:
3459-
return ''
3460-
3461-
file_content = self.get_file_binary_content(file_name, download_token)
3462-
if file_content is None:
3463-
return ''
3464-
3465-
seatable_ai_api = DTableAIAPI(
3466-
self.username,
3467-
self.auto_rule.org_id,
3468-
self.auto_rule.dtable_uuid,
3469-
SEATABLE_AI_SERVER_URL
3470-
)
3471-
ocr_text = seatable_ai_api.ocr(file_name, file_content)
3472-
return ocr_text.strip() if ocr_text else ''
3460+
return None
3461+
return self.get_file_binary_content(file_name, download_token)
34733462
except Exception as e:
3474-
auto_rule_logger.warning(f'rule {self.auto_rule.rule_id} ocr in custom prompt failed: {e}')
3475-
return ''
3463+
auto_rule_logger.warning(f'rule {self.auto_rule.rule_id} collect image failed: {e}')
3464+
return None
34763465

34773466
def fill_custom_prompt(self, custom_prompt, row_data):
34783467
"""Fill custom_prompt with field values using the same pattern as notifications"""
34793468
if not custom_prompt:
3480-
return ''
3481-
3469+
return '', []
3470+
34823471
# Find all field placeholders like {field_name}
34833472
blanks = set(re.findall(r'\{([^{]*?)\}', custom_prompt))
34843473
col_name_dict = {col.get('name'): col for col in self.auto_rule.table_info['columns']}
34853474
column_blanks = [blank for blank in blanks if blank in col_name_dict]
3486-
3475+
34873476
filled_prompt = custom_prompt
34883477
repo_id = self.config.get('repo_id')
3478+
images = []
34893479

34903480
for blank in column_blanks:
34913481
column_value = row_data.get(blank, '')
@@ -3494,17 +3484,19 @@ def fill_custom_prompt(self, custom_prompt, row_data):
34943484
column = col_name_dict.get(blank)
34953485
if column:
34963486
column_type = column.get('type')
3497-
# For image columns, extract text using OCR
34983487
if column_type == ColumnTypes.IMAGE and column_value and repo_id:
3499-
formatted_value = self._get_image_ocr_text(column_value, repo_id)
3488+
image_content = self._collect_image(column_value, repo_id)
3489+
if image_content:
3490+
images.append(image_content)
3491+
formatted_value = '[Image provided]'
35003492
else:
35013493
formatted_value = self._format_column_value_for_ai(column_value, column_type)
35023494
else:
35033495
formatted_value = ''
35043496

35053497
filled_prompt = filled_prompt.replace('{' + blank + '}', formatted_value)
35063498

3507-
return filled_prompt
3499+
return filled_prompt, images
35083500

35093501
def summary(self):
35103502
self.fill_summary_field()
@@ -3829,14 +3821,14 @@ def custom(self):
38293821

38303822
# Process custom_prompt with field replacements
38313823
custom_prompt = self.config.get('custom_prompt', '')
3832-
filled_prompt = self.fill_custom_prompt(custom_prompt, converted_row)
3824+
filled_prompt, images = self.fill_custom_prompt(custom_prompt, converted_row)
38333825
filled_prompt = filled_prompt[:AUTO_RULES_AI_CONTENT_MAX_LENGTH]
38343826
if not filled_prompt.strip():
38353827
custom_result = ''
38363828
else:
38373829
try:
38383830
seatable_ai_api = DTableAIAPI(self.username, self.auto_rule.org_id, self.auto_rule.dtable_uuid, SEATABLE_AI_SERVER_URL)
3839-
custom_result = seatable_ai_api.custom(filled_prompt)
3831+
custom_result = seatable_ai_api.custom(filled_prompt, images=images if images else None)
38403832
except Exception as e:
38413833
auto_rule_logger.exception(f'rule {self.auto_rule.rule_id} ai custom processing error: {e}')
38423834
self.auto_rule.append_warning({

dtable_events/utils/dtable_ai_api.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def extract(self, content, extract_fields, extract_prompt):
134134
logger.error(f"Failed to extract information: {response.text}")
135135
raise DTableAIAPIError()
136136

137-
def custom(self, content):
137+
def custom(self, content, images=None):
138138
"""Execute custom AI processing with user-defined prompt"""
139139
if not content or not content.strip():
140140
return ''
@@ -148,8 +148,13 @@ def custom(self, content):
148148

149149
url = f'{self.seatable_ai_server_url}/api/v1/ai/custom/'
150150
headers = gen_headers()
151-
response = requests.post(url, json=data, headers=headers, timeout=180)
152-
151+
152+
if images:
153+
files = [('file', (f'image_{i}.jpg', img, 'image/jpeg')) for i, img in enumerate(images)]
154+
response = requests.post(url, data=data, files=files, headers=headers, timeout=180)
155+
else:
156+
response = requests.post(url, json=data, headers=headers, timeout=180)
157+
153158
if response.status_code == 200:
154159
result = response.json()
155160
return result.get('result', '')

0 commit comments

Comments
 (0)