Last updated for NuGet package version 0.2.x

Installation

Install Commonbase from NuGet using a package manager or the dotnet CLI:

dotnet package add Commonbase

Usage

A Project ID and API Key are required for all Commonbase requests. You can find your project ID and generate an API key in the Commonbase Dashboard.

To create a completion, configure a CommonbaseClient with your API Key and provide your Project ID and prompt to CreateCompletionAsync.

Text Completion

To create a basic text completion, use CreateCompletionAsync with a prompt:

using Commonbase;

CommonbaseClient client = new(apiKey: "API_KEY");

var completion = await client.CreateCompletionAsync(
    prompt: "Hello!",
    projectId: "PROJECT_ID"
);

Console.WriteLine(completion.BestResult);

completion.BestResult returns the most relevant choice. completion.Choices contains the fill list of choices returned by the LLM:

foreach (var choice in completion.Choices)
{
    Console.WriteLine(choice.Text);
}

Chat Completion

To create a chat completion, provide a chatContext with list of chat messages and set the ProviderConfig.Params.Type to chat. In this mode, the prompt is prepended to the context as a system message.

using Commonbase;

CommonbaseClient client = new(apiKey: "API_KEY");

string systemMessage = "You are an assistant who helps users with tech problems.";
ChatMessage[] messages = new[] {
    new ChatMessage { Role = MessageRole.User, Content = "My internet isn't working." },
    new ChatMessage { Role = MessageRole.Assistant, Content = "Have you tried restarting your router?" },
    new ChatMessage { Role = MessageRole.User, Content = "Yes I've tried that." },
};

var response = await client.CreateCompletionAsync(
    prompt: systemMessage,
    chatContext: new ChatContext { Messages = messages },
    projectId: "PROJECT_ID",
    providerConfig: new CbOpenAIProviderConfig
    {
        Region = CbOpenAIRegion.EU,
        Params = new()
        {
            Type = RequestType.Chat
        }
    }
);

Console.WriteLine(response.BestResult);

Streaming

To stream the completion response as it is generated, use StreamCompletionAsync.

Streaming is useful when you want to display a completion “as it is typed” for a chatbot-like experience.

For longer completions, streaming lets you avoid hanging while you wait for a completed response from CreateCompletionAsync.

using Commonbase;

CommonbaseClient client = new(apiKey: "API_KEY");

var stream = client.StreamCompletionAsync(
    prompt: "Write me an essay about artificial intelligence.",
    projectId: "PROJECT_ID"
);

await foreach (CompletionResponse response in stream)
{
    if (!response.Completed)
        Console.Write(response.BestResult);
    else
        Console.WriteLine("\n\ndone");
}