jeffdapaz / VisualChatGPTStudio

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

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

Open jeffdapaz opened 1 year ago

jeffdapaz commented 1 year ago

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

graham83 commented 1 year ago

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)

jeffdapaz commented 1 year ago

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#.

graham83 commented 1 year ago

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:

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.

jeffdapaz commented 1 year ago

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.

graham83 commented 1 year ago

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>
jeffdapaz commented 1 year ago

Seems like an excellent solution.