-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added new mode `chat_completion`. - Chat completion works only within output panel. - Chat completion stores chat history, both questions and answers (it's the way the ChatGPT API works). - Added command to clear chat history. - Chat messages inserts by input panel. - Additional setting for chat model added `chat_model`. - `multimarkdown` setting dropped as deprecated. - `output_panel` setting dropped as deprecated. - Global code refactoring.
- Loading branch information
1 parent
b89df64
commit a09bda2
Showing
6 changed files
with
350 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import sublime | ||
import os | ||
from . import jl_utility as jl | ||
|
||
|
||
class Cacher(): | ||
def __init__(self) -> None: | ||
cache_dir = sublime.cache_path() | ||
plugin_cache_dir = os.path.join(cache_dir, "OpenAI completion") | ||
if not os.path.exists(plugin_cache_dir): | ||
os.makedirs(plugin_cache_dir) | ||
|
||
# Create the file path to store the data | ||
self.cache_file = os.path.join(plugin_cache_dir, "chat_history.jl") | ||
|
||
|
||
def read_all(self): | ||
json_objects = [] | ||
reader = jl.reader(self.cache_file) | ||
for json_object in reader: | ||
json_objects.append(json_object) | ||
|
||
return json_objects | ||
|
||
def append_to_cache(self, cache_lines): | ||
# Create a new JSON Lines writer for output.jl | ||
writer = jl.writer(self.cache_file) | ||
next(writer) | ||
writer.send(cache_lines[0]) | ||
# for line in cache_lines: | ||
# writer.send(line) | ||
|
||
def drop_first(self, number = 4): | ||
# Read all lines from the JSON Lines file | ||
with open(self.cache_file, "r") as file: | ||
lines = file.readlines() | ||
|
||
# Remove the specified number of lines from the beginning | ||
lines = lines[number:] | ||
|
||
# Write the remaining lines back to the cache file | ||
with open(self.cache_file, "w") as file: | ||
file.writelines(lines) | ||
|
||
def drop_all(self): | ||
with open(self.cache_file, "w") as file: | ||
pass # Truncate the file by opening it in 'w' mode and doing nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import json | ||
from typing import Iterator, Generator | ||
|
||
|
||
def reader(fname: str) -> Iterator[dict]: | ||
with open(fname) as file: | ||
for line in file: | ||
obj = json.loads(line.strip()) | ||
yield obj | ||
|
||
|
||
def writer(fname: str, mode: str = 'a') -> Generator[None, dict, None]: | ||
with open(fname, mode) as file: | ||
while True: | ||
obj = yield | ||
line = json.dumps(obj, ensure_ascii=False) | ||
file.write(f"{line}\n") | ||
|
||
|
||
# if __name__ == "__main__": | ||
# # Read employees from employees.jl | ||
# reader = jl_reader("employees.jl") | ||
|
||
# # Create a new JSON Lines writer for output.jl | ||
# writer = jl_writer("output.jl") | ||
# next(writer) | ||
|
||
# for employee in reader: | ||
# id = employee["id"] | ||
# name = employee["name"] | ||
# dept = employee["department"] | ||
# print(f"#{id} - {name} ({dept})") | ||
|
||
# # Write the employee data to output.jl | ||
# writer.send(employee) | ||
|
||
# # Close the writer | ||
# writer.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.