diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..71ad080 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,28 @@ +{ + "slidev.include": [ + "**/slides.md", + "dotnet/Workshops/IntroToSK/02_AI_Frameworks/04_Memory.md", + "dotnet/Workshops/IntroToSK/02_AI_Frameworks/08_Text_Search.md", + "dotnet/Workshops/IntroToSK/02_AI_Frameworks/07_Process_Framework.md", + "dotnet/Workshops/IntroToSK/02_AI_Frameworks/06_SK_Agent_Framework.md", + "dotnet/Workshops/IntroToSK/02_AI_Frameworks/05_Middleware.md", + "dotnet/Workshops/IntroToSK/02_AI_Frameworks/03_Tools_and_Function_Invocation.md", + "dotnet/Workshops/IntroToSK/02_AI_Frameworks/02_Evaluations_and_Tests.md", + "dotnet/Workshops/IntroToSK/02_AI_Frameworks/01_Prompts_and_Prompt_Execution.md", + "dotnet/Workshops/IntroToSK/02_AI_Frameworks/00_Introduction_to_AI_Development.md", + "dotnet/Workshops/IntroToSK/01_Prompt_Engineering/03_Anatomy_of_an_Effective_Prompt.md", + "dotnet/Workshops/IntroToSK/01_Prompt_Engineering/02_Types_of_Prompts.md", + "dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.2_Prompt_Engineering_Techniques.Reasoning_Techniques.md", + "dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.1_Prompt_Engineering_Techniques.Basic_Techniques.md", + "dotnet/Workshops/IntroToSK/01_Prompt_Engineering/01_Introduction_to_Prompt_Engineering.md", + "dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.5_Prompt_Engineering_Techniques.Constrained_Decoding.md", + "dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.4_Prompt_Engineering_Techniques.Parameter_Tuning.md", + "dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.3_Prompt_Engineering_Techniques.RAG.md", + "dotnet/Workshops/IntroToSK/01_Prompt_Engineering/09_Resources_for_Further_Learning.md", + "dotnet/Workshops/IntroToSK/01_Prompt_Engineering/08_Safety_Ethics_and_Fallback_Responses.md", + "dotnet/Workshops/IntroToSK/01_Prompt_Engineering/07_Testing_and_Evaluation_Strategies.md", + "dotnet/Workshops/IntroToSK/01_Prompt_Engineering/06_Best_Practices_for_Effective_Prompts.md", + "dotnet/Workshops/IntroToSK/01_Prompt_Engineering/05_Practical_Use_Cases_and_Examples.md", + "dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04_Prompt_Engineering_Techniques.md" + ] +} \ No newline at end of file diff --git a/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/01_Introduction_to_Prompt_Engineering.md b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/01_Introduction_to_Prompt_Engineering.md new file mode 100644 index 0000000..0605c24 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/01_Introduction_to_Prompt_Engineering.md @@ -0,0 +1,95 @@ +--- +title: "Introduction to Prompt Engineering" +doc-type: intro +module: "01 - Prompt Engineering" +order: 1 +tags: + - prompts + - sdk + +marp: true +theme: gaia +size: 16:9 +paginate: true + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + +--- + + + + +# 1. Introduction to Prompt Engineering + +- **Definition**: Crafting effective prompts to guide AI models toward optimal outputs. +- **Importance**: + - Enhances AI performance through clear instructions and context. + - Ensures accurate, relevant, and safe AI interactions. + - Improves control and predictability of AI responses. + +--- + +## Prerequisites +- Access to an AI language model supported by Azure Inference Services, OpenAI, or OLLAMA. +- Visual Studio Code or Codespaces. +- AI Toolkit VS Code Extension ([ms-windows-ai-studio.windows-ai-studio](vscode:extension/ms-windows-ai-studio.windows-ai-studio)). + +--- + +**Learning Objectives** +- Define prompt engineering and explain its significance in enterprise AI scenarios. +- Identify the key components and types of prompts. +- Structure prompts for clarity, safety, and effectiveness. +- Optimize prompts using measurable rubrics and evaluation metrics. + +--- + +## Summary + +This module on prompt engineering is designed for developers working in enterprise environments who want to master prompt engineering for AI systems. By the end of this section, you will be able to: +- Define prompt engineering and explain its significance in enterprise AI applications. +- Identify the key components and types of prompts. +- Learn how to structure prompts for clarity, safety, and effectiveness. +- Optimize prompts using measurable rubrics - whilst explaining your approach. +- Understand how to leverage tools like Semantic Kernel, Microsoft.Extensions.AI, and Prompty for prompt development and evaluation. + +Throughout the workshop, you will use Semantic Kernel and Prompty to design, test, and optimize prompts against measurable rubrics. + +--- + +## Table of Contents + +| # | Module | Time | Summary | +|---|--------|------|---------| +| 1 | [Introduction to Prompt Engineering](./01_Introduction_to_Prompt_Engineering.md) | 15min | Core concepts, importance, and objectives. | +| 2 | [Types of Prompts](./02_Types_of_Prompts.md) | 15min | Overview of prompt types. | +| 3 | [Anatomy of an Effective Prompt](./03_Anatomy_of_an_Effective_Prompt.md) | 15min | Required and optional prompt components. | +| 4 | [Prompt Engineering Techniques](./04_Prompt_Engineering_Techniques.md) | 30min | Basic and advanced techniques, parameter tuning. | +| 5 | [Practical Use Cases and Examples](./05_Practical_Use_Cases_and_Examples.md) | 20min | Real-world applications across scenarios. | +| 6 | [Best Practices for Effective Prompts](./06_Best_Practices_for_Effective_Prompts.md) | 15min | Guidelines for clarity, context, and iteration. | +| 7 | [Testing and Evaluation Strategies](./07_Testing_and_Evaluation_Strategies.md) | 15min | Methods for evaluating prompt quality. | +| 8 | [Safety, Ethics, and Fallback Responses](./08_Safety_Ethics_and_Fallback_Responses.md) | 20min | Handling sensitive content and fallback. | +| 9 | [Resources for Further Learning](./09_Resources_for_Further_Learning.md) | 10min | Documentation and training resources. | + +--- + +### Install the AI Toolkit VS Code Extension + +To install the "[ms-windows-ai-studio.windows-ai-studio](vscode:extension/ms-windows-ai-studio.windows-ai-studio)" extension in VSCode using the CLI, follow these steps: + +1. Open a terminal and run the following command to install the extension: + + ```bash + code --install-extension ms-windows-ai-studio.windows-ai-studio + ``` + +2. Ensure that the extension is successfully installed by running: + + ```bash + code --list-extensions | grep ms-windows-ai-studio.windows-ai-studio + ``` + +Alternatively, you can open this repository in a dev container and install the extension using the same command inside the container's terminal. diff --git a/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/02_Types_of_Prompts.md b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/02_Types_of_Prompts.md new file mode 100644 index 0000000..feab9b7 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/02_Types_of_Prompts.md @@ -0,0 +1,151 @@ +--- +title: "Types of Prompts" +doc-type: content +module: "01 - Prompt Engineering" +order: 2 +tags: + - prompts + +marp: true +theme: gaia +size: 16:9 +paginate: true + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + +--- + + + + +# Types of Prompts + +## Prerequisites +- Understand core principles from the Introduction to Prompt Engineering module. + +## Learning Objectives +- Describe different prompt types and their use cases. + +--- + +## Key Concepts +- Zero-shot prompts +- Few-shot and multi-shot prompts +- Chain of Thought prompts +- Zero-shot Chain of Thought prompts + +Prompts influence how an AI, like ChatGPT, understands and responds to your request. Mastering prompt types will help you achieve more accurate and useful results. + +--- + +### Zero-shot (Direct) + +**Definition:** Provide the AI with a direct instruction—no examples are given. +**Purpose:** Test the AI’s general capability to follow instructions. +**Example:** +> Translate the following sentence to French: "Where is the library?" + +**Use when:** You want a quick, straightforward answer or to test the model’s basic abilities. + +--- + +### Few-shot and Multi-shot +**Definition:** Add one (few-shot) or several (multi-shot) examples in your prompt to guide the AI’s response. +**Purpose:** Demonstrate the desired format, style, or logic with concrete examples. + +**Few-shot Example:** +``` +Q: What is the capital of France? +A: Paris +Q: What is the capital of Italy? +A: Rome +Q: What is the capital of Germany? +A: +``` + +**Multi-shot Example:** (More than two examples) +``` +Translate the following: +English: Hello | French: Bonjour +English: Good morning | French: Bonjour +English: Good night | French: +``` + +**Use when:** The task requires specific output style or handling uncommon instructions. + +--- + +### Chain of Thought (CoT) + +**Definition:** Ask the model to display its reasoning step-by-step. +**Purpose:** Promote transparency and accuracy in complex or multi-step problems. + +**Example:** +``` +Q: If there are 3 red balls and 2 blue balls in a box, and you take out one ball at random, what is the probability it is red? Explain your reasoning. +A: There are 3 red balls and 2 blue balls, so a total of 5 balls. The probability of drawing a red ball is 3 out of 5, or 3/5. +``` + +**Use when:** The task involves logical or mathematical reasoning that benefits from breaking down into steps. + +--- + +### Zero-shot Chain of Thought (Zero-shot CoT) + +**Definition:** Directly ask the model to reason step-by-step without providing examples. +**Purpose:** Encourages explicit reasoning without prior samples. + +**Example:** +> What is 27 times 16? Please think step by step. + +(AI might respond with intermediate calculation steps.) + +**Use when:** You want the model to engage in reasoning but haven’t supplied examples. + +--- + +### Tree of Thought + +**Definition:** A prompt structure that encourages the model to explore multiple reasoning paths or solutions in a branching, tree-like manner. Each branch represents a different line of reasoning or possible answer, allowing the model to backtrack and compare alternatives. + +**Purpose:** Useful for complex problem-solving where evaluating multiple options or strategies is beneficial. + +**Example:** +> You are solving a puzzle. At each step, list all possible moves and their consequences. Then, choose the most promising path and continue. If you reach a dead end, backtrack and try another branch. + +(AI might respond by outlining several possible moves at each step, evaluating them, and selecting the best one.) + +**Use when:** The task involves decision-making, planning, or problems with multiple possible solutions that benefit from exploring alternatives. + +--- + +### Graph of Thought + +**Definition:** A prompt structure that allows the model to reason using a network or graph, where ideas, facts, or reasoning steps are represented as nodes connected by relationships (edges). This enables the model to consider dependencies and interactions between different concepts. + +**Purpose:** Useful for tasks that require mapping relationships, integrating information from multiple sources, or handling non-linear reasoning. + +**Example:** +> Map out the relationships between climate change, renewable energy, and economic growth. Show how each concept influences the others, and identify feedback loops. + +(AI might respond with a diagram or description of nodes and connections, explaining how each factor affects the others.) + +**Use when:** The task involves complex interdependencies, systems thinking, or integrating information from various domains. + +--- + +### Sketch of Thought + +**Definition:** A prompt style that encourages the model to quickly outline or sketch key ideas, steps, or components before elaborating in detail. This can take the form of bullet points, diagrams, or high-level summaries. + +**Purpose:** Helps organize thinking, clarify structure, and ensure all important aspects are considered before deep reasoning. + +**Example:** +> Before writing a detailed essay on the impact of AI in healthcare, list the main points and arguments you plan to cover as a quick outline. + +(AI might respond with a bulleted list of key arguments or a rough structure for the essay.) + +**Use when:** You want to brainstorm, plan, or structure a response before generating detailed content. diff --git a/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/03_Anatomy_of_an_Effective_Prompt.md b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/03_Anatomy_of_an_Effective_Prompt.md new file mode 100644 index 0000000..23985c0 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/03_Anatomy_of_an_Effective_Prompt.md @@ -0,0 +1,118 @@ +--- +title: "Anatomy of an Effective Prompt" +doc-type: content +module: "01 - Prompt Engineering" +order: 3 +tags: + - prompts + - sdk + +marp: true +theme: gaia +size: 16:9 + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + +--- + + + + +# Anatomy of an Effective Prompt + +## Prerequisites +- Complete the Types of Prompts module. + +## Learning Objectives +- Describe required and optional components of an effective prompt. + +--- + +## Key Concepts +- Core Task +- System Instructions +- Examples +- Contextual Information + +Effective prompts are structured with deliberate components that enhance clarity, ensure context, and guide the language model toward the desired response. Below is a breakdown of the key elements: + +--- + +### Core Task (Required) +The core task is the critical anchor of any prompt and must be both clear and specific. This involves: + +- Clearly defined instructions or questions +- Eliminating ambiguity by stating exactly what needs to be accomplished +- Using direct language to focus the model’s attention + +**Example:** + +> Summarize the main points from the following article. + +--- + +### System Instructions +Use this section to: + +- Define the style, tone, or persona (e.g., "Respond as a technical expert") +- Impose constraints (e.g., "Limit your answer to three sentences" or "Avoid jargon") +- Clarify the expected format of output + +**Example:** + +> You are a friendly customer support agent. Answer in a helpful and concise manner. + +--- + +### Examples +Demonstrate desired input-output relationships with examples to: + +- Set expectations for style, structure, or reasoning +- Clarify edge cases or ambiguous requests + +**Format:** + +``` +Input: [Sample input] +Output: [Expected output] +``` + +**Example:** + +``` +Input: What is the capital of France? +Output: Paris +``` + +--- + +### Contextual Information +Provide relevant background or reference data, such as: + +- Definitions, reference documents, or data tables +- Context that grounds the response or clarifies assumptions + +**Example:** + +> Based on the attached project brief document, summarize the deliverables. + +--- + +## Roles in Prompting +Understand the distinct responsibilities in prompt-driven workflows: + +- **User:** Initiates the request, specifying tasks and providing necessary background or constraints. +- **Tool:** (If present) Applies prioritized instructions, policies, or constraints, often before passing to the assistant. +- **Assistant:** Generates the response, following the instructions, adapting to examples provided, and respecting any constraints. + +--- + +## Tips + +- Be explicit with required tasks. +- Layer optional components for increased accuracy. +- Use examples to reduce ambiguity. +- Specify roles and expectations as needed. diff --git a/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.1_Prompt_Engineering_Techniques.Basic_Techniques.md b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.1_Prompt_Engineering_Techniques.Basic_Techniques.md new file mode 100644 index 0000000..717340b --- /dev/null +++ b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.1_Prompt_Engineering_Techniques.Basic_Techniques.md @@ -0,0 +1,130 @@ +--- +title: "Prompt Engineering Techniques" +module: "01 - Prompt Engineering" +order: 4 +tags: + - prompts + - sdk +duration: "30min" +marp: true +theme: gaia +size: 16:9 +paginate: true +doc-type: content + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + +--- + + + + +# Prompt Engineering Techniques + +## Basic Techniques + +--- + +### Clear and Specific Instructions +Providing clear and specific instructions is foundational in prompt engineering. Ambiguous or generic prompts often lead to vague or incorrect outputs from language models. Instead, specify exactly what you expect: + +- Use unambiguous language. +- Indicate the format, scope, and depth required. +- Provide context as needed to reduce misunderstandings. + +**Example:** +Instead of "Explain climate change," prompt: + +> Summarize the primary causes of climate change in three bullet points, each with a one-sentence explanation. + +--- + +### Reference Text and Citations +Including reference text or source material enhances factual accuracy and reliability. Instructionally, it’s productive to: + +- Append background documents or snippets to your prompt. +- Ask the model to cite specific passages from the text. +- Use citation styles (e.g., inline quotes, (Author, Year)). + +**Tip:** +> Based on the information provided in the following text, answer the question and include a citation for your sources. + +--- + +### Task Decomposition +For multi-step or complex tasks, break down the instruction into digestible sub-tasks. This aligns the model’s output to your workflow and reduces cognitive load. + +- List the required steps explicitly. +- Sequence instructions using numbering or bullets. +- Confirm completion of each part before proceeding. + +**Example:** +1. Pull data from the attached table. +2. Summarize key trends. +3. Suggest one actionable next step. + +--- + +### Instruction Placement +The order of information within your prompt strongly influences the quality and relevance of responses. + +- Place the main task or question at the beginning for focus. +- Use separate paragraphs or sections for reference material and instructions. +- If requesting citations or format specifications, state them immediately before or after your main instruction. + +**Example Structure:** +- **Context/Background:** + [Relevant content] +- **Task:** + [Instruction statement] +- **Formatting:** + [How you want the answer structured] + +--- + +### Output Priming and Syntax +Priming with example outputs or specifying required syntax improves consistency and utility. + +- Provide sample answers or templates. +- Specify output limitations (e.g., bullet points, tables, JSON). +- If using the response as system input, state explicit formatting requirements. + +**Example Prompt for Priming:** +> List three types of renewable energy. Answer in the following format: + +- Solar +- Wind +- Hydroelectric + + +### Prompt Chaining + +**Definition:** Connecting several prompts so the output of one becomes the input to the next. This enables complex, multi-step workflows in which each prompt handles a distinct sub-task. + +**Concrete Uses:** +- Decomposing a challenging question into simpler parts, with separate prompts for each step. +- Popular with tasks like legal document analysis, multi-part data extraction, and multi-stage conversation agents. + +**Advantages:** Increases modularity, allows for iterative refinement, and makes error diagnosis easier by identifying failing steps within a chain. + +--- + +### Self-consistency and Reflexion + +--- + +**Self-consistency:** Improves reliability by running a prompt multiple times (using a high temperature for diversity) and then aggregating answers; the most frequent (self-consistent) result is chosen. + +**Steps:** +1. Generate multiple outputs using the same prompt (encourage reasoning diversity). +2. Extract answers from each. +3. Vote/aggregate to choose the most common answer. + +**Example:** Classify an ambiguous email repeatedly; if the majority of runs say "IMPORTANT", that label is returned. + +--- + +**Reflexion:** Prompt the model to critique or revise its own output, iteratively. Often involves metaprompts that ask for error checking, self-assessment, or explicit correction strategies. diff --git a/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.2_Prompt_Engineering_Techniques.Reasoning_Techniques.md b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.2_Prompt_Engineering_Techniques.Reasoning_Techniques.md new file mode 100644 index 0000000..d6f9fa3 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.2_Prompt_Engineering_Techniques.Reasoning_Techniques.md @@ -0,0 +1,143 @@ +--- +title: "Prompt Engineering Techniques" +module: "01 - Prompt Engineering" +order: 4 +tags: + - prompts + - sdk +duration: "30min" +marp: true +theme: gaia +size: 16:9 +paginate: true +doc-type: content + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + +--- + + + + +# Prompt Engineering Techniques + +## Advanced Techniques + +--- + +### Reasoning Frameworks / Cognitive Architectures + +--- + +#### Chain-of-Thought (CoT) Reasoning +CoT prompting guides LLMs to generate intermediate reasoning steps, improving their accuracy on complex problems (especially mathematics and stepwise logic). It works with general LLMs, does not require fine-tuning, and builds interpretability into outputs. + +**Example:** +Prompt: When I was 3 years old, my partner was 3 times my age. Now, I am 20 years old. How old is my partner? Let's think step by step. + +Output: +1. When I was 3, my partner was 3 * 3 = 9 years old. +2. The age gap is 6 years. +3. Now I am 20, so my partner is 20 + 6 = 26 years old. +**Answer:** 26 + +CoT can be used in zero-shot and few-shot settings; combining with examples can dramatically improve output reliability. + +**Use cases:** code generation, problem decomposition, synthetic data creation, or any scenario where "thinking aloud" through the answer is helpful. + +--- + +#### Tree-of-Thought (ToT) +Extends CoT by enabling the model to branch reasoning and explore multiple possibilities at each step, using a tree structure. ToT is ideal for complex problem-solving where backtracking and comparing alternatives are valuable. Each branch (or "thought") is a coherent reasoning step; models can traverse different branches, evaluate intermediate results, and choose the best solution path. + +**Example:** + +**Use cases:** +- Complex problem-solving (e.g., puzzles, planning, creative writing) +- Scenarios requiring exploration of alternatives and backtracking +- Tasks where evaluating and comparing multiple reasoning paths is beneficial + +--- + +#### Graph-of-Thought (GoT) +GoT represents reasoning as a flexible graph, not just a tree or chain. Thoughts (nodes) can connect in non-linear ways, allowing for iterative refinement and multi-directional reasoning. This mirrors complex human thought processes and is useful for tasks with interdependent subproblems. + +**Example:** +Suppose the problem is: "How do you solve the problem of climate change?" + +Instead of following a single, linear chain of reasoning, GoT breaks the problem into interconnected sub-questions (nodes), each exploring a different aspect. For example: + +* Node 1: What are the economic impacts of climate change? +Explores: Effects on the global and U.S. economy, costs of inaction, etc. +* Node 2: How does renewable energy help mitigate climate change? +Explores: Impact of renewables, global adoption, personal stories, etc. +* Node 3: What are policy solutions for climate change? +Explores: Carbon taxes, international agreements, local initiatives, etc. + +Each node can connect to others (e.g., economic impacts link to policy solutions), and the reasoning can move back and forth between them, revisiting and refining ideas as new insights emerge. This structure allows for multi-directional, iterative reasoning—mirroring how humans tackle complex, interdependent problems. + +**Use cases:** +- Multi-modal reasoning (combining text, images, data) +- Problems with interdependent steps or feedback loops +- Iterative tasks where revisiting and refining earlier steps is needed + +--- + +#### Sketch-of-Thought (SoT) +SoT uses expert lexicons and chunked symbolism as short-hand expressions to constrain reasoning about a specific domain and reduce token usage. + +**Example:** +For a logic puzzle, instead of writing "If A is true, then B must be false," SoT might use: "A→¬B". + +**Use cases:** +- Commonsense reasoning and multi-hop inference +- Fact-based recall tasks +- Scenarios requiring concise, structured, and efficient reasoning + +--- + +#### Active Prompts & ReasonFlux +Active Prompts dynamically adapt prompt content or structure in response to model outputs or user interaction, creating an iterative feedback loop for enhanced problem solving. + +ReasonFlux refers to managing and refining flows of reasoning across multiple cycles, often with feedback-driven improvement at each stage. + +**Example:** +In customer support, the model asks clarifying questions based on user responses, refining its answers iteratively. In data analysis, prompts are adjusted based on previous outputs to drill down into insights. + +**Use cases:** +- Interactive applications (customer support, tutoring) +- Data analysis and reporting +- Any scenario where iterative refinement and feedback improve results + +--- + +#### ReAct (Reason & Act) +Blends logical reasoning with tool use (e.g. searching the web, running code). The LLM alternates between “thinking” (reasoning) and “acting” (calling an external tool), updating internal state with results at every iteration. + + +**Example:** +Suppose the question is: +"How many rooms are in the hotel that is home to the Cirque du Soleil show Mystere?" + +The ReAct process would look like this: + +Thought: I need to find out which hotel hosts the Cirque du Soleil show Mystere. +Action: Search for "hotel hosting Cirque du Soleil Mystere". +Observation: The show Mystere is hosted at Treasure Island Hotel. +Thought: Now, I need to find out how many rooms are in Treasure Island Hotel. +Action: Search for "number of rooms in Treasure Island Hotel". +Observation: Treasure Island Hotel has 2,884 rooms. +Thought: I now know the final answer. +Final Answer: Treasure Island Hotel, which hosts Mystere, has 2,884 rooms. + +**Use cases:** +- Complex question answering requiring external information +- Web search and fact-checking tasks +- Data extraction and aggregation from multiple sources +- Tool-augmented reasoning (e.g., calculators, APIs) +- Interactive agents (assistants, chatbots) that need to reason and act iteratively +- Any scenario where combining logical reasoning with real-time actions improves accuracy or utility + diff --git a/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.3_Prompt_Engineering_Techniques.RAG.md b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.3_Prompt_Engineering_Techniques.RAG.md new file mode 100644 index 0000000..5c45e36 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.3_Prompt_Engineering_Techniques.RAG.md @@ -0,0 +1,88 @@ +--- +title: "Prompt Engineering Techniques" +module: "01 - Prompt Engineering" +order: 4 +tags: + - prompts + - sdk +duration: "30min" +marp: true +theme: gaia +size: 16:9 +paginate: true +doc-type: content + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + +--- + + + + +# Prompt Engineering Techniques + +## Retrieval-Augmented Generation (RAG) + +--- + +### Naive RAG / Cache Augmented Generation (CAG) + +This process simply includes all of the additional context that you need in the context window for the LLM. Instead of trying to intelligently include only the most relevant content, including the entire document allows for the LLM to utilize the full context and infer what is more relevant. + +***Benefits***: This simplifies retrieval and solution design. +***Detriments***: + * With sufficiently long contexts, current llms suffer from the "lost-in-the-middle" problem. + * This only works if you can fit the entire document in the context window. + +--- + +### Self-RAG + +This technique was introduced to allow a single model to self-determine if its own knowledge could be trusted. By fine-tuning a model to classify the 3 tasks (retrieve, generate, and critique), along with outputting special "reflection tokens" that signal which tasks to engage in, an llm can signal when its training set is insufficient and reduce hallucinations. + +***Benefits***: Allows an llm to act as its own fact-checking agent. +***Detriments***: Requires fine-tuning - which can be costly. + +--- + +### Vector RAG + +By using a vector embedding model, inputs can be converted into vectors that are then used to search a vector database for relevance. + +***Benefits*** Allows for semantic similarity search. +***Detriments*** + * The entire indexed dataset needs to be re-indexed if you change the embedding model used. + * Chunking strategies need to be designed to produce meaningful embeddings. + * Indexed search results must be semantically similar to the query - not necessarily an expected answer (though this can be addressed through techniques like HyDe.) + * "Local" queries about a document can be answered, but "global" queries about a dataset cannot be. + +--- + +### Graph RAG + +Created by Microsoft Research, graphRAG uses an llm to create and map knowledge to an ontology - and create community summaries using a clustering algorithm to generate aggregate summaries of node/edge relationships. + +**Benefits**: Answers "global" questions about a dataset instead of exclusively "local" questions about documents. +**Detriments** Ingestion is much more costly and retrieval becomes more time consuming to traverse the graph. + +--- + +### Path RAG + +Advances on GraphRAG techniques to use node/edge descriptions as natual language answers to user queries. + +**Benefits**: Achieves similar results to graphRAG, with far less compute time. +**Detriments**: Doesn't build community summaries, necessitating graph traversal to process "global" questions. + +--- + +### Agentic RAG + +By treating agents as a Mixture-of-Experts with domain expertise, an LLM can retrieve information relevant to the conversation context through interacting with other agents for the retrieval task, instead of their own toolsets. + +**Benefits**: Makes maintaining/scaling agents much simpler. +**Detriments**: Becomes more difficult to debug/trace. + diff --git a/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.4_Prompt_Engineering_Techniques.Parameter_Tuning.md b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.4_Prompt_Engineering_Techniques.Parameter_Tuning.md new file mode 100644 index 0000000..412c16b --- /dev/null +++ b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.4_Prompt_Engineering_Techniques.Parameter_Tuning.md @@ -0,0 +1,175 @@ +--- +title: "Prompt Engineering Techniques" +module: "01 - Prompt Engineering" +order: 4 +tags: + - prompts + - sdk +duration: "30min" +marp: true +theme: gaia +size: 16:9 +paginate: true +doc-type: content + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + +--- + + + + +# Prompt Engineering Techniques + +## Advanced Techniques + +--- + + +### Prompt Chaining + +**Definition:** Connecting several prompts so the output of one becomes the input to the next. This enables complex, multi-step workflows in which each prompt handles a distinct sub-task. + +**Concrete Uses:** +- Decomposing a challenging question into simpler parts, with separate prompts for each step. +- Popular with tasks like legal document analysis, multi-part data extraction, and multi-stage conversation agents. + +**Advantages:** Increases modularity, allows for iterative refinement, and makes error diagnosis easier by identifying failing steps within a chain. + +--- + +### Self-consistency and Reflexion + +--- + +**Self-consistency:** Improves reliability by running a prompt multiple times (using a high temperature for diversity) and then aggregating answers; the most frequent (self-consistent) result is chosen. + +**Steps:** +1. Generate multiple outputs using the same prompt (encourage reasoning diversity). +2. Extract answers from each. +3. Vote/aggregate to choose the most common answer. + +**Example:** Classify an ambiguous email repeatedly; if the majority of runs say "IMPORTANT", that label is returned. + +--- + +**Reflexion:** Prompt the model to critique or revise its own output, iteratively. Often involves metaprompts that ask for error checking, self-assessment, or explicit correction strategies. + +--- + +## Grounding, Structured Output, & Constrained Decoding + +--- + +**Grounding:** +Grounding refers to ensuring that model outputs are based on verifiable facts or external data, rather than just the model's internal knowledge. This is crucial for applications requiring high factual accuracy. + +--- + +**Structured Output:** +Design prompts to elicit responses in predefined, machine-readable formats (such as JSON, XML, tables). Vital for integration with downstream applications or automated data pipelines. + +--- + +**Constrained Decoding:** +Imposes syntactic, semantic, or business-rules constraints on output (e.g., requiring a valid JSON schema, restricting vocabulary, etc.) Useful for compliance, safety, and ensuring model responses can be programmatically parsed and trusted. Effective prompts balance clear instructions (what to do) and explicit constraints (what not to do). + +--- + +## External Tools Integration +**Tool Use (ReAct, LangChain, Plugins):** +Prompts direct LLMs to invoke tools, APIs, web search, code interpreters, databases, etc. Approaches like ReAct combine language reasoning with actions—LLM thinks, acts (calls a tool/API), observes results, and iterates. + +**Example:** Have the LLM search Google or execute code snippets as part of an answer; each reasoning step can update based on retrieved evidence. + +**Orchestration and Workflows:** +Orchestration refers to coordinating multiple tools, prompts, or models in a workflow to solve complex tasks, often using frameworks or pipelines for automation and reliability. + +--- + +## Parameter Tuning + +Careful parameter tuning is essential for effective prompt engineering with large language models (LLMs). Adjusting these parameters as part of the LLM output configuration process lets you control the randomness, determinism, creativity, and reproducibility of your model outputs for your specific use case. + +--- + +### Temperature and Top_p + +--- + +**Temperature:** + +Temperature controls the degree of randomness in the model's token selection. + +- **Low temperature (e.g., 0.0 - 0.2):** The model outputs are deterministic and focused; the highest probability token is always selected. Suitable for tasks requiring accuracy and consistency (e.g., fact extraction, math problems). +- **Medium temperature (e.g., 0.5):** Allows for some variation, balancing predictability and creativity. +- **High temperature (e.g., 0.9 or above):** Outputs become more diverse and creative with increased randomness, but risk producing less relevant or off-topic text. + +**Best practice:** For tasks requiring precise answers, set temperature to 0. For creative tasks, start with 0.7–0.9 and adjust based on observed output. + +--- + +**Top_p (Nucleus Sampling):** + +Top_p restricts possible next tokens to a subset accumulating to the top p probability mass. + +- **Low top_p (e.g., 0.8 - 0.9):** Results in more focused, deterministic outputs. +- **High top_p (close to 1):** Makes every likely token eligible, increasing diversity. + +**Interaction with temperature and top_k:** These controls can be used together, but at extremes (e.g., top_p = 1 or temperature = 0), the other settings become irrelevant. + +--- + +#### Practical Starting Points + +- For coherent and moderately creative results: `temperature = 0.2`, `top_p = 0.95` +- For maximum creativity: `temperature = 0.9`, `top_p = 0.99` +- For deterministic, fact-based tasks: `temperature = 0`, `top_p = 1` + +> **Tip:** Too much randomness (high temperature and/or top_p) can cause the model to generate loops or filler text, while too little can make outputs monotonous or repetitive. Always experiment and iterate for your task. + +--- + +### Seed Values + +**Purpose:** + +Seed values ensure the reproducibility of model outputs. When you set the same seed, you will (with some exceptions, e.g., model updates) get the same output for a given prompt and model configuration. + +This is especially useful for testing, debugging, or creating teaching materials, as it removes randomness from the workflow. + +> **Note:** Not all platforms expose a seed parameter to users. Where available, setting a seed is highly recommended for reproducibility in demonstrations or evaluations. + +--- + +### Model-specific Parameters + +--- + +**Parameter Diversity:** + +Each LLM and platform may offer different configuration settings beyond just temperature and top_p/top_k. Common additional parameters include: + +- **Max tokens/output length:** Controls how many tokens the LLM generates. More tokens allow for longer responses but increase computational cost and potential drift in responses. +- **Top_k:** Chooses from only the k most likely candidates for the next token. A smaller k makes output more deterministic; a larger k increases possible variety. +- **Stop sequences:** Specify strings at which the output should end, helpful for structured outputs. +- **Specialized parameters:** Some models/platforms offer extra configuration, e.g., controlling system or role behavior, response formatting, or safety/toxicity filters. + +--- + +**Model Behavior:** + +The impact of parameter values may differ between models. Always: + +- Review documentation for your chosen LLM/platform. +- Start with recommended/default settings and iteratively adjust. +- Document settings, results, and iterations to track what works best for your use case. + +--- + +### Best Practice + +Craft and test different prompts, analyze and document the results. Refine prompt design and configuration based on performance. When changing parameters or model, revisit previously used prompts and keep experimenting until the desired output is achieved. diff --git a/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.5_Prompt_Engineering_Techniques.Constrained_Decoding.md b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.5_Prompt_Engineering_Techniques.Constrained_Decoding.md new file mode 100644 index 0000000..c631142 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04.5_Prompt_Engineering_Techniques.Constrained_Decoding.md @@ -0,0 +1,46 @@ +--- +title: "Prompt Engineering Techniques" +module: "01 - Prompt Engineering" +order: 4 +tags: + - prompts + - sdk +duration: "30min" +marp: true +theme: gaia +size: 16:9 +paginate: true +transition: fade +doc-type: content + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + +--- + + + + +# Prompt Engineering Techniques + +## Advanced Techniques + +### Constrained Decoding + +Imposes syntactic, semantic, or business-rules constraints on output (e.g., requiring a valid JSON schema, restricting vocabulary, etc.) Useful for compliance, safety, and ensuring model responses can be programmatically parsed and trusted. Effective prompts balance clear instructions (what to do) and explicit constraints (what not to do). + +--- + +#### Grounding +Grounding refers to ensuring that model outputs are based on verifiable facts or external data, rather than just the model's internal knowledge. This is crucial for applications requiring high factual accuracy. + +--- + +#### Logit Biasing + +--- + +#### Structured Output +Design prompts to elicit responses in predefined, machine-readable formats (such as JSON, XML, tables). Vital for integration with downstream applications or automated data pipelines. diff --git a/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04_Prompt_Engineering_Techniques.md b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04_Prompt_Engineering_Techniques.md new file mode 100644 index 0000000..bbaabcb --- /dev/null +++ b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/04_Prompt_Engineering_Techniques.md @@ -0,0 +1,43 @@ +--- +title: "Prompt Engineering Techniques" +doc-type: content +module: "01 - Prompt Engineering" +order: 4 +tags: + - prompts + - sdk + +marp: true +theme: gaia +size: 16:9 +paginate: true +transition: fade + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + +--- + + + + +# Prompt Engineering Techniques + +## Prerequisites +- Complete Anatomy of an Effective Prompt module. + +## Learning Objectives +- Apply basic and advanced prompt engineering methods. +- Tune prompt parameters for optimal outputs. +- Integrate external tools and reasoning frameworks in prompts. + +--- + +## Key Concepts +- Basic Techniques +- Reasoning frameworks & Cognitive Architectures +- Retrieval Augmented Generation +- Parameter tuning (temperature, top_p) +- Constrained Decoding diff --git a/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/05_Practical_Use_Cases_and_Examples.md b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/05_Practical_Use_Cases_and_Examples.md new file mode 100644 index 0000000..9164d8d --- /dev/null +++ b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/05_Practical_Use_Cases_and_Examples.md @@ -0,0 +1,351 @@ +--- +title: "Practical Use Cases and Examples" +module: "01 - Prompt Engineering" +order: 5 +tags: + - prompts +duration: "20min" +marp: true +theme: gaia +size: 16:9 +paginate: true +transition: fade +doc-type: content + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + +--- + + + + +# Practical Use Cases and Examples + +## Prerequisites +- Complete the Prompt Engineering Techniques module. + +## Learning Objectives +- Apply prompt engineering techniques to code generation, translation, and text tasks. +- Compare prompt structures across different application scenarios. +- Evaluate model outputs for practical examples and refine prompts. + +--- + +## Key Concepts +- Code generation and optimization +- Text and language tasks (creative writing, summarization, translation, dialogue) +- Question answering and classification prompts + +--- + +## Code Generation and Optimization + +--- + +### Completion and Translation + +Large Language Models (LLMs) such as Gemini, GPT, or Claude excel at code completion and code translation tasks. Prompts can be crafted to help automate the generation of snippets, entire functions, or even translate code from one programming language to another. + +--- + +### Generating Code via Prompts + +LLMs can generate code in any language, offering significant productivity boosts. + +**Example prompt for code generation:** + +> **Goal:** Write a Bash script that renames all files in a folder by prepending "draft". +> +> **Prompt:** Write a code snippet in Bash, which asks for a folder name. Then it takes the contents of the folder and renames all the files inside by prepending the name draft to the file name. + +The LLM-generated solution might look like: + +```bash +#!/bin/bash +echo "Enter the folder name: " +read folder_name + +if [ ! -d "$folder_name" ]; then + echo "Folder does not exist." + exit 1 +fi + +files=( "$folder_name"/* ) + +for file in "${files[@]}"; do + new_file_name="draft_$(basename "$file")" + mv "$file" "$new_file_name" +done + +echo "Files renamed successfully." +``` + +**Tip:** Always review and test code generated by LLMs. + +--- + +### Code Explanation and Translation + +LLMs can explain code in plain language to speed up onboarding and collaboration, e.g.: + +> **Prompt:** Explain to me the below Bash code: ... +> +> **Output:** The model breaks down the script line by line in natural language. + +LLMs can translate code between languages. Example prompt: + +> **Prompt:** Translate the below Bash code to a Python snippet. +> +> **Output:** The LLM produces an equivalent Python script, potentially adapting to conventions or idioms of the target language. + +--- + +## Text and Language Tasks + +Prompt engineering for text and language tasks involves crafting prompts that guide large language models (LLMs) to perform tasks such as creative writing, summarization, translation, and dialogue generation. Below is an overview and best practices for each of these categories: + +--- + +### Creative Writing +**Definition:** Using LLMs to generate original content, such as stories, poems, or product descriptions. + +**Prompt Engineering Tips:** +- Use role prompting to assign a creative persona (e.g., "You are a poet," "You are a travel blogger"). +- Include contextual details. For example: “Write a short story about a robot discovering art in a futuristic city.” +- Experiment with output configurations: + - Higher temperature (e.g., 0.7–0.9) encourages creativity and diversity. + - Adjust top-K and top-P for more varied outputs (e.g., top-K = 40, top-P = 0.95). +- Be explicit with instructions: “Write in a humorous tone” or “Include a plot twist at the end.” + +**Example:** +> Act as a fantasy novelist. Write a short scene where a dragon negotiates peace with a village. + +--- + +### Summarization and Translation + +--- + +#### Summarization +**Definition:** Producing concise summaries of longer texts. + +**Prompt Engineering Tips:** +- Specify summary type: “Summarize the following text in three bullet points.” +- State the output length or structure: “Provide a 1-paragraph summary.” +- Use lower temperature for consistent and factual output (e.g., 0.2–0.3). +- System prompt can clarify purpose: “System: You are a professional editor who summarizes technical documents.” + +**Example:** +> Summarize the following article in one paragraph, focusing on key findings and implications. + +--- + +### Translation +**Definition:** Converting text from one language to another. + +**Prompt Engineering Tips:** +- Specify source and target languages: “Translate the following English text to French.” +- Clarify style/tone if needed: “Translate using formal business language.” +- Use low temperature for accuracy (e.g., 0.2). +- System prompt: “System: You are a certified translator.” + +**Example:** +> Translate the following paragraph to Spanish. Maintain a friendly and informal tone. + +The same output controls (temperature, top-K, top-P) can be applied for translation precision and style. + +--- + +### Dialogue Generation +**Definition:** Creating realistic, engaging multi-turn conversations. + +**Prompt Engineering Tips:** +- Use role prompting for persona adoption (“You are a customer service assistant”). +- Provide dialogue context, previous messages, and speaker roles to ensure continuity and relevance. +- Set instructions for output style: “Respond kindly and professionally.” +- Control temperature to balance creativity and coherence (0.2 for fact-based, 0.6+ for creative dialogue). + +**Example:** +> Act as a tech support agent. A user reports their laptop won’t turn on. Generate a 3-turn dialogue to help resolve the issue. + +For longer dialogues, consider limiting token length and ensuring prompts retain necessary context for multi-turn coherence. + +--- + +### General Best Practices Across Tasks +- Be specific about output requirements, structure, or format. +- Design with simplicity: Clear, concise, and unambiguous prompts yield better results. +- Use instructions over constraints: Tell the model what to do rather than what not to do. +- Iterate and experiment: Adjust configurations (temperature, top-K, top-P) and rephrase prompts for the best results. +- Include examples (for few-shot prompting) when zero-shot performance is insufficient. + +--- + +## Question Answering + +--- + +### Open-ended and Specific Questions + +Open-ended questions invite detailed responses, allowing the model to elaborate based on training knowledge or reasoning. Specific questions have clearer, narrower expectations, often factual or seeking precise answers. + +**Prompting Techniques:** + +- **Zero-shot prompting** (just provide the question): + - Example: `What caused the fall of the Roman Empire?` +- **Chain-of-Thought (CoT) prompting:** + - Encourages step-by-step reasoning, promoting accuracy—especially valuable for tasks requiring logical or multi-step solutions. + - Example: + > Q: When I was 3 years old, my partner was 3 times my age. Now I am 20 years old. How old is my partner? Let's think step by step. + > + > The model then outputs reasoning steps before the answer, improving reliability over just giving the final result. + +**Tips:** +- For factual, deterministic answers, use a low temperature (e.g., 0) to reduce randomness. +- For more creative open-ended responses, increase temperature/top-p, but monitor for coherence. + +--- + +### Multiple-Choice and Hypothetical Scenarios + +Multiple-choice questions benefit from: +- Giving examples of the format (few-shot prompting) for the model to follow. +- Explicit instruction to "choose one" or "select the best answer". + +Hypothetical scenarios are effectively handled by: +- Framing the context clearly (role/contextual prompting), e.g., + - `If you were a climate scientist, how would you approach reducing urban air pollution?` +- Encouraging explanation: + - `Please explain your reasoning for your selected answer.` + +**Techniques:** +- Use step-back prompting to have the model consider general principles before applying them to specifics. +- Self-consistency prompting: Generate multiple reasoning paths and select the most common answer if ambiguity exists. + +--- + +### Opinion-based Queries + +The model can provide perspectives, but it's important to clarify whether the answer should mimic an expert, a group, or a neutral party (role prompting). + +**Example Schema:** +- `As a historian, what is your perspective on the impact of the printing press?` +- `List the pros and cons of remote work for software engineers.` + +**Tips:** +- Set the role and style for more relevant or engaging answers (e.g., "as a policy analyst... in a formal tone"). +- If multiple perspectives are desired, explicitly request them: + - `Summarize both sides of this debate regarding renewable energy adoption.` + +--- + +### Best Practices for Question Answering Prompts + +- **Be explicit:** Clarify expectations in the prompt—structure, tone, desired length. +- **Ask for reasoning:** "Explain your answer" yields more robust, trustworthy results. +- **Use context and role:** Guides the model to answer from the desired perspective. +- **Few-shot examples:** Increase accuracy, especially for classification or structured answers. +- **Control randomness:** Adjust model parameters based on the creativity or determinism needed. +- **Test and iterate:** Small changes in phrasing or examples can have large effects—document your prompt attempts. + +--- + +## Image Generation and Editing + +--- + +### Multimodal Prompt Design + +**Definition:** + +Multimodal prompting refers to using a combination of input formats (e.g., text, images, audio) to guide a large language or generative model. For image generation/editing, this typically means using detailed text instructions—possibly combined with an input image—for more targeted outputs. + +--- + +**Best Practices:** + +- **Be explicit:** Clearly state what you want to generate or edit (subject, style, colors, composition, context). +- **Provide examples:** If possible, show examples of desired outputs, or include reference images. +- **Use clear, descriptive language:** Specify attributes like perspective, lighting, and emotional tone. +- **Leverage system/context/role prompting:** Frame your prompt to set the expectations, context, and "role" of the model (e.g., "You are an expert digital artist..."). + +--- + +**Template Example:** + +- **Prompt:** Generate a photorealistic image of a red sports car parked under a palm tree at sunset, viewed from a low angle. +- **Context:** The image should evoke a feeling of luxury and relaxation and use vivid, warm colors. + +If editing, use similar specificity: + +- **Prompt:** Edit the provided image by replacing the sky with a clear, blue sky with a few white clouds, and enhance the brightness. +- **Role:** You are a photo retoucher enhancing images for a travel magazine. + +*Note: Test and refine prompts iteratively for best results.* + +--- + +### Photorealistic and Artistic Images + +--- + +#### Photorealistic Image Prompting + +- Use precise, unambiguous language: specify real-world details, camera/lens type, lighting, realism level. +- Mention artistic techniques if needed, e.g., "bokeh background," "soft natural light." + +**Examples:** + +- "Create a high-resolution, photorealistic image of a golden retriever puppy sitting in a field of sunflowers. The lighting should be natural morning sunlight." +- "Generate a realistic photograph of a bustling Tokyo street at night, with neon signs and people carrying umbrellas." + +--- + +#### Artistic Image Prompting + +- State the style or movement: "in the style of Van Gogh," "cubist," "watercolor." +- Describe mood, palette, or composition elements: "Use cool blue and green tones for a calm, melancholic atmosphere." + +**Examples:** + +- "Create an abstract digital painting of a city skyline at dawn using pastel colors and impressionist brush strokes." +- "Generate a stylized illustration of a fox in a forest, in the style of Japanese woodblock prints." + +**Modifiers:** + +Use artistic modifiers (realism, fantastical, minimalism, dystopian, etc.) to steer the output as desired. + +--- + +### Abstract Images and Editing + +--- + +#### Abstract Image Generation + +- Focus on shapes, color palettes, textures, and conceptual ideas. +- Avoid specific objects unless required; use prompts like: + - "Generate a colorful abstract composition with swirling shapes and a sense of movement, inspired by Kandinsky." + - "Create an abstract image representing the concept of growth using gradients of green and blue." + +--- + +#### Image Editing Prompts + +- Be direct about the edit: "Remove the background," "Add a beam of light coming from the top left," or "Change the subject’s shirt to red." +- If the system allows referencing image regions, use spatial language or coordinates: "Blur the lower right corner slightly." +- Combine with purpose/context: "Enhance sharpness and boost saturation for use in a digital ad campaign." + +--- + +### General Best Practices For All Image Prompts + +- **Simplicity:** Use concise prompts; avoid unnecessary complexity. +- **Variables:** Consider using placeholders if programmatically generating prompts. +- **Experimentation:** Try various configurations (temperature, randomness, etc.) for variation and creativity. +- **Documentation:** Track your best-performing prompts for future reference and continued optimization. + +--- diff --git a/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/06_Best_Practices_for_Effective_Prompts.md b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/06_Best_Practices_for_Effective_Prompts.md new file mode 100644 index 0000000..24966f9 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/06_Best_Practices_for_Effective_Prompts.md @@ -0,0 +1,106 @@ +--- +title: "Best Practices for Effective Prompts" +module: "01 - Prompt Engineering" +order: 6 +tags: + - prompts +duration: "15min" +marp: true +theme: gaia +size: 16:9 +paginate: true +transition: fade +doc-type: content + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + +--- + + + + +# Best Practices for Effective Prompts + +## Prerequisites +- Complete Practical Use Cases and Examples module. + +## Learning Objectives +- Implement and evaluate best practices for prompt clarity and precision. +- Apply iterative prompt refinement strategies. + +--- + +## Key Concepts + +- Define clear objectives +- Provide context and background +- Demonstrate with examples +- Iterate and experiment +- Emphasize positive instructions +- Consider information order +- Offer alternative paths +- Optimize token usage + +--- + +## Define Clear Objectives + +- **Be explicit about your goal:** Clearly state what you expect the model to do. For example, specify if you want a summary, a classification, a code snippet, or a creative story. +- **State the role and output requirements:** Assigning a role (e.g., “Act as a travel guide...”) or defining the format helps the model align with your expectations. + +--- + +## Provide Context and Background + +- **Set the scene:** Give the model relevant information, background, or context necessary for the task. Examples include specifics about the audience, prior conversation, or relevant facts. +- **Use system, contextual, and role prompting:** + - *System prompts* define big-picture objectives. + - *Contextual prompts* supply situation-specific input. + - *Role prompts* assign an identity or perspective to the model. + +--- + +## Demonstrate with Examples +- **Show what you want:** Include one-shot or few-shot examples relevant to your task. +- **Edge cases:** Use diverse, high-quality examples (including edge cases) to increase robustness. + +--- + +## Be Precise and Descriptive +- **Clarity beats cleverness:** Use simple, direct language. +- **Describe the desired output:** Specify structure, style, and length (e.g., "Return the result in JSON", "Write three paragraphs in a conversational style"). + +--- + +## Iterate and Experiment +- **Prompt engineering is iterative:** Test, analyze, and revise your prompts based on model output. +- **Experiment with format and style:** Try inputting the task as a question, statement, or direct instruction to observe variations in results. +- **Document all attempts:** Track prompt iterations and results for learning and optimization. + +--- + +## Emphasize Critical Instructions +- **Use instructions over constraints:** Tell the model what to do rather than what not to do (positive instructions are generally more effective). +- **Reserve constraints** for strict requirements or avoiding harm. + +--- + +## Consider Information Order +- **Order matters:** Arrange information logically, leading with what’s most important and providing details sequentially. +- **For classification/few-shot prompts:** Mix up the order of example classes to avoid overfitting to sequence. + +--- + +## Provide Alternative Paths +- **Offer fallback options:** If there are multiple acceptable response types, clarify priorities or give instructions for alternatives. +- **Structured output:** For complex outputs, consider using schemas or structured formats like JSON. + +--- + +## Optimize Token Usage +- **Max token control:** Limit response length using configuration or by explicitly requesting brevity (e.g., “Explain quantum physics in a tweet.”). +- **Use variables:** Use placeholders for reusable parts of prompts, increasing flexibility for integration in automated systems. +- **Efficiency and cost:** Shorter prompts and controlled output save computation resources and reduce model costs. diff --git a/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/07_Testing_and_Evaluation_Strategies.md b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/07_Testing_and_Evaluation_Strategies.md new file mode 100644 index 0000000..67aded5 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/07_Testing_and_Evaluation_Strategies.md @@ -0,0 +1,111 @@ +--- +title: "Testing and Evaluation Strategies" +module: "01 - Prompt Engineering" +order: 7 +tags: + - prompts +duration: "15min" +marp: true +theme: gaia +size: 16:9 +paginate: true +transition: fade +doc-type: content + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + +--- + + + + +# Testing and Evaluation Strategies + +## Prerequisites +- Complete Best Practices for Effective Prompts module. + +## Learning Objectives +- Describe methods for evaluating and testing prompt performance. +- Implement automated evaluations and unit tests with gold standards. +- Apply consistency checks and iteration in prompt evaluation. + +--- + +## Key Concepts +- Iterative evaluation procedures +- Automated prompt engineering metrics +- Unit tests and gold-standard answers +- Self-consistency and multiple sampling + +This section covers robust procedures for evaluating, testing, and ensuring consistency in prompt engineering work. Effective evaluation is critical for both maintaining and improving large language model (LLM) performance. + +--- + +## Evaluation Procedures + +--- + +### Iterative Approach +Prompt engineering requires continual testing, analysis, and documentation of prompt iterations. Refine prompts based on comprehensive model performance analysis, and re-test as model configurations or architectures change. + +--- + +### Documentation +Use a template to record each experiment, including prompt name/version, goal, model and its configuration, prompt text, output, and performance feedback. This process makes it easier to compare changes, revisit work, and debug future errors. + +- Document fields such as iteration, status (OK/NOT OK/SOMETIMES OK), and qualitative feedback. +- Ideally, link prompts stored in systems like Vertex AI Studio for fast re-testing. + +--- + +### Separation of Concerns +Store prompts separately from code within the codebase for maintainability and transparency. + +### Special Considerations for RAG Systems +For retrieval-augmented generation, log details about inserted content, such as queries, chunk settings, and outputs that affect prompt content. + +--- + +## Automated Evaluations + +--- + +### Automatic Prompt Engineering +- Use LLMs themselves to generate and iterate on prompt variants. +- Evaluate candidates using metrics like BLEU or ROUGE to score for semantic similarity or task-based accuracy. +- Select and, if needed, further tweak the best-performing prompt(s). +- This process can drastically speed up prompt development and optimization. + +--- + +## Unit Tests and Gold-Standard Answers + +--- + +### Unit Testing +- For prompts with deterministic outputs (like code generation or mathematical solutions), manually create test cases with gold-standard (correct) answers. +- Re-run these systematically after any model or prompt changes. + +--- + +### Consistency Checks +- If using approaches like Chain of Thought or Self-Consistency, extract the final answer separately from the reasoning to enable comparison with gold-standard outputs and facilitate automated grading. +- Chain of Thought and self-consistency prompting are best with temperature set to 0, maximizing determinism for extraction and validation. + +--- + +### Multiple Sampling +- Use self-consistency checks: run the same prompt multiple times and analyze the spread of answers. +- For complex reasoning, select the most common answer or average the results for more robust outputs. + +--- + +## Best Practices + +- **Document everything:** Track all prompt experiments, even failed ones. +- **Automate where possible:** Leverage LLMs and scripts for efficiency in creating, scoring, and selecting prompts. +- **Systematic Testing:** Implement unit tests and regression tests around gold standards for mission-critical tasks. +- **Consistency is key:** Run prompts multiple times, across different models/versions, to establish reliability and minimize unexpected variation. diff --git a/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/08_Safety_Ethics_and_Fallback_Responses.md b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/08_Safety_Ethics_and_Fallback_Responses.md new file mode 100644 index 0000000..cfc6c57 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/08_Safety_Ethics_and_Fallback_Responses.md @@ -0,0 +1,84 @@ +--- +title: "Safety, Ethics, and Fallback Responses" +module: "01 - Prompt Engineering" +order: 8 +tags: + - prompts +duration: "20min" +marp: true +theme: gaia +size: 16:9 +paginate: true +transition: fade +doc-type: content + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + + /* Make slides scrollable if content overflows */ + section { + overflow: auto; + -webkit-overflow-scrolling: touch; + } + +--- + + + + +# Safety, Ethics, and Fallback Responses + +## Prerequisites + +- Complete Testing and Evaluation Strategies module. + +## Learning Objectives + +- Explain key safety and ethical considerations in prompt design. +- Implement and evaluate fallback mechanisms for sensitive content. + +--- + +## Key Concepts + +- Positive instructions vs constraints +- Fallback prompts and explicit responses +- Self-consistency prompting for reliability +- System and role-based safety directives + +--- + +## Addressing Sensitive Content +Prompt engineering for large language models (LLMs) must account for safety and ethics, especially when handling sensitive content. A key best practice is using positive instructions in prompts instead of negative constraints. For instance, instead of saying “Do not return toxic or biased content,” it’s usually more effective to explicitly instruct the LLM: “You should provide respectful, unbiased, and safe answers.” This direct instruction sets clear expectations while fostering creative, compliant output within desired boundaries. Constraints (explicit “do not…” statements) should be reserved only for cases where strict limits are required, such as to explicitly avoid legally or ethically sensitive output or to enforce tight output structures for safety or clarity reasons. + +--- + +### Best Practices for Addressing Sensitive Content with LLMs + +- **Use positive instructions:** Direct the model to produce safe and respectful outputs. +- **Apply constraints where necessary:** Only add negative (“do not…”) constraints for topics where explicit boundaries are essential. +- **Combine instructions and constraints strategically:** Start with clear positive instructions and include constraints only if needed for heightened safety or compliance. +- **Iterate and document results:** Experiment with different prompt formulations, review outputs, and iterate based on results, always documenting what works and what does not. + +--- + +## Fallback Mechanisms + +Fallback mechanisms are essential to ensure LLMs provide safe and appropriate responses, especially in the presence of sensitive or ambiguous prompts: + +- **Explicit Fallback Instructions:** Prompts can include specific fallback instructions, such as: “If the topic is sensitive or potentially harmful, respond with: ‘I am unable to provide a response to this request.’” +- **Self-consistency prompting:** By running the same prompt multiple times and aggregating the most common response, you can reduce the risk of unsafe or unethical outputs and improve reliability. This technique is particularly effective in ambiguous or “edge case” scenarios where LLMs’ responses might be variable. +- **Use of roles and system prompting:** By combining system prompts that set strong boundaries (e.g., “Always prioritize user safety over completeness in your answers”) with role prompts (e.g., responding as an ethicist or compliance officer), the likelihood of harmful content is reduced. +- **Token Limits and Output Formatting:** Strictly limiting output length and enforcing structured formats (e.g., JSON schemas with clear fields for allowed responses) can prevent the generation of unpredictable or unsafe outputs. + +--- + +### Example Fallback Prompt + +> You are an AI assistant. Provide clear, helpful, and safe answers. If you are asked for sensitive, harmful, or inappropriate content, reply: “Sorry, I can’t help with that request.” + +Or, with context: + +> If the user query contains medical, legal, or otherwise sensitive content, respond only with “I cannot provide information on this topic.” diff --git a/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/09_Resources_for_Further_Learning.md b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/09_Resources_for_Further_Learning.md new file mode 100644 index 0000000..9b92b13 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/01_Prompt_Engineering/09_Resources_for_Further_Learning.md @@ -0,0 +1,81 @@ +--- +title: "Resources for Further Learning" +module: "01 - Prompt Engineering" +order: 9 +tags: + - prompts +duration: "10min" +marp: true +theme: default +paginate: true +doc-type: refs + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + + section { + overflow-y: scroll; + } + +--- + + + + +# Resources for Further Learning + +## Prerequisites +Complete Safety, Ethics, and Fallback Responses module. + +## Learning Objectives +Identify key resources for deepening prompt engineering expertise. + +--- + +## Key Concepts +- Official documentation and guides +- Toolsets and frameworks +- Academic research and papers +- Hands-on training notebooks + +--- + +## References & Links + +To deepen your understanding and expertise in prompt engineering, it is crucial to explore official documentation, research papers, and take advantage of hands-on training courses. Below are curated resources spanning official guides, notebooks, academic papers, and platform documentation. + +--- + +### Official Documentation + +- **Gemini for Google Workspace Prompt Guide** + [https://inthecloud.withgoogle.com/gemini-for-google-workspace-prompt-guide/dl-cd.html](https://inthecloud.withgoogle.com/gemini-for-google-workspace-prompt-guide/dl-cd.html) + Practical guidance and examples for writing effective prompts for Gemini in Workspace contexts. + +- **Google Cloud Vertex AI: Introduction to Prompt Design** + [https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/introduction-prompt-design](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/introduction-prompt-design) + Comprehensive introduction to prompt engineering, including best practices and hands-on examples for deploying prompts within Vertex AI. + +--- + +### Toolsets + +- **Prompty** +- **Prompt Flow** +- **Prompt Pex** +- **DSPy** + +--- + +### Key Papers and Further Reading +- Zero-shot Learning (Wei et al., 2023) +- Few-shot Learning (Brown et al., 2023) +- Sketch-of-Thought () +- Take a Step Back: Reasoning via Abstraction (Zheng et al., 2023) +- Chain of Thought Prompting (Wei et al., 2023) +- Self Consistency in CoT (Wang et al., 2023) +- Tree of Thoughts (Yao et al., 2023) +- ReAct: Reason + Act (Yao et al., 2023) +- Automatic Prompt Engineering (Zhou et al., 2023) diff --git a/dotnet/Workshops/IntroToSK/02_AI_Frameworks/00_Introduction_to_AI_Development.md b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/00_Introduction_to_AI_Development.md new file mode 100644 index 0000000..725387b --- /dev/null +++ b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/00_Introduction_to_AI_Development.md @@ -0,0 +1,68 @@ +--- +title: "Introduction to AI Development in .NET" +module: "02 - AI Frameworks" +doc-type: intro +order: 0 +tags: + - AI + - .NET + - Introduction +duration: "15min" + +marp: true +theme: gaia +paginate: true +header: AI Frameworks - Introduction to AI Development +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + +--- + + + + +# Introduction to AI Development in .NET + +## Prerequisites + +- Prior knowledge of C# and basic AI concepts. +- Familiarity with dotnet project setup and NuGet package management. + +## Learning Objectives + +- Understand the key components of AI development workflows in dotnet. +- Describe prompt execution and evaluation mechanisms in common dotnet frameworks. +- Identify tools for function invocation and memory management. +- Recognize middleware and SK-specific frameworks used in AI applications. + +--- + +## Summary + +This section introduces the core concepts and workflows for developing AI applications in .NET. Readers will learn about prompt execution, evaluations, tooling, memory strategies, middleware, and SK-specific frameworks to build robust AI solutions. + +--- + + + +## Table-of-Contents + +| | Topic | Summary | +|--------------------------|---------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------| +| 1 | [Prompts & Prompt Execution](01_Prompts_and_Prompt_Execution.md) | Explore prompt templates (Prompty, Handlebars, Fluid) and structured execution via SK Kernel and IChatClient. | +| 2 | [Evaluations & Tests](02_Evaluations_and_Tests.md) | Automate testing and assess AI model outputs using the M.E.AI Evaluations SDK. | +| 3 | [Tools & Function Invocation](03_Tools_and_Function_Invocation.md) | Integrate external functions via SK KernelFunctions, plugins, and M.E.AI AITools. | +| 4 | [Memory](04_Memory.md) | Manage context with kernel memory, SK Vector Memory, and Microsoft.Extensions.VectorData. | +| 5 | [Middleware](05_Middleware.md) | Monitor and modify AI requests and responses using observability tools and SK filters. | +| 6 | [SK Agent Framework](06_SK_Agent_Framework.md) | Orchestrate AI workflows with the SK Agent Framework. | +| 7 | [Process Framework](07_Process_Framework.md) | Define and execute multi-step processes in AI applications. | +| 8 | [Text Search](08_Text_Search.md) | Implement text search capabilities for context retrieval within AI workflows. | + +--- + +## Workshop Content + +1. Navigate to [github/learn-sk]([https://github.com/Tyler-R-Kendrick/learn-sk) and pull down the repo locally or run ``` gh repo clone Tyler-R-Kendrick/learn-sk ``` with the GitHub CLI. +1. Follow the instructions in the README.md file to setup the project. +1. Execute each of the projects in the ```dotnet/DemoApp/Solutions``` folder and read each section. diff --git a/dotnet/Workshops/IntroToSK/02_AI_Frameworks/01_Prompts_and_Prompt_Execution.md b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/01_Prompts_and_Prompt_Execution.md new file mode 100644 index 0000000..0f771e4 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/01_Prompts_and_Prompt_Execution.md @@ -0,0 +1,92 @@ +--- +doc-type: lab +marp: true +theme: gaia +paginate: true + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + +--- + + + + +# Prompts and Prompt Execution + +## Prerequisites +- Prior experience with C# and .NET AI development. +- Familiarity with prompt engineering and Semantic Kernel basics. + +## Learning Objectives +- Identify different prompt template engine options (Prompty, Handlebars, Fluid). +- Execute prompts using Semantic Kernel and M.E.AI IChatClient. +- Generate and parse structured outputs. + +--- + +## Summary + +Provide an overview of prompt composition and execution in .NET using SK and M.E.AI. + +**Background/Context** +Discuss fundamentals of prompts, template rendering, and their role in LLM applications. + +--- + +## Key Concepts + +--- + +## Prompt Templates +- [SK Templates](https://learn.microsoft.com/en-us/semantic-kernel/concepts/prompts/prompt-template-syntax): Built in template syntax for prompt in Semantic Kernel. Allows for more descriptive inputs/outputs than most other alternatives. +- [Prompty](https://prompty.ai/docs/getting-started/concepts): A Microsoft template standard for prompting with a mature tooling ecosystem. +- [Yaml](https://learn.microsoft.com/en-us/semantic-kernel/concepts/prompts/yaml-schema): A common markup format +- [Handlebars](https://learn.microsoft.com/en-us/semantic-kernel/concepts/prompts/handlebars-prompt-templates?pivots=programming-language-csharp): Allows more natural substitution of template inputs. +- [Liquid](https://learn.microsoft.com/en-us/semantic-kernel/concepts/prompts/liquid-prompt-templates): Uses the liquid templating engine to do substitution. + +--- + +## SK Kernel Prompt Execution +- [Kernel prompt execution](https://learn.microsoft.com/en-us/semantic-kernel/concepts/prompts/prompt-execution): Kernel setup and prompt configuration. +- Context variables and chaining prompts + +--- + +## M.E.AI IChatClient Execution +- [IChatClient API](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.ai.ichatclient?view=azure-dotnet): Initialize and use the IChatClient for structured prompts and structured output definitions. +- Structured output parsing and validation + +--- + +## Implementation Guidelines +- Create a console app. +- Install Microsoft.Extensions.AI & Semantic Kernel. +- Load and execute an SK prompt, using the SK Kernel. +- Load and execute a string as a prompt using an IChatClient. + +--- + +## Best Practices +- Use parameterized templates to avoid injection risks. +- Validate template syntax at build time. +- Implement retry and timeout on prompt execution. + +--- + +## Challenge / Hands‑On Exercise + +Create a YAML Prompt, a Prompty Prompt, a Liquid prompt, and/or a Handlebar prompt that output structured json. + +--- + +## References & Links +- [SK Prompt Template Syntax](https://learn.microsoft.com/en-us/semantic-kernel/concepts/prompts/prompt-template-syntax) +- [Prompty Concepts](https://prompty.ai/docs/getting-started/concepts) +- [YAML Prompt Schema](https://learn.microsoft.com/en-us/semantic-kernel/concepts/prompts/yaml-schema) +- [Handlebars Prompt Templates](https://learn.microsoft.com/en-us/semantic-kernel/concepts/prompts/handlebars-prompt-templates?pivots=programming-language-csharp) +- [Liquid Prompt Templates](https://learn.microsoft.com/en-us/semantic-kernel/concepts/prompts/liquid-prompt-templates) +- [SK Prompt Execution Overview](https://learn.microsoft.com/en-us/semantic-kernel/concepts/prompts/prompt-execution) +- [M.E.AI IChatClient API](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.ai.ichatclient?view=azure-dotnet) diff --git a/dotnet/Workshops/IntroToSK/02_AI_Frameworks/02_Evaluations_and_Tests.md b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/02_Evaluations_and_Tests.md new file mode 100644 index 0000000..fd24438 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/02_Evaluations_and_Tests.md @@ -0,0 +1,69 @@ +--- +title: "Evaluations and Tests" +module: "02 - AI Frameworks" +order: 2 +doc-type: lab + +marp: true +theme: gaia +size: 16:9 + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + +--- + + + + +# Evaluations and Tests + +## Prerequisites +- Working knowledge of C# and .NET testing frameworks. +- Basic understanding of LLM prompting and evaluation metrics. + +## Learning Objectives +- Use M.E.AI Evaluations SDK for LLM output assessments. +- Design evaluation metrics and assertion strategies. +- Automate test cases to validate prompt quality. + +--- + +## Summary + +Overview of evaluation frameworks and testing strategies to ensure LLM application reliability. + +--- + +## Instructions + +Follow the instructions for the [LLM-Eval](https://learn.microsoft.com/en-us/dotnet/ai/tutorials/llm-eval) practice project at: https://learn.microsoft.com/en-us/dotnet/ai/tutorials/llm-eval + +--- + +## Key Concepts + +## M.E.AI Evaluations SDK +- Setup and configuration +- Defining evaluation tasks +- Scoring and reporting + +## Evaluation Metrics +- Precision, recall, F1 +- Human-in-the-loop vs automated scoring + +--- + +### Hands‑On Exercise +- Integrate Evaluations SDK into .NET test projects. +- Write tests for existing prompt execution. +- Execute batch evaluations and collect results. + +--- + +### Best Practices +- Write clear test cases with edge scenarios. +- Use CI pipelines to automate evaluations. +- Log detailed reports for analysis. diff --git a/dotnet/Workshops/IntroToSK/02_AI_Frameworks/03_Tools_and_Function_Invocation.md b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/03_Tools_and_Function_Invocation.md new file mode 100644 index 0000000..0983755 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/03_Tools_and_Function_Invocation.md @@ -0,0 +1,77 @@ +--- +title: "Tools and Function Invocation" +module: "02 - AI Frameworks" +doc-type: lab +tags: + - tools + - functions +duration: "20min" +marp: true +theme: gaia +paginate: true + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + + +--- + + + + +# Tools and Function Invocation + +## Prerequisites +- Working knowledge of C# and Semantic Kernel basics. +- Familiarity with function invocation patterns and plugin architecture. + +## Learning Objectives +- Understand SK KernelFunctions & Plugins (Semantic and Native Functions). +- Use M.E.AI AITools for function-based interactions. +- Configure and invoke custom tool integrations. + +--- + +## Summary +Covers how to extend LLM applications by integrating external tools and functions for richer capabilities. + +--- + +## Key Concepts + +- Tools / Functions +- Plugins + +--- + +## M.E.AI AITools +- Generic, unopinionated tools for converting delegates to AITools. + +## SK KernelFunctions & Plugins +- Semantic Functions: declarative prompt-driven blocks. +- Native Functions: code-based extensions. +- Plugin registration and discovery. +- Attribute based discovery. +- Microsoft.SemanticKernel.Extensions.ApiManifest + +--- + +## Implementation Guidelines +- Register functions in Semantic Kernel builder with Attributes. +- Import an Api Manifest as a toolset. +- Convert to AITools with an associated chat client. +- Invoke tools with auto function invocation from an IChatClient. + +--- + +## Challenge / Hands-on Exercise +- Use ModelContextProtocol dotnet to register tools for either SK or Microsoft.Extensions.AI. + +--- + +## Best Practices +- Limit tool surface area to required capabilities. +- Use secure storage for API keys and secrets. +- Validate inputs/outputs of tool calls. diff --git a/dotnet/Workshops/IntroToSK/02_AI_Frameworks/04_Memory.md b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/04_Memory.md new file mode 100644 index 0000000..36a9beb --- /dev/null +++ b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/04_Memory.md @@ -0,0 +1,90 @@ +--- +title: "Memory Management" +module: "02 - AI Frameworks" +doc-type: lab +order: 4 +tags: + - memory + - vector-storage +duration: "20min" +marp: true +theme: default +paginate: true + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + + section { + overflow-y: scroll; + } + + +--- + + + + +# Memory Management + +## Prerequisites +- Understanding of embedding generation and similarity search. +- Familiarity with memory persistence concepts in AI workflows. + +## Learning Objectives +- Differentiate between Kernel Memory, SK Vector Memory, and Microsoft.Extensions.VectorData. +- Configure and use a vector store for embedding and retrieval. +- Integrate memory persistence into chat workflows. + +--- + +## Summary +Covers how to persist and retrieve context in AI applications using in‑process and external memory stores. + +--- + +## Key Concepts +- Chat History +- SK Vector Memory +- Microsoft.Extensions.VectorData + + +--- + +### Chat History + +- IChatHistory +- IChatHistoryReducer + +--- + +### Kernel Memory + +- IKernelMemory abstraction +- In‑memory vs durable stores + +--- + +### SK Vector Memory +- Embedding models and stores +- Storage options: in‑memory, Azure Cosmos DB, Redis + +--- + +### Microsoft.Extensions.VectorData +- Configuration and DI support +- Extensible providers and adapters + +--- + +**Implementation Guidelines** +- Register and configure a VectorMemoryStore in DI. +- Ingest documents and use memory recall. + +--- + +**Best Practices** +- Normalize and chunk inputs for embeddings. +- Manage embedding model costs and latency. +- Implement cleanup policies for stale data. diff --git a/dotnet/Workshops/IntroToSK/02_AI_Frameworks/05_Middleware.md b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/05_Middleware.md new file mode 100644 index 0000000..579fbfd --- /dev/null +++ b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/05_Middleware.md @@ -0,0 +1,81 @@ +--- +title: "Middleware" +module: "02 - AI Frameworks" +doc-type: lab +order: 5 +tags: + - middleware + - observability + - filters +duration: "15min" + +marp: true +theme: default +paginate: true + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + + section { + overflow-y: scroll; + } + +--- + + + + +# Middleware + +## Prerequisites +- Familiarity with .NET pipeline and middleware patterns. +- Understanding of observability and logging basics. + +## Learning Objectives +- Explain the role of middleware in LLM application pipelines. +- Configure observability middleware for telemetry and logging. +- Implement and register SK filters for input/output processing. + +--- + +## Summary +Middleware components allow cross-cutting features such as logging, monitoring, and data transformation to be applied consistently across AI workflows. + +--- + +## Key Concepts +- Observability +- SK Filters + + +--- + +### Observability +- Telemetry collection with Application Insights or other providers +- Logging request/response payloads and metrics + +--- + +### SK Filters +- Interface definitions for filters (IFunctionFilter, IPromptFilter, and IAutoFunctionInvocationFilter) +- Common filter use cases (sanitization, masking, enrichment) + +--- + +## Implementation Guidelines +- Define custom filter classes implementing the SK filter interfaces. +- Configure logging providers and telemetry exporters via DI. + +--- + +## Best Practices +- Keep filters stateless and idempotent. +- Avoid logging sensitive data; implement masking filters when necessary. +- Use health checks and metrics to monitor middleware impact. + +--- + +## Challenge / Hands‑On Exercise +Create and register a logging filter that records prompt inputs and model outputs to Azure Application Insights. diff --git a/dotnet/Workshops/IntroToSK/02_AI_Frameworks/06_SK_Agent_Framework.md b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/06_SK_Agent_Framework.md new file mode 100644 index 0000000..5c1f1fd --- /dev/null +++ b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/06_SK_Agent_Framework.md @@ -0,0 +1,76 @@ +--- +title: "SK Agent Framework" +module: "02 - AI Frameworks" +doc-type: lab +order: 6 +tags: + - agent-framework + - sk-Agents +duration: "20min" +marp: true +theme: default +paginate: true + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + + section { + overflow-y: scroll; + } + + +--- + + + + +# SK Agent Framework + +## Prerequisites +- Knowledge of .NET AI application development and Semantic Kernel basics. +- Familiarity with AI agent concepts, skills, and plugin architectures. + +--- + +## Learning Objectives +- Understand the architecture and components of the SK Agent Framework. +- Configure and instantiate agents with skills and plugins. +- Build and run an autonomous agent workflow. + +--- + +## Summary +Explore how to use Semantic Kernel’s Agent Framework to create autonomous, goal-driven AI agents that coordinate skills and functions. + +--- + +## Key Concepts + +### Agents and Planners +- Planner types (e.g., backward, forward chaining) +- Agent orchestration loop + +### Skills and Plugins +- Semantic Functions as skills +- Native Functions and tool integration + +### Execution Context +- Per-agent state +- Cancellation and error handling + +--- + +## Implementation Guidelines +- Create an Agent to answer questions. +- Create an Agent to check answers. +- Create a group chat to find a correct solution with 3 maximum iterations. + +--- + +## Best Practices +- Limit agent goals to maintain control and predictability. +- Securely manage credentials for external tools. +- Monitor and log agent actions for troubleshooting. +- Use a single kernel per agent. diff --git a/dotnet/Workshops/IntroToSK/02_AI_Frameworks/07_Process_Framework.md b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/07_Process_Framework.md new file mode 100644 index 0000000..5ba7035 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/07_Process_Framework.md @@ -0,0 +1,77 @@ +--- +title: "Process Framework" +module: "02 - AI Frameworks" +doc-type: lab +order: 7 +tags: + - process-framework + - orchestration +duration: "20min" +marp: true +theme: default +paginate: true + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + + section { + overflow-y: scroll; + } + +--- + + + + +# Process Framework + +## Prerequisites +- Familiarity with Semantic Kernel Process Framework basics in .NET. +- Understanding of workflow orchestration and error handling patterns. + +## Learning Objectives +- Understand the Semantic Kernel Process Framework architecture. +- Define and orchestrate multi-step processes using workflows. +- Handle errors and cancellations in process execution. + +--- + +## Summary +Covers building structured workflows in AI applications with the Semantic Kernel Process Framework to manage complex, multi-step logic. + +--- + +## Key Concepts + +--- + +### Process Definition +- Defining steps and transitions in JSON or code +- Parallel vs sequential process flows + +--- + +### Workflow Execution +- Kernel.ProcessBuilder APIs +- Cancellation tokens and error propagation + +--- + +### Monitoring and Control +- Tracking process state +- Logging step outputs and metrics + +--- + +## Implementation Guidelines +- Create a process that runs your answer validation agent after your question answering agent provides a response, instead of using an Agent Group Chat. +- After a valid response, have the process workflow cache the response. + +--- + +**Best Practices** +- Keep steps small and focused. +- Implement retry policies for transient failures. +- Validate input schemas before execution. diff --git a/dotnet/Workshops/IntroToSK/02_AI_Frameworks/08_Text_Search.md b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/08_Text_Search.md new file mode 100644 index 0000000..1110eae --- /dev/null +++ b/dotnet/Workshops/IntroToSK/02_AI_Frameworks/08_Text_Search.md @@ -0,0 +1,81 @@ +--- +title: "Text Search" +module: "02 - AI Frameworks" +doc-type: lab +order: 8 +tags: + - text-search + - search +duration: "20min" +marp: true +theme: default +paginate: true + +header: Introduction to Prompt Engineering +footer: "© Microsoft Corporation. All rights reserved." +style: | + @import '../styles/msft.css'; + + section { + overflow-y: scroll; + } + +--- + + + + +# Text Search + +## Prerequisites +- Familiarity with embedding-based search concepts. +- Experience with vector stores and .NET DI configuration. + +--- + +## Learning Objectives +- Differentiate semantic search from traditional keyword search. +- Implement document retrieval using SK vector memory and external indices. +- Configure and execute search queries in .NET applications. +- Apply fallback strategies for robust search results. + +--- + +## Summary +Covers how to integrate text search capabilities into AI workflows using Semantic Kernel and vector stores for high‑quality document retrieval. + +--- + +## Key Concepts + +### Semantic Search +- Embedding generation and similarity measures +- Vector index structures (e.g., HNSW) + +### Lexical (Keyword) Search +- Inverted index basics +- Tradeoffs between speed and relevance + +### Fallback Strategies +- Combining semantic and lexical scores +- Thresholding and hybrid query patterns + +--- + +## Implementation Guidelines +- Configure a vector store (e.g., Azure Cognitive Search, Redis) in SK via DI. +- Populate the index with document embeddings and metadata. +- Construct and execute search requests using `kernel.Memory.SearchAsync()`. +- Handle pagination and result ranking programmatically. + +--- + +## Best Practices +- Preprocess and clean text before embedding (tokenization, stop‑word removal). +- Balance recall and precision via tuning of similarity thresholds. +- Monitor index performance and costs; shard or partition large indexes. + +--- + +## Challenge / Hands‑On Exercise +Build a console app that indexes a set of markdown files, then prompts the user for a query and displays the top 5 semantically relevant document snippets. diff --git a/dotnet/Workshops/IntroToSK/imgs/microsoft-logo.svg b/dotnet/Workshops/IntroToSK/imgs/microsoft-logo.svg new file mode 100644 index 0000000..4bc8937 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/imgs/microsoft-logo.svg @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/dotnet/Workshops/IntroToSK/styles/msft.css b/dotnet/Workshops/IntroToSK/styles/msft.css new file mode 100644 index 0000000..daed793 --- /dev/null +++ b/dotnet/Workshops/IntroToSK/styles/msft.css @@ -0,0 +1,41 @@ +@import 'default'; + +.msft-logo, header { + background-image: url(../imgs/microsoft-logo.svg) !important; + background-repeat: no-repeat; + background-position: top left; + background-size: 100% 100px; +} + +/* Layout for sticky header/footer and scrollable main content */ +:root, section { + min-height: 100vh; + display: flex; + flex-direction: column; +} + +header, footer { + flex: auto; + flex-direction: column; + padding-top: 1rem; + padding-bottom: 1rem; + background-color: inherit; + color: inherit; + height: 100px; + width: 100%; +} + +.title-slide { + background-color: #243A5E !important; + color: #fff !important; +} + +.title-slide h1 { + color: #62ADB9 !important; +} + +/* Ensure slides scroll when content overflows */ +section { + overflow: auto !important; + -webkit-overflow-scrolling: touch !important; +}