C#调用 AI学习从0开始-第1阶段(基础与工具)-第2天Prompt工程基础

什么是 AI Prompt?

  1. 最简单定义
    Prompt 就是你给 AI 的「指令、提示词、问话、要求」。
    你跟 AI 说的每一句话、每一段要求,都是 Prompt。
    就像:
    你给人下任务 = 给 AI 写 Prompt
    人能不能做好,看你指令清不清楚
    AI 能不能答好,全看 Prompt 写得好不好

任务:

1.修改代码,加入 System Prompt(角色设定)

2.尝试 Few-shot(给几个例子)

3.尝试约束输出格式(JSON/Markdown)

源码:使用下面源码,按顺序修改messages内容后调试,感受变化。

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace ConsoleApp1.BLL
{
    //Prompt工程基础
    //加入 System Prompt(角色设定)
    //尝试 Few-shot(给几个例子)
    //尝试约束输出格式(JSON/Markdown)
    public class Day2_Prompt
    {
        public static async Task Day2(string apiKey, string url)
        {

            // 替换成你的阿里云百炼 API Key
            //const string apiKey = "此处写你申请的API Key";
            //const string url = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";

            var client = new HttpClient();
            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");

            var requestBody = new
            {
                model = "qwen-turbo",
                //4. 约束输出位xml格式
                messages = new[]
                    {
                        new { role = "system", content = "只输出 XML 完整格式,不要有其他内容。" },
                        new { role = "user", content = "RAG 是一种结合检索和生成的技术,可以解决大模型的幻觉问题。" }  //AI 回答:{"summary":"RAG 是一种结合检索和生成的技术,可以解决大模型的幻觉问题。", "keywords":["RAG", "检索", "生成", "大模型", "幻觉问题"]}
                    }

                // 3. 约束输出为 JSON
                //messages = new[]
                //    {
                //        new { role = "system", content = "只输出 JSON 格式,不要有其他内容。格式:{\"summary\":\"...\", \"keywords\":[\"...\"]}" },
                //        new { role = "user", content = "RAG 是一种结合检索和生成的技术,可以解决大模型的幻觉问题。" }  //AI 回答:{"summary":"RAG 是一种结合检索和生成的技术,可以解决大模型的幻觉问题。", "keywords":["RAG", "检索", "生成", "大模型", "幻觉问题"]}
                //    }

                //2.Few-shot少样本学习
                //messages = new[]
                //{
                //    new { role = "system", content = "将用户输入分类为:技术问题、闲聊、其他" },
                //    new { role = "user", content = "C# 怎么读取文件" },
                //    new { role = "assistant", content = "技术问题" },
                //    new { role = "user", content = "今天天气不错" },
                //    new { role = "assistant", content = "闲聊" },
                //    //new { role = "user", content = "什么是 RAG" }  // 让 AI 分类这个  //AI 回答:技术问题
                //    new { role = "user", content = "今天吃什么" }  // 让 AI 分类这个 //AI 回答:闲聊
                //}

                //1.加入 System Prompt(角色设定)
                //messages = new[]
                //{
                //    new { role="system",content="你是一个.net技术专家,回答要简洁、准确,不超过50个字。"},  //AI 回答:.NET 是由微软开发的跨平台开发框架,用于构建多种类型的应用程序。
                //    new { role = "user", content = "用一句话说明什么是 .NET" }
                //}


            };

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

            Console.WriteLine("正在调用阿里云百炼 AI...\n");

            try
            {
                var response = await client.PostAsync(url, content);
                var responseString = await response.Content.ReadAsStringAsync();

                if (response.IsSuccessStatusCode)
                {
                    var doc = JsonDocument.Parse(responseString);
                    var answer = doc.RootElement
                        .GetProperty("choices")[0]
                        .GetProperty("message")
                        .GetProperty("content")
                        .GetString();
                    Console.WriteLine($"AI 回答:{answer}");


                }
                else
                {
                    Console.WriteLine($"HTTP 错误:{response.StatusCode}");
                    Console.WriteLine($"响应内容:{responseString}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"异常:{ex.Message}");

            }
        }

    }
}

跑通后,试试不同的 System Prompt 和输出格式。

相关推荐
triplemeng几秒前
AI正在改变“真实”的含义:从《黑客帝国》、鲍德里亚到生成式人工智能
人工智能·深度学习·神经网络·生成式·黑客帝国·鲍德里亚·后真相
创世宇图1 分钟前
从 Cantus 看 AI Coding 新趋势:当模型不再追求“通用榜单第一”
人工智能
bittersuite5 分钟前
文本预处理,语言模型
人工智能·深度学习·机器学习
听你说329 分钟前
AI 浪潮席卷 2026 ChinaJoy,KTC 移动屏解锁移动智能显示全新交互形态
人工智能·智能家居
宇宙第一小趴菜14 分钟前
七、机器学习建模流程
人工智能·机器学习
奥莱维25 分钟前
智慧酒店解决方案的未来形态_从单房智能到全域无感服务
大数据·人工智能
zhang1338308907527 分钟前
CG-85D 水工大坝渗压监测振弦式传感器
运维·服务器·网络·人工智能·自动化
paopaokaka_luck29 分钟前
基于springboot3+vue3的云南本土影视文旅推荐平台(协同过滤算法、Echarts图形化分析)
前端·spring boot·学习·算法·echarts·mybatis