【生成式人工智慧與機器學習導論2025】第1講:一堂課搞懂生成式人工智慧的原理
Understanding Generative Artificial Intelligence Principles
Introduction to Generative AI
- The course aims to provide a basic understanding of the principles behind generative artificial intelligence, which many students have likely encountered in daily life through platforms like OpenAI's ChatGPT, Google's Gemini, or Anthropic's Claude.
Structure of the Course
- The lesson is divided into two parts: the first half focuses on theoretical principles while the second half involves practical implementation using open-source models to validate these principles.
Basic Functionality of Generative AI Platforms
- These platforms primarily function by taking text input (e.g., questions) and generating text responses. However, they can also handle voice and image inputs/outputs.
Language Models Explained
- At their core, platforms like ChatGPT are language models that essentially perform a form of "text relay," predicting subsequent words based on given prompts.
Token Concept in Language Models
- In generative AI, a "token" refers to potential outputs during text generation. The incomplete sentences provided as input are termed "prompts," which guide the model in predicting the next token.
Mechanism Behind Answer Generation
Probability Distribution in Token Selection
- When presented with a prompt, language models generate a probability distribution for possible tokens. Each token receives a score indicating its likelihood of being selected next.
Dice Rolling Analogy for Output Generation
- The model effectively "rolls dice" based on this probability distribution to select which token follows the prompt. This process explains why answers may vary even when identical prompts are used.
Knowledge Requirements for Effective Text Relay
Importance of Language and World Knowledge
- Successful text relay requires both linguistic knowledge (understanding grammar and vocabulary connections) and world knowledge (factual information about reality).
Internal Operations of Language Models
Function Representation in Language Models
- A language model can be conceptualized as a function where inputs (prompts) lead to outputs (probability distributions), necessitating vast parameters for complex relationships between them.
Enhancements for Question Answering Capabilities
Use of Chat Templates
- To ensure relevant responses, platforms often append additional context around user prompts—known as chat templates—before processing them through the model.
Multi-turn Dialogue Management
Contextual Awareness in Conversations
- For multi-turn dialogues, previous exchanges are included within each new prompt via chat templates so that models maintain contextual awareness throughout conversations.
Limitations and Challenges Faced by Generative AI
Phenomenon of AI Hallucination
- Generative models sometimes produce incorrect or fabricated information—a phenomenon referred to as "AI hallucination." This occurs because they do not rely on databases but rather generate responses based solely on learned patterns from training data.
Context Engineering Importance
Ensuring Sufficient Input Information
- Users must provide adequate context within prompts—termed context engineering—to enable models to generate accurate outputs based on their probabilistic predictions.
Understanding Token Generation in Language Models
Introduction to Token Generation
- The discussion begins with a question about identity, leading to a demonstration of how language models generate responses based on input prompts.
- The model generates responses by creating line breaks and connecting phrases, illustrating the flow of conversation.
Probability-Based Token Selection
- Initially, the code selects the highest probability token; however, true language modeling involves randomness in token selection to avoid repetitive answers.
- By implementing a dice-roll mechanism for token selection, varied outputs are achieved instead of consistent responses.
Challenges with Randomness
- Using random methods can lead to nonsensical outputs since low-probability tokens may be selected, resulting in incoherent sentences.
- To mitigate this issue, only high-probability tokens should participate in the selection process to maintain sentence coherence.
Implementing Top-K Sampling
- The implementation involves selecting from the top K tokens based on their probabilities during generation. This approach helps produce more sensible outputs.
- Setting K too high (e.g., 100) risks generating strange content while setting it too low (K = 1) results in predictable outputs.
Enhancing Output Quality with Chat Templates
- The initial attempts at generating conversational responses reveal that without structured templates, the model fails to provide meaningful answers.
- Introducing a chat template format improves response quality significantly by framing user inputs and AI replies clearly.
Utilizing Official Chat Templates
Importance of Structured Prompts
- A well-defined system prompt is crucial for guiding the model's understanding and ensuring accurate context during conversations.
- Using an official chat template simplifies interactions and enhances output relevance compared to improvised formats.
Applying Chat Templates Programmatically
- The
Tokenizer.Apply_Chat_templatefunction automates encoding while integrating chat templates into prompts seamlessly.
Advanced Interaction Techniques
Multi-Turn Conversations
- For effective multi-turn dialogues, historical context must be provided alongside new inputs so that the model can generate coherent follow-up responses.
Example Implementation of Historical Context
- When asking about previous questions or statements made by users, it's essential to include all relevant dialogue history for accurate continuity in conversation.
Simplifying Model Interactions with Pipelines
Streamlining Processes with Pipeline Functionality
- Utilizing Hugging Face's Pipeline feature allows users to bypass complex encoding/decoding steps when interacting with models directly.
Flexibility Across Different Models
- Users can easily switch between different models (e.g., LLaMA and Gemma), demonstrating flexibility while maintaining similar interaction structures.
Turn any video into a summary like this
YouTube links, meetings, lectures. With transcripts, search, and chat.