CorfedoCorfedoDocs

Microsoft.Extensions.AI

Give an IChatClient Corfedo's code tools, or use Corfedo AI as an IChatClient that keeps the conversation.

Corfedo.Extensions.AI brings Corfedo to Microsoft.Extensions.AI in .NET 10. It gives your IChatClient Corfedo's read tools, or makes Corfedo AI an IChatClient of its own.

Before you begin

  • An API key file with Search and read code. Add Ask Corfedo AI to use Corfedo AI as the model. See API keys.
  • The packages Corfedo.Client and Corfedo.Extensions.AI, and Microsoft.Extensions.AI for the invocation middleware. The .NET SDK page says where the packages come from.

Give your model Corfedo's tools

using Corfedo.Client;
using Corfedo.Extensions.AI;
using Microsoft.Extensions.AI;

using var corfedo = await CorfedoClient.FromApiKeyFileAsync(
    Environment.GetEnvironmentVariable("CORFEDO_API_KEY_FILE")!);
var tools = corfedo.GetAITools();
using var chat = existingModel.AsBuilder().UseFunctionInvocation().Build();
var answer = await chat.GetResponseAsync("Where is the retry policy configured?",
    new ChatOptions { Tools = [.. tools] });

existingModel is your IChatClient. You own its instructions, model and invocation loop. Do not add UseFunctionInvocation when you use ChatClientAgent, which runs its own loop.

GetAITools() returns 9 tools by default, in the groups Search, Code and Catalog. Choose fewer with Groups, or name single tools:

var tools = corfedo.GetAITools(new()
{
    Groups = CorfedoAIToolGroups.None,
    Tools = ["search_code", "read_file"]
});

Add CorfedoAITools.Instructions to your system prompt so the model knows that a run of * in returned code is a masked credential, not a defect.

Check: the response cites files from your repositories, and the tool calls in it name search_code or read_file.

Use Corfedo AI as the model

CorfedoChatClient answers with Corfedo AI. Corfedo keeps the conversation, runs its own tools and uses the profile's model and settings.

using var chat = new CorfedoChatClient(corfedo).AsBuilder()
    .UseFunctionInvocation().Build();
var first = await chat.GetResponseAsync("How are search requests routed?");
var next = await chat.GetResponseAsync("Which modules call that code?", new()
{
    ConversationId = first.ConversationId,
    RawRepresentationFactory = _ => new CorfedoChatOptions
    {
        References = [new() { Kind = AiReferenceKind.Repository, Repository = "acme/search" }],
        Limits = new() { ModelCallBudget = 8 }
    }
});

A call sends one new text message, or the results of the tools Corfedo AI asked your application to run. Earlier messages are not sent again.

OptionEffect
ConversationIdContinues a conversation
CorfedoChatOptions through RawRepresentationFactorySources, References, Limits and Corfedo's own Tools for a new message
ToolsYour functions. Corfedo AI asks for them with FunctionCallContent, and UseFunctionInvocation returns the results.
ResponseFormat with a JSON schemaA structured answer. Streaming then yields it once, after the run ends.

The profile chosen for the API answers, and the response names it as ModelId. A call that sets ChatOptions.ModelId is rejected.

ResponseId is the run ID. RawRepresentation holds the run with its outcome, citations and usage. Cancelling a call ends its subscription, not the run. CancelRunAsync(response.ResponseId) on the CorfedoChatClient stops the run.

Check: the second answer refers to the first, which shows the conversation continued.

Troubleshooting

What you seeWhyWhat to do
CorfedoUnsupportedInputExceptionThe call has a system message, non-text content, a generation setting or an unknown option. The profile owns instructions and settings.Remove it, or change the profile
CorfedoRunExceptionThe run ended without an answerRead its outcome. The run is readable through the core client.
CorfedoOutputDiscardedExceptionText already streamed was replaced after the run recoveredRead the final answer with client.Ai.GetRunAsync
AccessDeniedThe key lacks Ask Corfedo AI, or Corfedo AI is off for API clientsSee Request access to Corfedo AI

On this page