python 如何使用GPT-3 API提取所需信息

t9aqgxwy  于 2023-01-19  发布在  Python
关注(0)|答案(3)|浏览(146)

我尝试了本文中提到的步骤。
https://matthewbilyeu.com/blog/2022-09-01/responding-to-recruiter-emails-with-gpt-3
有截图称:这里有一个来自OpenAIPlayground的例子。
我在"playground"中输入了所有文本,但没有得到类似的响应,如该图像所示。我期望类似的文本,如{"name":"William", "company":"BillCheese"},我不知道如何配置openAI Web界面中的参数。
更新:
我用这个代码:

import json
import re, textwrap 
 
import openai
openai.api_key = 'xxx'

prompt = f"""
Hi Matt! This is Steve Jobs with Inforation Edge Limited ! I'm interested in having you join our team here. 
"""

completion = openai.Completion.create(
    model="text-davinci-002",
    prompt=textwrap.dedent(prompt),
    max_tokens=20,
    temperature=0,
)

try:
    json_str_response = completion.choices[0].text
    json_str_response_clean = re.search(r".*(\{.*\})", json_str_response).groups()[0]
    print (json.loads(json_str_response_clean))

except (AttributeError, json.decoder.JSONDecodeError) as exception:
    print("Could not decode completion response from OpenAI:")
    print(completion)
    raise exception

得到了这个错误:

Could not decode completion response from OpenAI:
AttributeError: 'NoneType' object has no attribute 'groups'
goucqfw6

goucqfw61#

您遇到了这个问题:Regex: AttributeError: 'NoneType' object has no attribute 'groups'
看看这句台词:

json_str_response_clean = re.search(r".*(\{.*\})", json_str_response).groups()[0]

正则表达式找不到任何与模式匹配的内容,所以返回None。None没有.groups(),所以你会得到一个错误。我没有足够的细节来进一步说明,但是上面的链接可能会让你找到。

7rtdyuoh

7rtdyuoh2#

我不知道为什么提问者和我上面的一个回复都使用RegEx,根据OpenAI documentation,一个Completion将返回一个JSON对象。
不需要复杂地捕获特定的内容--只需将返回加载到字典中并访问所需的字段:

import json

# ...

# Instead of the try ... except block, just load it into a dictionary.
response = json.loads(completion.choices[0].text)

# Access whatever field you need
response["..."]
oxcyiej7

oxcyiej73#

这对我很有效:

question = "Write a python function to detect anomlies in a given time series"

response = openai.Completion.create(
    model="text-davinci-003",
    prompt=question,
    temperature=0.9,
    max_tokens=150,
    top_p=1,
    frequency_penalty=0.0,
    presence_penalty=0.6,
    stop=[" Human:", " AI:"]
)

print(response)
print("==========Python Code=========")
print(response["choices"][0]["text"])

相关问题