Skip to content

Commit fcd4c4c

Browse files
committed
Merge branch 'dev'
2 parents 9e1095b + 68b6e4e commit fcd4c4c

File tree

10 files changed

+430
-74
lines changed

10 files changed

+430
-74
lines changed

app.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
from os import environ
2-
# import openai
2+
import openai
33
from tests.unit.fixture import api
44
from openaiclient.controller import Controller
5-
6-
# get API key from usr folder
7-
# openai.api_key = environ["OPENAI_API_KEY"]
5+
from openaiclient.controller.controller import StartKey as sk
86

97
controller = Controller(api)
108

11-
controller.start()
9+
# get API key from environment
10+
try:
11+
openai.api_key = environ["OPENAI_API_KEY"]
12+
controller.start()
13+
except Exception as err:
14+
controller.start(sk.NO_API_KEY)

openaiclient/controller/controller.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
* buildRequest - builds the request from a request object
66
"""
7-
7+
from enum import Enum
88
from openaiclient.model.request.completion import CompletionRequest
99
from openaiclient.model.request.edit import EditRequest
1010
from openaiclient.model.models import Models
@@ -44,8 +44,12 @@ def __init__(self, api):
4444
# the response
4545
self._response = None
4646

47-
def start(self) -> None:
48-
self.view.mainWindow()
47+
def start(self, startVal) -> None:
48+
match startVal:
49+
case StartKey.GOOD_START:
50+
self.view.mainWindow()
51+
case StartKey.NO_API_KEY:
52+
self.view.apiWindow()
4953

5054
@property
5155
def models(self):
@@ -118,6 +122,10 @@ def getEditSettings(self):
118122
return self.getSettings(EditRequest)
119123
"""
120124

125+
class StartKey(Enum):
126+
GOOD_START = 0
127+
NO_API_KEY = 1
128+
121129

122130
if __name__ == "__main__":
123131

openaiclient/model/request/completion.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ def __init__(self, module, models):
5757
self._settings.remove("prompt")
5858

5959
def getResponse(self):
60+
61+
request = dict(self._requestDict)
62+
request['model'] = request['model'].name
63+
6064
return Response(
6165
self._module.Completion.create(
62-
**self._requestDict,
66+
**request,
6367
)
6468
)
6569

openaiclient/view/frame/apiframe.py

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
"""
2+
APIFrame class
3+
"""
4+
from increment import Increment
5+
import tkinter as tk
6+
from tkinter import ttk
7+
import webbrowser
8+
import os
9+
import openai
10+
from openaiclient.view.frame import BaseFrame
11+
from tests.unit.fixture import api
12+
13+
14+
class APIFrame(BaseFrame):
15+
"""
16+
A class for creating the frame for the API window when no
17+
API key is discovered on startup
18+
"""
19+
20+
def __init__(self, main, controller):
21+
super().__init__(main, controller)
22+
23+
self._api_key = tk.StringVar()
24+
25+
def create(self):
26+
col = Increment()
27+
row = Increment()
28+
29+
tempFrame1 = tk.Frame(self.master)
30+
31+
tempCol = Increment()
32+
tempRow = Increment()
33+
34+
self.addHorizSeparator(tempCol, tempRow, tempFrame1)
35+
36+
tk.Label(
37+
tempFrame1,
38+
text=instruction.strip(),
39+
justify=tk.LEFT,
40+
font=("", 10, ""),
41+
).grid(
42+
column=tempCol,
43+
row=tempRow,
44+
padx=10,
45+
columnspan=2
46+
)
47+
48+
~tempCol
49+
+tempRow
50+
51+
self.addHorizSeparator(tempCol, tempRow, tempFrame1)
52+
53+
tempFrame1.grid(
54+
column=col,
55+
row=row,
56+
columnspan=2
57+
)
58+
59+
~tempCol
60+
~tempRow
61+
62+
~col
63+
+row
64+
65+
tempFrame2 = tk.Frame(self.master)
66+
67+
tempCol = Increment()
68+
tempRow = Increment()
69+
70+
tk.Button(
71+
tempFrame2,
72+
text="Get API Key",
73+
font=("", 10, ""),
74+
command=self.getAPI
75+
).grid(
76+
column=tempCol,
77+
row=tempRow,
78+
padx=10,
79+
pady=(5,5),
80+
ipadx=2
81+
)
82+
83+
+tempCol
84+
85+
tk.Button(
86+
tempFrame2,
87+
text="Use Test API",
88+
font=("", 10, ""),
89+
command=self.setTestAPI
90+
).grid(
91+
column=tempCol,
92+
row=tempRow,
93+
padx=10,
94+
pady=(5,5),
95+
ipadx=2
96+
)
97+
98+
tempFrame2.grid(
99+
column=col,
100+
row=row,
101+
columnspan=2
102+
)
103+
104+
~col
105+
+row
106+
107+
tk.Label(
108+
self,
109+
text="API Key:",
110+
font=("", 10, ""),
111+
).grid(
112+
column=col,
113+
row=row,
114+
pady=(5,10),
115+
padx=(10,5),
116+
)
117+
118+
+col
119+
120+
tk.Entry(
121+
self,
122+
width=30,
123+
textvariable=self._api_key
124+
).grid(
125+
column=col,
126+
row=row,
127+
pady=5,
128+
padx=(5,10),
129+
)
130+
131+
~col
132+
+row
133+
134+
tk.Button(
135+
self,
136+
text="Save API Key",
137+
font=("", 10, ""),
138+
width=30,
139+
command=self.setAPIKey
140+
).grid(
141+
column=col,
142+
row=row,
143+
pady=(5,10),
144+
padx=10,
145+
columnspan=2
146+
)
147+
148+
def addHorizSeparator(self, col, row, frame):
149+
~col
150+
151+
ttk.Separator(
152+
frame,
153+
orient="horizontal",
154+
).grid(
155+
column=col,
156+
row=row,
157+
columnspan=2,
158+
sticky=tk.W+tk.E,
159+
padx=10,
160+
pady=5
161+
)
162+
163+
+row
164+
165+
def getAPI(self):
166+
api_url = "https://platform.openai.com/account/api-keys"
167+
webbrowser.open(api_url)
168+
169+
def setTestAPI(self):
170+
self.controller._api = api
171+
self.master.destroy()
172+
173+
def setAPIKey(self):
174+
self.controller._module = openai
175+
self.controller._module.api_key = \
176+
os.environ['OPENAI_API_KEY'] = self._api_key.get()
177+
178+
self.master.destroy()
179+
180+
181+
instruction="""
182+
If you would like to use OpenAI's API,
183+
please log into your account and generate
184+
the API key there, then paste the API key
185+
into the designated space below.
186+
187+
You can also click the "Generate API Key"
188+
below to open the login link to get the
189+
API key.
190+
191+
If you just want to test out the application,
192+
select the "Use Test API" button below.
193+
"""

