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.ClientandCorfedo.Extensions.AI, andMicrosoft.Extensions.AIfor 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.
| Option | Effect |
|---|---|
ConversationId | Continues a conversation |
CorfedoChatOptions through RawRepresentationFactory | Sources, References, Limits and Corfedo's own Tools for a new message |
Tools | Your functions. Corfedo AI asks for them with FunctionCallContent, and UseFunctionInvocation returns the results. |
ResponseFormat with a JSON schema | A 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 see | Why | What to do |
|---|---|---|
CorfedoUnsupportedInputException | The 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 |
CorfedoRunException | The run ended without an answer | Read its outcome. The run is readable through the core client. |
CorfedoOutputDiscardedException | Text already streamed was replaced after the run recovered | Read the final answer with client.Ai.GetRunAsync |
AccessDenied | The key lacks Ask Corfedo AI, or Corfedo AI is off for API clients | See Request access to Corfedo AI |
Related
- Streaming explains recovery and answer semantics.
- Client tools covers tools your application runs.
AI frameworks
Give your own model Corfedo's code tools, or use Corfedo AI as the model, in Microsoft.Extensions.AI, the Vercel AI SDK, LangChain or Pydantic AI.
Vercel AI SDK
Give an AI SDK model Corfedo's code tools with createCorfedoTools, or use Corfedo AI as a language model that keeps the conversation.