C# 调用腾讯混元大模型

2023-12-20 01:00:01

写在前面

今天用C#调用了一下腾讯混元大模型,调用代码贴一下,具体的效果等深入使用后再来评价。

GitHub - TencentCloud/tencentcloud-sdk-dotnet: Tencent Cloud API 3.0 SDK for .NET

腾讯混元大模型简介_腾讯混元大模型购买指南_腾讯混元大模型操作指南-腾讯云 (tencent.com)

代码实现

using Newtonsoft.Json;
using System;
using System.Threading.Tasks;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Cvm.V20170312;
using TencentCloud.Cvm.V20170312.Models;
using TencentCloud.Cwp.V20180228;
using TencentCloud.Hunyuan.V20230901;
using TencentCloud.Hunyuan.V20230901.Models;

namespace TencentCloudExamples
{
    class Sample
    {
        static void Main(string[] args)
        {
            // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
            Credential cred = new Credential
            {
                SecretId = "xxx",
                SecretKey = "xxx"
            };
            GetChatStd(cred);

        }

        private static string GetRegionTest(Credential cred)
        {
            try
            {
                // 实例化一个client选项,可选的,没有特殊需求可以跳过
                ClientProfile clientProfile = new ClientProfile();
                // 实例化一个http选项,可选的,没有特殊需求可以跳过
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = ("cvm.tencentcloudapi.com");
                clientProfile.HttpProfile = httpProfile;

                // 实例化要请求产品的client对象,clientProfile是可选的
                CvmClient client = new CvmClient(cred, "", clientProfile);
                // 实例化一个请求对象,每个接口都会对应一个request对象
                DescribeRegionsRequest req = new DescribeRegionsRequest();

                // 返回的resp是一个DescribeRegionsResponse的实例,与请求对象对应
                var resp = client.DescribeRegions(req);
                resp.Wait();
                var result = resp.Result;
                if (result.RegionSet.Length > 0)
                {
                    var firstRegion = result.RegionSet[0];
                    Console.WriteLine(firstRegion.Region);
                    return firstRegion.Region;
                }

                // 输出json格式的字符串回包
                //Console.WriteLine(AbstractModel.ToJsonString(result));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return string.Empty;
        }

        private static void GetChatStd(Credential cred)
        {
            try
            {
                // 实例化一个client选项,可选的,没有特殊需求可以跳过
                ClientProfile clientProfile = new ClientProfile();
                // 实例化一个http选项,可选的,没有特殊需求可以跳过
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = ("hunyuan.tencentcloudapi.com");
                clientProfile.HttpProfile = httpProfile;


                var region = GetRegionTest(cred);

                // 实例化要请求产品的client对象,clientProfile是可选的
                HunyuanClient client = new HunyuanClient(cred, region, clientProfile);
                 实例化一个请求对象,每个接口都会对应一个request对象
                //GetTokenCountRequest req = new GetTokenCountRequest();
                //req.Prompt = "我是谁";
                 返回的resp是一个GetTokenCountResponse的实例,与请求对象对应
                //GetTokenCountResponse resp = client.GetTokenCountSync(req);
                 输出json格式的字符串回包
                //Console.WriteLine(AbstractModel.ToJsonString(resp));

                var req = new ChatStdRequest();
                Message[] messages = { new Message() { Role = "user", Content = "写一个二分法算法" } };
                req.Messages = messages;
                req.TopP = 0;
                req.Temperature= 1;

                var rsp = client.ChatStdSync(req);
                foreach (var item in rsp)
                {
                    var ob = JsonConvert.DeserializeObject<ResultItem>(item.Data);
                    foreach (var choicesItem in ob.Choices)
                    {
                        Console.Write(choicesItem.Delta.Content);
                    }
                }
                Console.WriteLine();
                Console.WriteLine("---回答结束---");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            Console.Read();
        }
    }
     
    public class Delta
    {
        public string Role { get; set; }

        public string Content { get; set; }
    }

    public class ChoicesItem
    {
        public string FinishReason { get; set; }

        public Delta Delta { get; set; }
    }

    public class Usage
    {
        public int PromptTokens { get; set; }

        public int CompletionTokens { get; set; }

        public int TotalTokens { get; set; }
    }

    public class ResultItem
    {
        public string Note { get; set; }

        public List<ChoicesItem> Choices { get; set; }

        public int Created { get; set; }

        public string Id { get; set; }

        public Usage Usage { get; set; }
    }

}

调用结果:

?

文章来源:https://blog.csdn.net/rjcql/article/details/135096287
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。