Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert GH extension to use MEAI and Ollama. #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 15 additions & 27 deletions src/blackbeard-extension-cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,29 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.AI;

const string GithubCopilotCompletionsUrl = "https://api.githubcopilot.com/chat/completions";
const string OllamaEndpoint = "http://localhost:11434";
const string OllamaModel = "llama3";

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Ahoy, matey! Welcome to the Blackbeard Pirate GitHub Copilot Extension!");
app.MapPost("/", async (
[FromHeader(Name = "X-GitHub-Token")] string githubToken,
[FromBody] Request userRequest) =>
app.MapPost("/", async (HttpContext ctx) =>
{
userRequest.Stream = true;
userRequest.Messages.Insert(0, new Message
var openAiRequest = await OpenAISerializationHelpers.DeserializeChatCompletionRequestAsync(ctx.Request.Body, ctx.RequestAborted);

openAiRequest.Options.ModelId = null;
openAiRequest.Messages.Insert(0, new ChatMessage
{
Role = "system",
Content = "Concisely reply as if you were Blackbeard the Pirate."
Role = ChatRole.System,
Contents = [new TextContent("Concisely reply as if you were Blackbeard the Pirate.")]
});

using HttpClient httpClient = new()
{
DefaultRequestHeaders = { Authorization = new("Bearer", githubToken) }
};
using IChatClient chatClient = new OllamaChatClient(OllamaEndpoint, OllamaModel);
var streamingResponse = chatClient.CompleteStreamingAsync(openAiRequest.Messages, openAiRequest.Options, ctx.RequestAborted);

var copilotLLMResponse = await httpClient.PostAsJsonAsync(GithubCopilotCompletionsUrl, userRequest);
return Results.Stream(await copilotLLMResponse.Content.ReadAsStreamAsync(), "application/json");
ctx.Response.StatusCode = StatusCodes.Status200OK;
ctx.Response.ContentType = "application/json";
await OpenAISerializationHelpers.SerializeStreamingAsync(ctx.Response.Body, streamingResponse, cancellationToken: ctx.RequestAborted);
});

app.Run();

public record Request
{
public bool Stream { get; set; }
public List<Message> Messages { get; set; } = [];
}

public record Message
{
public required string Role { get; set; }
public required string Content { get; set; }
}
5 changes: 5 additions & 0 deletions src/blackbeard-extension-cs/blackbeard-extension-cs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI.Ollama" Version="9.1.0-preview.1.25065.1" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.1.0-preview.1.25065.1" />
</ItemGroup>

</Project>