Skip to content

Commit ac5f925

Browse files
committed
Added context file storage and retrieval features.
1 parent cc4fdfd commit ac5f925

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

src/main.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import requests
55
import logging
66
import sys
7+
import json
78
import tiktoken
89

910
import matplotlib.pyplot as plt
@@ -179,15 +180,56 @@ def plot_rsp_times(self):
179180
ax.set_ylabel("Response Time (s)")
180181
ax.set_title("ChatGPT API Response Time")
181182
ax.tick_params(axis='x', rotation=45)
182-
ax.xaxis.labelpad = 35
183+
ax.xaxis.labelpad = 85
183184
ax.yaxis.labelpad = 35
184185

185186
# Increase the bottom and left margins of the plot
186-
plt.subplots_adjust(bottom=0.2, left=0.15, fontsize=8)
187+
plt.subplots_adjust(bottom=0.2, left=0.15)
188+
189+
plt.xticks(rotation=45, fontsize=6)
187190

188191
# Save the figure as a PNG file
189192
plt.savefig("response_times.png")
190193

191194
# Show the plot
192195
plt.show()
193196

197+
def dump_context_to_a_file(self, filename="context"):
198+
self.dicts_to_jsonl(data_list=self.prev_msgs, filename=filename)
199+
200+
201+
def dicts_to_jsonl(self, data_list: list, filename: str, compress: bool = True) -> None:
202+
"""
203+
Method saves list of dicts into jsonl file.
204+
:param data_list: (list) list of dicts to be stored,
205+
:param filename: (str) path to the output file. If suffix .jsonl is not given then methods appends
206+
.jsonl suffix into the file.
207+
:param compress: (bool) should file be compressed into a gzip archive?
208+
"""
209+
sjsonl = '.jsonl'
210+
211+
# Check filename
212+
if not filename.endswith(sjsonl):
213+
filename = filename + sjsonl
214+
215+
# Save data
216+
with open(filename, 'w') as out:
217+
for ddict in data_list:
218+
jout = json.dumps(ddict) + '\n'
219+
out.write(jout)
220+
221+
def load_context_from_a_file(self, file):
222+
223+
sjsonl = '.jsonl'
224+
225+
# Check filename
226+
if not filename.endswith(sjsonl):
227+
filename = filename + sjsonl
228+
229+
self.prev_msgs = []
230+
231+
with open(file, encoding='utf-8') as json_file:
232+
for line in json_file.readlines():
233+
self.prev_msgs.append(line)
234+
235+
return self.prev_msgs

tests/main.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import requests
55
import logging
66
import sys
7+
import json
78
import tiktoken
89

910
import matplotlib.pyplot as plt
@@ -187,9 +188,49 @@ def plot_rsp_times(self):
187188

188189
plt.xticks(rotation=45, fontsize=6)
189190

191+
190192
# Save the figure as a PNG file
191193
plt.savefig("response_times.png")
192194

193195
# Show the plot
194196
plt.show()
195197

198+
def dump_context_to_a_file(self, filename="context"):
199+
self.dicts_to_jsonl(data_list=self.prev_msgs, filename=filename)
200+
201+
202+
def dicts_to_jsonl(self, data_list: list, filename: str, compress: bool = True) -> None:
203+
"""
204+
Method saves list of dicts into jsonl file.
205+
:param data_list: (list) list of dicts to be stored,
206+
:param filename: (str) path to the output file. If suffix .jsonl is not given then methods appends
207+
.jsonl suffix into the file.
208+
:param compress: (bool) should file be compressed into a gzip archive?
209+
"""
210+
sjsonl = '.jsonl'
211+
212+
# Check filename
213+
if not filename.endswith(sjsonl):
214+
filename = filename + sjsonl
215+
216+
# Save data
217+
with open(filename, 'w') as out:
218+
for ddict in data_list:
219+
jout = json.dumps(ddict) + '\n'
220+
out.write(jout)
221+
222+
def load_context_from_a_file(self, filename):
223+
224+
sjsonl = '.jsonl'
225+
226+
# Check filename
227+
if not filename.endswith(sjsonl):
228+
filename = filename + sjsonl
229+
230+
self.prev_msgs = []
231+
232+
with open(filename, encoding='utf-8') as json_file:
233+
for line in json_file.readlines():
234+
self.prev_msgs.append(line)
235+
236+
return self.prev_msgs

tests/test_main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
chatgptsmtclient.query("List the top 10 upcoming startups in India?")
1111
chatgptsmtclient.query("Ok thanks, can you giv me the valuation of these startups in tabuar format")
1212

13+
chatgptsmtclient.dump_context_to_a_file("context")
14+
chatgptsmtclient.load_context_from_a_file("context")
15+
1316
chatgptsmtclient.print_metrics()

0 commit comments

Comments
 (0)