How to extract required information using GPT-3 API

Question:

I tried the steps mentioned in this article.

https://matthewbilyeu.com/blog/2022-09-01/responding-to-recruiter-emails-with-gpt-3

There is a screenshot that says: Here’s an example from the OpenAI Playground.

I typed all the text in "playground" but do not get similar response as shown in that image. I expected similar text like {"name":"William", "company":"BillCheese"} I am not sure how to configure the parameters in openAI web interface.


Update:

I used this code:

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

and got this error:

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

||

Answers:

You’re running into this problem: Regex: AttributeError: 'NoneType' object has no attribute 'groups'

Take a look at this line:

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

The regex can’t find anything matching the pattern, so it returns None. None does not have .groups() so you get an error. I don’t have enough details to go much further, but the link above might get you there.

Answered By: Brian MacKay

I don’t know why both the questioner as well as one reply above me are using RegEx. According to the OpenAI documentation, a Completion will return a JSON object.

No need to catch specific things complexly – just load the return into a dictionary and access the fields you need:

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["..."]
Answered By: J. M. Arnold

this worked for me:

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"])
Answered By: Ali Ait-Bachir
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.