jeffdapaz / VisualChatGPTStudio

Add chatGPT functionalities directly on Visual Studio
https://marketplace.visualstudio.com/items?itemName=jefferson-pires.VisualChatGPTStudio
MIT License
186 stars 48 forks source link

Add Summary generates additional garbage code #25

Closed vgelmut closed 1 year ago

vgelmut commented 1 year ago

this is original code:

public class ByTransactionState : Specification<TransactionPosition>
            {
                private int stateId;

                public ByTransactionState(int stateId)
                {
                    this.stateId = stateId;
                }

after selecting first line and generating Summary (Alt+F7), it produced a Summary, but with additional lines:

            /// <summary>
            /// Specification for filtering TransactionPositions by their TransactionState.
            /// </summary>
            /// <param name="state">The TransactionState to filter by.</param>
            public ByTransactionState(TransactionState state)

        // ...

            // ...

public class ByTransactionState : Specification<TransactionPosition>
            {
                private int stateId;

                public ByTransactionState(int stateId)
                {
                    this.stateId = stateId;
                }

Seems like it starts adding some garbage after several Summary generations on classes. Works well on class Properties, Constructors.

jeffdapaz commented 1 year ago

This is because the OpenAI API itself that is responding in this way.

But I'll see if I can somehow stop this.

vgelmut commented 1 year ago

I'm not quite sure how this should work. I tried different ways to call Alt+7 (Generate Summary), but only selecting class definition line seems to work. Selecting whole class throwing an error about not able to proceed multiple objects (something like that).

It tries to proceed whole class which must be correct way. It would be awesome if it wouldn't require to select the line with the class definition, but just using class as a current cursor selection (same as R# or VS itself doing for example). I spotted that at some point garbage response additional to Summary block contains just a part of internal class code but partially broken. Is that what chatGPT responded? With what request? Is it possible to see what you are sending actually to chatGPT and what is the real response like the logs? It would be very helpful to see what's going on and may be rewrite it different way

jeffdapaz commented 1 year ago

Don't worry, I already have the fix 😉

Basically the command sent to the API is as follows:

The request defined in the options (by default it is "Only write a comment as C# summary format like"), plus a Summary example to make it clear to the API what I expect as a response, plus all the code that the user has selected for the API have a context to comment on.

In the end with your example the command would be this:

Only write a comment as C# summary format like

/// <summary>
/// 
/// </summary>

public class ByTransactionState : Specification<TransactionPosition>

However, with this command, the API sometimes believes that it needs to include the selected command in the response. That's why in my fix I added the word "for" to make it clear that I only want the Summary. So now the command looks like this:

Only write a comment as C# summary format like

/// <summary>
/// 
/// </summary>

for

public class ByTransactionState : Specification<TransactionPosition>

With this approach alone, the response has already improved by about 90%. However, from time to time I still received more than what was due as a response, and to definitively solve it now I am sending stop sequences (https://platform.openai.com/docs/api-reference/completions/create#completions/create-stop).

And as stop sequences I'm using "public, private and internal". So if API starts to rewrite the method for example, it stops responding.

About your other concerns. It is necessary to select the code to execute the commands because this is how I can capture the code that the user wants something to be done with it.

As for the selection limit, it is for some reasons, such as avoiding consuming too many tokens, avoiding exceeding the limit of tokens per request, knowing exactly where the API response should be written, etc.

Finally, I will publish the fix on next release.

vgelmut commented 1 year ago

@jeffdapaz it would be just helpful it would work on class/property or other item as selection would selecting a full line.