OpenAI GPT-3 API:如何将响应解析为有序列表或字典?

np8igboo  于 2023-03-24  发布在  其他
关注(0)|答案(2)|浏览(197)

GPT-3很棒,但是解析它的结果有点头疼,或者我错过了什么?例如,我要求GPT-3写一些关于“数字营销”的东西,它返回了一些有趣的东西:

\n\n1. Topic: The Benefits of Digital Marketing \nHeadlines: \na. Unlocking the 
Potential of Digital Marketing \nb. Harnessing the Power of Digital Marketing for 
Your Business \nc. How to Maximize Your Return on Investment with Digital Marketing 
\nd. Exploring the Benefits of a Comprehensive Digital Marketing Strategy \ne. 
Leveraging Technology to Take Your Business to the Next Level with Digital Marketing  
\n\n2. Topic: Social Media Strategies for Effective Digital Marketing  \nHeadlines:  
\na. Crafting an Engaging Social Media Presence for Maximum Impact \nb. How to Reach 
and Engage Your Target Audience Through Social Media Platforms  \nc. Optimizing Your 
Content Strategy for Maximum Reach on Social Media Platforms   \nd. Utilizing Paid 
Advertising Strategies on Social Media Platforms   \t\t\t\t\t\t\t     e .Analyzing 
and Improving Performance Across Multiple Social Networks\n\n3. 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\n\n4Topic : 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\n\n5Topic : 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

正如你所看到的,它给我发回了一个与“数字营销”相关的主题列表,其中有一些标题(显然是从A到E).我在这里和那里看到一些换行符和制表.所以我的第一React是在换行符上拆分文本,但看起来格式并不是处处相等,因为在响应的后半部分有很少的换行符(这使得它不准确)。我想做的是重新格式化输出,这样我就可以有一个主题和标题的列表。类似于这样:

[
     {"Topic 1": ["headline 1", "headline 2","..."]},
     {"Topic 2": ["headline 1", "headline 2","..."]},
     {"Topic 3": ["headline 1", "headline 2","..."]}
]

也许在我的请求中有一个参数要发送,但是我在文档中没有找到任何东西。所以我想我最好的办法是使用regex重新格式化。在这里我看到一个模式Topic:Headlines:,但并不总是这样。一致的是每个元素前面的数字(like Ì., II., 1., 2. or a., b.),但有时看起来像a ..(你可以在响应的末尾看到。
你知道怎么做吗?(我使用python,但可以从另一种语言适应)

cwxwcias

cwxwcias1#

使用Edits endpoint

如果您运行test.py,OpenAI API将返回以下完成:

1. Topic: The Benefits of Digital Marketing 
Headlines: 

* 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

测试.py

import openai

openai.api_key = '<OPENAI_API_KEY>'

response = openai.Edit.create(
  model = 'text-davinci-edit-001',
  input = '\n\n1. Topic: The Benefits of Digital Marketing \nHeadlines: \na. Unlocking the Potential of Digital Marketing \nb. Harnessing the Power of Digital Marketing for Your Business \nc. How to Maximize Your Return on Investment with Digital Marketing \nd. Exploring the Benefits of a Comprehensive Digital Marketing Strategy \ne. Leveraging Technology to Take Your Business to the Next Level with Digital Marketing',
  instruction = 'Make this readable'
)

content = response['choices'][0]['text']

print(content)
uajslkp6

uajslkp62#

你可以让GPT以JSON格式提供响应,你只需要将其作为提示的一部分进行训练。下面是一个示例提示:
提供一个与气候变化有关的3个主题的列表,并为每个主题提供3个标题。
您的响应应该是JSON格式。下面是预期的JSON格式:

[
     {"Topic 1": ["headline 1", "headline 2","..."]},
     {"Topic 2": ["headline 1", "headline 2","..."]},
     {"Topic 3": ["headline 1", "headline 2","..."]}
]

结束提示
您可能需要稍微修改一下提示符,但是我已经成功地构建了以JSON格式返回响应的电子邮件解析器。

相关问题