openaiclient/view/frame/input/completioninput.py

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
CompletionInput class
33
"""
44
import tkinter as tk
5+
from tkinter import ttk
6+
from increment import Increment
57
from openaiclient.view.frame.baseframe import BaseFrame
68

79

@@ -17,19 +19,45 @@ def __init__(self, main, controller):
1719
)
1820

1921
def create(self):
22+
row = Increment()
23+
col = Increment()
24+
25+
self.addHorizSeparator(col, row)
26+
2027
tk.Label(
28+
self,
2129
text="Completion Endpoint",
22-
font=("", 10, "")
30+
font=("", 14, "")
2331
).grid(
24-
column=0,
25-
row=1,
26-
pady=10
32+
column=col,
33+
row=row,
34+
pady=0
2735
)
36+
37+
+row
38+
39+
self.addHorizSeparator(col, row)
40+
41+
"""
42+
tk.Button(
43+
self,
44+
text="test"
45+
).grid(
46+
column=col,
47+
row=row,
48+
pady=10,
49+
padx=10,
50+
sticky=tk.W
51+
)
52+
"""
53+
54+
+row
2855

2956
self._prompt.grid(
30-
column=0,
31-
row=1,
57+
column=col,
58+
row=row,
3259
padx=10,
60+
pady=5
3361
)
3462

3563
"""
@@ -38,14 +66,16 @@ def create(self):
3866
self.underlineUpdate
3967
)
4068
"""
69+
70+
+row
4171

4272
tk.Button(
4373
master=self,
4474
text="Send",
4575
command=self.sendInput
4676
).grid(
47-
column=0,
48-
row=2,
77+
column=col,
78+
row=row,
4979
padx=10,
5080
pady=10,
5181
ipadx=40
@@ -54,6 +84,23 @@ def create(self):
5484
self.tags()
5585

5686
return self
87+
88+
def addHorizSeparator(self, col, row):
89+
~col
90+
91+
ttk.Separator(
92+
self,
93+
orient="horizontal",
94+
).grid(
95+
column=col,
96+
row=row,
97+
columnspan=2,
98+
sticky=tk.W+tk.E,
99+
padx=10,
100+
pady=5
101+
)
102+
103+
+row
57104

58105
def tags(self):
59106
pass

0 commit comments

Comments
 (0)