第二节:使用AzureAIFoundry构建AI解决方案


文档摘要

第2节:使用 Azure AI Foundry 构建 AI 解决方案 摘要 探索如何使用 Foundry Local 和 Azure AI Foundry 构建可操作的生成式 AI(GenAI)工作流。学习高级提示工程、集成结构化数据,并通过可复现的管道协调任务。尽管重点是文档和数据问答的检索增强生成(RAG),但这些模式可以推广到更广泛的生成式 AI 解决方案设计。 学习目标 在本节结束时,您将能够: 掌握提示工程:设计有效的系统提示和基础策略 实现 RAG 模式:使用向量搜索构建基于文档的问答系统 集成结构化数据:在 AI 工作流中处理 CSV、JSON 和表格数据 构建生产级 RAG:使用 Chainlit 创建可扩展的 RAG 应用 连接本地与云端:了解从 Foundry

第2节:使用 Azure AI Foundry 构建 AI 解决方案

摘要

探索如何使用 Foundry Local 和 Azure AI Foundry 构建可操作的生成式 AI(GenAI)工作流。学习高级提示工程、集成结构化数据,并通过可复现的管道协调任务。尽管重点是文档和数据问答的检索增强生成(RAG),但这些模式可以推广到更广泛的生成式 AI 解决方案设计。

学习目标

在本节结束时,您将能够:

  • 掌握提示工程:设计有效的系统提示和基础策略
  • 实现 RAG 模式:使用向量搜索构建基于文档的问答系统
  • 集成结构化数据:在 AI 工作流中处理 CSV、JSON 和表格数据
  • 构建生产级 RAG:使用 Chainlit 创建可扩展的 RAG 应用
  • 连接本地与云端:了解从 Foundry Local 到 Azure AI Foundry 的迁移路径

前置条件

  • 完成第1节(Foundry Local 设置)
  • 基本了解向量数据库和嵌入
  • 具备 Python 编程经验
  • 熟悉文档处理概念

跨平台环境快速启动(Windows 和 macOS)

Windows PowerShell:

py -m venv .venv .\.venv\Scripts\Activate.ps1 pip install --upgrade pip pip install foundry-local-sdk openai sentence-transformers ragas datasets scikit-learn

macOS / Linux:

python3 -m venv .venv source .venv/bin/activate python -m pip install --upgrade pip pip install foundry-local-sdk openai sentence-transformers ragas datasets scikit-learn

如果您的环境中尚未提供 Foundry Local 的 macOS 二进制文件,请在 Windows 虚拟机或容器中运行服务并设置:

export FOUNDRY_LOCAL_ENDPOINT=http://<windows-host>:5273/v1

验证:Foundry Local 环境检查

在开始演示之前,验证您的本地环境:

foundry --version # Ensure CLI is installed foundry status # Service status foundry model run phi-4-mini # Start baseline SLM curl http://localhost:5273/v1/models # Validate API (should list running model)

如果最后一条命令失败,请启动(或重启)服务:foundry service start

演示流程(30分钟)

1. 系统提示和基础策略(10分钟)

步骤 1.1:高级提示工程

创建 samples/02-rag-solutions/prompt_engineering.py

