File size: 2,729 Bytes
436c4c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import openai
import os
from dotenv import load_dotenv

load_dotenv()

openai.api_key = os.getenv("API_KEY")


#Takes in a sentence and returns a list of dicts consisiting of key-value pairs of masked words and lists of the possible replacements
def predict_masked_words(sentence, n_suggestions=5):

    prompt = (
          f"Given a sentence with masked words, masked word can be one or more than one, indicated by [MASK], generate {n_suggestions} possible words to fill each mask. "
          "Return the results as a list of dictionaries, where each dictionary key is a masked word and its value is a list of 5 potential words to fill that mask.\n\n"
          "Example input: \"The [MASK] fox [MASK] over the [MASK] dog.\"\n\n"
          "Example output:\n"
          "[\n"
          "  {\n"
          "    \"[MASK]1\": [\"quick\", \"sly\", \"red\", \"clever\", \"sneaky\"]\n"
          "  },\n"
          "  {\n"
          "    \"[MASK]2\": [\"jumped\", \"leaped\", \"hopped\", \"sprang\", \"bounded\"]\n"
          "  },\n"
          "  {\n"
          "    \"[MASK]3\": [\"lazy\", \"sleeping\", \"brown\", \"tired\", \"old\"]\n"
          "  }\n"
          "]\n\n"
          "Example input: \"The [MASK] [MASK] ran swiftly across the [MASK] field.\"\n\n"
          "Example output:\n"
          "[\n"
          "  {\n"
          "    \"[MASK]1\": [\"tall\", \"fierce\", \"young\", \"old\", \"beautiful\"]\n"
          "  },\n"
          "  {\n"
          "    \"[MASK]2\": [\"lion\", \"tiger\", \"horse\", \"cheetah\", \"deer\"]\n"
          "  },\n"
          "  {\n"
          "    \"[MASK]3\": [\"green\", \"wide\", \"sunny\", \"open\", \"empty\"]\n"
          "  }\n"
          "]\n\n"
          "Example input: \"It was a [MASK] day when the train arrived at the station.\"\n\n"
          "Example output:\n"
          "[\n"
          "  {\n"
          "    \"[MASK]1\": [\"sunny\", \"rainy\", \"cloudy\", \"foggy\", \"stormy\"]\n"
          "  },\n"
          "]\n\n"
          "Now, please process the following sentence:\n"
          f"{sentence}"
      )


    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": prompt}
        ],
        max_tokens=100,
        n=1,
        stop=None,
        temperature=0.7
    )

    print(response['choices'][0]['message']['content'])


# sentence = "Evacuations and storm [MASK] began on Sunday night as forecasters projected that Hurricane Dorian would hit into Florida’s west coast on Wednesday as a major hurricane packing life-threatening winds and storm surge."
# predict_masked_words(sentence, n_suggestions=5)