
通过上一篇文章可以知道,对话的发送是通过IChatClient
实例,发送消息并获取响应实现,具体深入学习微软官方文档。可以知道响应前发送消息上下文,使用GetResponseAsync()
重载传入消息列表。
cs
Task<ChatResponse> GetResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default(CancellationToken))
设置消息上下文
将消息历史作为问答中的上下文前置。
cs
internal class Program
{
async static Task Main(string[] args)
{
// 忽略不变部分
// 输入上下文 添加多轮会话历史 GetResponseAsync() 接受消息列表
// 获取http请求响应(非流式)
Console.WriteLine(await client.GetResponseAsync([
new (ChatRole.Assistant, "You are a helpful AI assistant"), // 助理角色 发送消息
new (ChatRole.User, "Who are you?") // 用户角色 发送消息
]));
Console.ReadLine();
}
}
或者直接使用ChatMessage
实例集合。
cs
internal class Program
{
async static Task Main(string[] args)
{
// 忽略不变部分
// 输入上下文 添加多轮会话历史 GetResponseAsync() 接受消息列表
// 获取http请求响应(非流式)
List<ChatMessage> history = // 消息历史
[
new ChatMessage(ChatRole.Assistant, "You are a helpful AI assistant"),
new ChatMessage(ChatRole.User, "Who are you?"),
];
Console.WriteLine(await client.GetResponseAsync(history));
Console.ReadLine();
}
}
输出结果如下:
对话配置(可跳过)
对于问答会话中的请求配置也同样可以通过GetResponseAsync()
在获取响应结果前进行配置,配置使用ChatOptions
,问答配置包括如下主要内容,可按照需求进行适当配置。
cs
// 会话标识符
public string? ConversationId { get; set; }
// 请求指令
public string? Instructions { get; set; }
// 生成回复的温度
public float? Temperature { get; set; }
// 回复最大 token 数
public int? MaxOutputTokens { get; set; }
// 核采样因子(top p)
public float? TopP { get; set; }
// 生成时考虑的最可能 token 数(top k)
public int? TopK { get; set; }
// 重复 token 频率惩罚
public float? FrequencyPenalty { get; set; }
// 已出现 token 的惩罚
public float? PresencePenalty { get; set; }
// 随机种子
public long? Seed { get; set; }
// 回复格式
public ChatResponseFormat? ResponseFormat { get; set; }
// 模型 ID
public string? ModelId { get; set; }
// 停止序列列表
public IList<string>? StopSequences { get; set; }
// 是否允许多工具调用
public bool? AllowMultipleToolCalls { get; set; }
// 工具模式
public ChatToolMode? ToolMode { get; set; }
// 工具列表
[JsonIgnore]
public IList<AITool>? Tools { get; set; }
// 原始选项工厂回调
[JsonIgnore]
public Func<IChatClient, object?>? RawRepresentationFactory { get; set; }
// 附加属性
public AdditionalPropertiesDictionary? AdditionalProperties { get; set; }
控制台案例实现(英语助手)
用户进行控制台输入与输出,通过循环将用户输入与模型响应上下文历史,实现一个简单的对话练习。
cs
internal class Program
{
async static Task Main(string[] args)
{
string apikey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");// 通过环境变量获取apikey
string model = "deepseek-chat"; // 模型名称
string endpoint = "https://api.deepseek.com/v1"; // deepseek 中 提供兼容openai的访问api
// 创建IChatClient实例
IChatClient client = new OpenAI.Chat.ChatClient(model, new ApiKeyCredential(apikey), new OpenAIClientOptions {
Endpoint = new Uri(endpoint) // 指定api访问地址
})
.AsIChatClient(); // 将chatclient转换为ichatclient实现实例
// 输入上下文 添加多轮会话历史 GetResponseAsync() 接受消息列表
// 获取http请求响应(非流式)
List<ChatMessage> history = [new ChatMessage(ChatRole.Assistant, "你是一个英语助手,拥有10年初中教学经验,对于用户提问,英文回复并用中文解释。")]; // 构建基本背景消息
// 通过控制台输入添加用户角色信息且大模型响应消息作为最后的消息
while (true) {
Console.Write("Q:");
// 输入询问内容并存储到消息集合中
history.Add(new ChatMessage(ChatRole.User, Console.ReadLine()));
// 执行对话请求
ChatResponse response = await client.GetResponseAsync(history);
// 输出对话结果
Console.WriteLine(response);
// 添加对话结果保存到历史集合中
history.AddMessages(response);
}
}
}
输出结果如下:
修改为流式输出
通过提供的GetStreamingResponseAsync
实现实时响应流对话,与GetResponseAsync
返回完整的响应结果不同,实时流返回的是一个异步迭代器的泛型类型IAsyncEnumerable<T>
,具体细节可以查看官方文档,返回泛型集合实际类为ChatResponseUpdate,通过遍历方式获取响应输出。
对案例进行改造。
cs
internal class Program
{
async static Task Main(string[] args)
{
// 省略不变的内容代码,仅仅修改循环内容
while (true) {
Console.Write("Q:");
string request = Console.ReadLine();
if (request.Contains("bye"))
{
break;
}
// 输入询问内容并存储到消息集合中
history.Add(new ChatMessage(ChatRole.User, request));
//-------------------------实时流调整-----------------
// 将输出的响应结果进行集合保存
List<ChatResponseUpdate> updates = [];
// 执行对话请求
//ChatResponse response = await client.GetResponseAsync(history);
await foreach (ChatResponseUpdate update in client.GetStreamingResponseAsync(history)) {
// 输出实时流对话结果
Console.Write(update);
// 存储此次消息响应单项
updates.Add(update);
}
// 添加对话结果保存到历史集合中
history.AddMessages(updates);
// 每一次对话输出完成进行换行
Console.WriteLine();
//-------------------------实时流调整-----------------
}
Console.WriteLine("会话结束");
}
}
输出结果如下:
动态输出过程如下: