如何获取请求响应中字符串的内容?

我原本在开发一个基于GPT-2的Web应用程序,但效果不理想,于是决定切换到官方OpenAI的GPT-3。我发送了以下请求:

response = openai.Completion.create(  engine="davinci",  prompt="Hello",  temperature=0.7,  max_tokens=64,  top_p=1,  frequency_penalty=0,  presence_penalty=0)

当我打印响应时,得到的是这样的结果:

{  "choices": [    {      "finish_reason": "length",      "index": 0,      "logprobs": null,      "text": ", everyone, and welcome to the first installment of the new opening"    }  ],  "created": 1624033807,  "id": "cmpl-3CBfb8yZAFEUIVXfZO90m77dgd9V4",  "model": "davinci:2020-05-03",  "object": "text_completion"}

但我只想打印出其中的文本内容,那么如何才能打印出响应列表中的”text”值呢?提前感谢您的帮助,祝您有美好的一天。


回答:

使用字典的键索引和列表的索引

x = {"choices": [{"finish_reason": "length",                  "text": ", everyone, and welcome to the first installment of the new opening"}], }text = x['choices'][0]['text']print(text)  # , everyone, and welcome to the first installment of the new opening

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注