Skip to content

Commit

Permalink
Testing improving code assistant inference with examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
highbyte committed Oct 4, 2024
1 parent c0d11e6 commit 6e382a9
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private void DrawUIItems()
{
try
{
var codeSuggestionBackend = CodeSuggestionConfigurator.CreateCodeSuggestion(C64HostConfig.CodeSuggestionBackendType, _configuration, C64BasicCodingAssistant.CODE_COMPLETION_LANGUAGE_DESCRIPTION);
var codeSuggestionBackend = CodeSuggestionConfigurator.CreateCodeSuggestion(C64HostConfig.CodeSuggestionBackendType, _configuration, C64BasicCodingAssistant.CODE_COMPLETION_LANGUAGE_DESCRIPTION, C64BasicCodingAssistant.CODE_COMPLETION_EXAMPLE_MESSAGES);
codingAssistantInfoLabel.DisplayText = "Testing...";
codingAssistantInfoLabel.TextColor = Color.White;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ NAudioAudioHandlerContext audioHandlerContext

var renderer = new C64SadConsoleRenderer(c64, renderContext);

ICodeSuggestion codeSuggestion = CodeSuggestionConfigurator.CreateCodeSuggestion(c64HostConfig.CodeSuggestionBackendType, _configuration, C64BasicCodingAssistant.CODE_COMPLETION_LANGUAGE_DESCRIPTION, defaultToNoneIdConfigError: true);
ICodeSuggestion codeSuggestion = CodeSuggestionConfigurator.CreateCodeSuggestion(c64HostConfig.CodeSuggestionBackendType, _configuration, C64BasicCodingAssistant.CODE_COMPLETION_LANGUAGE_DESCRIPTION, C64BasicCodingAssistant.CODE_COMPLETION_EXAMPLE_MESSAGES, defaultToNoneIdConfigError: true);
var c64BasicCodingAssistant = new C64BasicCodingAssistant(c64, codeSuggestion, _loggerFactory);
var inputHandler = new C64SadConsoleInputHandler(c64, inputHandlerContext, _loggerFactory, c64BasicCodingAssistant, c64HostConfig.BasicAIAssistantDefaultEnabled);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,12 @@ public static async Task<ICodeSuggestion> GetCodeSuggestionImplementation(C64Hos
if (c64HostConfig.CodeSuggestionBackendType == CodeSuggestionBackendTypeEnum.OpenAI)
{
var openAIApiConfig = await GetOpenAIConfig(localStorageService);
codeSuggestion = new OpenAICodeSuggestion(openAIApiConfig, C64BasicCodingAssistant.CODE_COMPLETION_LANGUAGE_DESCRIPTION);
codeSuggestion = new OpenAICodeSuggestion(openAIApiConfig, C64BasicCodingAssistant.CODE_COMPLETION_LANGUAGE_DESCRIPTION, C64BasicCodingAssistant.CODE_COMPLETION_EXAMPLE_MESSAGES);
}
else if (c64HostConfig.CodeSuggestionBackendType == CodeSuggestionBackendTypeEnum.SelfHostedOpenAICompatible)
{
var openAIApiConfig = await GetSelfHostedOpenAICompatibleConfig(localStorageService);
codeSuggestion = new OpenAICodeSuggestion(openAIApiConfig, C64BasicCodingAssistant.CODE_COMPLETION_LANGUAGE_DESCRIPTION);
codeSuggestion = new OpenAICodeSuggestion(openAIApiConfig, C64BasicCodingAssistant.CODE_COMPLETION_LANGUAGE_DESCRIPTION, C64BasicCodingAssistant.CODE_COMPLETION_EXAMPLE_MESSAGES);
}
else if (c64HostConfig.CodeSuggestionBackendType == CodeSuggestionBackendTypeEnum.CustomEndpoint)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,11 @@
ICodeSuggestion codeSuggestion;
if(C64HostConfig.CodeSuggestionBackendType == CodeSuggestionBackendTypeEnum.OpenAI)
{
codeSuggestion = new OpenAICodeSuggestion(_openAIApiConfig, C64BasicCodingAssistant.CODE_COMPLETION_LANGUAGE_DESCRIPTION);
codeSuggestion = new OpenAICodeSuggestion(_openAIApiConfig, C64BasicCodingAssistant.CODE_COMPLETION_LANGUAGE_DESCRIPTION, C64BasicCodingAssistant.CODE_COMPLETION_EXAMPLE_MESSAGES);
}
else if(C64HostConfig.CodeSuggestionBackendType == CodeSuggestionBackendTypeEnum.SelfHostedOpenAICompatible)
{
codeSuggestion = new OpenAICodeSuggestion(_selfHostedOpenAICompatibleAIApiConfig, C64BasicCodingAssistant.CODE_COMPLETION_LANGUAGE_DESCRIPTION);
codeSuggestion = new OpenAICodeSuggestion(_selfHostedOpenAICompatibleAIApiConfig, C64BasicCodingAssistant.CODE_COMPLETION_LANGUAGE_DESCRIPTION, C64BasicCodingAssistant.CODE_COMPLETION_EXAMPLE_MESSAGES);
}
else if(C64HostConfig.CodeSuggestionBackendType == CodeSuggestionBackendTypeEnum.CustomEndpoint)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Highbyte.DotNet6502.AI.CodingAssistant.Inference;
using Microsoft.Extensions.Configuration;

namespace Highbyte.DotNet6502.AI.CodingAssistant;
Expand All @@ -8,16 +9,17 @@ public static ICodeSuggestion CreateCodeSuggestion(
CodeSuggestionBackendTypeEnum codeSuggestionBackendType,
IConfiguration configuration,
string programmingLanguage,
List<ChatMessage> examples,
bool defaultToNoneIdConfigError = false)
{
ICodeSuggestion codeSuggestion;
try
{
codeSuggestion = codeSuggestionBackendType switch
{
CodeSuggestionBackendTypeEnum.OpenAI => new OpenAICodeSuggestion(configuration, programmingLanguage),
CodeSuggestionBackendTypeEnum.OpenAI => new OpenAICodeSuggestion(configuration, programmingLanguage, examples ?? new()),
CodeSuggestionBackendTypeEnum.SelfHostedOpenAICompatible => new OpenAICodeSuggestion(configuration, programmingLanguage, examples ?? new()),
CodeSuggestionBackendTypeEnum.CustomEndpoint => new CustomAIEndpointCodeSuggestion(configuration, programmingLanguage),
CodeSuggestionBackendTypeEnum.SelfHostedOpenAICompatible => new OpenAICodeSuggestion(configuration, programmingLanguage),
CodeSuggestionBackendTypeEnum.None => new NoCodeSuggestion(),
_ => throw new NotImplementedException($"CodeSuggestionBackendType '{codeSuggestionBackendType}' is not implemented.")
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Highbyte.DotNet6502.AI.CodingAssistant.Inference;
public struct CodeCompletionConfig
{
public string ProgrammingLanguage { get; set; }

public List<ChatMessage> Examples { get; set; }

//public string? Parameters { get; set; }
//public string? UserRole { get; set; }
//public string[]? UserPhrases { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ Predict what text the user in would insert at the cursor position indicated by ^

List<ChatMessage> messages =
[
// System instruction
new(ChatMessageRole.System, systemMessageBuilder.ToString()),

new(ChatMessageRole.User, @$"USER_TEXT: {textBefore}^^^{textAfter}")
];

// Add examples
messages.AddRange(config.Examples);

// Add user-entered text
messages.Add(new(ChatMessageRole.User, @$"{textBefore}^^^{textAfter}"));

return new ChatParameters
{
Messages = messages,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ public class OpenAICodeSuggestion : ICodeSuggestion
private readonly CodeCompletionConfig _codeCompletionConfig;
private readonly CodeCompletionInference _codeCompletionInference;

public OpenAICodeSuggestion(IConfiguration configuration, string programmingLanguage)
: this(new ApiConfig(configuration), programmingLanguage)
public OpenAICodeSuggestion(IConfiguration configuration, string programmingLanguage, List<ChatMessage> examples)
: this(new ApiConfig(configuration), programmingLanguage, examples)
{
}

public OpenAICodeSuggestion(ApiConfig apiConfig, string programmingLanguage)
public OpenAICodeSuggestion(ApiConfig apiConfig, string programmingLanguage, List<ChatMessage> examples)
{
_isAvailable = true;
_lastError = null;
_inferenceBackend = new OpenAIInferenceBackend(apiConfig);
_codeCompletionConfig = new CodeCompletionConfig { ProgrammingLanguage = programmingLanguage };

// Workaround for examples seemingly messing up OpenAI inference, but is required for local Ollama models?
List<ChatMessage> usingExamples = apiConfig.SelfHosted ? examples : new();

_codeCompletionConfig = new CodeCompletionConfig { ProgrammingLanguage = programmingLanguage, Examples = usingExamples };
_codeCompletionInference = new CodeCompletionInference();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text;
using System.Timers;
using Highbyte.DotNet6502.AI.CodingAssistant;
using Highbyte.DotNet6502.AI.CodingAssistant.Inference;
using Highbyte.DotNet6502.Systems.Commodore64.TimerAndPeripheral;
using Highbyte.DotNet6502.Systems.Commodore64.Video;
using Highbyte.DotNet6502.Utils;
Expand Down Expand Up @@ -47,6 +48,27 @@ public class C64BasicCodingAssistant

public const string CODE_COMPLETION_LANGUAGE_DESCRIPTION = "Commodore 64 Basic";

// Examples for inference
public static List<ChatMessage> CODE_COMPLETION_EXAMPLE_MESSAGES = [
new(ChatMessageRole.User, "20 rem ask user for name\n30 ^^^"),
new(ChatMessageRole.Assistant, "OK:[input \"What's your name?\"; n$]END_INSERTION"),

new(ChatMessageRole.User, "10 print^^^"),
new(ChatMessageRole.Assistant, "OK:[\"hello world!\"]END_INSERTION"),

new(ChatMessageRole.User, "10 print \"he^^^"),
new(ChatMessageRole.Assistant, "OK:[llo world!\"]END_INSERTION"),

new(ChatMessageRole.User, "10 print \"Wha^^^ your name?\""),
new(ChatMessageRole.Assistant, "OK:[t is]END_INSERTION"),

new(ChatMessageRole.User, "20 go^^^"),
new(ChatMessageRole.Assistant, "OK:[to 10]END_INSERTION"),

new(ChatMessageRole.User, "30 print \"Hello world!\"\n40 go^^^"),
new(ChatMessageRole.Assistant, "OK:[to 30]END_INSERTION"),
];

public async Task CheckAvailability()
{
await _codeSuggestion.CheckAvailability();
Expand Down Expand Up @@ -310,11 +332,6 @@ private bool GetText(out string textBeforeCursor, out string textAfterCursor)
// textAfterCursorSb.Append((char)asciiCode);
//}

// Hack: If there is nothing after cursor, add a newline to make sure there is something to query AI with
if (textAfterCursorSb.Length == 0)
{
textBeforeCursorSb.AppendLine();
}
// TODO: Build textAfterCursor to include rest of basic lines

// Set out parameters
Expand Down

0 comments on commit 6e382a9

Please sign in to comment.