File size: 3,637 Bytes
886d623
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "95d56fde-3f1a-482f-aa25-40687d16e5dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"books\": [\n",
      "    {\n",
      "      \"book_id\": 1,\n",
      "      \"title\": \"The Enigma of Elysium\",\n",
      "      \"author\": \"Aria Nightshade\",\n",
      "      \"genre\": \"Fantasy\"\n",
      "    },\n",
      "    {\n",
      "      \"book_id\": 2,\n",
      "      \"title\": \"Shadows of Serendipity\",\n",
      "      \"author\": \"Elijah Blackwood\",\n",
      "      \"genre\": \"Mystery\"\n",
      "    },\n",
      "    {\n",
      "      \"book_id\": 3,\n",
      "      \"title\": \"Whispers in the Wind\",\n",
      "      \"author\": \"Luna Silvermoon\",\n",
      "      \"genre\": \"Romance\"\n",
      "    }\n",
      "  ]\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import openai\n",
    "import os\n",
    "\n",
    "openai.api_key = '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'\n",
    "MODEL_NAME = 'gpt-3.5-turbo'\n",
    "\n",
    "# TEXT = f\"\"\"\n",
    "# You should express what you want a model to do by \\\n",
    "# providing instructions that are as clear and \\\n",
    "# specific as you can possibly make them. \\\n",
    "# This will guide the model towards the desired output, \\\n",
    "# and reduce the chances of receiving irrelevent \\\n",
    "# or incorrec5 responses. Don't confuse writing a \\\n",
    "# clear prompt with writing a short prompt. \\\n",
    "# In many case, longer prompts provide more clearity \\\n",
    "# and context for the model, which can lead to \\\n",
    "# more detailed and relevant outputs.\n",
    "# \"\"\"\n",
    "\n",
    "# PROMPT = f\"\"\"\n",
    "# summarize the text delimited by triple backticks\\\n",
    "# into a single sentence.\n",
    "# ```{TEXT}```\n",
    "# \"\"\"\n",
    "\n",
    "PROMPT = f\"\"\"\n",
    "Generate a list of three made-up book titles along \\\n",
    "with their authors and genres.\n",
    "Provide them in JSON format with the following keys:\n",
    "book_id, title, author, genre.\n",
    "\"\"\"\n",
    "\n",
    "class Assignment4:\n",
    "    input_val = ''\n",
    "    def __init__(self, model):\n",
    "        self.model = model\n",
    "        \n",
    "    def get_input(self):\n",
    "        self.input_val = input('Enter your message')\n",
    "\n",
    "    def get_completion(self, prompt):\n",
    "        messages = [{\n",
    "            'role': 'user',\n",
    "            'content': prompt\n",
    "        }]\n",
    "        response = openai.ChatCompletion.create(\n",
    "            model=self.model,\n",
    "            messages=messages,\n",
    "            temperature=0\n",
    "        )\n",
    "        return response.choices[0].message['content']\n",
    "\n",
    "    \n",
    "obj = Assignment4(MODEL_NAME)\n",
    "print(obj.get_completion(PROMPT))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc90ad16-7a94-42ef-bdd4-9ec176da6210",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}