使用C#(winform,Console...)调用通义千问大模型(灵积大模型)

要如何与自己的工作相互结合,解决业务问题?我想第一步先考虑用熟悉的语言来调用大语言模型吧。

官方没有直接给出具体的示例代码,以下是完整代码,喜欢点赞收藏转发吧,记得给留个来源说明。通过调用http请求:

using System;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
using System.Threading.Tasks;
using System.Net.Http.Headers;

public class Program
{
    private readonly static string _requestUri = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
    private readonly static string _apiKey = "密钥"; // 请替换为您的阿里云密钥信息
    private readonly static string _model = "qwen-max";

    private static async Task CallQWen(string question)
    {
        using (var client = new HttpClient())
        {
            // 创建模型类
            var requestObj = new QianWenRequest
            {
                Model = _model,
                Input = new Input
                {
                    Prompt = question
                }
            };

            var settings = new JsonSerializerSettings
            {
                Formatting = Formatting.Indented,
                StringEscapeHandling = StringEscapeHandling.EscapeNonAscii
            };

            // 将对象序列化为JSON字符串
            string requestJson = JsonConvert.SerializeObject(requestObj, settings);
            Console.WriteLine(requestJson);

            var request = new HttpRequestMessage(HttpMethod.Post, _requestUri);
            //定义Body
            var content = new StringContent(requestJson.ToLower(), Encoding.UTF8, "application/json");
            request.Content = content;

            //定义header
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); // 正确做法
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", $"{_apiKey}");

            var response = await client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                var responseBody = await response.Content.ReadAsStringAsync();
                
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("通义千问的回答:");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(responseBody);
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"请求失败,状态码:{response.StatusCode}");
                Console.ForegroundColor = ConsoleColor.White;
            }
        }
    }

    static async Task Main(string[] args)
    {
        Console.OutputEncoding = System.Text.Encoding.UTF8;
        // 对于.NET Core 3.0+
        Console.InputEncoding = System.Text.Encoding.UTF8;

        while (true)
        {
            // 设置前景色为绿色
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("请输入您的问题(按'q'或者使用ctrl+c退出):");
            Console.ForegroundColor = ConsoleColor.White;

            string userInput = Console.ReadLine();

            Console.WriteLine("输入的内容为:" + userInput);
            if (userInput.ToLower() == "q")
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("退出程序...");
                Console.ForegroundColor = ConsoleColor.White;
                break;  //用户输入'q'时跳出循环,结束程序
            }
            
            //调用定义好的函数进行对话并处理响应
            await CallQWen(userInput);
        }
    }
}

public class QianWenRequest
{
    public string Model { get; set; }
    public Input Input { get; set; }
}

public class Input
{
    public string Prompt { get; set; }
}

你可能感兴趣的:(AI,C#,c#,开发语言,通义千问,AI,大模型,文心一言)