gpt4 book ai didi

python - OpenAI GPT-3 API : How to parse the response into an ordered list or dictionary?

转载 作者:行者123 更新时间:2023-12-02 05:47:24 26 4
gpt4 key购买 nike

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: 模式,但情况并非总是如此。一致的是每个元素前面的数字 (如 Ì., II., 1., 2. 或 a., b.) 但有时它看起来像 a ..(例如,您可以在响应末尾看到它。

知道怎么做吗? (我为此使用 python,但可以从另一种语言改编)

最佳答案

使用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)

关于python - OpenAI GPT-3 API : How to parse the response into an ordered list or dictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75429596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com