VisualChatGPTStudio icon indicating copy to clipboard operation
VisualChatGPTStudio copied to clipboard

Add code completion using chatGPT like Code Snippets and/or "Display statement completion"

Open jeffdapaz opened this issue 2 years ago • 6 comments

The ideia is enter the prompt within the Editor similar to code snippet flow. Then select output from ChatGPT for statement completion, or modify prompt to regenerate response (using same chat thread). Similar to implementing code snippets and / or statement completion.

See:

https://learn.microsoft.com/en-us/visualstudio/extensibility/walkthrough-implementing-code-snippets?view=vs-2022&tabs=csharp

https://learn.microsoft.com/en-us/visualstudio/extensibility/walkthrough-displaying-statement-completion?view=vs-2022&tabs=csharp

jeffdapaz avatar Mar 10 '23 11:03 jeffdapaz

My initial thought was to use the ChatGPT endpoint however OpenAI still recommend using their codex model. I see there are new ways to provide Codex model with more context to the completion.

For inserting code provide prompt (before insert) and suffix (after insert) ... response = openai.Completion.create( model="code-davinci-002", prompt="def foo(bar):\n", suffix="\n print(bar)", temperature=0, max_tokens=256, top_p=1, frequency_penalty=0, presence_penalty=0 )

For editing code provide input (code) and instruction (refactor to do XYZ) and hit the Edit endpoint instead ... response = openai.Edit.create( model="code-davinci-edit-001", input="print(names)", instruction="Print each character on a new line", temperature=0, top_p=1 )

Another change is can apparently use the Edit endpoint for Completions with empty Input and it will be used as a Completion (https://platform.openai.com/docs/guides/code/editing-code)

graham83 avatar Mar 20 '23 06:03 graham83

I could be wrong, but I think it's not possible to use the model codex for C#.

I performed some tests through the OpenAI playground and I didn't have good results, the answers were always in the javascript language.

Unless I missed something for the model codex can "understand" C#.

jeffdapaz avatar Mar 20 '23 13:03 jeffdapaz

Currently we are using Codex anytime we use either code-davinci-002 and I understand that chatgpt does use the codex model too. I 'feel' with C# that ChatGPT is being way more useful for code completion and recommendation then basic code completion using codex. This is probably due to the InstructGPT model allowing us to set context and instruct the model on what we want.

Some changes I am happy to look into then:

  • Add ChatGPT model (in preparation for GPT4 access) and use the ChatRequest interface provided by OpenAI nuget package to create request. Parse response so that we only insert code blocks into editor. Ideally we can output additional information to the Visual Chat GPT panel, and accept input from the Code Completion / Snippet input.
  • Add ability to edit the ChatGPT response code - which means need to maintain conversation in memory somehow or recall the last code block inserted from a ChatGPT response as input to the follow up prompt.

All very exciting if can get it to work. I really like your editor interface so would be good to have the ChatGPT conversation with code generation all output to the Editor. Perhaps can markup this code generation in #region blocks or something so can modify it. Will do some investigation.

graham83 avatar Mar 20 '23 23:03 graham83

Your ideas are very good.

Please feel free to do these analyzes and if you need anything from me just say so.

However I just published a new release that uses the turbo model. With it it is possible to keep the conversation in memory as you were suggesting. I even know that with GPT-4 this will also be possible.

So if you need to check how it works, take a look at the code.

jeffdapaz avatar Mar 21 '23 01:03 jeffdapaz

Great. I will rebase onto the turbo model. I have a prototype running where we hold a conversation with chatgpt endpoint in editor, parsing the response into comments, remarks and code. Have added a separate Command for resetting the conversation. Can also carry over the conversation into the Tool window. So before get too diverged from your work I will refactor to use the Turbo Model for holding the conversation.

Prompt:

// Initialize a dictionary with all words of the alphabet as key and a word starting with that letter as value

Output:

/// <summary>
/// To initialize a dictionary with all words of the alphabet as key and a word starting with that letter as value, you can use the following code:
/// 
/// 
/// </summary>
// Initialize a dictionary with all words of the alphabet as key and a word starting with that letter as value
var words = new List<(char, string)>()
{
    ('A', "Apple"),
    ('B', "Beast"),
    ('C', "Cat"),
    ('D', "Dog"),
    ('E', "Elephant"),
    ('F', "Fish"),
    ('G', "Giraffe"),
    ('H', "Horse"),
    ('I', "Iguana"),
    ('J', "Jaguar"),
    ('K', "Kangaroo"),
    ('L', "Lion"),
    ('M', "Monkey"),
    ('N', "Nightingale"),
    ('O', "Owl"),
    ('P', "Penguin"),
    ('Q', "Quail"),
    ('R', "Rabbit"),
    ('S', "Snake"),
    ('T', "Tiger"),
    ('U', "Umbrella"),
    ('V', "Vulture"),
    ('W', "Wolf"),
    ('X', "Xylophone"),
    ('Y', "Yak"),
    ('Z', "Zebra")
};

// Print the dictionary
foreach (var word in words)
{
    Console.WriteLine($"{word.Item1}: {word.Item2}");
}
/// <remarks>
/// This code initializes a list of tuples with the letters of the alphabet as the first item of each tuple and a word that starts with that letter as the second item of each tuple. It then prints out the contents of the list using a foreach loop. The word "Beast" has been used instead of "Banana".
/// </remarks>

graham83 avatar Mar 22 '23 00:03 graham83

Seems like an excellent solution.

jeffdapaz avatar Mar 22 '23 11:03 jeffdapaz

Closing because this functionality was added on release 3.0

jeffdapaz avatar Aug 15 '24 14:08 jeffdapaz