├── .gitignore ├── Copilot ├── CommandFilter.cs ├── InlinePredictionManager.cs ├── Suggestions.cs └── TextViewCreationListener.cs ├── LICENSE ├── OpenAI_API ├── APIAuthentication.cs ├── ApiResultBase.cs ├── Chat │ ├── ChatContent.cs │ ├── ChatEndpoint.cs │ ├── ChatMessage.cs │ ├── ChatMessageRole.cs │ ├── ChatRequest.cs │ ├── ChatResult.cs │ ├── Conversation.cs │ └── IChatEndpoint.cs ├── Completions │ ├── CompletionEndpoint.cs │ ├── CompletionRequest.cs │ ├── CompletionResult.cs │ └── ICompletionEndpoint.cs ├── Embedding │ ├── EmbeddingEndpoint.cs │ ├── EmbeddingRequest.cs │ ├── EmbeddingResult.cs │ └── IEmbeddingEndpoint.cs ├── EndpointBase.cs ├── Files │ ├── File.cs │ ├── FilesEndpoint.cs │ └── IFilesEndpoint.cs ├── Functions │ ├── FunctionRequest.cs │ └── FunctionResult.cs ├── IOpenAIAPI.cs ├── Images │ ├── IImageGenerationEndpoint.cs │ ├── ImageGenerationEndpoint.cs │ ├── ImageGenerationRequest.cs │ ├── ImageResponseFormat.cs │ ├── ImageResult.cs │ └── ImageSize.cs ├── Model │ ├── IModelsEndpoint.cs │ ├── Model.cs │ └── ModelsEndpoint.cs ├── Moderation │ ├── IModerationEndpoint.cs │ ├── ModerationEndpoint.cs │ ├── ModerationRequest.cs │ └── ModerationResult.cs ├── OpenAIAPI.cs ├── OpenAI_API.csproj └── Usage.cs ├── Properties └── AssemblyInfo.cs ├── README-zh.md ├── README.md ├── ReleaseNotes.md ├── Resources ├── DB.png ├── FileTypes │ ├── config.png │ ├── cs.png │ ├── css.png │ ├── folder.png │ ├── html.png │ ├── js.png │ ├── json.png │ ├── md.png │ ├── sln.png │ ├── sql.png │ ├── ts.png │ ├── vb.png │ ├── vs.png │ ├── xaml.png │ └── xml.png ├── Highlighting.xshd ├── Icon.png ├── addComments.png ├── addSummary.png ├── addSummaryForAll.png ├── addTests.png ├── api.png ├── askAnything.png ├── bot.png ├── cancel.png ├── cancelCommand.png ├── chatGPT.png ├── check.png ├── clear.png ├── close.png ├── cloud.png ├── codeReview.png ├── complete.png ├── copy.png ├── customAfter.png ├── customBefore.png ├── customReplace.png ├── diffView.png ├── edit_color.png ├── explain.png ├── findBugs.png ├── garbage.png ├── garbage_color.png ├── git.png ├── image.png ├── information.png ├── optimize.png ├── optimizeDiffView.png ├── paste.png ├── send.png ├── sendCode.png ├── solutionContext.png ├── translate.png ├── turbo.png ├── vs.png └── wordWrap.png ├── VSCommandTable.cs ├── VSCommandTable.vsct ├── VisualChatGPTStudio.csproj ├── VisualChatGPTStudio.sln ├── VisualChatGPTStudio2019 ├── .gitignore ├── Icon2019.png ├── Properties │ └── AssemblyInfo.cs ├── Resources │ └── Icon2019.png ├── VisualChatGPTStudio2019.csproj ├── source.extension.cs └── source.extension.vsixmanifest ├── VisualChatGPTStudioShared ├── Agents │ ├── ApiAgent │ │ ├── ApiAgent.cs │ │ ├── ApiItem.cs │ │ └── ApiTagItem.cs │ └── SqlServerAgent.cs ├── Commands │ ├── AddComments.cs │ ├── AddSummary.cs │ ├── AddSummaryForAll.cs │ ├── AddTests.cs │ ├── AskAnything.cs │ ├── BaseCommand.cs │ ├── BaseGenericCommand.cs │ ├── Cancel.cs │ ├── Complete.cs │ ├── CustomAfter.cs │ ├── CustomBefore.cs │ ├── CustomReplace.cs │ ├── Explain.cs │ ├── FindBugs.cs │ ├── Optimize.cs │ ├── OptimizeDiffView.cs │ ├── TerminalWindowCodeReviewCommand.cs │ ├── TerminalWindowCommand.cs │ ├── TerminalWindowSolutionContextCommand.cs │ ├── TerminalWindowTurboCommand.cs │ └── Translate.cs ├── Options │ ├── ApiAgent │ │ ├── OptionApiAgent.cs │ │ ├── OptionApiAgentWindow.xaml │ │ └── OptionApiAgentWindow.xaml.cs │ ├── Commands │ │ ├── Commands.cs │ │ ├── OptionCommands.cs │ │ ├── OptionCommandsWindow.xaml │ │ └── OptionCommandsWindow.xaml.cs │ └── OptionPageGrid.cs ├── ToolWindows │ ├── CodeReview │ │ ├── CodeReviewItem.cs │ │ ├── TerminalWindowCodeReview.cs │ │ ├── TerminalWindowCodeReviewControl.cs │ │ └── TerminalWindowCodeReviewControl.xaml │ ├── SolutionContext │ │ ├── TerminalWindowSolutionContext.cs │ │ ├── TerminalWindowSolutionContextControl.cs │ │ └── TerminalWindowSolutionContextControl.xaml │ ├── TerminalWindow │ │ ├── TerminalWindow.cs │ │ ├── TerminalWindowControl.xaml │ │ └── TerminalWindowControl.xaml.cs │ ├── TerminalWindowHelper.cs │ └── Turbo │ │ ├── ChatEntity.cs │ │ ├── ChatListControlItem.cs │ │ ├── ChatMessageSegment.cs │ │ ├── ChatUserControlsItem.cs │ │ ├── TerminalWindowTurbo.cs │ │ ├── TerminalWindowTurboControl.xaml │ │ ├── TerminalWindowTurboControl.xaml.cs │ │ ├── ucChat.xaml │ │ ├── ucChat.xaml.cs │ │ ├── ucChatHeader.xaml │ │ ├── ucChatHeader.xaml.cs │ │ ├── ucChatItem.xaml │ │ └── ucChatItem.xaml.cs ├── Utils │ ├── ApiHandler.cs │ ├── AttachImage.cs │ ├── CodeCompletion │ │ ├── CompletionData.cs │ │ └── CompletionManager.cs │ ├── CommandImage.cs │ ├── Constants.cs │ ├── DiffView.cs │ ├── EnumHelper.cs │ ├── Enums.cs │ ├── GitChanges.cs │ ├── HighlightingDefinitionConverter.cs │ ├── Http │ │ ├── ChatGPTHttpClientFactory.cs │ │ ├── HttpClientCustom.cs │ │ ├── HttpLogs.cs │ │ └── RequestCaptureHandler.cs │ ├── Repositories │ │ ├── ApiAgentRepository.cs │ │ ├── ChatRepository.cs │ │ └── Repository.cs │ └── TextFormat.cs ├── VisualChatGPTStudioShared.projitems ├── VisualChatGPTStudioShared.shproj └── VisuallChatGPTStudioPackage.cs ├── icons8-chatgpt-512.png ├── lib ├── osx-arm64 │ └── libgit2-a2bde63.dylib └── win32 │ ├── arm64 │ └── git2-a2bde63.dll │ ├── x64 │ └── git2-a2bde63.dll │ └── x86 │ └── git2-a2bde63.dll ├── source.extension.cs └── source.extension.vsixmanifest /Copilot/TextViewCreationListener.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.Editor; 2 | using Microsoft.VisualStudio.Shell; 3 | using Microsoft.VisualStudio.Shell.Interop; 4 | using Microsoft.VisualStudio.Text.Editor; 5 | using Microsoft.VisualStudio.TextManager.Interop; 6 | using Microsoft.VisualStudio.Utilities; 7 | using System.ComponentModel.Composition; 8 | using System.Threading.Tasks; 9 | 10 | namespace JeffPires.VisualChatGPTStudio.Copilot 11 | { 12 | /// 13 | /// Exports the IVsTextViewCreationListener to handle the creation of text views in Visual Studio. 14 | /// 15 | [Export(typeof(IVsTextViewCreationListener))] 16 | [ContentType("code")] 17 | [TextViewRole(PredefinedTextViewRoles.Editable)] 18 | internal class TextViewCreationListener : IVsTextViewCreationListener 19 | { 20 | /// 21 | /// Gets or sets the IVsEditorAdaptersFactoryService instance used to create and manage editor adapters. 22 | /// 23 | [Import] 24 | internal IVsEditorAdaptersFactoryService AdapterService { get; set; } 25 | 26 | /// 27 | /// Handles the creation of a Visual Studio text view, initializing necessary components such as the prediction manager and command filter. 28 | /// 29 | public void VsTextViewCreated(IVsTextView textViewAdapter) 30 | { 31 | IWpfTextView view = AdapterService.GetWpfTextView(textViewAdapter); 32 | 33 | if (view == null) 34 | { 35 | return; 36 | } 37 | 38 | VisuallChatGPTStudioPackage package = GetPackageAsync().Result; 39 | 40 | if (package == null) 41 | { 42 | return; 43 | } 44 | 45 | InlinePredictionManager predictionManager = new(view); 46 | 47 | CommandFilter commandFilter = new(view, predictionManager, package.OptionsGeneral); 48 | commandFilter.AttachToView(textViewAdapter); 49 | } 50 | 51 | /// 52 | /// Asynchronously retrieves the VisuallChatGPTStudioPackage instance. 53 | /// 54 | /// 55 | /// The task result contains the VisuallChatGPTStudioPackage instance if found; otherwise, null. 56 | /// 57 | private async Task GetPackageAsync() 58 | { 59 | await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); 60 | 61 | IVsShell shell = (IVsShell)ServiceProvider.GlobalProvider.GetService(typeof(SVsShell)); 62 | 63 | if (shell != null) 64 | { 65 | shell.LoadPackage(ref PackageGuids.VisuallChatGPTStudio, out IVsPackage package); 66 | 67 | return package as VisuallChatGPTStudioPackage; 68 | } 69 | 70 | return null; 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Jefferson Pires 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /OpenAI_API/ApiResultBase.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using OpenAI_API.Models; 3 | using System; 4 | 5 | namespace OpenAI_API 6 | { 7 | /// 8 | /// Represents a result from calling the OpenAI API, with all the common metadata returned from every endpoint 9 | /// 10 | abstract public class ApiResultBase 11 | { 12 | 13 | /// The time when the result was generated 14 | [JsonIgnore] 15 | public DateTime? Created => CreatedUnixTime.HasValue ? (DateTime?)(DateTimeOffset.FromUnixTimeSeconds(CreatedUnixTime.Value).DateTime) : null; 16 | 17 | /// 18 | /// The time when the result was generated in unix epoch format 19 | /// 20 | [JsonProperty("created")] 21 | public long? CreatedUnixTime { get; set; } 22 | 23 | /// 24 | /// Which model was used to generate this result. 25 | /// 26 | [JsonProperty("model")] 27 | public Model Model { get; set; } 28 | 29 | /// 30 | /// Object type, ie: text_completion, file, fine-tune, list, etc 31 | /// 32 | [JsonProperty("object")] 33 | public string Object { get; set; } 34 | 35 | /// 36 | /// The organization associated with the API request, as reported by the API. 37 | /// 38 | [JsonIgnore] 39 | public string Organization { get; internal set; } 40 | 41 | /// 42 | /// The server-side processing time as reported by the API. This can be useful for debugging where a delay occurs. 43 | /// 44 | [JsonIgnore] 45 | public TimeSpan ProcessingTime { get; internal set; } 46 | 47 | /// 48 | /// The request id of this API call, as reported in the response headers. This may be useful for troubleshooting or when contacting OpenAI support in reference to a specific request. 49 | /// 50 | [JsonIgnore] 51 | public string RequestId { get; internal set; } 52 | 53 | /// 54 | /// The Openai-Version used to generate this response, as reported in the response headers. This may be useful for troubleshooting or when contacting OpenAI support in reference to a specific request. 55 | /// 56 | [JsonIgnore] 57 | public string OpenaiVersion { get; internal set; } 58 | } 59 | } -------------------------------------------------------------------------------- /OpenAI_API/Chat/ChatContent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System; 3 | 4 | namespace OpenAI_API.Chat 5 | { 6 | /// 7 | /// Represents the base class for chat content, providing a common type identifier. 8 | /// 9 | public abstract class ChatContentBase 10 | { 11 | /// 12 | /// Represents the type of the object, initialized with a default value. 13 | /// 14 | [JsonProperty("type")] 15 | public string Type { get; private set; } 16 | 17 | /// 18 | /// Initializes a new instance of the ChatContentBase class with the specified type. 19 | /// 20 | /// The type of the chat content. 21 | public ChatContentBase(string type) 22 | { 23 | Type = type; 24 | } 25 | } 26 | 27 | /// 28 | /// Represents the content of a chat message in text format. 29 | /// Inherits from the ChatContentBase class, specifying the content type as "text". 30 | /// 31 | public class ChatContentForText : ChatContentBase 32 | { 33 | /// 34 | /// Represents a text property that is serialized to JSON with the key "text". 35 | /// 36 | [JsonProperty("text")] 37 | public string Text { get; set; } 38 | 39 | /// 40 | /// Initializes a new instance of the ChatContentForText class with the specified text. 41 | /// 42 | /// The text content to initialize the instance with. 43 | public ChatContentForText(string text) : base(text) 44 | { 45 | Text = text; 46 | } 47 | } 48 | 49 | /// 50 | /// Represents the content of a chat message that contains an image, 51 | /// initialized with the specified image data. 52 | /// 53 | public class ChatContentForImage : ChatContentBase 54 | { 55 | /// 56 | /// Represents the URL of an image, initialized with a base64 encoded JPEG image data. 57 | /// 58 | [JsonProperty("image_url")] 59 | public ImageUrl ImageUrl { get; private set; } 60 | 61 | /// 62 | /// Initializes a new instance of the ChatContentForImage class with the provided image data. 63 | /// Converts the byte array to a Base64 string and constructs an image URL in the "data:image/jpeg;base64" format. 64 | /// 65 | /// The byte array representing the image data. 66 | public ChatContentForImage(byte[] imageData) : base("image_url") 67 | { 68 | ImageUrl = new ImageUrl("data:image/jpeg;base64," + Convert.ToBase64String(imageData)); 69 | } 70 | } 71 | 72 | /// 73 | /// Represents an image URL with a specified URL string. 74 | /// 75 | public class ImageUrl 76 | { 77 | /// 78 | /// Represents a URL property that can be serialized to and from JSON. 79 | /// 80 | [JsonProperty("url")] 81 | public string Url { get; set; } 82 | 83 | /// 84 | /// Initializes a new instance of the ImageUrl class with the specified URL. 85 | /// 86 | /// The URL of the image. 87 | public ImageUrl(string url) 88 | { 89 | Url = url; 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /OpenAI_API/Chat/ChatMessage.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using OpenAI_API.Functions; 3 | using System.Collections.Generic; 4 | 5 | namespace OpenAI_API.Chat 6 | { 7 | /// 8 | /// Chat message sent or received from the API. Includes who is speaking in the "role" and the message text in the "content" 9 | /// 10 | public class ChatMessage 11 | { 12 | /// 13 | /// Creates an empty , with defaulting to 14 | /// 15 | public ChatMessage() 16 | { 17 | this.Role = ChatMessageRole.User; 18 | } 19 | 20 | /// 21 | /// Initializes a new instance of the ChatMessage class with the specified role and content. 22 | /// 23 | /// The role associated with the chat message (e.g., user, system, assistant). 24 | /// The content of the chat message. 25 | public ChatMessage(ChatMessageRole role, object content) 26 | { 27 | this.Role = role; 28 | this.Content = content; 29 | } 30 | 31 | /// 32 | /// Initializes a new instance of the ChatMessage class with the specified role, content, and function ID. 33 | /// 34 | /// The role associated with the chat message. 35 | /// The content of the chat message. 36 | /// The identifier for the function associated with the chat message. 37 | public ChatMessage(ChatMessageRole role, object content, string functionId) 38 | { 39 | this.Role = role; 40 | this.Content = content; 41 | this.FunctionId = functionId; 42 | } 43 | 44 | [JsonProperty("role")] 45 | internal string rawRole { get; set; } 46 | 47 | /// 48 | /// The role of the message, which can be "system", "assistant" or "user" 49 | /// 50 | [JsonIgnore] 51 | public ChatMessageRole Role 52 | { 53 | get 54 | { 55 | return ChatMessageRole.FromString(rawRole); 56 | } 57 | set 58 | { 59 | rawRole = value.ToString(); 60 | } 61 | } 62 | 63 | /// 64 | /// The content of the message 65 | /// 66 | [JsonProperty("content")] 67 | public object Content { get; set; } 68 | 69 | /// 70 | /// An optional name of the user in a multi-user chat 71 | /// 72 | [JsonProperty("name")] 73 | public string Name { get; set; } 74 | 75 | /// 76 | /// Functions to be executed. 77 | /// 78 | [JsonProperty("tool_calls")] 79 | public IReadOnlyList Functions { get; set; } 80 | 81 | /// 82 | /// The function id. 83 | /// 84 | [JsonProperty("tool_call_id")] 85 | public string FunctionId { get; set; } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /OpenAI_API/Chat/ChatMessageRole.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OpenAI_API.Chat 4 | { 5 | /// 6 | /// Represents the Role of a . Typically, a conversation is formatted with a system message first, followed by alternating user and assistant messages. See the OpenAI docs for more details about usage. 7 | /// 8 | public class ChatMessageRole : IEquatable 9 | { 10 | /// 11 | /// Constructor is private to force usage of strongly typed values 12 | /// 13 | /// 14 | private ChatMessageRole(string value) { Value = value; } 15 | 16 | /// 17 | /// Gets the singleton instance of based on the string value. 18 | /// 19 | /// Muse be one of "system", "user", or "assistant" 20 | /// 21 | public static ChatMessageRole FromString(string roleName) 22 | { 23 | switch (roleName) 24 | { 25 | case "system": 26 | return ChatMessageRole.System; 27 | case "user": 28 | return ChatMessageRole.User; 29 | case "assistant": 30 | return ChatMessageRole.Assistant; 31 | case "tool": 32 | return ChatMessageRole.Tool; 33 | default: 34 | return null; 35 | } 36 | } 37 | 38 | private string Value { get; } 39 | 40 | /// 41 | /// The system message helps set the behavior of the assistant. 42 | /// 43 | public static ChatMessageRole System { get; } = new ChatMessageRole("system"); 44 | 45 | /// 46 | /// The user messages help instruct the assistant. They can be generated by the end users of an application, or set by a developer as an instruction. 47 | /// 48 | public static ChatMessageRole User { get; } = new ChatMessageRole("user"); 49 | 50 | /// 51 | /// The assistant messages help store prior responses. They can also be written by a developer to help give examples of desired behavior. 52 | /// 53 | public static ChatMessageRole Assistant { get; } = new ChatMessageRole("assistant"); 54 | 55 | /// 56 | /// The tool message to indicates a result of a function execution 57 | /// 58 | public static ChatMessageRole Tool { get; } = new ChatMessageRole("tool"); 59 | 60 | /// 61 | /// Gets the string value for this role to pass to the API 62 | /// 63 | /// The size as a string 64 | public override string ToString() 65 | { 66 | return Value; 67 | } 68 | 69 | /// 70 | /// Determines whether this instance and a specified object have the same value. 71 | /// 72 | /// The ChatMessageRole to compare to this instance 73 | /// true if obj is a ChatMessageRole and its value is the same as this instance; otherwise, false. If obj is null, the method returns false 74 | public override bool Equals(object obj) 75 | { 76 | return Value.Equals((obj as ChatMessageRole).Value); 77 | } 78 | 79 | /// 80 | /// Returns the hash code for this object 81 | /// 82 | /// A 32-bit signed integer hash code 83 | public override int GetHashCode() 84 | { 85 | return Value.GetHashCode(); 86 | } 87 | 88 | /// 89 | /// Determines whether this instance and a specified object have the same value. 90 | /// 91 | /// The ChatMessageRole to compare to this instance 92 | /// true if other's value is the same as this instance; otherwise, false. If other is null, the method returns false 93 | public bool Equals(ChatMessageRole other) 94 | { 95 | return Value.Equals(other.Value); 96 | } 97 | 98 | /// 99 | /// Gets the string value for this role to pass to the API 100 | /// 101 | /// The ChatMessageRole to convert 102 | public static implicit operator String(ChatMessageRole value) { return value.Value; } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /OpenAI_API/Chat/ChatResult.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System.Collections.Generic; 3 | 4 | namespace OpenAI_API.Chat 5 | { 6 | /// 7 | /// Represents a result from calling the Chat API 8 | /// 9 | public class ChatResult : ApiResultBase 10 | { 11 | /// 12 | /// The identifier of the result, which may be used during troubleshooting 13 | /// 14 | [JsonProperty("id")] 15 | public string Id { get; set; } 16 | 17 | /// 18 | /// The list of choices that the user was presented with during the chat interaction 19 | /// 20 | [JsonProperty("choices")] 21 | public IReadOnlyList Choices { get; set; } 22 | 23 | /// 24 | /// The usage statistics for the chat interaction 25 | /// 26 | [JsonProperty("usage")] 27 | public ChatUsage Usage { get; set; } 28 | 29 | /// 30 | /// A convenience method to return the content of the message in the first choice of this response 31 | /// 32 | /// The content of the message, not including . 33 | public override string ToString() 34 | { 35 | if (Choices != null && Choices.Count > 0) 36 | { 37 | return Choices[0].ToString(); 38 | } 39 | else 40 | { 41 | return null; 42 | } 43 | } 44 | } 45 | 46 | /// 47 | /// A message received from the API, including the message text, index, and reason why the message finished. 48 | /// 49 | public class ChatChoice 50 | { 51 | /// 52 | /// The index of the choice in the list of choices 53 | /// 54 | [JsonProperty("index")] 55 | public int Index { get; set; } 56 | 57 | /// 58 | /// The message that was presented to the user as the choice 59 | /// 60 | [JsonProperty("message")] 61 | public ChatMessage Message { get; set; } 62 | 63 | /// 64 | /// The reason why the chat interaction ended after this choice was presented to the user 65 | /// 66 | [JsonProperty("finish_reason")] 67 | public string FinishReason { get; set; } 68 | 69 | /// 70 | /// Partial message "delta" from a stream. For example, the result from StreamChatEnumerableAsync. 71 | /// If this result object is not from a stream, this will be null 72 | /// 73 | [JsonProperty("delta")] 74 | public ChatMessage Delta { get; set; } 75 | 76 | /// 77 | /// A convenience method to return the content of the message in this response 78 | /// 79 | /// The content of the message in this response, not including . 80 | public override string ToString() 81 | { 82 | return Message.Content?.ToString(); 83 | } 84 | } 85 | 86 | /// 87 | /// How many tokens were used in this chat message. 88 | /// 89 | public class ChatUsage : Usage 90 | { 91 | /// 92 | /// The number of completion tokens used during the chat interaction 93 | /// 94 | [JsonProperty("completion_tokens")] 95 | public int CompletionTokens { get; set; } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /OpenAI_API/Completions/CompletionResult.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using OpenAI_API.Embedding; 3 | using System.Collections.Generic; 4 | 5 | namespace OpenAI_API.Completions 6 | { 7 | /// 8 | /// Represents a completion choice returned by the Completion API. 9 | /// 10 | public class Choice 11 | { 12 | /// 13 | /// The main text of the completion 14 | /// 15 | [JsonProperty("text")] 16 | public string Text { get; set; } 17 | 18 | /// 19 | /// If multiple completion choices we returned, this is the index withing the various choices 20 | /// 21 | [JsonProperty("index")] 22 | public int Index { get; set; } 23 | 24 | /// 25 | /// If the request specified , this contains the list of the most likely tokens. 26 | /// 27 | [JsonProperty("logprobs")] 28 | public Logprobs Logprobs { get; set; } 29 | 30 | /// 31 | /// If this is the last segment of the completion result, this specifies why the completion has ended. 32 | /// 33 | [JsonProperty("finish_reason")] 34 | public string FinishReason { get; set; } 35 | 36 | /// 37 | /// Gets the main text of this completion 38 | /// 39 | public override string ToString() 40 | { 41 | return Text; 42 | } 43 | } 44 | 45 | /// 46 | /// API usage as reported by the OpenAI API for this request 47 | /// 48 | public class CompletionUsage : Usage 49 | { 50 | /// 51 | /// How many tokens are in the completion(s) 52 | /// 53 | [JsonProperty("completion_tokens")] 54 | public short CompletionTokens { get; set; } 55 | } 56 | 57 | /// 58 | /// Represents a result from calling the Completion API 59 | /// 60 | public class CompletionResult : ApiResultBase 61 | { 62 | /// 63 | /// The identifier of the result, which may be used during troubleshooting 64 | /// 65 | [JsonProperty("id")] 66 | public string Id { get; set; } 67 | 68 | /// 69 | /// The completions returned by the API. Depending on your request, there may be 1 or many choices. 70 | /// 71 | [JsonProperty("choices")] 72 | public List Completions { get; set; } 73 | 74 | /// 75 | /// API token usage as reported by the OpenAI API for this request 76 | /// 77 | [JsonProperty("usage")] 78 | public CompletionUsage Usage { get; set; } 79 | 80 | /// 81 | /// Gets the text of the first completion, representing the main result 82 | /// 83 | public override string ToString() 84 | { 85 | if (Completions != null && Completions.Count > 0) 86 | return Completions[0].ToString(); 87 | else 88 | return $"CompletionResult {Id} has no valid output"; 89 | } 90 | } 91 | 92 | 93 | public class Logprobs 94 | { 95 | [JsonProperty("tokens")] 96 | public List Tokens { get; set; } 97 | 98 | [JsonProperty("token_logprobs")] 99 | public List TokenLogprobs { get; set; } 100 | 101 | [JsonProperty("top_logprobs")] 102 | public IList> TopLogprobs { get; set; } 103 | 104 | [JsonProperty("text_offset")] 105 | public List TextOffsets { get; set; } 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /OpenAI_API/Embedding/EmbeddingEndpoint.cs: -------------------------------------------------------------------------------- 1 | using OpenAI_API.Models; 2 | using System.Threading.Tasks; 3 | 4 | namespace OpenAI_API.Embedding 5 | { 6 | /// 7 | /// OpenAI’s text embeddings measure the relatedness of text strings by generating an embedding, which is a vector (list) of floating point numbers. The distance between two vectors measures their relatedness. Small distances suggest high relatedness and large distances suggest low relatedness. 8 | /// 9 | public class EmbeddingEndpoint : EndpointBase, IEmbeddingEndpoint 10 | { 11 | /// 12 | /// This allows you to send request to the recommended model without needing to specify. Every request uses the model 13 | /// 14 | public EmbeddingRequest DefaultEmbeddingRequestArgs { get; set; } = new EmbeddingRequest() { Model = Model.AdaTextEmbedding }; 15 | 16 | /// 17 | /// The name of the endpoint, which is the final path segment in the API URL. For example, "embeddings". 18 | /// 19 | protected override string Endpoint { get { return "embeddings"; } } 20 | 21 | /// 22 | /// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of as . 23 | /// 24 | /// 25 | internal EmbeddingEndpoint(OpenAIAPI api) : base(api) { } 26 | 27 | /// 28 | /// Ask the API to embedd text using the default embedding model 29 | /// 30 | /// Text to be embedded 31 | /// Asynchronously returns the embedding result. Look in its property of to find the vector of floating point numbers 32 | public async Task CreateEmbeddingAsync(string input) 33 | { 34 | EmbeddingRequest req = new EmbeddingRequest(DefaultEmbeddingRequestArgs.Model, input); 35 | return await CreateEmbeddingAsync(req); 36 | } 37 | 38 | /// 39 | /// Ask the API to embedd text using a custom request 40 | /// 41 | /// Request to be send 42 | /// Asynchronously returns the embedding result. Look in its property of to find the vector of floating point numbers 43 | public async Task CreateEmbeddingAsync(EmbeddingRequest request) 44 | { 45 | return await HttpPost(postData: request); 46 | } 47 | 48 | /// 49 | /// Ask the API to embedd text using the default embedding model 50 | /// 51 | /// Text to be embedded 52 | /// Asynchronously returns the first embedding result as an array of floats. 53 | public async Task GetEmbeddingsAsync(string input) 54 | { 55 | EmbeddingRequest req = new EmbeddingRequest(DefaultEmbeddingRequestArgs.Model, input); 56 | var embeddingResult = await CreateEmbeddingAsync(req); 57 | return embeddingResult?.Data?[0]?.Embedding; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /OpenAI_API/Embedding/EmbeddingRequest.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using OpenAI_API.Models; 3 | 4 | namespace OpenAI_API.Embedding 5 | { 6 | /// 7 | /// Represents a request to the Completions API. Matches with the docs at the OpenAI docs 8 | /// 9 | public class EmbeddingRequest 10 | { 11 | /// 12 | /// ID of the model to use. You can use to see all of your available models, or use a standard model like . 13 | /// 14 | [JsonProperty("model")] 15 | public string Model { get; set; } 16 | 17 | /// 18 | /// Main text to be embedded 19 | /// 20 | [JsonProperty("input")] 21 | public string Input { get; set; } 22 | 23 | /// 24 | /// Cretes a new, empty 25 | /// 26 | public EmbeddingRequest() 27 | { 28 | 29 | } 30 | 31 | /// 32 | /// Creates a new with the specified parameters 33 | /// 34 | /// The model to use. You can use to see all of your available models, or use a standard model like . 35 | /// The prompt to transform 36 | public EmbeddingRequest(Model model, string input) 37 | { 38 | Model = model; 39 | this.Input = input; 40 | } 41 | 42 | /// 43 | /// Creates a new with the specified input and the model. 44 | /// 45 | /// The prompt to transform 46 | public EmbeddingRequest(string input) 47 | { 48 | Model = OpenAI_API.Models.Model.AdaTextEmbedding; 49 | this.Input = input; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /OpenAI_API/Embedding/EmbeddingResult.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace OpenAI_API.Embedding 6 | { 7 | /// 8 | /// Represents an embedding result returned by the Embedding API. 9 | /// 10 | public class EmbeddingResult : ApiResultBase 11 | { 12 | /// 13 | /// List of results of the embedding 14 | /// 15 | [JsonProperty("data")] 16 | public List Data { get; set; } 17 | 18 | /// 19 | /// Usage statistics of how many tokens have been used for this request 20 | /// 21 | [JsonProperty("usage")] 22 | public Usage Usage { get; set; } 23 | 24 | /// 25 | /// Allows an EmbeddingResult to be implicitly cast to the array of floats repsresenting the first ebmedding result 26 | /// 27 | /// The to cast to an array of floats. 28 | public static implicit operator float[](EmbeddingResult embeddingResult) 29 | { 30 | return embeddingResult.Data.FirstOrDefault()?.Embedding; 31 | } 32 | } 33 | 34 | /// 35 | /// Data returned from the Embedding API. 36 | /// 37 | public class Data 38 | { 39 | /// 40 | /// Type of the response. In case of Data, this will be "embedding" 41 | /// 42 | [JsonProperty("object")] 43 | 44 | public string Object { get; set; } 45 | 46 | /// 47 | /// The input text represented as a vector (list) of floating point numbers 48 | /// 49 | [JsonProperty("embedding")] 50 | public float[] Embedding { get; set; } 51 | 52 | /// 53 | /// Index 54 | /// 55 | [JsonProperty("index")] 56 | public int Index { get; set; } 57 | 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /OpenAI_API/Embedding/IEmbeddingEndpoint.cs: -------------------------------------------------------------------------------- 1 | using OpenAI_API.Models; 2 | using System.Threading.Tasks; 3 | 4 | namespace OpenAI_API.Embedding 5 | { 6 | /// 7 | /// An interface for , for ease of mock testing, etc 8 | /// 9 | public interface IEmbeddingEndpoint 10 | { 11 | /// 12 | /// This allows you to send request to the recommended model without needing to specify. Every request uses the model 13 | /// 14 | EmbeddingRequest DefaultEmbeddingRequestArgs { get; set; } 15 | 16 | /// 17 | /// Ask the API to embedd text using the default embedding model 18 | /// 19 | /// Text to be embedded 20 | /// Asynchronously returns the embedding result. Look in its property of to find the vector of floating point numbers 21 | Task CreateEmbeddingAsync(string input); 22 | 23 | /// 24 | /// Ask the API to embedd text using a custom request 25 | /// 26 | /// Request to be send 27 | /// Asynchronously returns the embedding result. Look in its property of to find the vector of floating point numbers 28 | Task CreateEmbeddingAsync(EmbeddingRequest request); 29 | 30 | /// 31 | /// Ask the API to embedd text using the default embedding model 32 | /// 33 | /// Text to be embedded 34 | /// Asynchronously returns the first embedding result as an array of floats. 35 | Task GetEmbeddingsAsync(string input); 36 | } 37 | } -------------------------------------------------------------------------------- /OpenAI_API/Files/File.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace OpenAI_API.Files 4 | { 5 | /// 6 | /// Represents a single file used with the OpenAI Files endpoint. Files are used to upload and manage documents that can be used with features like Fine-tuning. 7 | /// 8 | public class File : ApiResultBase 9 | { 10 | /// 11 | /// Unique id for this file, so that it can be referenced in other operations 12 | /// 13 | [JsonProperty("id")] 14 | public string Id { get; set; } 15 | 16 | /// 17 | /// The name of the file 18 | /// 19 | [JsonProperty("filename")] 20 | public string Name { get; set; } 21 | 22 | /// 23 | /// What is the purpose of this file, fine-tune, search, etc 24 | /// 25 | [JsonProperty("purpose")] 26 | public string Purpose { get; set; } 27 | 28 | /// 29 | /// The size of the file in bytes 30 | /// 31 | [JsonProperty("bytes")] 32 | public long Bytes { get; set; } 33 | 34 | /// 35 | /// Timestamp for the creation time of this file 36 | /// 37 | [JsonProperty("created_at")] 38 | public long CreatedAt { get; set; } 39 | 40 | /// 41 | /// When the object is deleted, this attribute is used in the Delete file operation 42 | /// 43 | [JsonProperty("deleted")] 44 | public bool Deleted { get; set; } 45 | 46 | /// 47 | /// The status of the File (ie when an upload operation was done: "uploaded") 48 | /// 49 | [JsonProperty("status")] 50 | public string Status { get; set; } 51 | 52 | /// 53 | /// The status details, it could be null 54 | /// 55 | [JsonProperty("status_details")] 56 | public string StatusDetails { get; set; } 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /OpenAI_API/Files/FilesEndpoint.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Net.Http; 5 | using System.Threading.Tasks; 6 | 7 | namespace OpenAI_API.Files 8 | { 9 | /// 10 | /// The API endpoint for operations List, Upload, Delete, Retrieve files 11 | /// 12 | public class FilesEndpoint : EndpointBase, IFilesEndpoint 13 | { 14 | /// 15 | /// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of as . 16 | /// 17 | /// 18 | internal FilesEndpoint(OpenAIAPI api) : base(api) { } 19 | 20 | /// 21 | /// The name of the endpoint, which is the final path segment in the API URL. For example, "files". 22 | /// 23 | protected override string Endpoint { get { return "files"; } } 24 | 25 | /// 26 | /// Get the list of all files 27 | /// 28 | /// 29 | /// 30 | public async Task> GetFilesAsync() 31 | { 32 | return (await HttpGet()).Data; 33 | } 34 | 35 | /// 36 | /// Returns information about a specific file 37 | /// 38 | /// The ID of the file to use for this request 39 | /// 40 | public async Task GetFileAsync(string fileId) 41 | { 42 | return await HttpGet($"{Url}/{fileId}"); 43 | } 44 | 45 | 46 | /// 47 | /// Returns the contents of the specific file as string 48 | /// 49 | /// The ID of the file to use for this request 50 | /// 51 | public async Task GetFileContentAsStringAsync(string fileId) 52 | { 53 | return await HttpGetContent($"{Url}/{fileId}/content"); 54 | } 55 | 56 | /// 57 | /// Delete a file 58 | /// 59 | /// The ID of the file to use for this request 60 | /// 61 | public async Task DeleteFileAsync(string fileId) 62 | { 63 | return await HttpDelete($"{Url}/{fileId}"); 64 | } 65 | 66 | 67 | /// 68 | /// Upload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact OpenAI if you need to increase the storage limit 69 | /// 70 | /// The name of the file to use for this request 71 | /// The intendend purpose of the uploaded documents. Use "fine-tune" for Fine-tuning. This allows us to validate the format of the uploaded file. 72 | public async Task UploadFileAsync(string filePath, string purpose = "fine-tune") 73 | { 74 | var content = new MultipartFormDataContent 75 | { 76 | { new StringContent(purpose), "purpose" }, 77 | { new ByteArrayContent(System.IO.File.ReadAllBytes(filePath)), "file", Path.GetFileName(filePath) } 78 | }; 79 | 80 | return await HttpPost(Url, content); 81 | } 82 | 83 | /// 84 | /// A helper class to deserialize the JSON API responses. This should not be used directly. 85 | /// 86 | private class FilesData : ApiResultBase 87 | { 88 | [JsonProperty("data")] 89 | public List Data { get; set; } 90 | [JsonProperty("object")] 91 | public string Obj { get; set; } 92 | } 93 | } 94 | 95 | 96 | } 97 | -------------------------------------------------------------------------------- /OpenAI_API/Files/IFilesEndpoint.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | 4 | namespace OpenAI_API.Files 5 | { 6 | /// 7 | /// An interface for , for ease of mock testing, etc 8 | /// 9 | public interface IFilesEndpoint 10 | { 11 | /// 12 | /// Get the list of all files 13 | /// 14 | /// 15 | /// 16 | Task> GetFilesAsync(); 17 | 18 | /// 19 | /// Returns information about a specific file 20 | /// 21 | /// The ID of the file to use for this request 22 | /// 23 | Task GetFileAsync(string fileId); 24 | 25 | /// 26 | /// Returns the contents of the specific file as string 27 | /// 28 | /// The ID of the file to use for this request 29 | /// 30 | Task GetFileContentAsStringAsync(string fileId); 31 | 32 | /// 33 | /// Delete a file 34 | /// 35 | /// The ID of the file to use for this request 36 | /// 37 | Task DeleteFileAsync(string fileId); 38 | 39 | /// 40 | /// Upload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact OpenAI if you need to increase the storage limit 41 | /// 42 | /// The name of the file to use for this request 43 | /// The intendend purpose of the uploaded documents. Use "fine-tune" for Fine-tuning. This allows us to validate the format of the uploaded file. 44 | Task UploadFileAsync(string filePath, string purpose = "fine-tune"); 45 | } 46 | } -------------------------------------------------------------------------------- /OpenAI_API/Functions/FunctionRequest.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace OpenAI_API.Functions 6 | { 7 | /// 8 | /// Class to request functions executions from AI 9 | /// 10 | public class FunctionRequest 11 | { 12 | /// 13 | /// Request type 14 | /// 15 | [JsonProperty("type")] 16 | public string Type => "function"; 17 | 18 | /// 19 | /// Details of the function to be executed 20 | /// 21 | [JsonProperty("function")] 22 | public Function Function { get; set; } 23 | } 24 | 25 | /// 26 | /// Details of the function to be executed 27 | /// 28 | public class Function 29 | { 30 | /// 31 | /// Function name 32 | /// 33 | [JsonProperty("name")] 34 | public string Name { get; set; } 35 | 36 | /// 37 | /// Details on when and how to use the function 38 | /// 39 | [JsonProperty("description")] 40 | public string Description { get; set; } 41 | 42 | /// 43 | /// Function parameters 44 | /// 45 | [JsonProperty("parameters")] 46 | public Parameter Parameters { get; set; } 47 | 48 | /// 49 | /// Setting strict to true will ensure function calls reliably adhere to the function schema, instead of being best effort. We recommend always enabling strict mode. 50 | /// 51 | [JsonProperty("strict")] 52 | public bool Strict => false; 53 | } 54 | 55 | /// 56 | /// Function parameter 57 | /// 58 | public class Parameter 59 | { 60 | /// 61 | /// Parameter type 62 | /// 63 | [JsonProperty("type")] 64 | public string Type => "object"; 65 | 66 | /// 67 | /// Parameter properties list 68 | /// 69 | [JsonProperty("properties")] 70 | public Dictionary Properties { get; set; } 71 | 72 | /// 73 | /// Indicate if the parameter is mandatory. 74 | /// All fields in properties must be marked as required. 75 | /// 76 | [JsonProperty("required")] 77 | public List Required 78 | { 79 | get 80 | { 81 | return Properties.Select(p => p.Key).ToList(); 82 | } 83 | } 84 | 85 | /// 86 | /// Must be set to false for each object in the parameters 87 | /// 88 | [JsonProperty("additionalProperties")] 89 | public bool AdditionalProperties => false; 90 | } 91 | 92 | /// 93 | /// Represents a parameter property 94 | /// 95 | public class Property 96 | { 97 | /// 98 | /// Property types 99 | /// You can denote optional fields by adding null as a type option 100 | /// 101 | [JsonProperty("type")] 102 | public List Types { get; set; } 103 | 104 | /// 105 | /// Property description 106 | /// 107 | [JsonProperty("description")] 108 | public string Description { get; set; } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /OpenAI_API/Functions/FunctionResult.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace OpenAI_API.Functions 4 | { 5 | /// 6 | /// Details of the function to be executed 7 | /// 8 | public class FunctionResult 9 | { 10 | /// 11 | /// Used later to submit the function result back to the AI. 12 | /// 13 | [JsonProperty("id")] 14 | public string Id { get; set; } 15 | 16 | /// 17 | /// It will probably always be "function", indicating that the execution of a function is being requested. 18 | /// 19 | /// 20 | [JsonProperty("type")] 21 | public string Type { get; set; } 22 | 23 | /// 24 | /// Gets or sets the function to call, represented by the object. 25 | /// 26 | [JsonProperty("function")] 27 | public FunctionToCall Function { get; set; } 28 | } 29 | 30 | /// 31 | /// Represents a class that encapsulates a function or method to be called. 32 | /// 33 | public class FunctionToCall 34 | { 35 | /// 36 | /// The name of the function to be executed. 37 | /// 38 | [JsonProperty("name")] 39 | public string Name { get; set; } 40 | 41 | /// 42 | /// Gets or sets the function's arguments. 43 | /// 44 | [JsonProperty("arguments")] 45 | public string Arguments { get; set; } 46 | } 47 | } -------------------------------------------------------------------------------- /OpenAI_API/IOpenAIAPI.cs: -------------------------------------------------------------------------------- 1 | using OpenAI_API.Chat; 2 | using OpenAI_API.Completions; 3 | using OpenAI_API.Embedding; 4 | using OpenAI_API.Files; 5 | using OpenAI_API.Images; 6 | using OpenAI_API.Models; 7 | using OpenAI_API.Moderation; 8 | 9 | namespace OpenAI_API 10 | { 11 | /// 12 | /// An interface for , for ease of mock testing, etc 13 | /// 14 | public interface IOpenAIAPI 15 | { 16 | /// 17 | /// Base url for OpenAI 18 | /// for OpenAI, should be "https://api.openai.com/{0}/{1}" 19 | /// for Azure, should be "https://(your-resource-name.openai.azure.com/openai/deployments/(deployment-id)/{1}?api-version={0}" 20 | /// 21 | string ApiUrlFormat { get; set; } 22 | 23 | /// 24 | /// Version of the Rest Api 25 | /// 26 | string ApiVersion { get; set; } 27 | 28 | /// 29 | /// The API authentication information to use for API calls 30 | /// 31 | APIAuthentication Auth { get; set; } 32 | 33 | /// 34 | /// Text generation in the form of chat messages. This interacts with the ChatGPT API. 35 | /// 36 | IChatEndpoint Chat { get; } 37 | 38 | /// 39 | /// Classify text against the OpenAI Content Policy. 40 | /// 41 | IModerationEndpoint Moderation { get; } 42 | 43 | /// 44 | /// Text generation is the core function of the API. You give the API a prompt, and it generates a completion. The way you “program” the API to do a task is by simply describing the task in plain english or providing a few written examples. This simple approach works for a wide range of use cases, including summarization, translation, grammar correction, question answering, chatbots, composing emails, and much more (see the prompt library for inspiration). 45 | /// 46 | ICompletionEndpoint Completions { get; } 47 | 48 | /// 49 | /// The API lets you transform text into a vector (list) of floating point numbers. The distance between two vectors measures their relatedness. Small distances suggest high relatedness and large distances suggest low relatedness. 50 | /// 51 | IEmbeddingEndpoint Embeddings { get; } 52 | 53 | /// 54 | /// The API endpoint for querying available Engines/models 55 | /// 56 | IModelsEndpoint Models { get; } 57 | 58 | /// 59 | /// The API lets you do operations with files. You can upload, delete or retrieve files. Files can be used for fine-tuning, search, etc. 60 | /// 61 | IFilesEndpoint Files { get; } 62 | 63 | /// 64 | /// The API lets you do operations with images. You can Given a prompt and/or an input image, the model will generate a new image. 65 | /// 66 | IImageGenerationEndpoint ImageGenerations { get; } 67 | } 68 | } -------------------------------------------------------------------------------- /OpenAI_API/Images/IImageGenerationEndpoint.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using OpenAI_API.Models; 3 | 4 | namespace OpenAI_API.Images 5 | { 6 | /// 7 | /// An interface for . Given a prompt, the model will generate a new image. 8 | /// 9 | public interface IImageGenerationEndpoint 10 | { 11 | /// 12 | /// Ask the API to Creates an image given a prompt. 13 | /// 14 | /// Request to be send 15 | /// Asynchronously returns the image result. Look in its 16 | Task CreateImageAsync(ImageGenerationRequest request); 17 | 18 | /// 19 | /// Ask the API to Creates an image given a prompt. 20 | /// 21 | /// A text description of the desired image(s) 22 | /// The model to use for generating the image. Defaults to . 23 | /// Asynchronously returns the image result. Look in its 24 | Task CreateImageAsync(string input, Model model = null); 25 | } 26 | } -------------------------------------------------------------------------------- /OpenAI_API/Images/ImageGenerationEndpoint.cs: -------------------------------------------------------------------------------- 1 | using OpenAI_API.Models; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OpenAI_API.Images 8 | { 9 | /// 10 | /// Given a prompt, the model will generate a new image. 11 | /// 12 | public class ImageGenerationEndpoint : EndpointBase, IImageGenerationEndpoint 13 | { 14 | /// 15 | /// The name of the endpoint, which is the final path segment in the API URL. For example, "image". 16 | /// 17 | protected override string Endpoint { get { return "images/generations"; } } 18 | 19 | /// 20 | /// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of as . 21 | /// 22 | /// 23 | internal ImageGenerationEndpoint(OpenAIAPI api) : base(api) { } 24 | 25 | /// 26 | /// Ask the API to Creates an image given a prompt. 27 | /// 28 | /// A text description of the desired image(s) 29 | /// The model to use for generating the image. Defaults to . 30 | /// Asynchronously returns the image result. Look in its 31 | public async Task CreateImageAsync(string input, Model model = null) 32 | { 33 | ImageGenerationRequest req = new ImageGenerationRequest(prompt: input, model: model); 34 | return await CreateImageAsync(req); 35 | } 36 | 37 | /// 38 | /// Ask the API to Creates an image given a prompt. 39 | /// 40 | /// Request to be send 41 | /// Asynchronously returns the image result. Look in its 42 | public async Task CreateImageAsync(ImageGenerationRequest request) 43 | { 44 | return await HttpPost(postData: request); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /OpenAI_API/Images/ImageResponseFormat.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace OpenAI_API.Images 7 | { 8 | /// 9 | /// Represents available response formats for image generation endpoints 10 | /// 11 | public class ImageResponseFormat 12 | { 13 | private ImageResponseFormat(string value) { Value = value; } 14 | 15 | private string Value { get; set; } 16 | 17 | /// 18 | /// Requests an image that is 256x256 19 | /// 20 | public static ImageResponseFormat Url { get { return new ImageResponseFormat("url"); } } 21 | /// 22 | /// Requests an image that is 512x512 23 | /// 24 | public static ImageResponseFormat B64_json { get { return new ImageResponseFormat("b64_json"); } } 25 | 26 | 27 | /// 28 | /// Gets the string value for this response format to pass to the API 29 | /// 30 | /// The response format as a string 31 | public override string ToString() 32 | { 33 | return Value; 34 | } 35 | 36 | /// 37 | /// Gets the string value for this response format to pass to the API 38 | /// 39 | /// The ImageResponseFormat to convert 40 | public static implicit operator String(ImageResponseFormat value) { return value; } 41 | 42 | internal class ImageResponseJsonConverter : JsonConverter 43 | { 44 | public override ImageResponseFormat ReadJson(JsonReader reader, Type objectType, ImageResponseFormat existingValue, bool hasExistingValue, JsonSerializer serializer) 45 | { 46 | return new ImageResponseFormat(reader.ReadAsString()); 47 | } 48 | 49 | public override void WriteJson(JsonWriter writer, ImageResponseFormat value, JsonSerializer serializer) 50 | { 51 | writer.WriteValue(value.ToString()); 52 | } 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /OpenAI_API/Images/ImageResult.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace OpenAI_API.Images 7 | { 8 | /// 9 | /// Represents an image result returned by the Image API. 10 | /// 11 | public class ImageResult : ApiResultBase 12 | { 13 | /// 14 | /// List of results of the embedding 15 | /// 16 | [JsonProperty("data")] 17 | public List Data { get; set; } 18 | 19 | /// 20 | /// Gets the url or base64-encoded image data of the first result, or null if there are no results 21 | /// 22 | /// 23 | public override string ToString() 24 | { 25 | if (Data?.Count > 0) 26 | { 27 | return Data[0].Url ?? Data[0].Base64Data; 28 | } 29 | else 30 | { 31 | return null; 32 | } 33 | } 34 | } 35 | 36 | /// 37 | /// Data returned from the Image API. 38 | /// 39 | public class Data 40 | { 41 | /// 42 | /// The url of the image result 43 | /// 44 | [JsonProperty("url")] 45 | 46 | public string Url { get; set; } 47 | 48 | /// 49 | /// The base64-encoded image data as returned by the API 50 | /// 51 | [JsonProperty("b64_json")] 52 | public string Base64Data { get; set; } 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /OpenAI_API/Images/ImageSize.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using OpenAI_API.Models; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace OpenAI_API.Images 8 | { 9 | /// 10 | /// Represents available sizes for image generation endpoints 11 | /// 12 | public class ImageSize 13 | { 14 | internal ImageSize(string value) { Value = value; } 15 | 16 | private string Value { get; set; } 17 | 18 | /// 19 | /// Only for DALL-E 2. Requests an image that is 256x256 20 | /// 21 | public static ImageSize _256 { get { return new ImageSize("256x256"); } } 22 | /// 23 | /// Only for DALL-E 2. Requests an image that is 512x512 24 | /// 25 | public static ImageSize _512 { get { return new ImageSize("512x512"); } } 26 | /// 27 | /// Works with both DALL-E 2 and 3. Requests and image that is 1024x1024. 28 | /// 29 | public static ImageSize _1024 { get { return new ImageSize("1024x1024"); } } 30 | 31 | /// 32 | /// Only for DALL-E 3. Requests a tall image that is 1024x1792. 33 | /// 34 | public static ImageSize _1024x1792 { get { return new ImageSize("1024x1792"); } } 35 | 36 | /// 37 | /// Only for DALL-E 3. Requests a wide image that is 1792x1024. 38 | /// 39 | public static ImageSize _1792x1024 { get { return new ImageSize("1792x1024"); } } 40 | 41 | /// 42 | /// Gets the string value for this size to pass to the API 43 | /// 44 | /// The size as a string 45 | public override string ToString() 46 | { 47 | return Value; 48 | } 49 | 50 | /// 51 | /// Returns true is the string value of the sizes match 52 | /// 53 | /// The other object to compare to 54 | /// True is the sizes are the same 55 | public override bool Equals(object obj) 56 | { 57 | if (obj is null) 58 | return false; 59 | else if (obj is ImageSize) 60 | return this.Value.Equals(((ImageSize)obj).Value); 61 | else if (obj is string) 62 | return this.Value.Equals((string)obj); 63 | else 64 | return false; 65 | } 66 | 67 | /// 68 | public override int GetHashCode() 69 | { 70 | return Value.GetHashCode(); 71 | } 72 | 73 | public static bool operator ==(ImageSize a, ImageSize b) 74 | { 75 | return a.Equals(b); 76 | } 77 | public static bool operator !=(ImageSize a, ImageSize b) 78 | { 79 | return !a.Equals(b); 80 | } 81 | 82 | /// 83 | /// Gets the string value for this size to pass to the API 84 | /// 85 | /// The ImageSize to convert 86 | public static implicit operator String(ImageSize value) { return value; } 87 | 88 | internal class ImageSizeJsonConverter : JsonConverter 89 | { 90 | public override void WriteJson(JsonWriter writer, ImageSize value, JsonSerializer serializer) 91 | { 92 | writer.WriteValue(value.ToString()); 93 | } 94 | 95 | public override ImageSize ReadJson(JsonReader reader, Type objectType, ImageSize existingValue, bool hasExistingValue, JsonSerializer serializer) 96 | { 97 | return new ImageSize(reader.ReadAsString()); 98 | } 99 | } 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /OpenAI_API/Model/IModelsEndpoint.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | 4 | namespace OpenAI_API.Models 5 | { 6 | /// 7 | /// An interface for , for ease of mock testing, etc 8 | /// 9 | public interface IModelsEndpoint 10 | { 11 | /// 12 | /// Get details about a particular Model from the API, specifically properties such as and permissions. 13 | /// 14 | /// The id/name of the model to get more details about 15 | /// Asynchronously returns the with all available properties 16 | Task RetrieveModelDetailsAsync(string id); 17 | 18 | /// 19 | /// Get details about a particular Model from the API, specifically properties such as and permissions. 20 | /// 21 | /// The id/name of the model to get more details about 22 | /// Obsolete: IGNORED 23 | /// Asynchronously returns the with all available properties 24 | Task RetrieveModelDetailsAsync(string id, APIAuthentication auth = null); 25 | 26 | /// 27 | /// List all models via the API 28 | /// 29 | /// Asynchronously returns the list of all s 30 | Task> GetModelsAsync(); 31 | } 32 | } -------------------------------------------------------------------------------- /OpenAI_API/Model/ModelsEndpoint.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Threading.Tasks; 5 | 6 | namespace OpenAI_API.Models 7 | { 8 | /// 9 | /// The API endpoint for querying available models 10 | /// 11 | public class ModelsEndpoint : EndpointBase, IModelsEndpoint 12 | { 13 | /// 14 | /// The name of the endpoint, which is the final path segment in the API URL. For example, "models". 15 | /// 16 | protected override string Endpoint { get { return "models"; } } 17 | 18 | /// 19 | /// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of as . 20 | /// 21 | /// 22 | internal ModelsEndpoint(OpenAIAPI api) : base(api) { } 23 | 24 | /// 25 | /// Get details about a particular Model from the API, specifically properties such as and permissions. 26 | /// 27 | /// The id/name of the model to get more details about 28 | /// Asynchronously returns the with all available properties 29 | public async Task RetrieveModelDetailsAsync(string id) 30 | { 31 | string resultAsString = await HttpGetContent($"{Url}/{id}"); 32 | var model = JsonConvert.DeserializeObject(resultAsString); 33 | return model; 34 | } 35 | 36 | /// 37 | /// List all models via the API 38 | /// 39 | /// Asynchronously returns the list of all s 40 | public async Task> GetModelsAsync() 41 | { 42 | return (await HttpGet()).data; 43 | } 44 | 45 | /// 46 | /// Get details about a particular Model from the API, specifically properties such as and permissions. 47 | /// 48 | /// The id/name of the model to get more details about 49 | /// Obsolete: IGNORED 50 | /// Asynchronously returns the with all available properties 51 | [Obsolete("Use the overload without the APIAuthentication parameter instead, as custom auth is no longer used.", false)] 52 | public async Task RetrieveModelDetailsAsync(string id, APIAuthentication auth = null) 53 | { 54 | return await this.RetrieveModelDetailsAsync(id); 55 | } 56 | 57 | /// 58 | /// A helper class to deserialize the JSON API responses. This should not be used directly. 59 | /// 60 | private class JsonHelperRoot : ApiResultBase 61 | { 62 | [JsonProperty("data")] 63 | public List data { get; set; } 64 | [JsonProperty("object")] 65 | public string obj { get; set; } 66 | 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /OpenAI_API/Moderation/IModerationEndpoint.cs: -------------------------------------------------------------------------------- 1 | using OpenAI_API.Models; 2 | using System.Threading.Tasks; 3 | 4 | namespace OpenAI_API.Moderation 5 | { 6 | /// 7 | /// An interface for , which classifies text against the OpenAI Content Policy 8 | /// 9 | public interface IModerationEndpoint 10 | { 11 | /// 12 | /// This allows you to send request to the recommended model without needing to specify. OpenAI recommends using the model 13 | /// 14 | ModerationRequest DefaultModerationRequestArgs { get; set; } 15 | 16 | /// 17 | /// Ask the API to classify the text using a custom request. 18 | /// 19 | /// Request to send to the API 20 | /// Asynchronously returns the classification result 21 | Task CallModerationAsync(ModerationRequest request); 22 | 23 | /// 24 | /// Ask the API to classify the text using the default model. 25 | /// 26 | /// Text to classify 27 | /// Asynchronously returns the classification result 28 | Task CallModerationAsync(string input); 29 | } 30 | } -------------------------------------------------------------------------------- /OpenAI_API/Moderation/ModerationEndpoint.cs: -------------------------------------------------------------------------------- 1 | using OpenAI_API.Models; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace OpenAI_API.Moderation 8 | { 9 | /// 10 | /// This endpoint classifies text against the OpenAI Content Policy 11 | /// 12 | public class ModerationEndpoint : EndpointBase, IModerationEndpoint 13 | { 14 | /// 15 | /// This allows you to send request to the recommended model without needing to specify. OpenAI recommends using the model 16 | /// 17 | public ModerationRequest DefaultModerationRequestArgs { get; set; } = new ModerationRequest() { Model = Model.TextModerationLatest }; 18 | 19 | /// 20 | /// The name of the endpoint, which is the final path segment in the API URL. For example, "completions". 21 | /// 22 | protected override string Endpoint { get { return "moderations"; } } 23 | 24 | /// 25 | /// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of as . 26 | /// 27 | /// 28 | internal ModerationEndpoint(OpenAIAPI api) : base(api) { } 29 | 30 | /// 31 | /// Ask the API to classify the text using the default model. 32 | /// 33 | /// Text to classify 34 | /// Asynchronously returns the classification result 35 | public async Task CallModerationAsync(string input) 36 | { 37 | ModerationRequest req = new ModerationRequest(input, DefaultModerationRequestArgs.Model); 38 | return await CallModerationAsync(req); 39 | } 40 | 41 | /// 42 | /// Ask the API to classify the text using a custom request. 43 | /// 44 | /// Request to send to the API 45 | /// Asynchronously returns the classification result 46 | public async Task CallModerationAsync(ModerationRequest request) 47 | { 48 | return await HttpPost(postData: request); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /OpenAI_API/Moderation/ModerationRequest.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using OpenAI_API.Models; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Reflection; 7 | using System.Text; 8 | 9 | namespace OpenAI_API.Moderation 10 | { 11 | /// 12 | /// Represents a request to the Moderations API. 13 | /// 14 | public class ModerationRequest 15 | { 16 | 17 | /// 18 | /// Which Moderation model to use for this request. Two content moderations models are available: and . The default is which will be automatically upgraded over time.This ensures you are always using our most accurate model.If you use , we will provide advanced notice before updating the model. Accuracy of may be slightly lower than for . 19 | /// 20 | [JsonProperty("model")] 21 | public string Model { get; set; } 22 | 23 | /// 24 | /// The input text to classify 25 | /// 26 | [JsonIgnore] 27 | public string Input 28 | { 29 | get 30 | { 31 | if (Inputs == null) 32 | return null; 33 | else 34 | return Inputs.FirstOrDefault(); 35 | } 36 | set 37 | { 38 | Inputs = new string[] { value }; 39 | } 40 | } 41 | 42 | /// 43 | /// An array of inputs to classify 44 | /// 45 | [JsonProperty("input")] 46 | public string[] Inputs { get; set; } 47 | 48 | /// 49 | /// Cretes a new, empty 50 | /// 51 | public ModerationRequest() 52 | { 53 | 54 | } 55 | 56 | /// 57 | /// Creates a new with the specified parameters 58 | /// 59 | /// The prompt to classify 60 | /// The model to use. You can use to see all of your available models, or use a standard model like . 61 | public ModerationRequest(string input, Model model) 62 | { 63 | Model = model; 64 | this.Input = input; 65 | } 66 | 67 | /// 68 | /// Creates a new with the specified parameters 69 | /// 70 | /// An array of prompts to classify 71 | /// The model to use. You can use to see all of your available models, or use a standard model like . 72 | public ModerationRequest(string[] inputs, Model model) 73 | { 74 | Model = model; 75 | this.Inputs = inputs; 76 | } 77 | 78 | /// 79 | /// Creates a new with the specified input(s) and the model. 80 | /// 81 | /// One or more prompts to classify 82 | public ModerationRequest(params string[] input) 83 | { 84 | Model = OpenAI_API.Models.Model.TextModerationLatest; 85 | this.Inputs = input; 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /OpenAI_API/OpenAIAPI.cs: -------------------------------------------------------------------------------- 1 | using OpenAI_API.Chat; 2 | using OpenAI_API.Completions; 3 | using OpenAI_API.Embedding; 4 | using OpenAI_API.Files; 5 | using OpenAI_API.Images; 6 | using OpenAI_API.Models; 7 | using OpenAI_API.Moderation; 8 | using System.Net.Http; 9 | 10 | namespace OpenAI_API 11 | { 12 | /// 13 | /// Entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints 14 | /// 15 | public class OpenAIAPI : IOpenAIAPI 16 | { 17 | /// 18 | /// Base url for OpenAI 19 | /// for OpenAI, should be "https://api.openai.com/{0}/{1}" 20 | /// for Azure, should be "https://(your-resource-name.openai.azure.com/openai/deployments/(deployment-id)/{1}?api-version={0}" 21 | /// 22 | public string ApiUrlFormat { get; set; } = "https://api.openai.com/{0}/{1}"; 23 | 24 | /// 25 | /// Version of the Rest Api 26 | /// 27 | public string ApiVersion { get; set; } = "v1"; 28 | 29 | /// 30 | /// The API authentication information to use for API calls 31 | /// 32 | public APIAuthentication Auth { get; set; } 33 | 34 | /// 35 | /// Optionally provide an IHttpClientFactory to create the client to send requests. 36 | /// 37 | public IHttpClientFactory HttpClientFactory { get; set; } 38 | 39 | /// 40 | /// Creates a new entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints 41 | /// 42 | /// The API authentication information to use for API calls, or to attempt to use the , potentially loading from environment vars or from a config file. 43 | public OpenAIAPI(APIAuthentication apiKeys = null) 44 | { 45 | this.Auth = apiKeys.ThisOrDefault(); 46 | Completions = new CompletionEndpoint(this); 47 | Models = new ModelsEndpoint(this); 48 | Files = new FilesEndpoint(this); 49 | Embeddings = new EmbeddingEndpoint(this); 50 | Chat = new ChatEndpoint(this); 51 | Moderation = new ModerationEndpoint(this); 52 | ImageGenerations = new ImageGenerationEndpoint(this); 53 | } 54 | 55 | /// 56 | /// Creates an instance of the OpenAIAPI configured for Azure with the specified resource name and deployment ID. 57 | /// 58 | /// The name of the Azure resource where the OpenAI service is deployed. 59 | /// The ID of the specific deployment of the OpenAI model. 60 | /// The API authentication key used to access the OpenAI service. 61 | /// 62 | /// An instance of the OpenAIAPI configured for Azure with the provided parameters. 63 | /// 64 | public static OpenAIAPI ForAzure(string YourResourceName, string deploymentId, APIAuthentication apiKey) 65 | { 66 | return new OpenAIAPI(apiKey) 67 | { 68 | ApiVersion = "2022-12-01", 69 | ApiUrlFormat = $"https://{YourResourceName}.openai.azure.com/openai/deployments/{deploymentId}/" + "{1}?api-version={0}" 70 | }; 71 | } 72 | 73 | /// 74 | /// Creates an instance of the OpenAIAPI configured for Azure with the specified URL and API key. 75 | /// 76 | /// The endpoint URL for the Azure OpenAI service. 77 | /// The API authentication key used to access the service. 78 | /// 79 | /// An instance of OpenAIAPI configured to communicate with the Azure service at the specified URL. 80 | /// 81 | public static OpenAIAPI ForAzure(string url, APIAuthentication apiKey) 82 | { 83 | return new OpenAIAPI(apiKey) { ApiVersion = string.Empty, ApiUrlFormat = url }; 84 | } 85 | 86 | /// 87 | /// Text generation is the core function of the API. You give the API a prompt, and it generates a completion. The way you “program” the API to do a task is by simply describing the task in plain english or providing a few written examples. This simple approach works for a wide range of use cases, including summarization, translation, grammar correction, question answering, chatbots, composing emails, and much more (see the prompt library for inspiration). 88 | /// 89 | public ICompletionEndpoint Completions { get; } 90 | 91 | /// 92 | /// The API lets you transform text into a vector (list) of floating point numbers. The distance between two vectors measures their relatedness. Small distances suggest high relatedness and large distances suggest low relatedness. 93 | /// 94 | public IEmbeddingEndpoint Embeddings { get; } 95 | 96 | /// 97 | /// Text generation in the form of chat messages. This interacts with the ChatGPT API. 98 | /// 99 | public IChatEndpoint Chat { get; } 100 | 101 | /// 102 | /// Classify text against the OpenAI Content Policy. 103 | /// 104 | public IModerationEndpoint Moderation { get; } 105 | 106 | /// 107 | /// The API endpoint for querying available Engines/models 108 | /// 109 | public IModelsEndpoint Models { get; } 110 | 111 | /// 112 | /// The API lets you do operations with files. You can upload, delete or retrieve files. Files can be used for fine-tuning, search, etc. 113 | /// 114 | public IFilesEndpoint Files { get; } 115 | 116 | /// 117 | /// The API lets you do operations with images. Given a prompt and/or an input image, the model will generate a new image. 118 | /// 119 | public IImageGenerationEndpoint ImageGenerations { get; } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /OpenAI_API/OpenAI_API.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.0 5 | 8.0 6 | False 7 | 8 | OpenAI API 9 | A simple C# / .NET library to use with OpenAI's APIs, including GPT 3.5, GPT 4, ChatGPT, DALL-E, etc. Independently developed, this is not an official library and I am not affiliated with OpenAI. An OpenAI or Azure OpenAI account is required. 10 | This library is licensed CC-0, in the public domain 11 | CC0-1.0 12 | 13 | 14 | OpenAI, AI, ML, API, ChatGPT, DALLE, GPT3, GPT-3, GPT4, GPT-4, DALL-E 15 | OpenAI API 16 | 17 | OpenAI 18 | 1.8 19 | 1.8.0.0 20 | 1.8.0.0 21 | True 22 | 23 | True 24 | git 25 | 26 | 27 | true 28 | 29 | true 30 | snupkg 31 | 32 | true 33 | 34 | 35 | 36 | 37 | 38 | False 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /OpenAI_API/Usage.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace OpenAI_API 7 | { 8 | /// 9 | /// Usage statistics of how many tokens have been used for this request. 10 | /// 11 | public class Usage 12 | { 13 | /// 14 | /// How many tokens did the prompt consist of 15 | /// 16 | [JsonProperty("prompt_tokens")] 17 | public int PromptTokens { get; set; } 18 | 19 | /// 20 | /// How many tokens did the request consume total 21 | /// 22 | [JsonProperty("total_tokens")] 23 | public int TotalTokens { get; set; } 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using JeffPires.VisualChatGPTStudio; 2 | using System.Reflection; 3 | using System.Runtime.InteropServices; 4 | 5 | [assembly: AssemblyTitle(Vsix.Name)] 6 | [assembly: AssemblyDescription(Vsix.Description)] 7 | [assembly: AssemblyConfiguration("")] 8 | [assembly: AssemblyCompany(Vsix.Author)] 9 | [assembly: AssemblyProduct(Vsix.Name)] 10 | [assembly: AssemblyCopyright(Vsix.Author)] 11 | [assembly: AssemblyTrademark("")] 12 | [assembly: AssemblyCulture("")] 13 | 14 | [assembly: ComVisible(false)] 15 | 16 | [assembly: AssemblyVersion(Vsix.Version)] 17 | [assembly: AssemblyFileVersion(Vsix.Version)] 18 | 19 | namespace System.Runtime.CompilerServices 20 | { 21 | public class IsExternalInit { } 22 | } -------------------------------------------------------------------------------- /Resources/DB.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/DB.png -------------------------------------------------------------------------------- /Resources/FileTypes/config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/FileTypes/config.png -------------------------------------------------------------------------------- /Resources/FileTypes/cs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/FileTypes/cs.png -------------------------------------------------------------------------------- /Resources/FileTypes/css.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/FileTypes/css.png -------------------------------------------------------------------------------- /Resources/FileTypes/folder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/FileTypes/folder.png -------------------------------------------------------------------------------- /Resources/FileTypes/html.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/FileTypes/html.png -------------------------------------------------------------------------------- /Resources/FileTypes/js.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/FileTypes/js.png -------------------------------------------------------------------------------- /Resources/FileTypes/json.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/FileTypes/json.png -------------------------------------------------------------------------------- /Resources/FileTypes/md.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/FileTypes/md.png -------------------------------------------------------------------------------- /Resources/FileTypes/sln.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/FileTypes/sln.png -------------------------------------------------------------------------------- /Resources/FileTypes/sql.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/FileTypes/sql.png -------------------------------------------------------------------------------- /Resources/FileTypes/ts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/FileTypes/ts.png -------------------------------------------------------------------------------- /Resources/FileTypes/vb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/FileTypes/vb.png -------------------------------------------------------------------------------- /Resources/FileTypes/vs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/FileTypes/vs.png -------------------------------------------------------------------------------- /Resources/FileTypes/xaml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/FileTypes/xaml.png -------------------------------------------------------------------------------- /Resources/FileTypes/xml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/FileTypes/xml.png -------------------------------------------------------------------------------- /Resources/Highlighting.xshd: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Resources/Icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/Icon.png -------------------------------------------------------------------------------- /Resources/addComments.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/addComments.png -------------------------------------------------------------------------------- /Resources/addSummary.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/addSummary.png -------------------------------------------------------------------------------- /Resources/addSummaryForAll.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/addSummaryForAll.png -------------------------------------------------------------------------------- /Resources/addTests.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/addTests.png -------------------------------------------------------------------------------- /Resources/api.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/api.png -------------------------------------------------------------------------------- /Resources/askAnything.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/askAnything.png -------------------------------------------------------------------------------- /Resources/bot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/bot.png -------------------------------------------------------------------------------- /Resources/cancel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/cancel.png -------------------------------------------------------------------------------- /Resources/cancelCommand.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/cancelCommand.png -------------------------------------------------------------------------------- /Resources/chatGPT.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/chatGPT.png -------------------------------------------------------------------------------- /Resources/check.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/check.png -------------------------------------------------------------------------------- /Resources/clear.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/clear.png -------------------------------------------------------------------------------- /Resources/close.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/close.png -------------------------------------------------------------------------------- /Resources/cloud.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/cloud.png -------------------------------------------------------------------------------- /Resources/codeReview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/codeReview.png -------------------------------------------------------------------------------- /Resources/complete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/complete.png -------------------------------------------------------------------------------- /Resources/copy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/copy.png -------------------------------------------------------------------------------- /Resources/customAfter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/customAfter.png -------------------------------------------------------------------------------- /Resources/customBefore.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/customBefore.png -------------------------------------------------------------------------------- /Resources/customReplace.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/customReplace.png -------------------------------------------------------------------------------- /Resources/diffView.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/diffView.png -------------------------------------------------------------------------------- /Resources/edit_color.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/edit_color.png -------------------------------------------------------------------------------- /Resources/explain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/explain.png -------------------------------------------------------------------------------- /Resources/findBugs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/findBugs.png -------------------------------------------------------------------------------- /Resources/garbage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/garbage.png -------------------------------------------------------------------------------- /Resources/garbage_color.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/garbage_color.png -------------------------------------------------------------------------------- /Resources/git.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/git.png -------------------------------------------------------------------------------- /Resources/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/image.png -------------------------------------------------------------------------------- /Resources/information.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/information.png -------------------------------------------------------------------------------- /Resources/optimize.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/optimize.png -------------------------------------------------------------------------------- /Resources/optimizeDiffView.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/optimizeDiffView.png -------------------------------------------------------------------------------- /Resources/paste.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/paste.png -------------------------------------------------------------------------------- /Resources/send.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/send.png -------------------------------------------------------------------------------- /Resources/sendCode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/sendCode.png -------------------------------------------------------------------------------- /Resources/solutionContext.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/solutionContext.png -------------------------------------------------------------------------------- /Resources/translate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/translate.png -------------------------------------------------------------------------------- /Resources/turbo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/turbo.png -------------------------------------------------------------------------------- /Resources/vs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/vs.png -------------------------------------------------------------------------------- /Resources/wordWrap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/Resources/wordWrap.png -------------------------------------------------------------------------------- /VSCommandTable.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------ 2 | // 3 | // This file was generated by the free extension VSIX Synchronizer 4 | // 5 | // ------------------------------------------------------------------------------ 6 | using System; 7 | 8 | namespace JeffPires.VisualChatGPTStudio 9 | { 10 | /// 11 | /// Helper class that exposes all GUIDs used across VS Package. 12 | /// 13 | internal sealed partial class PackageGuids 14 | { 15 | public const string VisuallChatGPTStudioString = "a8eb0343-b35c-4175-95d8-50549e609ff3"; 16 | public static Guid VisuallChatGPTStudio = new Guid(VisuallChatGPTStudioString); 17 | } 18 | 19 | /// 20 | /// Helper class that encapsulates all CommandIDs uses across VS Package. 21 | /// 22 | internal sealed partial class PackageIds 23 | { 24 | public const int Complete = 0x0000; 25 | public const int AddTests = 0x0100; 26 | public const int FindBugs = 0x0200; 27 | public const int Optimize = 0x0300; 28 | public const int OptimizeDiffView = 0x0301; 29 | public const int Explain = 0x0400; 30 | public const int AddComments = 0x0500; 31 | public const int AddSummary = 0x0600; 32 | public const int AddSummaryForAll = 0x0601; 33 | public const int AskAnything = 0x0700; 34 | public const int Translate = 0x0701; 35 | public const int CustomBefore = 0x0800; 36 | public const int CustomAfter = 0x0900; 37 | public const int CustomReplace = 0x0901; 38 | public const int Cancel = 0x0999; 39 | } 40 | } -------------------------------------------------------------------------------- /VisualChatGPTStudio.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.3.32929.385 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisualChatGPTStudio", "VisualChatGPTStudio.csproj", "{E0B7C5AC-2E05-4849-81E4-8BF62BBCD16A}" 7 | EndProject 8 | Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "VisualChatGPTStudioShared", "VisualChatGPTStudioShared\VisualChatGPTStudioShared.shproj", "{8D02BB3E-2996-4B5C-A4BE-C1514B7B93DF}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisualChatGPTStudio2019", "VisualChatGPTStudio2019\VisualChatGPTStudio2019.csproj", "{17478673-FB35-4FF3-8EA2-9B2849B490BD}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI_API", "OpenAI_API\OpenAI_API.csproj", "{FB118D8F-18E1-4B77-9882-4EE77469CB8D}" 13 | EndProject 14 | Global 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 16 | Debug|Any CPU = Debug|Any CPU 17 | Debug|arm64 = Debug|arm64 18 | Debug|x86 = Debug|x86 19 | Release|Any CPU = Release|Any CPU 20 | Release|arm64 = Release|arm64 21 | Release|x86 = Release|x86 22 | EndGlobalSection 23 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 24 | {E0B7C5AC-2E05-4849-81E4-8BF62BBCD16A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {E0B7C5AC-2E05-4849-81E4-8BF62BBCD16A}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {E0B7C5AC-2E05-4849-81E4-8BF62BBCD16A}.Debug|arm64.ActiveCfg = Debug|arm64 27 | {E0B7C5AC-2E05-4849-81E4-8BF62BBCD16A}.Debug|arm64.Build.0 = Debug|arm64 28 | {E0B7C5AC-2E05-4849-81E4-8BF62BBCD16A}.Debug|x86.ActiveCfg = Debug|x86 29 | {E0B7C5AC-2E05-4849-81E4-8BF62BBCD16A}.Debug|x86.Build.0 = Debug|x86 30 | {E0B7C5AC-2E05-4849-81E4-8BF62BBCD16A}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {E0B7C5AC-2E05-4849-81E4-8BF62BBCD16A}.Release|Any CPU.Build.0 = Release|Any CPU 32 | {E0B7C5AC-2E05-4849-81E4-8BF62BBCD16A}.Release|arm64.ActiveCfg = Release|arm64 33 | {E0B7C5AC-2E05-4849-81E4-8BF62BBCD16A}.Release|arm64.Build.0 = Release|arm64 34 | {E0B7C5AC-2E05-4849-81E4-8BF62BBCD16A}.Release|x86.ActiveCfg = Release|x86 35 | {E0B7C5AC-2E05-4849-81E4-8BF62BBCD16A}.Release|x86.Build.0 = Release|x86 36 | {17478673-FB35-4FF3-8EA2-9B2849B490BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 37 | {17478673-FB35-4FF3-8EA2-9B2849B490BD}.Debug|Any CPU.Build.0 = Debug|Any CPU 38 | {17478673-FB35-4FF3-8EA2-9B2849B490BD}.Debug|arm64.ActiveCfg = Debug|arm64 39 | {17478673-FB35-4FF3-8EA2-9B2849B490BD}.Debug|arm64.Build.0 = Debug|arm64 40 | {17478673-FB35-4FF3-8EA2-9B2849B490BD}.Debug|x86.ActiveCfg = Debug|x86 41 | {17478673-FB35-4FF3-8EA2-9B2849B490BD}.Debug|x86.Build.0 = Debug|x86 42 | {17478673-FB35-4FF3-8EA2-9B2849B490BD}.Release|Any CPU.ActiveCfg = Release|Any CPU 43 | {17478673-FB35-4FF3-8EA2-9B2849B490BD}.Release|Any CPU.Build.0 = Release|Any CPU 44 | {17478673-FB35-4FF3-8EA2-9B2849B490BD}.Release|arm64.ActiveCfg = Release|arm64 45 | {17478673-FB35-4FF3-8EA2-9B2849B490BD}.Release|arm64.Build.0 = Release|arm64 46 | {17478673-FB35-4FF3-8EA2-9B2849B490BD}.Release|x86.ActiveCfg = Release|x86 47 | {17478673-FB35-4FF3-8EA2-9B2849B490BD}.Release|x86.Build.0 = Release|x86 48 | {FB118D8F-18E1-4B77-9882-4EE77469CB8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 49 | {FB118D8F-18E1-4B77-9882-4EE77469CB8D}.Debug|Any CPU.Build.0 = Debug|Any CPU 50 | {FB118D8F-18E1-4B77-9882-4EE77469CB8D}.Debug|arm64.ActiveCfg = Debug|Any CPU 51 | {FB118D8F-18E1-4B77-9882-4EE77469CB8D}.Debug|arm64.Build.0 = Debug|Any CPU 52 | {FB118D8F-18E1-4B77-9882-4EE77469CB8D}.Debug|x86.ActiveCfg = Debug|Any CPU 53 | {FB118D8F-18E1-4B77-9882-4EE77469CB8D}.Debug|x86.Build.0 = Debug|Any CPU 54 | {FB118D8F-18E1-4B77-9882-4EE77469CB8D}.Release|Any CPU.ActiveCfg = Release|Any CPU 55 | {FB118D8F-18E1-4B77-9882-4EE77469CB8D}.Release|Any CPU.Build.0 = Release|Any CPU 56 | {FB118D8F-18E1-4B77-9882-4EE77469CB8D}.Release|arm64.ActiveCfg = Release|Any CPU 57 | {FB118D8F-18E1-4B77-9882-4EE77469CB8D}.Release|arm64.Build.0 = Release|Any CPU 58 | {FB118D8F-18E1-4B77-9882-4EE77469CB8D}.Release|x86.ActiveCfg = Release|Any CPU 59 | {FB118D8F-18E1-4B77-9882-4EE77469CB8D}.Release|x86.Build.0 = Release|Any CPU 60 | EndGlobalSection 61 | GlobalSection(SolutionProperties) = preSolution 62 | HideSolutionNode = FALSE 63 | EndGlobalSection 64 | GlobalSection(ExtensibilityGlobals) = postSolution 65 | SolutionGuid = {D6B55750-5329-496A-8EAD-898A2B73D370} 66 | EndGlobalSection 67 | GlobalSection(SharedMSBuildProjectFiles) = preSolution 68 | VisualChatGPTStudioShared\VisualChatGPTStudioShared.projitems*{17478673-fb35-4ff3-8ea2-9b2849b490bd}*SharedItemsImports = 4 69 | VisualChatGPTStudioShared\VisualChatGPTStudioShared.projitems*{8d02bb3e-2996-4b5c-a4be-c1514b7b93df}*SharedItemsImports = 13 70 | VisualChatGPTStudioShared\VisualChatGPTStudioShared.projitems*{e0b7c5ac-2e05-4849-81e4-8bf62bbcd16a}*SharedItemsImports = 4 71 | EndGlobalSection 72 | EndGlobal 73 | -------------------------------------------------------------------------------- /VisualChatGPTStudio2019/Icon2019.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/VisualChatGPTStudio2019/Icon2019.png -------------------------------------------------------------------------------- /VisualChatGPTStudio2019/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using JeffPires.VisualChatGPTStudio; 2 | using System.Reflection; 3 | using System.Runtime.InteropServices; 4 | 5 | [assembly: AssemblyTitle(Vsix.Name)] 6 | [assembly: AssemblyDescription(Vsix.Description)] 7 | [assembly: AssemblyConfiguration("")] 8 | [assembly: AssemblyCompany(Vsix.Author)] 9 | [assembly: AssemblyProduct(Vsix.Name)] 10 | [assembly: AssemblyCopyright(Vsix.Author)] 11 | [assembly: AssemblyTrademark("")] 12 | [assembly: AssemblyCulture("")] 13 | 14 | [assembly: ComVisible(false)] 15 | 16 | [assembly: AssemblyVersion(Vsix.Version)] 17 | [assembly: AssemblyFileVersion(Vsix.Version)] 18 | 19 | namespace System.Runtime.CompilerServices 20 | { 21 | public class IsExternalInit { } 22 | } -------------------------------------------------------------------------------- /VisualChatGPTStudio2019/Resources/Icon2019.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffdapaz/VisualChatGPTStudio/2c3bd8d3d4b0320af2f014519e38b5b32b3f9bca/VisualChatGPTStudio2019/Resources/Icon2019.png -------------------------------------------------------------------------------- /VisualChatGPTStudio2019/source.extension.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------ 2 | // 3 | // This file was generated by the extension VSIX Synchronizer 4 | // 5 | // ------------------------------------------------------------------------------ 6 | namespace JeffPires.VisualChatGPTStudio 7 | { 8 | internal sealed partial class Vsix 9 | { 10 | public const string Id = "VisuallChatGPTStudio.4854bc98-735d-4622-a0cf-30454b50aa0f"; 11 | public const string Name = "VisuallChatGPTStudio"; 12 | public const string Description = @"Empty VSIX Project."; 13 | public const string Language = "en-US"; 14 | public const string Version = "1.0"; 15 | public const string Author = "Avanade"; 16 | public const string Tags = ""; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /VisualChatGPTStudio2019/source.extension.vsixmanifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Visual chatGPT Studio 2019 6 | Visual Studio extension that integrates advanced AI capabilities, enabling tasks like code suggestions, bug detection, optimization, unit test generation, and more directly within the development environment. 7 | Icon2019.png 8 | Resources\Icon.png 9 | Visual Studio, chatGPT, CoPilot, OpenAI 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Agents/ApiAgent/ApiItem.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace VisualChatGPTStudioShared.Agents.ApiAgent 4 | { 5 | /// 6 | /// Represents an API definition. 7 | /// 8 | public class ApiItem 9 | { 10 | /// 11 | /// API definition ID 12 | /// 13 | public string Id { get; set; } 14 | 15 | /// 16 | /// Gets or sets the API's name. 17 | /// 18 | public string Name { get; set; } 19 | 20 | /// 21 | /// Gets or sets the API's base URL. 22 | /// 23 | public string BaseUrl { get; set; } 24 | 25 | /// 26 | /// If true, all responses will be send to AI, when false, only HTTP status will be send. 27 | /// 28 | public bool SendResponsesToAI { get; set; } 29 | 30 | /// 31 | /// Gets or sets the collection of API tag items. 32 | /// 33 | public List Tags { get; set; } 34 | 35 | /// 36 | /// Gets or sets the API's definition. 37 | /// 38 | public string Definition { get; set; } 39 | } 40 | } -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Agents/ApiAgent/ApiTagItem.cs: -------------------------------------------------------------------------------- 1 | namespace VisualChatGPTStudioShared.Agents.ApiAgent 2 | { 3 | /// 4 | /// Represents a tag used in an API context. 5 | /// 6 | public class ApiTagItem 7 | { 8 | private int type; 9 | 10 | /// 11 | /// Gets or sets the tag key. 12 | /// 13 | public string Key { get; set; } 14 | 15 | /// 16 | /// Gets or sets the tag value. 17 | /// 18 | public string Value { get; set; } 19 | 20 | /// 21 | /// Gets or sets the type of the API tag, represented by the enumeration. 22 | /// 23 | public ApiTagType Type 24 | { 25 | get 26 | { 27 | return (ApiTagType)type; 28 | } 29 | set 30 | { 31 | type = (int)value; 32 | } 33 | } 34 | 35 | /// 36 | /// Gets or sets the type as an integer. 37 | /// 38 | public int TypeAsInteger 39 | { 40 | get 41 | { 42 | return type; 43 | } 44 | set 45 | { 46 | type = value; 47 | } 48 | } 49 | } 50 | 51 | /// 52 | /// Represents the types of API tags, such as Header or QueryString, used for categorizing API parameters. 53 | /// 54 | public enum ApiTagType 55 | { 56 | Header = 0, 57 | QueryString = 1 58 | } 59 | } -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/AddComments.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; 2 | using JeffPires.VisualChatGPTStudio.Commands; 3 | using JeffPires.VisualChatGPTStudio.Options.Commands; 4 | using JeffPires.VisualChatGPTStudio.Utils; 5 | 6 | namespace JeffPires.VisualChatGPTStudio 7 | { 8 | [Command(PackageIds.AddComments)] 9 | internal sealed class AddComments : BaseGenericCommand 10 | { 11 | protected override CommandType GetCommandType(string selectedText) 12 | { 13 | if (CodeContainsMultipleLines(selectedText)) 14 | { 15 | return CommandType.Replace; 16 | } 17 | 18 | return CommandType.InsertBefore; 19 | } 20 | 21 | protected override string GetCommand(string selectedText) 22 | { 23 | if (CodeContainsMultipleLines(selectedText)) 24 | { 25 | return OptionsCommands.GetCommandAsync(CommandsType.AddCommentsForLines).Result; 26 | } 27 | 28 | return OptionsCommands.GetCommandAsync(CommandsType.AddCommentsForLine).Result; 29 | } 30 | 31 | private bool CodeContainsMultipleLines(string code) 32 | { 33 | return code.Contains("\r\n") || code.Contains("\n") || code.Contains("\r"); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/AddSummary.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; 2 | using JeffPires.VisualChatGPTStudio.Commands; 3 | using JeffPires.VisualChatGPTStudio.Options.Commands; 4 | using JeffPires.VisualChatGPTStudio.Utils; 5 | 6 | namespace JeffPires.VisualChatGPTStudio 7 | { 8 | [Command(PackageIds.AddSummary)] 9 | internal sealed class AddSummary : BaseGenericCommand 10 | { 11 | protected override CommandType GetCommandType(string selectedText) 12 | { 13 | return CommandType.InsertBefore; 14 | } 15 | 16 | protected override string GetCommand(string selectedText) 17 | { 18 | string command = OptionsCommands.GetCommandAsync(CommandsType.AddSummary).Result; 19 | 20 | if (string.IsNullOrWhiteSpace(command)) 21 | { 22 | return string.Empty; 23 | } 24 | 25 | return TextFormat.FormatCommandForSummary($"{command}\r\n\r\n{{0}}\r\n\r\n", selectedText); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/AddTests.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; 2 | using EnvDTE; 3 | using JeffPires.VisualChatGPTStudio.Commands; 4 | using JeffPires.VisualChatGPTStudio.Options.Commands; 5 | using JeffPires.VisualChatGPTStudio.Utils; 6 | 7 | namespace JeffPires.VisualChatGPTStudio 8 | { 9 | [Command(PackageIds.AddTests)] 10 | internal sealed class AddTests : BaseGenericCommand 11 | { 12 | protected override CommandType GetCommandType(string selectedText) 13 | { 14 | return CommandType.InsertAfter; 15 | } 16 | 17 | protected override string GetCommand(string selectedText) 18 | { 19 | return OptionsCommands.GetCommandAsync(CommandsType.AddTests).Result + Utils.Constants.PROVIDE_ONLY_CODE_INSTRUCTION; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/AskAnything.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; 2 | using JeffPires.VisualChatGPTStudio.Commands; 3 | using JeffPires.VisualChatGPTStudio.Utils; 4 | 5 | namespace JeffPires.VisualChatGPTStudio 6 | { 7 | [Command(PackageIds.AskAnything)] 8 | internal sealed class AskAnything : BaseGenericCommand 9 | { 10 | protected override CommandType GetCommandType(string selectedText) 11 | { 12 | return CommandType.InsertAfter; 13 | } 14 | 15 | protected override string GetCommand(string selectedText) 16 | { 17 | return OptionsGeneral.ToolWindowSystemMessage; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/BaseCommand.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; 2 | using EnvDTE; 3 | using JeffPires.VisualChatGPTStudio.Options; 4 | using JeffPires.VisualChatGPTStudio.Options.Commands; 5 | using Microsoft.VisualStudio.Shell; 6 | using System; 7 | using System.Threading; 8 | using Constants = JeffPires.VisualChatGPTStudio.Utils.Constants; 9 | 10 | namespace JeffPires.VisualChatGPTStudio.Commands 11 | { 12 | /// 13 | /// Base abstract class for commands 14 | /// 15 | /// The type of the command. 16 | internal abstract class BaseCommand : Community.VisualStudio.Toolkit.BaseCommand where TCommand : class, new() 17 | { 18 | /// 19 | /// Gets or sets the cancellation token source. 20 | /// 21 | protected CancellationTokenSource CancellationTokenSource 22 | { 23 | get 24 | { 25 | return ((VisuallChatGPTStudioPackage)this.Package).CancellationTokenSource; 26 | } 27 | set 28 | { 29 | ((VisuallChatGPTStudioPackage)this.Package).CancellationTokenSource = value; 30 | } 31 | } 32 | 33 | /// 34 | /// Gets the OptionsGeneral property of the VisualChatGPTStudioPackage. 35 | /// 36 | protected OptionPageGridGeneral OptionsGeneral 37 | { 38 | get 39 | { 40 | return ((VisuallChatGPTStudioPackage)this.Package).OptionsGeneral; 41 | } 42 | } 43 | 44 | /// 45 | /// Gets the OptionsCommands property of the VisualChatGPTStudioPackage. 46 | /// 47 | protected OptionCommands OptionsCommands 48 | { 49 | get 50 | { 51 | return ((VisuallChatGPTStudioPackage)this.Package).OptionsCommands; 52 | } 53 | } 54 | 55 | /// 56 | /// Gets the DTE object. 57 | /// 58 | /// The DTE object. 59 | protected async System.Threading.Tasks.Task GetDTEAsync() 60 | { 61 | return await VS.GetServiceAsync(); 62 | } 63 | 64 | /// 65 | /// Validates the API key stored in the OptionsGeneral class. 66 | /// 67 | /// 68 | /// Returns true if the API key is valid, false otherwise. 69 | /// 70 | protected bool ValidateAPIKey() 71 | { 72 | if (OptionsGeneral.AzureEntraIdAuthentication) 73 | { 74 | return true; 75 | } 76 | 77 | if (string.IsNullOrWhiteSpace(OptionsGeneral.ApiKey)) 78 | { 79 | System.Windows.Forms.MessageBox.Show(Constants.MESSAGE_SET_API_KEY, Constants.EXTENSION_NAME); 80 | 81 | Package.ShowOptionPage(typeof(OptionPageGridGeneral)); 82 | 83 | return false; 84 | } 85 | 86 | return true; 87 | } 88 | 89 | /// 90 | /// Validates the code selected by the user. 91 | /// 92 | /// The selected code. 93 | /// True if the code is valid, false otherwise. 94 | protected bool ValidateCodeSelected(string selectedCode) 95 | { 96 | if (string.IsNullOrWhiteSpace(selectedCode)) 97 | { 98 | System.Windows.Forms.MessageBox.Show("Please select the code.", Constants.EXTENSION_NAME); 99 | 100 | return false; 101 | } 102 | 103 | return true; 104 | } 105 | 106 | /// 107 | /// Formats the document. 108 | /// 109 | protected async System.Threading.Tasks.Task FormatDocumentAsync() 110 | { 111 | try 112 | { 113 | await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); 114 | 115 | (await GetDTEAsync()).ExecuteCommand(Constants.EDIT_DOCUMENT_COMMAND); 116 | } 117 | catch (Exception) //Some documents do not support formatting 118 | { 119 | 120 | } 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/Cancel.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; using Microsoft.VisualStudio.Shell; namespace JeffPires.VisualChatGPTStudio.Commands { 2 | /// 3 | /// Command to cancel all another commands that is running. 4 | /// 5 | [Command(PackageIds.Cancel)] internal sealed class Cancel : BaseCommand { 6 | /// 7 | /// Executes the command asynchronously. 8 | /// 9 | protected override async System.Threading.Tasks.Task ExecuteAsync(OleMenuCmdEventArgs e) { CancellationTokenSource?.Cancel(); } } } -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/Complete.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; 2 | using EnvDTE; 3 | using JeffPires.VisualChatGPTStudio.Options.Commands; 4 | using JeffPires.VisualChatGPTStudio.Utils; 5 | 6 | namespace JeffPires.VisualChatGPTStudio.Commands 7 | { 8 | [Command(PackageIds.Complete)] 9 | internal sealed class Complete : BaseGenericCommand 10 | { 11 | protected override CommandType GetCommandType(string selectedText) 12 | { 13 | return CommandType.InsertAfter; 14 | } 15 | 16 | protected override string GetCommand(string selectedText) 17 | { 18 | if (string.IsNullOrWhiteSpace(selectedText)) 19 | { 20 | return string.Empty; 21 | } 22 | 23 | return TextFormat.FormatForCompleteCommand(OptionsCommands.GetCommandAsync(CommandsType.Complete).Result + Utils.Constants.PROVIDE_ONLY_CODE_INSTRUCTION, docView.FilePath); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/CustomAfter.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; 2 | using JeffPires.VisualChatGPTStudio.Commands; 3 | using JeffPires.VisualChatGPTStudio.Options.Commands; 4 | using JeffPires.VisualChatGPTStudio.Utils; 5 | 6 | namespace JeffPires.VisualChatGPTStudio 7 | { 8 | [Command(PackageIds.CustomAfter)] 9 | internal sealed class CustomAfter : BaseGenericCommand 10 | { 11 | protected override CommandType GetCommandType(string selectedText) 12 | { 13 | return CommandType.InsertAfter; 14 | } 15 | 16 | protected override string GetCommand(string selectedText) 17 | { 18 | return OptionsCommands.GetCommandAsync(CommandsType.CustomAfter).Result; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/CustomBefore.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; 2 | using JeffPires.VisualChatGPTStudio.Commands; 3 | using JeffPires.VisualChatGPTStudio.Options.Commands; 4 | using JeffPires.VisualChatGPTStudio.Utils; 5 | 6 | namespace JeffPires.VisualChatGPTStudio 7 | { 8 | [Command(PackageIds.CustomBefore)] 9 | internal sealed class CustomBefore : BaseGenericCommand 10 | { 11 | protected override CommandType GetCommandType(string selectedText) 12 | { 13 | return CommandType.InsertBefore; 14 | } 15 | 16 | protected override string GetCommand(string selectedText) 17 | { 18 | return OptionsCommands.GetCommandAsync(CommandsType.CustomBefore).Result; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/CustomReplace.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; 2 | using JeffPires.VisualChatGPTStudio.Commands; 3 | using JeffPires.VisualChatGPTStudio.Options.Commands; 4 | using JeffPires.VisualChatGPTStudio.Utils; 5 | 6 | namespace JeffPires.VisualChatGPTStudio 7 | { 8 | [Command(PackageIds.CustomReplace)] 9 | internal sealed class CustomReplace : BaseGenericCommand 10 | { 11 | protected override CommandType GetCommandType(string selectedText) 12 | { 13 | return CommandType.Replace; 14 | } 15 | 16 | protected override string GetCommand(string selectedText) 17 | { 18 | return OptionsCommands.GetCommandAsync(CommandsType.CustomReplace).Result; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/Explain.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; 2 | using JeffPires.VisualChatGPTStudio.Commands; 3 | using JeffPires.VisualChatGPTStudio.Options.Commands; 4 | using JeffPires.VisualChatGPTStudio.Utils; 5 | 6 | namespace JeffPires.VisualChatGPTStudio 7 | { 8 | [Command(PackageIds.Explain)] 9 | internal sealed class Explain : BaseGenericCommand 10 | { 11 | protected override CommandType GetCommandType(string selectedText) 12 | { 13 | return CommandType.InsertBefore; 14 | } 15 | 16 | protected override string GetCommand(string selectedText) 17 | { 18 | return OptionsCommands.GetCommandAsync(CommandsType.Explain).Result; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/FindBugs.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; 2 | using JeffPires.VisualChatGPTStudio.Commands; 3 | using JeffPires.VisualChatGPTStudio.Options.Commands; 4 | using JeffPires.VisualChatGPTStudio.Utils; 5 | 6 | namespace JeffPires.VisualChatGPTStudio 7 | { 8 | [Command(PackageIds.FindBugs)] 9 | internal sealed class FindBugs : BaseGenericCommand 10 | { 11 | protected override CommandType GetCommandType(string selectedText) 12 | { 13 | return CommandType.InsertAfter; 14 | } 15 | 16 | protected override string GetCommand(string selectedText) 17 | { 18 | return OptionsCommands.GetCommandAsync(CommandsType.FindBugs).Result; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/Optimize.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; 2 | using JeffPires.VisualChatGPTStudio.Commands; 3 | using JeffPires.VisualChatGPTStudio.Options.Commands; 4 | using JeffPires.VisualChatGPTStudio.Utils; 5 | 6 | namespace JeffPires.VisualChatGPTStudio 7 | { 8 | [Command(PackageIds.Optimize)] 9 | internal sealed class Optimize : BaseGenericCommand 10 | { 11 | protected override CommandType GetCommandType(string selectedText) 12 | { 13 | return CommandType.Replace; 14 | } 15 | 16 | protected override string GetCommand(string selectedText) 17 | { 18 | return OptionsCommands.GetCommandAsync(CommandsType.Optimize).Result + Utils.Constants.PROVIDE_ONLY_CODE_INSTRUCTION; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/OptimizeDiffView.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; 2 | using JeffPires.VisualChatGPTStudio.Options.Commands; 3 | using JeffPires.VisualChatGPTStudio.Utils; 4 | using JeffPires.VisualChatGPTStudio.Utils.API; 5 | using Microsoft.VisualStudio.Shell; 6 | using System; 7 | using System.Threading; 8 | using System.Windows.Forms; 9 | using VisualChatGPTStudioShared.Utils; 10 | 11 | namespace JeffPires.VisualChatGPTStudio.Commands 12 | { 13 | /// 14 | /// Command to add summary for the entire class. 15 | /// 16 | [Command(PackageIds.OptimizeDiffView)] 17 | internal sealed class OptimizeDiffView : BaseCommand 18 | { 19 | /// 20 | /// Executes the ChatGPT optimization process for the selected code and shows on a diff view. 21 | /// 22 | protected override async System.Threading.Tasks.Task ExecuteAsync(OleMenuCmdEventArgs e) 23 | { 24 | try 25 | { 26 | if (!ValidateAPIKey()) 27 | { 28 | return; 29 | } 30 | 31 | string command = await OptionsCommands.GetCommandAsync(CommandsType.Optimize); 32 | 33 | if (string.IsNullOrWhiteSpace(command)) 34 | { 35 | System.Windows.MessageBox.Show(string.Format(Constants.MESSAGE_SET_COMMAND, nameof(Optimize)), Constants.EXTENSION_NAME); 36 | 37 | return; 38 | } 39 | 40 | DocumentView docView = await VS.Documents.GetActiveDocumentViewAsync(); 41 | 42 | string selectedText = docView.TextView.Selection.StreamSelectionSpan.GetText(); 43 | 44 | if (!ValidateCodeSelected(selectedText)) 45 | { 46 | return; 47 | } 48 | 49 | await VS.StatusBar.ShowProgressAsync(Constants.MESSAGE_WAITING_CHATGPT, 1, 2); 50 | 51 | CancellationTokenSource = new CancellationTokenSource(); 52 | 53 | string result = OptionsGeneral.UseCompletion && OptionsGeneral.Service == OpenAIService.OpenAI 54 | ? await ApiHandler.GetCompletionResponseAsync(OptionsGeneral, command + Constants.PROVIDE_ONLY_CODE_INSTRUCTION, selectedText, OptionsGeneral.StopSequences?.Split([','], StringSplitOptions.RemoveEmptyEntries), CancellationTokenSource.Token) 55 | : await ApiHandler.GetResponseAsync(OptionsGeneral, command + Constants.PROVIDE_ONLY_CODE_INSTRUCTION, selectedText, OptionsGeneral.StopSequences?.Split([','], StringSplitOptions.RemoveEmptyEntries), CancellationTokenSource.Token); 56 | 57 | result = TextFormat.RemoveCodeTagsFromOpenAIResponses(result.ToString()); 58 | 59 | await DiffView.ShowDiffViewAsync(docView.FilePath, selectedText, result); 60 | 61 | await VS.StatusBar.ShowProgressAsync(Constants.MESSAGE_WAITING_CHATGPT, 2, 2); 62 | } 63 | catch (Exception ex) 64 | { 65 | await VS.StatusBar.ShowProgressAsync(ex.Message, 2, 2); 66 | 67 | if (ex is not OperationCanceledException) 68 | { 69 | Logger.Log(ex); 70 | 71 | System.Windows.Forms.MessageBox.Show(ex.Message, Constants.EXTENSION_NAME, MessageBoxButtons.OK, MessageBoxIcon.Warning); 72 | } 73 | } 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/TerminalWindowCodeReviewCommand.cs: -------------------------------------------------------------------------------- 1 | using JeffPires.VisualChatGPTStudio.ToolWindows; 2 | using Microsoft.VisualStudio.Shell; 3 | using System; 4 | using System.ComponentModel.Design; 5 | 6 | namespace JeffPires.VisualChatGPTStudio.Commands 7 | { 8 | /// 9 | /// Command handler 10 | /// 11 | internal sealed class TerminalWindowCodeReviewCommand 12 | { 13 | /// 14 | /// Command ID. 15 | /// 16 | public const int CommandId = 259; 17 | 18 | /// 19 | /// Command menu group (command set GUID). 20 | /// 21 | public static readonly Guid CommandSet = new("8b0b1a54-4655-4dae-8984-022f82a739f2"); 22 | 23 | /// 24 | /// VS Package that provides this command, not null. 25 | /// 26 | private readonly AsyncPackage package; 27 | 28 | /// 29 | /// Initializes a new instance of the class. 30 | /// Adds our command handlers for menu (commands must exist in the command table file) 31 | /// 32 | /// Owner package, not null. 33 | /// Command service to add command to, not null. 34 | private TerminalWindowCodeReviewCommand(AsyncPackage package, OleMenuCommandService commandService) 35 | { 36 | this.package = package ?? throw new ArgumentNullException(nameof(package)); 37 | commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); 38 | 39 | CommandID menuCommandID = new(CommandSet, CommandId); 40 | MenuCommand menuItem = new(this.Execute, menuCommandID); 41 | commandService.AddCommand(menuItem); 42 | } 43 | 44 | /// 45 | /// Gets the instance of the command. 46 | /// 47 | public static TerminalWindowCodeReviewCommand Instance 48 | { 49 | get; 50 | private set; 51 | } 52 | 53 | /// 54 | /// Initializes the singleton instance of the command. 55 | /// 56 | /// Owner package, not null. 57 | public static async System.Threading.Tasks.Task InitializeAsync(AsyncPackage package) 58 | { 59 | // Switch to the main thread - the call to AddCommand in TerminalWindowCommand's constructor requires 60 | // the UI thread. 61 | await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); 62 | 63 | OleMenuCommandService commandService = await package.GetServiceAsync((typeof(IMenuCommandService))) as OleMenuCommandService; 64 | Instance = new TerminalWindowCodeReviewCommand(package, commandService); 65 | } 66 | 67 | /// 68 | /// Shows the tool window when the menu item is clicked. 69 | /// 70 | /// The event sender. 71 | /// The event args. 72 | private void Execute(object sender, EventArgs e) 73 | { 74 | _ = this.package.JoinableTaskFactory.RunAsync(async delegate 75 | { 76 | await package.ShowToolWindowAsync(typeof(TerminalWindowCodeReview), 0, true, package.DisposalToken); 77 | }); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/TerminalWindowCommand.cs: -------------------------------------------------------------------------------- 1 | using JeffPires.VisualChatGPTStudio.ToolWindows; 2 | using Microsoft.VisualStudio.Shell; 3 | using System; 4 | using System.ComponentModel.Design; 5 | 6 | namespace JeffPires.VisualChatGPTStudio.Commands 7 | { 8 | /// 9 | /// Command handler 10 | /// 11 | internal sealed class TerminalWindowCommand 12 | { 13 | /// 14 | /// Command ID. 15 | /// 16 | public const int CommandId = 256; 17 | 18 | /// 19 | /// Command menu group (command set GUID). 20 | /// 21 | public static readonly Guid CommandSet = new("8b0b1a54-4655-4dae-8984-022f82a739f2"); 22 | 23 | /// 24 | /// VS Package that provides this command, not null. 25 | /// 26 | private readonly AsyncPackage package; 27 | 28 | /// 29 | /// Initializes a new instance of the class. 30 | /// Adds our command handlers for menu (commands must exist in the command table file) 31 | /// 32 | /// Owner package, not null. 33 | /// Command service to add command to, not null. 34 | private TerminalWindowCommand(AsyncPackage package, OleMenuCommandService commandService) 35 | { 36 | this.package = package ?? throw new ArgumentNullException(nameof(package)); 37 | commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); 38 | 39 | CommandID menuCommandID = new(CommandSet, CommandId); 40 | MenuCommand menuItem = new(this.Execute, menuCommandID); 41 | commandService.AddCommand(menuItem); 42 | } 43 | 44 | /// 45 | /// Gets the instance of the command. 46 | /// 47 | public static TerminalWindowCommand Instance 48 | { 49 | get; 50 | private set; 51 | } 52 | 53 | /// 54 | /// Initializes the singleton instance of the command. 55 | /// 56 | /// Owner package, not null. 57 | public static async System.Threading.Tasks.Task InitializeAsync(AsyncPackage package) 58 | { 59 | // Switch to the main thread - the call to AddCommand in TerminalWindowCommand's constructor requires 60 | // the UI thread. 61 | await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); 62 | 63 | OleMenuCommandService commandService = await package.GetServiceAsync((typeof(IMenuCommandService))) as OleMenuCommandService; 64 | Instance = new TerminalWindowCommand(package, commandService); 65 | } 66 | 67 | /// 68 | /// Sends a request to the ChatGPT window. 69 | /// 70 | /// The command to send to the ChatGPT window. 71 | /// The selected text to be sent. 72 | public async System.Threading.Tasks.Task RequestToWindowAsync(string command, string selectedText) 73 | { 74 | TerminalWindow window = await package.ShowToolWindowAsync(typeof(TerminalWindow), 0, true, package.DisposalToken) as TerminalWindow; 75 | 76 | if (window == null) 77 | { 78 | throw new Exception("Please, open the tool window first."); 79 | } 80 | 81 | await window.RequestToWindowAsync(command, selectedText); 82 | } 83 | 84 | /// 85 | /// Shows the tool window when the menu item is clicked. 86 | /// 87 | /// The event sender. 88 | /// The event args. 89 | private void Execute(object sender, EventArgs e) 90 | { 91 | _ = this.package.JoinableTaskFactory.RunAsync(async delegate 92 | { 93 | await package.ShowToolWindowAsync(typeof(TerminalWindow), 0, true, package.DisposalToken); 94 | }); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/TerminalWindowTurboCommand.cs: -------------------------------------------------------------------------------- 1 | using JeffPires.VisualChatGPTStudio.ToolWindows.Turbo; 2 | using Microsoft.VisualStudio.Shell; 3 | using System; 4 | using System.ComponentModel.Design; 5 | 6 | namespace JeffPires.VisualChatGPTStudio.Commands 7 | { 8 | /// 9 | /// Command handler 10 | /// 11 | internal sealed class TerminalWindowTurboCommand 12 | { 13 | /// 14 | /// Command ID. 15 | /// 16 | public const int CommandId = 257; 17 | 18 | /// 19 | /// Command menu group (command set GUID). 20 | /// 21 | public static readonly Guid CommandSet = new Guid("8b0b1a54-4655-4dae-8984-022f82a739f2"); 22 | 23 | /// 24 | /// VS Package that provides this command, not null. 25 | /// 26 | private readonly AsyncPackage package; 27 | 28 | /// 29 | /// Initializes a new instance of the class. 30 | /// Adds our command handlers for menu (commands must exist in the command table file) 31 | /// 32 | /// Owner package, not null. 33 | /// Command service to add command to, not null. 34 | private TerminalWindowTurboCommand(AsyncPackage package, OleMenuCommandService commandService) 35 | { 36 | this.package = package ?? throw new ArgumentNullException(nameof(package)); 37 | commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); 38 | 39 | CommandID menuCommandID = new CommandID(CommandSet, CommandId); 40 | MenuCommand menuItem = new MenuCommand(this.Execute, menuCommandID); 41 | commandService.AddCommand(menuItem); 42 | } 43 | 44 | /// 45 | /// Gets the instance of the command. 46 | /// 47 | public static TerminalWindowTurboCommand Instance 48 | { 49 | get; 50 | private set; 51 | } 52 | 53 | /// 54 | /// Initializes the singleton instance of the command. 55 | /// 56 | /// Owner package, not null. 57 | public static async System.Threading.Tasks.Task InitializeAsync(AsyncPackage package) 58 | { 59 | // Switch to the main thread - the call to AddCommand in TerminalWindowTurboCommand's constructor requires 60 | // the UI thread. 61 | await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); 62 | 63 | OleMenuCommandService commandService = await package.GetServiceAsync((typeof(IMenuCommandService))) as OleMenuCommandService; 64 | Instance = new TerminalWindowTurboCommand(package, commandService); 65 | } 66 | 67 | /// 68 | /// Shows the tool window when the menu item is clicked. 69 | /// 70 | /// The event sender. 71 | /// The event args. 72 | private void Execute(object sender, EventArgs e) 73 | { 74 | _ = this.package.JoinableTaskFactory.RunAsync(async delegate 75 | { 76 | await package.ShowToolWindowAsync(typeof(TerminalWindowTurbo), 0, true, package.DisposalToken); 77 | }); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Commands/Translate.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; 2 | using JeffPires.VisualChatGPTStudio; 3 | using JeffPires.VisualChatGPTStudio.Commands; 4 | using JeffPires.VisualChatGPTStudio.Options.Commands; 5 | using JeffPires.VisualChatGPTStudio.Utils; 6 | 7 | namespace VisualChatGPTStudioShared.Commands 8 | { 9 | [Command(PackageIds.Translate)] 10 | internal sealed class Translate : BaseGenericCommand 11 | { 12 | protected override CommandType GetCommandType(string selectedText) 13 | { 14 | return CommandType.Replace; 15 | } 16 | 17 | protected override string GetCommand(string selectedText) 18 | { 19 | return OptionsCommands.GetCommandAsync(CommandsType.Translate).Result; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Options/ApiAgent/OptionApiAgent.cs: -------------------------------------------------------------------------------- 1 | using JeffPires.VisualChatGPTStudio.Options.ApiAgent; 2 | using Microsoft.VisualStudio.Shell; 3 | using System.Runtime.InteropServices; 4 | using System.Windows; 5 | 6 | namespace VisualChatGPTStudioShared.Options.ApiAgent 7 | { 8 | /// 9 | /// Represents a handling options within a UI element dialog page for the Agents APIs. 10 | /// 11 | [ComVisible(true)] 12 | public class OptionApiAgent : UIElementDialogPage 13 | { 14 | protected override UIElement Child 15 | { 16 | get 17 | { 18 | return new OptionApiAgentWindow(); 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Options/Commands/Commands.cs: -------------------------------------------------------------------------------- 1 | namespace JeffPires.VisualChatGPTStudio.Options.Commands { 2 | /// 3 | /// Represents a class that contains various commands. 4 | /// 5 | public class Commands { public string ProjectName { get; set; } = "SetProjectName"; public string Complete { get; set; } public string AddTests { get; set; } public string FindBugs { get; set; } public string Optimize { get; set; } public string Explain { get; set; } public string AddSummary { get; set; } public string AddCommentsForLine { get; set; } public string AddCommentsForLines { get; set; } public string Translate { get; set; } public string CustomBefore { get; set; } public string CustomAfter { get; set; } public string CustomReplace { get; set; } } } -------------------------------------------------------------------------------- /VisualChatGPTStudioShared/Options/Commands/OptionCommandsWindow.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 39 |