Microsoft.Extensions.AI deepseek ChatClient democ#
·
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using OpenAI;
using OpenAI.Chat;
using System.ClientModel;
using System.Threading.Tasks;
namespace MyOpenAiApp
{
internal class Program
{
static async Task Main(string[] args)
{
string model = "deepseek-v4-pro";
string key = "sk-xxxx";
OpenAIClientOptions opt = new OpenAIClientOptions();
opt.Endpoint = new Uri("https://api.deepseek.com/v1");
var client = new OpenAIClient(
new ApiKeyCredential( key),opt
);
IChatClient chatClient = client.GetChatClient(model).AsIChatClient();
// Start the conversation with context for the AI model
List<Microsoft.Extensions.AI.ChatMessage> chatHistory =
[
new Microsoft.Extensions.AI.ChatMessage(ChatRole.System, """
You are a friendly hiking enthusiast who helps people discover fun hikes in their area.
You introduce yourself when first saying hello.
When helping people out, you always ask them for this information
to inform the hiking recommendation you provide:
1. The location where they would like to hike
2. What hiking intensity they are looking for
You will then provide three suggestions for nearby hikes that vary in length
after you get that information. You will also share an interesting fact about
the local nature on the hikes when making a recommendation. At the end of your
response, ask if there is anything else you can help with.
""")
];
string? userPrompt = "whoami";
chatHistory.Add(new Microsoft.Extensions.AI.ChatMessage(ChatRole.User, userPrompt));
var resp = chatClient.GetStreamingResponseAsync(chatHistory);
await foreach (ChatResponseUpdate item in resp) {
Console.Write(item.Text);
}
}
}
}
更多推荐
所有评论(0)