来源:互联网 更新时间:2026-08-26 13:40
提示工程系列已经聊到了第五篇。前面几篇,我们分别讨论了文本提示技术和多语言提示技术,算是把单模态的玩法摸了个大概。从这一篇开始,视角要彻底打开了——我们要跨出单一模态的边界,正式进入多模态提示技术的地盘。这项技术允许AI系统同时处理并理解文本、图像、音频等多种类型的数据,听起来是不是已经开始有意思了?现在,我们就来探讨,究竟怎么设计和实现那些能看得懂、听得懂、还能说会道的AI系统。
进入技术细节之前,可以先聊聊背景:多模态AI为什么这么重要?说到底,有五个核心原因值得关注。
多模态AI的核心,一言蔽之,就是“跨模态信息整合”。它通常走这么几步:
话不多说,直接进入几种典型的多模态提示技术。

这大概是目前最常用的多模态提示方式,将图像信息和文字描述结合起来输入给模型。
import openai
import base64
def image_text_prompting(image_path, text_prompt):
with open(image_path, "rb") as image_file:
encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
prompt = f"""
[IMAGE]{encoded_image}[/IMAGE]
Based on the image above, {text_prompt}
"""
response = openai.Completion.create(
engine="da vinci",
prompt=prompt,
max_tokens=150,
temperature=0.7
)
return response.choices[0].text.strip()
# 使用示例
image_path = "path/to/your/image.jpg"
text_prompt = "describe what you see in detail."
result = image_text_prompting(image_path, text_prompt)
print(result)
这段代码示范了如何把图像信息编码进提示词,然后让模型基于图像内容执行任务——本质是让模型“看图说话”。
音频与文本的结合玩法,常用于语音识别、音乐情感分析等场景。
import openai
import librosa
def audio_text_prompting(audio_path, text_prompt):
y, sr = librosa.load(audio_path)
mel_spectrogram = librosa.feature.melspectrogram(y=y, sr=sr)
audio_features = mel_spectrogram.flatten()[:1000].tolist()
prompt = f"""
Audio features: {audio_features}
Based on the audio represented by these features, {text_prompt}
"""
response = openai.Completion.create(
engine="da vinci",
prompt=prompt,
max_tokens=150,
temperature=0.7
)
return response.choices[0].text.strip()
# 使用示例
audio_path = "path/to/your/audio.wa v"
text_prompt = "describe the main instruments you hear and the overall mood of the music."
result = audio_text_prompting(audio_path, text_prompt)
print(result)
这个例子把音频特征编码到提示词里,让模型基于声音内容做分析或生成。
视频更复杂——它既有图像序列又有音频,还多了一个时间维度。
import openai
import cv2
import librosa
import numpy as np
def video_text_prompting(video_path, text_prompt, sample_rate=1):
cap = cv2.VideoCapture(video_path)
frames = []
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
if len(frames) % sample_rate == 0:
frames.append(frame)
cap.release()
y, sr = librosa.load(video_path)
a vg_frame = np.mean(frames, axis=0).flatten()[:1000].tolist()
mel_spectrogram = librosa.feature.melspectrogram(y=y, sr=sr)
audio_features = mel_spectrogram.flatten()[:1000].tolist()
prompt = f"""
Video features:
Visual: {a vg_frame}
Audio: {audio_features}
Based on the video represented by these features, {text_prompt}
"""
response = openai.Completion.create(
engine="da vinci",
prompt=prompt,
max_tokens=200,
temperature=0.7
)
return response.choices[0].text.strip()
# 使用示例
video_path = "path/to/your/video.mp4"
text_prompt = "describe the main events happening in the video and the overall atmosphere."
result = video_text_prompting(video_path, text_prompt)
print(result)
这个方案把视频的视觉特征和音频特征一起打包进提示词,让模型基于整个视频内容生成描述。
真刀真枪落地应用时,下面几个技术点可能帮你少走很多弯路。
不同模态之间的信息必须在语义上对齐——这是模型正确理解多模态输入的前提。
def align_modalities(image_features, text_description):
prompt = f"""
Image features: {image_features}
Text description: {text_description}
Ensure that the text description accurately reflects the content of the image.
If there are any discrepancies, provide a corrected description.
Aligned description:
"""
# 使用这个提示调用模型来对齐模态
引导模型去关注不同模态中与当前任务最相关的部分。
def cross_modal_attention(image_features, audio_features, text_query):
prompt = f"""
Image features: {image_features}
Audio features: {audio_features}
Query: {text_query}
Focus on the aspects of the image and audio that are most relevant to the query.
Describe what you find:
"""
# 使用这个提示调用模型来实现跨模态注意力
把思维链(Chain-of-Thought)技术从纯文本场景扩展到多模态领域。
def multimodal_cot(image_features, text_description, question):
prompt = f"""
Image features: {image_features}
Text description: {text_description}
Question: {question}
Let's approach this step-by-step:
1) What are the key elements in the image?
2) How does the text description relate to these elements?
3) What information from both sources is relevant to the question?
4) Based on this analysis, what is the answer to the question?
Step 1:
"""
# 使用这个提示调用模型来实现多模态思维链
评估多模态AI比单模态系统复杂得多,建议从以下几个角度入手:
def multimodal_evaluation(ground_truth, prediction, image_features, audio_features):
text_score = calculate_bleu(ground_truth, prediction)
image_relevance = evaluate_image_relevance(image_features, prediction)
audio_relevance = evaluate_audio_relevance(audio_features, prediction)
combined_score = (text_score + image_relevance + audio_relevance) / 3
return combined_score
def evaluate_image_relevance(image_features, text):
prompt = f"""
Image features: {image_features}
Generated text: {text}
On a scale of 1-10, how relevant is the generated text to the image content?
Score:
"""
# 调用模型来评估图像相关性
def evaluate_audio_relevance(audio_features, text):
prompt = f"""
Audio features: {audio_features}
Generated text: {text}
On a scale of 1-10, how relevant is the generated text to the audio content?
Score:
"""
# 调用模型来评估音频相关性
来看一个实战用例——多模态新闻分析系统。这个系统需要同时处理文本、图像和视频组成的新闻内容,最终输出综合分析报告。
import openai
import cv2
import librosa
import numpy as np
from transformers import pipeline
class MultimodalNewsAnalyzer:
def __init__(self):
self.text_summarizer = pipeline("summarization")
self.image_captioner = pipeline("image-to-text")
def analyze_news(self, text, image_path, video_path):
text_summary = self.summarize_text(text)
image_caption = self.caption_image(image_path)
video_features = self.extract_video_features(video_path)
analysis = self.generate_analysis(text_summary, image_caption, video_features)
return analysis
def summarize_text(self, text):
return self.text_summarizer(text, max_length=100, min_length=30, do_sample=False)[0]['summary_text']
def caption_image(self, image_path):
return self.image_captioner(image_path)[0]['generated_text']
def extract_video_features(self, video_path):
cap = cv2.VideoCapture(video_path)
frames = []
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frames.append(frame)
cap.release()
a vg_frame = np.mean(frames, axis=0).flatten()[:1000].tolist()
y, sr = librosa.load(video_path)
mel_spectrogram = librosa.feature.melspectrogram(y=y, sr=sr)
audio_features = mel_spectrogram.flatten()[:1000].tolist()
return {"visual": a vg_frame, "audio": audio_features}
def generate_analysis(self, text_summary, image_caption, video_features):
prompt = f"""
Analyze the following news content and generate a comprehensive report:
Text Summary: {text_summary}
Image Content: {image_caption}
Video Features:
- Visual: {video_features['visual']}
- Audio: {video_features['audio']}
Please provide a detailed analysis covering the following aspects:
1. Main topic and key points
2. Sentiment and tone
3. Visual elements and their significance
4. Audio elements (if any) and their impact
5. Overall credibility and potential biases
6. Suggestions for further investigation
Analysis:
"""
response = openai.Completion.create(
engine="da vinci",
prompt=prompt,
max_tokens=500,
temperature=0.7
)
return response.choices[0].text.strip()
# 使用示例
analyzer = MultimodalNewsAnalyzer()
text = """
Breaking news: A new renewable energy project has been announced today.
The project aims to provide clean energy to over 1 million homes by 2025.
Environmental groups ha ve praised the initiative, while some local communities
express concerns about the impact on wildlife.
"""
image_path = "path/to/solar_panel_image.jpg"
video_path = "path/to/news_report_video.mp4"
analysis = analyzer.analyze_news(text, image_path, video_path)
print(analysis)
这个实现有几个值得注意的设计思路:
技术再酷,也不能回避几个核心难题。
def attention_based_fusion(image_features, text_features, audio_features):
prompt = f"""
Given the following features from different modalities:
Image: {image_features}
Text: {text_features}
Audio: {audio_features}
Please analyze the importance of each modality for the current task,
assigning attention weights (0-1) to each. Then, provide a fused representation
that takes these weights into account.
Attention weights:
Image weight:
Text weight:
Audio weight:
Fused representation:
"""
# 基于注意力的模态融合
def cross_modal_consistency_check(image_description, text_content, audio_transcript):
prompt = f"""
Image description: {image_description}
Text content: {text_content}
Audio transcript: {audio_transcript}
Please analyze the consistency across these modalities:
1. Are there any contradictions between the image, text, and audio?
2. If inconsistencies exist, which modality do you think is more reliable and why?
3. Provide a consistent summary that reconciles any discrepancies.
Analysis:
"""
# 跨模态一致性检查
def efficient_multimodal_processing(image_features, text_content, audio_features):
prompt = f"""
Given the following multimodal input:
Image features (compressed): {image_features}
Text content: {text_content}
Audio features (compressed): {audio_features}
Please perform the analysis in the following order to maximize efficiency:
1. Quick text analysis
2. If necessary based on text, analyze image features
3. Only if critical information is still missing, analyze audio features
Provide your analysis at each step and explain why you decided to proceed to the next step (if applicable).
Analysis:
"""
# 高效多模态处理
多模态AI还在快速发展期,有几个方向值得关注:
多模态提示技术,正把AI的能力边界从纯文本向外大大拓展。通过本文介绍的技术和最佳实践,相信你已经有了打造多模态AI应用的足够素材。当然,这个领域挑战依然密集,需要持续探索和迭代。随着技术不断演进,多模态AI最终会让我们更自如地理解和处理这个复杂世界。
腾讯ima怎么把微信内容一键导入知识库?
腾讯ima怎么创建共享知识库?
Celestia价格预测2026-2032:TIA币能否引领山寨币上涨行情?历史价格回顾
比特币(BTC)核心周期指标复刻历史走势 价格或跌破5.8万美元关键支撑位
比特币 2025 年价格预测:BTC 的未来走势
新浪互联网热点小时报丨2026年07月26日16时_今日实时互联网热点速递
新浪机器学习热点小时报丨2026年07月25日18时_今日实时机器学习热点速递
WorkBuddy微信版怎么获得积分?
新浪人工智能热点小时报丨2026年07月30日18时_今日实时人工智能热点速递
5000元起的鼠标哪个最值得入手?
腾讯ima知识库怎么分类管理?
短剧《史上最强洪荒修为》剧情介绍
海尔消毒柜自动消毒如何中止
博世壁挂炉关闭暖气怎么操作
男生高性价比充电头?
车载冰箱重置到出厂设置几步?
Windy卫星云图怎么看?云层变化识别技巧
WorkBuddy积分怎么获得?
5000-6000元鼠标有什么推荐?
管线机怎么接云米净水器
手机号码测吉凶
本站所有软件,都由网友上传,如有侵犯你的版权,请发邮件haolingcc@hotmail.com 联系删除。 版权所有 Copyright@2012-2013 haoling.cc