#!/usr/bin/env python3 """ Advanced Prompt Engineering with Foundry Local Demo: System prompts, grounding, and context management Reference: https://learn.microsoft.com/en-us/azure/ai-foundry/foundry-local/reference/reference-sdk?pivots=programming-language-python """ import os from openai import OpenAI from typing import List, Dict, Any class PromptEngineer: """Advanced prompt engineering utilities for Foundry Local""" def __init__(self, base_url: str = "http://localhost:5273/v1"): self.client = OpenAI( base_url=base_url, api_key="not-needed" ) def create_grounded_prompt(self, context: str, question: str, domain: str = "general") -> List[Dict[str, str]]: """Create a grounded prompt with context and domain expertise""" system_prompts = { "general": "You are a helpful AI assistant. Use the provided context to answer questions accurately and concisely.", "medical": "You are a medical AI assistant. Provide evidence-based responses using the medical literature context. Always include disclaimers about consulting healthcare professionals.", "legal": "You are a legal research assistant. Analyze the provided legal documents and statutes. Note that this is for informational purposes only.", "technical": "You are a technical documentation assistant. Provide detailed, accurate responses based on the technical documentation provided.", "financial": "You are a financial analysis assistant. Use the provided financial data to give insights while noting this is not financial advice." } return [ { "role": "system", "content": system_prompts.get(domain, system_prompts["general"]) }, { "role": "user", "content": f""" Context Information: {context} Question: {question} Please provide a comprehensive answer based on the context above. If the context doesn't contain enough information to fully answer the question, please state that clearly. """.strip() } ] def chat_with_grounding(self, context: str, question: str, model: str = "phi-4-mini", domain: str = "general") -> Dict[str, Any]: """Execute grounded chat completion""" messages = self.create_grounded_prompt(context, question, domain) try: response = self.client.chat.completions.create( model=model, messages=messages, max_tokens=1000, temperature=0.3, # Lower temperature for more consistent responses top_p=0.9 ) return { "answer": response.choices[0].message.content, "model": response.model, "tokens": response.usage.total_tokens if response.usage else None, "context_length": len(context), "domain": domain } except Exception as e: return {"error": str(e)} def demo_grounding_strategies(): """Demonstrate different grounding strategies""" engineer = PromptEngineer() # Sample contexts for different domains contexts = { "technical": """ Microsoft Foundry Local is a development platform that enables running AI models locally on Windows devices. It supports various model formats including ONNX and provides hardware acceleration through DirectML. The platform includes a CLI for model management and an OpenAI-compatible API for integration. Models can be cached locally and run without internet connectivity. """, "financial": """ Q3 2024 Results: Revenue $45.2M (up 23% YoY), Operating Margin 18.5%, Cash Flow $12.3M, R&D Investment $8.7M (19% of revenue). Key metrics: Customer Acquisition Cost $1,200, Lifetime Value $15,600, Monthly Churn 2.1%. Geographic breakdown: North America 65%, Europe 25%, APAC 10%. """ } questions = { "technical": "How does Foundry Local handle model caching and what are the benefits?", "financial": "What is the current financial health and what are the key performance indicators?" } for domain in ["technical", "financial"]: print(f"\n{'='*50}") print(f"Domain: {domain.upper()}") print(f"{'='*50}") result = engineer.chat_with_grounding( context=contexts[domain], question=questions[domain], domain=domain ) if "error" in result: print(f"Error: {result['error']}") else: print(f"Answer: {result['answer']}") print(f"Tokens used: {result['tokens']}") print(f"Context length: {result['context_length']} characters") if __name__ == "__main__": demo_grounding_strategies()

步骤 1.2:测试基础策略

# Ensure phi-4-mini is running foundry model run phi-4-mini # Run the prompt engineering demo python samples/02-rag-solutions/prompt_engineering.py

2. 集成表格数据与提示(CSV 问答)(10分钟)

步骤 2.1:CSV 数据集成

创建 samples/02-rag-solutions/csv_qa_system.py

