Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslavyaroslav committed Apr 25, 2023
2 parents 6243a6b + a624ccd commit 40332a9
Show file tree
Hide file tree
Showing 18 changed files with 633 additions and 216 deletions.
27 changes: 24 additions & 3 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
[
{
"caption": "OpenAI Complete",
"caption": "OpenAI: Complete",
"command": "openai",
"args": {
"mode": "completion"
}
},
{
"caption": "OpenAI Insert",
"caption": "OpenAI: New Message",
"command": "openai",
"args": {
"mode": "chat_completion"
}
},
{
"caption": "OpenAI: Reset Chat History",
"command": "openai",
"args": {
"mode": "reset_chat_history"
}
},
{
"caption": "OpenAI: Refresh Chat",
"command": "openai",
"args": {
"mode": "refresh_output_panel"
}
},
{
"caption": "OpenAI: Insert",
"command": "openai",
"args": {
"mode": "insertion"
}
},
{
"caption": "OpenAI Edit",
"caption": "OpenAI: Edit",
"command": "openai",
"args": {
"mode": "edition"
Expand Down
94 changes: 82 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,40 @@ OpenAI Completion is a Sublime Text 4 plugin that uses the OpenAI natural langua
- Append suggested text to selected code
- Insert suggested text instead of placeholder in selected code
- Edit selected code according to a given command
- **ChatGPT mode support**.
- [Multi]Markdown syntax with syntax highlight support (ChatGPT mode only).
- Proxy support.
- **GPT-4 support**.

### ChatGPT completion demo

### Demo
<details><summary>Click to see screens</summary>
![](static/image1.png)

![](static/chatgpt_completion/image1.png)

---
![](static/image2.png)
![](static/chatgpt_completion/image2.png)

---
![](static/image3.png)
![](static/chatgpt_completion/image3.png)

</details>

### Simple completion demo

<details><summary>Click to see screens</summary>

![](static/simple_completion/image1.png)

---
![](static/image4.png)
![](static/simple_completion/image2.png)

---
![](static/simple_completion/image3.png)

---
![](static/simple_completion/image4.png)

</details>

## Requirements
Expand All @@ -29,24 +50,73 @@ OpenAI Completion is a Sublime Text 4 plugin that uses the OpenAI natural langua
- Internet connection

## Usage

### ChatGPT usage

ChatGPT mode works the following way:
1. Run the `OpenAI: New Message` command
2. Wait until OpenAI performs a response (be VERY patient in the case of the GPT-4 model it's way slower than you could imagine).
3. On the Response plugin opens the `OpenAI completion` output panel with the whole log of your chat at [any] active Window.
4. If you would like to fetch chat history to another window manually, you can do that by running the `OpenAI: Refresh Chat` command.
5. When you're done or want to start all over you should run the `OpenAI: Reset Chat History` command, which deletes the chat cache.

> **Note**
> You can bind both of the most usable commands `OpenAI: New Message` and `OpenAI: Show output panel`, to do that please follow `Settings` -> `Package Control` -> `OpenAI completion` -> `Key Bindings`.
> **Note**
> As for now there's just a single history instance. I guess this limitation would disappear sometime.
### Single shot completion usage

1. Open the Sublime Text 4 editor and select some code.
2. Open the command palette and run the `OpenAI Append`, `OpenAI Insert`, or `OpenAI Edit` command.
- To use the `OpenAI Insert` command, the selected code should include a placeholder `[insert]`. This can be modified in the settings.
2. Open the command palette and run the `OpenAI: Complete`, `OpenAI: Insert`, or `OpenAI: Edit` commands.
- To use the `OpenAI: Insert` command, the selected code should include a placeholder `[insert]`. This can be modified in the settings.
3. **The plugin will send the selected code to the OpenAI servers**, using your API key, to generate a suggestion for editing the code.
4. The suggestion will modify the selected code in the editor, according to the command you ran (append, insert, or edit).

### Other features

### [Multi]Markdown syntax with syntax highlight support

ChatGPT output panel supports markdown syntax highlight. It should just work (if it's not please report an issue).

Although it's highly recommended to install the [`MultimarkdownEditing`](https://sublimetext-markdown.github.io/MarkdownEditing/) to apply syntax highlighting for code snippets provided by ChatGPT. `OpenAI completion` should just pick it up implicitly for the output panel content.

### Proxy support

That's it. Now you can set up a proxy for this plugin.
You can setup it up by overriding the proxy property in the `OpenAI completion` settings like follow:

```json
"proxy": {
"address": "127.0.0.1",
"port": 9898
}
```

### GPT-4 support

> **Note**
> You have to have access to the `GPT-4` model within your account, to use that feature.
It should just work, just set the `chat_model` setting to `GPT-4`. Please be patient while working with it. (1) It's **very** slow and (2) an answer would appear only after it finishes its prompt. It could take up to 10 seconds easily.


## Settings
The OpenAI Completion plugin has a settings file where you can set your OpenAI API key. This is required for the plugin to work. To set your API key, open the settings within `Preferences` -> `Package Settings` -> `OpenAI` -> `Settings` and paste your API key in the token property, as follows:

```JSON
{
"token": "sk-your-token",
}
```

## Note
Please note that OpenAI is a paid service, and you will need to have an API key and sufficient credit to use the plugin.
## Disclaimers

> **Note**
> Please note that OpenAI is a paid service, and you will need to have an API key and sufficient credit to use this plugin.
Additionally, **all selected code will be sent to the OpenAI servers for processing, so make sure you have the necessary permissions to do so**.
> **Warning**
> **All selected code will be sent to the OpenAI servers for processing, so make sure you have all necessary permissions to do so**.
## Disclamer
This one was at 80% written by that thing itself including this readme. I was here mostly for debugging purposes, rather then designing and researching. This is pure magic, i swear.
> This one was at 80% written by that thing itself including this readme. I was here mostly for debugging purposes, rather than designing and researching. This is pure magic, I swear.
46 changes: 46 additions & 0 deletions cacher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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.history_file = os.path.join(plugin_cache_dir, "chat_history.jl")

def read_all(self):
json_objects = []
reader = jl.reader(self.history_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.history_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.history_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.history_file, "w") as file:
file.writelines(lines)

def drop_all(self):
with open(self.history_file, "w") as _:
pass # Truncate the file by opening it in 'w' mode and doing nothing
38 changes: 38 additions & 0 deletions jl_utility.py
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()
3 changes: 3 additions & 0 deletions messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"2.0.0": "messages/2.0.0.txt"
}
50 changes: 50 additions & 0 deletions messages/2.0.0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
=> 2.0.0

# Features summary
- ChatGPT mode support.
- [Multi]Markdown syntax with syntax highlight support (ChatGPT mode only).
- Proxy support.
- GPT-4 support.

## ChatGPT mode

ChatGPT mode works the following way:
1. Run the `OpenAI: New Message` command
2. Wait until OpenAI performs a response (be VERY patient in the case of the GPT-4 model it's way slower than you could imagine).
3. On the Response plugin opens the `OpenAI completion` output panel with the whole log of your chat at [any] active Window.
4. If you would like to fetch chat history to another window manually, you can do that by running the `OpenAI: Refresh Chat` command.
5. When you're done or want to start all over you should run the `OpenAI: Reset Chat History` command, which deletes the chat cache.

> You can bind both of the most usable commands `OpenAI: New Message` and `OpenAI: Show output panel`, to do that please follow `Settings`->`Package Control`->`OpenAI completion`->`Key Bindings`.

> As for now there's just a single history instance. I guess this limitation would disappear sometime, but highly likely it wouldn't be soon.

## [Multi]Markdown syntax with syntax highlight support (ChatGPT mode only).

ChatGPT output panel supports markdown syntax highlight. It should just work (if it's not please report an issue).

Although it's highly recommended to install the [`MultimarkdownEditing`](https://sublimetext-markdown.github.io/MarkdownEditing/) to apply syntax highlighting for code snippets provided by ChatGPT. `OpenAI completion` should just pick it up implicitly for the output panel content.

## Proxy support

That's it. Now you can set up a proxy for this plugin.
You can setup it up by overriding the proxy property in the `OpenAI completion` settings like follow:

```json
// Proxy setting
"proxy": {
// Proxy address
"address": "127.0.0.1",

// Proxy port
"port": 9898
}
```

## GPT-4 support

It should just work, just set the `chat_model` setting to `GPT-4`. Please be patient while working with it. (1) It's **very** slow and an answer would appear only after it finishes its prompt. It could take up to 10 seconds easily.

## Disclaimer

Unfortunately, this version hasn't got covered with comprehensive testing, so there could be bugs. Please report them, so I'd be happy to release a patch.
48 changes: 48 additions & 0 deletions messages/install.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Features summary
- ChatGPT mode support.
- [Multi]Markdown syntax with syntax highlight support (ChatGPT mode only).
- Proxy support.
- GPT-4 support.

## ChatGPT mode

ChatGPT mode works the following way:
1. Run the `OpenAI: New Message` command
2. Wait until OpenAI performs a response (be VERY patient in the case of the GPT-4 model it's way slower than you could imagine).
3. On the Response plugin opens the `OpenAI completion` output panel with the whole log of your chat at [any] active Window.
4. If you would like to fetch chat history to another window manually, you can do that by running the `OpenAI: Refresh Chat` command.
5. When you're done or want to start all over you should run the `OpenAI: Reset Chat History` command, which deletes the chat cache.

> You can bind both of the most usable commands `OpenAI: New Message` and `OpenAI: Show output panel`, to do that please follow `Settings`->`Package Control`->`OpenAI completion`->`Key Bindings`.

> As for now there's just a single history instance. I guess this limitation would disappear sometime, but highly likely it wouldn't be soon.

## [Multi]Markdown syntax with syntax highlight support (ChatGPT mode only).

ChatGPT output panel supports markdown syntax highlight. It should just work (if it's not please report an issue).

Although it's highly recommended to install the [`MultimarkdownEditing`](https://sublimetext-markdown.github.io/MarkdownEditing/) to apply syntax highlighting for code snippets provided by ChatGPT. `OpenAI completion` should just pick it up implicitly for the output panel content.

## Proxy support

That's it. Now you can set up a proxy for this plugin.
You can setup it up by overriding the proxy property in the `OpenAI completion` settings like follow:

```json
// Proxy setting
"proxy": {
// Proxy address
"address": "127.0.0.1",

// Proxy port
"port": 9898
}
```

## GPT-4 support

It should just work, just set the `chat_model` setting to `GPT-4`. Please be patient while working with it. (1) It's **very** slow and an answer would appear only after it finishes its prompt. It could take up to 10 seconds easily.

## Disclaimer

Unfortunately, this version hasn't got covered with comprehensive testing, so there could be bugs. Please report them, so I'd be happy to release a patch.
Loading

0 comments on commit 40332a9

Please sign in to comment.