如何解析OpenAI GPT-3 API返回的非规范响应为结构化数据?
Python实现GPT-3返回内容的结构化转换
核心思路
先清理原始文本中的格式问题(多余换行、制表符、格式错误),再用正则表达式提取主题与对应标题,最后组装成目标JSON结构。
具体实现代码
import re import json def parse_gpt_response(raw_text): # 1. 清理文本格式 # 移除多余换行、制表符和连续空格 cleaned_text = re.sub(r'\s+', ' ', raw_text).strip() # 修复格式错误:"4Topic"改为"4. Topic","a .."改为"a." cleaned_text = re.sub(r'(\d+)Topic', r'\1. Topic', cleaned_text) cleaned_text = re.sub(r'a\s*\.\.', r'a.', cleaned_text) # 合并被拆分的单词(如"Digital Marketin g") cleaned_text = re.sub(r'(\w)\s+(\w)', r'\1\2', cleaned_text) # 2. 提取所有主题模块 topic_blocks = re.findall(r'(\d+\. Topic: .+?)(?=\d+\. Topic:|$)', cleaned_text, re.DOTALL) # 3. 处理每个主题,提取名称和标题列表 result = [] for idx, block in enumerate(topic_blocks, 1): # 提取主题名称 topic_match = re.search(r'Topic: (.+?)(?= Headlines:)', block) if not topic_match: continue topic_name = topic_match.group(1).strip() # 提取所有标题 headlines = re.findall(r'[a-e]\. (.+?)(?=[a-e]\.|$)', block) cleaned_headlines = [h.strip() for h in headlines] # 组装成目标结构 result.append({f"Topic {idx}": cleaned_headlines}) # 4. 生成格式化JSON return json.dumps(result, indent=4) # 示例调用 raw_response = """ 1. Topic: The Benefits of Digital Marketing Headlines: a. Unlocking the Potential of Digital Marketing b. Harnessing the Power of Digital Marketing for Your Business c. How to Maximize Your Return on Investment with Digital Marketing d. Exploring the Benefits of a Comprehensive Digital Marketing Strategy e. Leveraging Technology to Take Your Business to the Next Level with Digital Marketing 2. Topic: Social Media Strategies for Effective Digital Marketing Headlines: a. Crafting an Engaging Social Media Presence for Maximum Impact b. How to Reach and Engage Your Target Audience Through Social Media Platforms c. Optimizing Your Content Strategy for Maximum Reach on Social Media Platforms d. Utilizing Paid Advertising Strategies on Social Media Platforms e .Analyzing and Improving Performance Across Multiple Social Networks 3. Topic: SEO Best Practices for Effective Digital Marketing Headlines: a .Understanding Search Engine Algorithms and Optimizing Content Accordingly b .Developing an Effective SEO Strategy That Delivers Results c .Leveraging Keywords and Metadata For Maximum Visibility d .Exploring Advanced SEO Techniques To Increase Traffic e .Analyzing Performance Data To Improve Rankings 4Topic : Email Campaigns For Successful Digital Marketin g Headlines : a .Creating Compelling Email Campaigns That Drive Results b .Optimizing Email Deliverability For Maximum Impact c .Utilizing Automation Tools To Streamline Email Campaign Management d .Measuring Performance And Analyzing Data From Email Campaigns e .Exploring Creative Ways To Increase Open Rates On Emails 5Topic : Mobile Advertising Strategies For Effective Digita l Marketin g Headlines : a ..Maximizing Reach With Mobile Ads b ..Understanding User Behavior On Mobile Devices c ..Optimizing Ads For Different Screen Sizes d ..Leveraging Location- Based Targeting To Increase Relevance e ..Analyzing Performance Data From Mobile Ads """ print(parse_gpt_response(raw_response))
代码说明
- 文本清理:通过正则移除冗余空白字符,修复"4Topic""a.."这类格式错误,同时合并被换行拆分的单词。
- 主题模块提取:匹配
数字. Topic:的模式,分割出每个独立的主题区块。 - 标题提取:在每个主题区块内,匹配
a-e. 内容的格式,提取并清理所有标题。 - JSON生成:将处理后的主题与标题组装成目标结构,用
json.dumps输出格式化的JSON结果。
运行代码后,会输出符合要求的结构化JSON:
[ { "Topic 1": [ "Unlocking the Potential of Digital Marketing", "Harnessing the Power of Digital Marketing for Your Business", "How to Maximize Your Return on Investment with Digital Marketing", "Exploring the Benefits of a Comprehensive Digital Marketing Strategy", "Leveraging Technology to Take Your Business to the Next Level with Digital Marketing" ] }, { "Topic 2": [ "Crafting an Engaging Social Media Presence for Maximum Impact", "How to Reach and Engage Your Target Audience Through Social Media Platforms", "Optimizing Your Content Strategy for Maximum Reach on Social Media Platforms", "Utilizing Paid Advertising Strategies on Social Media Platforms", "Analyzing and Improving Performance Across Multiple Social Networks" ] }, { "Topic 3": [ "Understanding Search Engine Algorithms and Optimizing Content Accordingly", "Developing an Effective SEO Strategy That Delivers Results", "Leveraging Keywords and Metadata For Maximum Visibility", "Exploring Advanced SEO Techniques To Increase Traffic", "Analyzing Performance Data To Improve Rankings" ] }, { "Topic 4": [ "Creating Compelling Email Campaigns That Drive Results", "Optimizing Email Deliverability For Maximum Impact", "Utilizing Automation Tools To Streamline Email Campaign Management", "Measuring Performance And Analyzing Data From Email Campaigns", "Exploring Creative Ways To Increase Open Rates On Emails" ] }, { "Topic 5": [ "Maximizing Reach With Mobile Ads", "Understanding User Behavior On Mobile Devices", "Optimizing Ads For Different Screen Sizes", "Leveraging Location-Based Targeting To Increase Relevance", "Analyzing Performance Data From Mobile Ads" ] } ]
内容的提问来源于stack exchange,提问作者Dany M
相关产品推荐
相关产品推荐