#!/usr/bin/env python3 """ CSV Q&A System with Foundry Local Demo: Structured data integration and tabular reasoning Reference: https://learn.microsoft.com/en-us/azure/ai-foundry/foundry-local/reference/reference-sdk?pivots=programming-language-python """ import pandas as pd import json import os from openai import OpenAI from typing import Dict, Any, List import io class CSVQASystem: """CSV Question-Answering system using Foundry Local""" def __init__(self, base_url: str = "http://localhost:5273/v1"): self.client = OpenAI( base_url=base_url, api_key="not-needed" ) self.data = None self.summary_stats = None def load_csv_data(self, csv_path: str) -> bool: """Load and analyze CSV data""" try: self.data = pd.read_csv(csv_path) self.summary_stats = self._generate_summary_stats() return True except Exception as e: print(f"Error loading CSV: {e}") return False def _generate_summary_stats(self) -> Dict[str, Any]: """Generate comprehensive summary statistics""" stats = { "shape": self.data.shape, "columns": list(self.data.columns), "dtypes": self.data.dtypes.to_dict(), "null_counts": self.data.isnull().sum().to_dict(), "sample_rows": self.data.head(3).to_dict('records') } # Add numerical statistics for numeric columns numeric_cols = self.data.select_dtypes(include=['number']).columns if len(numeric_cols) > 0: stats["numeric_summary"] = self.data[numeric_cols].describe().to_dict() # Add categorical summaries categorical_cols = self.data.select_dtypes(include=['object']).columns if len(categorical_cols) > 0: stats["categorical_summary"] = {} for col in categorical_cols: stats["categorical_summary"][col] = { "unique_count": self.data[col].nunique(), "top_values": self.data[col].value_counts().head(5).to_dict() } return stats def create_data_context(self, question: str) -> str: """Create relevant data context for the question""" context_parts = [ f"Dataset Overview:", f"- Shape: {self.summary_stats['shape'][0]} rows, {self.summary_stats['shape'][1]} columns", f"- Columns: {', '.join(self.summary_stats['columns'])}" ] # Add sample data context_parts.append("\nSample Data:") for i, row in enumerate(self.summary_stats['sample_rows'][:3]): context_parts.append(f"Row {i+1}: {json.dumps(row, default=str)}") # Add relevant statistics based on question content question_lower = question.lower() if any(word in question_lower for word in ['average', 'mean', 'sum', 'count', 'max', 'min', 'statistics']): if 'numeric_summary' in self.summary_stats: context_parts.append("\nNumerical Statistics:") for col, stats in self.summary_stats['numeric_summary'].items(): context_parts.append(f"{col}: mean={stats['mean']:.2f}, std={stats['std']:.2f}, min={stats['min']}, max={stats['max']}") if any(word in question_lower for word in ['category', 'group', 'type', 'unique']): if 'categorical_summary' in self.summary_stats: context_parts.append("\nCategorical Data Summary:") for col, info in self.summary_stats['categorical_summary'].items(): context_parts.append(f"{col}: {info['unique_count']} unique values, top: {list(info['top_values'].keys())[:3]}") return "\n".join(context_parts) def answer_question(self, question: str, model: str = "phi-4-mini") -> Dict[str, Any]: """Answer questions about the CSV data""" if self.data is None: return {"error": "No data loaded. Please load CSV data first."} context = self.create_data_context(question) messages = [ { "role": "system", "content": """ You are a data analysis assistant. You have access to a CSV dataset and its summary statistics. Answer questions about the data accurately based on the provided context. If calculations are needed, explain your reasoning. If the data doesn't contain enough information to answer the question, state that clearly. """.strip() }, { "role": "user", "content": f""" Data Context: {context} Question: {question} Please analyze the data and provide a comprehensive answer. """.strip() } ] try: response = self.client.chat.completions.create( model=model, messages=messages, max_tokens=800, temperature=0.2 # Low temperature for factual data analysis ) return { "answer": response.choices[0].message.content, "model": response.model, "tokens": response.usage.total_tokens if response.usage else None, "dataset_shape": self.data.shape } except Exception as e: return {"error": str(e)} def create_sample_dataset(): """Create a sample dataset for demonstration""" # Create sample sales data sales_data = { 'Date': ['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04', '2024-01-05', '2024-01-06', '2024-01-07', '2024-01-08', '2024-01-09', '2024-01-10'], 'Product': ['Laptop', 'Phone', 'Tablet', 'Laptop', 'Phone', 'Accessories', 'Laptop', 'Tablet', 'Phone', 'Accessories'], 'Sales_Amount': [1200, 800, 600, 1100, 850, 150, 1300, 580, 780, 200], 'Quantity': [1, 1, 1, 1, 1, 3, 1, 1, 1, 4], 'Region': ['North', 'South', 'East', 'West', 'North', 'South', 'East', 'West', 'North', 'South'], 'Sales_Rep': ['Alice', 'Bob', 'Charlie', 'Diana', 'Alice', 'Bob', 'Charlie', 'Diana', 'Alice', 'Bob'] } df = pd.DataFrame(sales_data) csv_path = "samples/02-rag-solutions/sample_sales_data.csv" # Ensure directory exists os.makedirs(os.path.dirname(csv_path), exist_ok=True) df.to_csv(csv_path, index=False) return csv_path def demo_csv_qa(): """Demonstrate CSV Q&A capabilities""" # Create sample dataset csv_path = create_sample_dataset() print(f"Created sample dataset: {csv_path}") # Initialize Q&A system qa_system = CSVQASystem() # Load data if not qa_system.load_csv_data(csv_path): print("Failed to load CSV data") return print(f"\nLoaded dataset with shape: {qa_system.data.shape}") # Example questions questions = [ "What is the total sales amount?", "Which product has the highest average sales amount?", "How many sales were made in the North region?", "Who is the top performing sales representative?", "What is the average quantity sold per transaction?" ] for i, question in enumerate(questions, 1): print(f"\n{'='*60}") print(f"Question {i}: {question}") print(f"{'='*60}") result = qa_system.answer_question(question) if "error" in result: print(f"Error: {result['error']}") else: print(f"Answer: {result['answer']}") print(f"Tokens used: {result['tokens']}") if __name__ == "__main__": demo_csv_qa()

