在IM即时通讯系统中接入DeepSeek等AI大模型

SendChatRequestAsync(string prompt)

{
    try
    {
        // 构造请求体
        var requestBody = new
        {
            model = "deepseek-chat", // 根据API文档调整模型名称
            messages = new[]
            {
                new
                {
                    role = "user",
                    content = prompt
                }
            },
            temperature = 0.7,
            max_tokens = 1000
        };

// 序列化为JSON

        var json = JsonSerializer.Serialize(requestBody);
        var content = new StringContent(json, Encoding.UTF8, "application/json");

// 发送POST请求

        var response = await _httpClient.PostAsync("chat/completions", content);

// 确保请求成功

        response.EnsureSuccessStatusCode();

// 读取并返回响应内容

        var responseContent = await response.Content.ReadAsStringAsync();
        return responseContent;
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error calling DeepSeek API: {ex.Message}");
        throw;
    }
}

} “`   服务端的主要业务逻辑如下: (1)当服务端收到来自客户端的AI提问时,便在DB中插入一条记录,该记录的ProcessResult字段的值是“处理中”,然后将该问题通过上面的DeepSeekApiClient提交给AI大模型,并等待回复。 (2)当服务端收到了AI大模型的回复(或超时、或报错)后,再更新DB中对应的那条记录的ProcessResult、Answer等字段的值。接着,再将AI回复发送给客户端。 (3)特殊情况处理:比如,当在等待大模型回复期间,如果客户端掉线了的情况。   到这里,在傲瑞通中集成DeepSeek等AI大模型的工作就完成了,有兴趣的朋友欢迎留言多多交流!