emacs-openai / openai

Elisp library for the OpenAI API
GNU General Public License v3.0
100 stars 17 forks source link

Awkward behavior due to openai-completion-select-insert calling (forward-paragraph) #20

Open rommeswi opened 1 year ago

rommeswi commented 1 year ago

If you use openai-completion-select-insert on a selected paragraph, then the response will be inserted after the following paragraph instead of directly after the paragraph.

This happens because openai-completion-select-insert calls (forward-paragraph) before insertion. Of course it is equally awkward if after selecting only part of a paragraph and sending it to openai, the result appears in the middle of the paragraph, so just removing (forward-paragraph) would not be good.

I do not know an elegant solution to this issue. Perhaps it is possible to pass an optional argument to openai-completion-select-insert not to skip to the next paragraph? Similar issues may arise due to newline characters being added (in some use cases these might not be desirable). If one could pass optional arguments such as "prefix", "suffix" (which default to "\n") and "position" (which defaults to "(goto-char end) (forward-paragraph)"), then this would allow users to use for example advice-add to fine-tune the placement of the response.

jcs090218 commented 1 year ago

Yeah, you are correct. I've re-implemented it in 6a1d270fcdf89226f22122df930afe17f85d3209, so it works better in most circumstances. Basically, it will always insert the output with newlines at the front and at the end. This way user can see the output better and know what to do later on (remove it? or merge it into the paragraph? etc). Can you try the latest version? Thanks!

rommeswi commented 1 year ago

I tested the new version and found that if I submit a statement such as (1) "Define a Hamel basis in R^2.\n" or (2) "Define a Hamel basis in R^2.", the new version inserts the response to (1) before "^2" and to (2) after "^2" but before "."

jcs090218 commented 1 year ago

Can you paste a screenshot or steps to reproduce the issue? It's hard to imagine the error by reading the description. Thanks!

rommeswi commented 1 year ago

Write "Define a Hamel basis in R^2." into the scratch buffer. Mark the entire text above without any newline. Call "openai-completion-select-insert" command.

Observed behavior: Buffer changes to: "Define a Hamel basis in R^2

A Hamel basis in R^2 is any set of two linearly independent vectors that span the entire space. For example, a Hamel basis in R^2 can be chosen as {(1,0), (0,1)}.

."

Notice the extra dot at the end of the text which is now missing from the end of the original text. The behavior becomes even stranger if one marks "Define a Hamel basis in R^2.\n".

jcs090218 commented 1 year ago

This is the result on my end:

Define a Hamel basis in R^2.

A Hamel basis for R^2 is an unordered set of two linearly independent vectors
{v1, v2} such that no vector in the set can be expressed as a linear combination
of the other vectors. For example, a Hamel basis in R^2 could be {v1 = (1,2), v2
= (3,4)}.

It seems fine to me. 🤔

rommeswi commented 1 year ago

I wonder what the issue is. Must be something related to spacemacs I would guess. Somehow both the chatgpt and this package don't work well with each other.