步骤 2.2:测试 CSV 问答系统

# Run the CSV Q&A demo python samples/02-rag-solutions/csv_qa_system.py

3. 初始项目:改进 02-grounding-data(5分钟)

步骤 3.1:增强的文档 RAG 系统

创建 samples/02-rag-solutions/document_rag.py

#!/usr/bin/env python3 """ Document RAG System with Foundry Local Demo: Document processing, vector search, and retrieval-augmented generation Reference: https://learn.microsoft.com/en-us/azure/ai-foundry/foundry-local/reference/reference-sdk?pivots=programming-language-python """ import os from openai import OpenAI import requests from typing import List, Dict, Any import numpy as np from sklearn.metrics.pairwise import cosine_similarity from sklearn.feature_extraction.text import TfidfVectorizer import json class SimpleRAGSystem: """Simple RAG system using TF-IDF for demonstration""" def __init__(self, base_url: str = "http://localhost:5273/v1"): self.client = OpenAI( base_url=base_url, api_key="not-needed" ) self.documents = [] self.vectorizer = None self.doc_vectors = None def add_documents(self, documents: List[str]): """Add documents to the knowledge base""" self.documents.extend(documents) self._create_vectors() def _create_vectors(self): """Create TF-IDF vectors for documents""" if not self.documents: return self.vectorizer = TfidfVectorizer( max_features=1000, stop_words='english', ngram_range=(1, 2) ) self.doc_vectors = self.vectorizer.fit_transform(self.documents) def retrieve_relevant_docs(self, query: str, top_k: int = 3) -> List[Dict[str, Any]]: """Retrieve most relevant documents for a query""" if not self.documents or self.vectorizer is None: return [] # Vectorize query query_vector = self.vectorizer.transform([query]) # Calculate similarities similarities = cosine_similarity(query_vector, self.doc_vectors).flatten() # Get top-k documents top_indices = np.argsort(similarities)[::-1][:top_k] results = [] for idx in top_indices: if similarities[idx] > 0.1: # Minimum similarity threshold results.append({ "content": self.documents[idx], "similarity": float(similarities[idx]), "index": int(idx) }) return results def generate_answer(self, question: str, model: str = "phi-4-mini", max_context_docs: int = 3) -> Dict[str, Any]: """Generate answer using retrieved documents""" # Retrieve relevant documents relevant_docs = self.retrieve_relevant_docs(question, max_context_docs) if not relevant_docs: context = "No relevant documents found in the knowledge base." else: context_parts = [] for i, doc in enumerate(relevant_docs, 1): context_parts.append(f"Document {i} (relevance: {doc['similarity']:.3f}):\n{doc['content']}") context = "\n\n".join(context_parts) messages = [ { "role": "system", "content": """ You are a helpful AI assistant that answers questions based on provided documents. Use the context documents to provide accurate, detailed answers. If the documents don't contain sufficient information, say so clearly. Always cite which documents you're referencing in your answer. """.strip() }, { "role": "user", "content": f""" Context Documents: {context} Question: {question} Please provide a comprehensive answer based on the context documents above. """.strip() } ] try: response = self.client.chat.completions.create( model=model, messages=messages, max_tokens=1000, temperature=0.3 ) return { "answer": response.choices[0].message.content, "model": response.model, "tokens": response.usage.total_tokens if response.usage else None, "retrieved_docs": len(relevant_docs), "context_length": len(context) } except Exception as e: return {"error": str(e)} def create_sample_knowledge_base() -> List[str]: """Create a sample knowledge base about AI and technology""" documents = [ """ Microsoft Foundry Local is a comprehensive development platform that enables developers to run AI models locally on Windows devices. It provides hardware acceleration through DirectML and supports various model formats including ONNX. The platform includes a command-line interface for model management and an OpenAI-compatible API for seamless integration. """, """ Edge AI refers to the deployment of artificial intelligence algorithms directly on edge devices, such as smartphones, IoT devices, and local computers. This approach reduces latency, improves privacy, and enables offline functionality. Edge AI is particularly important for real-time applications and scenarios where data privacy is critical. """, """ Small Language Models (SLMs) are compressed versions of large language models that maintain much of their capabilities while requiring significantly fewer computational resources. Examples include Microsoft's Phi models, which can run efficiently on consumer hardware. SLMs are ideal for edge deployment and privacy-sensitive applications. """, """ Vector databases store and retrieve data based on vector representations, enabling semantic search and similarity matching. They are essential components in RAG (Retrieval-Augmented Generation) systems, where relevant context is retrieved to enhance AI responses. Popular vector databases include Chroma, Pinecone, and Weaviate. """, """ Prompt engineering is the practice of crafting effective prompts to guide AI model behavior and improve response quality. Techniques include few-shot learning, chain-of-thought prompting, and system message optimization. Well-designed prompts can significantly improve model performance on specific tasks. """, """ Azure AI Foundry provides cloud-based AI development capabilities, including model training, deployment, and monitoring. It offers integration with Azure services and supports both custom and pre-trained models. The platform enables seamless scaling from local development to enterprise deployment. """ ] return [doc.strip() for doc in documents] def demo_document_rag(): """Demonstrate document RAG capabilities""" # Create RAG system rag_system = SimpleRAGSystem() # Add sample knowledge base documents = create_sample_knowledge_base() rag_system.add_documents(documents) print(f"Loaded {len(documents)} documents into knowledge base") # Example questions questions = [ "What is Microsoft Foundry Local and what are its key features?", "How do Small Language Models differ from regular language models?", "What is the role of vector databases in RAG systems?", "What are the benefits of Edge AI?", "How can I improve my prompt engineering skills?" ] for i, question in enumerate(questions, 1): print(f"\n{'='*70}") print(f"Question {i}: {question}") print(f"{'='*70}") result = rag_system.generate_answer(question) if "error" in result: print(f"Error: {result['error']}") else: print(f"Answer: {result['answer']}") print(f"Retrieved {result['retrieved_docs']} documents") print(f"Tokens used: {result['tokens']}") if __name__ == "__main__": demo_document_rag()

