The Writer SDK has its own ChatMessage and ChatResponse types that conflict with Microsoft.Extensions.AI. Use the Meai alias:
1
usingMeai=Microsoft.Extensions.AI;
Chat Completions
WriterClient implements IChatClient, so you can use it with the standard MEAI interface.
1 2 3 4 5 6 7 8 910
usingWriter;usingMeai=Microsoft.Extensions.AI;Meai.IChatClientclient=newWriterClient(apiKey:Environment.GetEnvironmentVariable("WRITER_API_KEY")!);varresponse=awaitclient.GetResponseAsync("What is the capital of France?",newMeai.ChatOptions{ModelId="palmyra-x-004"});Console.WriteLine(response.Text);
Streaming
1 2 3 4 5 6 7 8 91011
usingWriter;usingMeai=Microsoft.Extensions.AI;Meai.IChatClientclient=newWriterClient(apiKey:Environment.GetEnvironmentVariable("WRITER_API_KEY")!);awaitforeach(varupdateinclient.GetStreamingResponseAsync("Explain quantum computing in simple terms.",newMeai.ChatOptions{ModelId="palmyra-x-004"})){Console.Write(update.Text);}
usingWriter;usingMeai=Microsoft.Extensions.AI;Meai.IChatClientclient=newWriterClient(apiKey:Environment.GetEnvironmentVariable("WRITER_API_KEY")!);vartool=Meai.AIFunctionFactory.Create((stringlocation)=>$"The weather in {location} is sunny, 22C.","GetWeather","Gets the current weather for a given location.");varoptions=newMeai.ChatOptions{ModelId="palmyra-x-004",Tools=[tool],};varmessages=newList<Meai.ChatMessage>{new(Meai.ChatRole.User,"What is the weather in London?"),};while(true){varresponse=awaitclient.GetResponseAsync(messages,options);messages.AddRange(response.ToChatMessages());if(response.FinishReason==Meai.ChatFinishReason.ToolCalls){varresults=awaitresponse.CallToolsAsync(options);messages.AddRange(results);continue;}Console.WriteLine(response.Text);break;}