# Microsoft.Extensions.AI

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

Source: https://docs.corfedo.ai/connect/frameworks/microsoft-extensions-ai/

`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](https://docs.corfedo.ai/connect/authentication/#api-keys).
- The packages `Corfedo.Client` and `Corfedo.Extensions.AI`, and `Microsoft.Extensions.AI` for the invocation middleware. The [.NET SDK](https://docs.corfedo.ai/connect/sdks/dotnet/) page says where the packages come from.

## Give your model Corfedo's tools

```csharp
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:

```csharp
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.

```csharp
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](https://docs.corfedo.ai/connect/api/ai/profiles/) 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](https://docs.corfedo.ai/connect/authentication/#request-access-to-corfedo-ai) |

## Related

- [Streaming](https://docs.corfedo.ai/connect/api/ai/streaming/) explains recovery and answer semantics.
- [Client tools](https://docs.corfedo.ai/connect/api/ai/client-tools/) covers tools your application runs.