4. 展示 CLI 到 Azure 的迁移路径(5分钟)

步骤 4.1:迁移策略概述

创建 samples/02-rag-solutions/migration_guide.py

#!/usr/bin/env python3 """ Foundry Local to Azure AI Foundry Migration Guide Demo: Code patterns and migration strategies Reference: https://learn.microsoft.com/en-us/azure/ai-foundry/foundry-local/reference/reference-sdk?pivots=programming-language-python """ import os from openai import OpenAI from typing import Dict, Any, Optional class UnifiedAIClient: """Unified client that works with both Foundry Local and Azure AI Foundry""" def __init__(self, environment: str = "local", azure_endpoint: Optional[str] = None, azure_api_key: Optional[str] = None, azure_api_version: str = "2024-08-01-preview"): self.environment = environment if environment == "local": # Foundry Local configuration self.client = OpenAI( base_url="http://localhost:5273/v1", api_key="not-needed" ) self.default_model = "phi-4-mini" elif environment == "azure": # Azure AI Foundry configuration if not azure_endpoint or not azure_api_key: raise ValueError("Azure endpoint and API key required for Azure environment") self.client = OpenAI( base_url=f"{azure_endpoint}/openai/deployments", api_key=azure_api_key, default_headers={"api-version": azure_api_version} ) self.default_model = "gpt-4" # Or your Azure deployment name else: raise ValueError("Environment must be 'local' or 'azure'") def chat_completion(self, messages: list, model: Optional[str] = None, **kwargs) -> Dict[str, Any]: """Unified chat completion that works in both environments""" model = model or self.default_model try: response = self.client.chat.completions.create( model=model, messages=messages, **kwargs ) return { "success": True, "response": response.choices[0].message.content, "model": response.model, "tokens": response.usage.total_tokens if response.usage else None, "environment": self.environment } except Exception as e: return { "success": False, "error": str(e), "environment": self.environment } def get_available_models(self) -> Dict[str, Any]: """Get available models in current environment""" try: if self.environment == "local": # For Foundry Local, we'd typically use the CLI # This is a simplified example return { "success": True, "models": ["phi-4-mini", "qwen2.5-0.5b", "deepseek-coder-1.3b"], "environment": "local" } else: # For Azure, you might query the deployments endpoint models_response = self.client.models.list() return { "success": True, "models": [model.id for model in models_response.data], "environment": "azure" } except Exception as e: return { "success": False, "error": str(e), "environment": self.environment } def demo_migration_patterns(): """Demonstrate migration patterns between local and cloud""" print("Foundry Local to Azure AI Foundry Migration Demo") print("=" * 60) # Test message test_messages = [ { "role": "system", "content": "You are a helpful AI assistant. Provide concise, accurate responses." }, { "role": "user", "content": "Explain the benefits of edge AI in 2-3 sentences." } ] # Test with Foundry Local print("\n1. Testing with Foundry Local:") print("-" * 40) try: local_client = UnifiedAIClient(environment="local") local_result = local_client.chat_completion( messages=test_messages, max_tokens=200, temperature=0.7 ) if local_result["success"]: print(f"✓ Local Response: {local_result['response']}") print(f" Model: {local_result['model']}") print(f" Tokens: {local_result['tokens']}") else: print(f"✗ Local Error: {local_result['error']}") except Exception as e: print(f"✗ Local Setup Error: {e}") # Show Azure configuration (commented out as it requires credentials) print("\n2. Azure AI Foundry Configuration:") print("-" * 40) print(""" # To migrate to Azure AI Foundry, configure as follows: azure_client = UnifiedAIClient( environment="azure", azure_endpoint="https://your-resource.openai.azure.com", azure_api_key="your-api-key", azure_api_version="2024-08-01-preview" ) # Same API calls work in both environments! azure_result = azure_client.chat_completion( messages=test_messages, max_tokens=200, temperature=0.7 ) """) # Migration strategy print("\n3. Migration Strategy:") print("-" * 40) print(""" Step 1: Develop and test locally with Foundry Local Step 2: Use environment variables for configuration Step 3: Test with Azure AI Foundry in staging Step 4: Deploy to production with Azure AI Foundry Benefits of this approach: ✓ Faster development cycle (no network latency) ✓ Lower development costs (no API charges) ✓ Privacy during development (local processing) ✓ Easy scaling to production (same API) """) # Configuration examples print("\n4. Environment-based Configuration:") print("-" * 40) print(""" # .env file for development AI_ENVIRONMENT=local FOUNDRY_LOCAL_URL=http://localhost:5273/v1 DEFAULT_MODEL=phi-4-mini # .env file for production AI_ENVIRONMENT=azure AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com AZURE_OPENAI_API_KEY=your-api-key AZURE_OPENAI_API_VERSION=2024-08-01-preview DEFAULT_MODEL=gpt-4 """) if __name__ == "__main__": demo_migration_patterns()

