.net使用Microsoft.Extensions.AI调用大模型
创始人
2026-03-06 21:06:51
0

1、安装NuGet包

dotnet add package Microsoft.Extensions.AI

dotnet add package Microsoft.Extensions.AI.OpenAI

2、使用Microsoft.Extensions.AI调用大模型

(1)非流式输出

using Microsoft.Extensions.AI;
using OpenAI;
var chatclient=new OpenAIClient(
    new System.ClientModel.ApiKeyCredential("ms-ee0ab5e3-d373-498b-923e-c727be632d50"),
    new OpenAIClientOptions{
        Endpoint = new Uri("https://api-inference.modelscope.cn/v1")
    }).GetChatClient("deepseek-ai/DeepSeek-V3.2").AsIChatClient();
/*调用本地大模型
using OllamaSharp;  //dotnet add package OllamaSharp
IChatClient chatclient=OllamaApiClient(new Uri("http://localhost:11434/"), "phi3:mini");*/
while(true){
    Console.Write("Q:")
    string msg=Console.ReadLine("")??"exit";
    if(msg=="exit")  break;
    Console.Write("AI:");
    Console.WriteLine(await chatclient.GetResponseAsync(msg));  //一次性输出大模型的回答
}

(2)带对话历史(上下文,短期记忆)的非流式输出

using Microsoft.Extensions.AI;
using OpenAI;
var chatclient=new OpenAIClient(
    new System.ClientModel.ApiKeyCredential("ms-ee0ab5e3-d373-498b-923e-c727be632d50"),
    new OpenAIClientOptions{
        Endpoint = new Uri("https://api-inference.modelscope.cn/v1")
    }).GetChatClient("deepseek-ai/DeepSeek-V3.2").AsIChatClient();
List<ChatMessage> history=[];
history.Add(new ChatMessage(ChatRole.System,"你是一名python专家,回答python专业问题"));
while(true){
    Console.Write("Q:")
    string msg=Console.ReadLine("")??"exit";
    if(msg=="exit")  break;
    history.Add(new(ChatRole.User,msg));
    Console.Write("AI:");
    ChatResponse response=await chatclient.GetResponseAsync(history)
    Console.WriteLine(response);
    history.AddMessages(response);
}

(3)流式输出

using Microsoft.Extensions.AI;
using OpenAI;
var chatclient=new OpenAIClient(
    new System.ClientModel.ApiKeyCredential("ms-ee0ab5e3-d373-498b-923e-c727be632d50"),
    new OpenAIClientOptions{
        Endpoint = new Uri("https://api-inference.modelscope.cn/v1")
    }).GetChatClient("deepseek-ai/DeepSeek-V3.2").AsIChatClient();
while(true){
    Console.Write("Q:")
    string msg=Console.ReadLine("")??"exit";
    if(msg=="exit")  break;
    Console.Write("AI:");
    await foreach(ChatResponseUpdate update in chatclient.GetStreamingResponseAsync(msg)){
        Console.Write(update);
    }
    Console.WriteLine();
}

(4)带对话历史(上下文,短期记忆)的流式输出

using Microsoft.Extensions.AI;
using OpenAI;
var chatclient=new OpenAIClient(
    new System.ClientModel.ApiKeyCredential("ms-ee0ab5e3-d373-498b-923e-c727be632d50"),
    new OpenAIClientOptions{
        Endpoint = new Uri("https://api-inference.modelscope.cn/v1")
    }).GetChatClient("deepseek-ai/DeepSeek-V3.2").AsIChatClient();
List<ChatMessage> history=[];
history.Add(new(ChatRole.System,"你是一名python专家,回答python专业问题"));
while(true){
    Console.Write("Q:")
    string msg=Console.ReadLine("")??"exit";
    if(msg=="exit")  break;
    history.Add(new(ChatRole.User,msg));
    List<ChatResponseUpdate> updates=[];
    Console.Write("AI:");
    await foreach(ChatResponseUpdate update in GetStreamingResponseAsync(history){
        Console.Write(update);
        updates.Add(update);
    }    
    Console.WriteLine();
    history.AddMessages(updates);
}

3、在asp.net core中使用


上一篇:.net中使用openai库调用大模型

下一篇:没有了

相关内容

.net使用Microso...
1、安装NuGet包dotnet add package Micr...
2026-03-06 21:06:51

热门资讯

okhttp的使用 android网络框架之OKhttp一个处理网络请求的开源项目,是安卓端最火热的轻量级框架,适用于 ...
.net中使用openai库调... 1、安装NuGet包dotnet add package OpenAI #官方地址https://...
jupyter noteboo... 当前jupyter notebook要使用python3.12版才不会出现兼容性问题。1、安装pip...
unity3d控制物体移动(触... float x;float y;if (Input.touchCount > 0) { //触控 ...
asp.net core mv... asp.net core mvc view页面变量中文乱码,即razor变量中文乱码,特别是从数据库...
.net 6中使用Proces... 在 .net framework时代,如果想使用默认浏览器打开一个url可以使用以下代码:Syste...
asp.net core由于大... 1、efcore在windows中使用数据库时表名不区分大小写,但在linux中区分大小写。例如:p...
.net使用Microsoft... 1、安装NuGet包dotnet add package Microsoft.Extensions....
c#、python、php、v... c#Math.Round():四舍五入。若只有一个5时,则前偶舍,前奇入。Math.Ceiling(...