步骤 4.2:测试迁移模式

# Run the migration demo python samples/02-rag-solutions/migration_guide.py

涵盖的关键概念

1. 高级提示工程

  • 系统提示:领域特定的专家角色
  • 基础策略:上下文集成技术
  • 温度控制:平衡创造力与一致性
  • Token 管理:高效使用上下文

2. 结构化数据集成

  • CSV 处理:Pandas 与 AI 模型的集成
  • 统计分析:自动化数据总结
  • 上下文创建:基于查询动态生成上下文
  • 多格式支持:JSON、CSV 和表格数据

3. RAG 实现模式

  • 向量搜索:TF-IDF 和余弦相似度
  • 文档检索:相关性评分和排序
  • 上下文组合:多文档综合
  • 答案生成:基于上下文的回答生成

4. 云迁移策略

  • 统一 API:本地和云端的单一代码库
  • 环境抽象:基于配置的部署
  • 开发工作流:本地 → 测试环境 → 生产环境
  • 成本优化:本地开发,云端生产

生产环境注意事项

1. 性能优化

# Optimize for production RAG rag_config = { "max_context_docs": 5, "similarity_threshold": 0.15, "max_tokens": 1000, "temperature": 0.2, "chunk_size": 500, "chunk_overlap": 50 }

2. 错误处理

# Robust error handling try: result = rag_system.generate_answer(question) if "error" in result: # Fallback to general knowledge fallback_result = client.chat.completions.create( model="phi-4-mini", messages=[{"role": "user", "content": question}] ) except Exception as e: # Log error and provide graceful degradation logger.error(f"RAG system error: {e}")

3. 监控与可观测性

# Track RAG performance metrics = { "retrieval_time": time.time() - start_time, "context_relevance": avg_similarity_score, "token_usage": response.usage.total_tokens, "user_satisfaction": feedback_score }

后续步骤

完成本节后:

  1. 探索第3节:Foundry Local 中的开源模型
  2. 构建生产级 RAG:使用 Chainlit 实现(示例 04)
  3. 高级向量搜索:与 Chroma 或 Pinecone 集成
  4. 云迁移:部署到 Azure AI Foundry
  5. 评估 RAG 质量:运行 cd Workshop/samples;python -m session02.rag_eval_ragas,测量答案相关性、真实性和上下文精确度

可选增强

类别 增强 理由 指导方向
检索 用向量存储(FAISS / Chroma)替换 TF-IDF 提高语义召回率和可扩展性 分块文档(500–800 字符),嵌入,持久化索引
混合索引 语义 + 关键词双重过滤 提高数值/代码查询的精确度 先按关键词过滤,再按余弦相似度排序
嵌入 评估多种嵌入模型 优化相关性与速度 A/B 测试:MiniLM vs E5-small vs 本地托管编码器
缓存 缓存嵌入和检索结果 降低重复查询延迟 使用简单的磁盘 pickle / sqlite 和哈希键
评估 扩展 ragas 数据集 提供统计意义上的质量 策划 50–100 个问答 + 上下文;按主题分层
指标 跟踪检索和生成时间 性能分析 捕获 retrieval_msgen_mstokens 每次调用
安全措施 添加幻觉回退机制 提供更安全的答案 如果真实性 < 阈值 → 答复:“上下文不足。”
回退 本地 → Azure 模型级联 提升混合质量 在低置信度时通过相同的 OpenAI API 路由到云端
确定性 稳定的比较运行 可重复的评估集 固定种子,temperature=0,禁用采样随机性
监控 持久化评估运行历史 回归检测 追加 JSON 行,包含时间戳 + 指标变化

示例:添加检索时间

import time start_ret = time.time() idxs = retrieve(query) retrieval_ms = (time.time() - start_ret) * 1000 start_gen = time.time() text, usage = chat_once(alias, messages=messages, max_tokens=250, temperature=0.2) gen_ms = (time.time() - start_gen) * 1000 record = {"retrieval_ms": retrieval_ms, "gen_ms": gen_ms, "tokens": getattr(usage,'total_tokens',None)}

使用 ragas 扩展评估

  1. 准备一个包含字段的 JSONL:questionanswercontextsground_truths(列表)
  2. 转换为 Dataset.from_list(list_of_dicts)
  3. 运行 evaluate(dataset, metrics=[...])
  4. 存储指标(CSV/JSON)以进行趋势分析。

向量存储快速入门(FAISS)

import faiss, numpy as np index = faiss.IndexFlatIP(embeddings.shape[1]) index.add(embeddings) # embeddings = np.array([...]) normalized D, I = index.search(query_vec, k)

对于磁盘持久化,使用 faiss.write_index(index, "kb.index")

附加资源

文档

示例代码

课程时长:30 分钟实践 + 15 分钟问答
难度级别:中级
前置条件:完成第1节,具备基础 Python 知识

示例场景与工作坊映射

工作坊脚本 / 笔记本 场景 目标 核心数据集 / 来源 示例问题
samples/session02/rag_pipeline.py / notebooks/session02_rag_pipeline.ipynb 内部支持知识库回答隐私和性能常见问题 使用嵌入实现最小内存 RAG 脚本中的 DOCS 列表(5 个短段落) 为什么使用 RAG 进行本地推理?
samples/session02/rag_eval_ragas.py / notebooks/session02_rag_eval_ragas.ipynb 质量分析员建立检索真实性基线指标 在小型合成数据集上计算 ragas 指标 DOCSQUESTIONSGROUND_TRUTH 数组 本地推理的优势是什么?
prompt_engineering.py(高级) 领域专家设计多领域的基础提示 比较领域系统提示和 token 影响 内联 contexts 字典 Foundry Local 如何处理模型缓存?
csv_qa_system.py 销售运营探索导出数据的交互式分析 总结并查询小型销售数据片段 生成的 sample_sales_data.csv(10 行) 哪种产品的平均销售额最高?
document_rag.py 产品团队探索内部 Wiki 的文档 RAG 检索并引用相关文档 create_sample_knowledge_base() 列表 边缘 AI 的优势是什么?
migration_guide.py 架构师准备云迁移计划 演示本地 → Azure API 的一致性 静态测试提示 用 2–3 句话解释边缘 AI 的优势。

数据集片段

内联 RAG 管道文档列表:

DOCS = [ "Foundry Local provides an OpenAI-compatible local inference endpoint.", "Retrieval Augmented Generation (RAG) improves answer grounding by injecting relevant context passages.", "Edge AI reduces latency and preserves privacy by executing models locally.", "Small Language Models can achieve competitive quality with reduced resource usage.", "Vector similarity search retrieves semantically relevant documents for a query.", ]

Ragas 评估真实值元组:

QUESTIONS = ["What advantage does local inference offer?", "How does RAG improve answer grounding?"] GROUND_TRUTH = [ "Local inference reduces latency and preserves privacy.", "RAG adds retrieved context snippets to improve factual grounding." ]

场景叙述

支持工程团队希望快速原型化一个系统,用于回答内部常见问题,同时避免将客户数据暴露到外部。第2节的成果从最小的临时 RAG(无持久化)→ 结构化 CSV 问答 → 带引用的文档检索 → 客观质量评估(ragas)→ 准备好 Azure 测试的迁移策略。

扩展路径

使用可选增强表进行改进:用 FAISS/Chroma 替换 TF-IDF,扩大评估语料库(50–100 问答),在真实性 < 阈值时添加回退机制到更大的模型。

免责声明
本文档使用AI翻译服务Co-op Translator进行翻译。尽管我们努力确保翻译的准确性,但请注意,自动翻译可能包含错误或不准确之处。原始语言的文档应被视为权威来源。对于重要信息,建议使用专业人工翻译。我们对因使用此翻译而产生的任何误解或误读不承担责任。


作者与出处
原作者: microsoft
来源:microsoft
许可证:MIT
整理: 灏天文库整理
由灏天文库结构化整理,提供目录导航、全文检索与在线阅读,便于系统化学习
发布者: 作者: microsoft 转发
评论区 (0)
U