運行大型語言模型(LLMS)由於其硬件需求而提出了重大挑戰,但是存在許多選項以使這些功能強大的工具可訪問。當今的景觀提供了幾種方法 – 從攝入型號到Openai和Anthropic等主要參與者提供的API,再到通過擁抱面孔和Ollama等平台部署開源替代方案。無論您是遠程連接模型還是在本地運行它們,了解及時工程和輸出結構等關鍵技術都可以大大提高特定應用程序的性能。本文探討了實施LLM的實際方面,為開發人員提供了瀏覽硬件約束,選擇適當的部署方法並通過經過驗證的技術優化模型輸出的知識。
1。 使用LLM API:快速介紹
LLM API提供了一種直接訪問強大語言模型的方法,而無需管理基礎架構。這些服務處理複雜的計算要求,使開發人員可以專注於實施。在本教程中,我們將使用示例了解這些LLM的實現,以更直接和麵向產品的方式發揮其高級潛力。為了保持這一教程簡潔,我們只限於僅用於實施部分的封閉源模型,最後,我們添加了對開源模型的高級概述。
2。 實施封閉源LLM:基於API的解決方案
封閉的源LLM通過直接的API接口提供了強大的功能,在提供最先進的性能的同時,需要最小的基礎架構。這些模型由OpenAI,Anthropic和Google等公司維護,為開發人員提供了可通過簡單的API呼叫訪問的可訪問生產的情報。
2.1 讓我們探索如何使用最容易訪問的封閉源API之一,即人類API。
# First, install the Anthropic Python library
!pip install anthropic
import anthropic
import os
client = anthropic.Anthropic(
api_key=os.environ.get("YOUR_API_KEY"), # Store your API key as an environment variable
)
2.1.1 應用程序:在上下文問題上回答用戶指南的機器人
import anthropic
import os
from typing import Dict, List, Optional
class ClaudeDocumentQA:
"""
An agent that uses Claude to answer questions based strictly on the content
of a provided document.
"""
def __init__(self, api_key: Optional(str) = None):
"""Initialize the Claude client with API key."""
self.client = anthropic.Anthropic(
api_key="YOUR_API_KEY",
)
# Updated to use the correct model string format
self.model = "claude-3-7-sonnet-20250219"
def process_question(self, document: str, question: str) -> str:
"""
Process a user question based on document context.
Args:
document: The text document to use as context
question: The user's question about the document
Returns:
Claude's response answering the question based on the document
"""
# Create a system prompt that instructs Claude to only use the provided document
system_prompt = """
You are a helpful assistant that answers questions based ONLY on the information
provided in the DOCUMENT below. If the answer cannot be found in the document,
say "I cannot find information about this in the provided document."
Do not use any prior knowledge outside of what's explicitly stated in the document.
"""
# Construct the user message with document and question
user_message = f"""
DOCUMENT:
{document}
QUESTION:
{question}
Answer the question using only information from the DOCUMENT above. If the information
isn't in the document, say so clearly.
"""
try:
# Send request to Claude
response = self.client.messages.create(
model=self.model,
max_tokens=1000,
temperature=0.0, # Low temperature for factual responses
system=system_prompt,
messages=(
{"role": "user", "content": user_message}
)
)
return response.content(0).text
except Exception as e:
# Better error handling with details
return f"Error processing request: {str(e)}"
def batch_process(self, document: str, questions: List(str)) -> Dict(str, str):
"""
Process multiple questions about the same document.
Args:
document: The text document to use as context
questions: List of questions to answer
Returns:
Dictionary mapping questions to answers
"""
results = {}
for question in questions:
results = self.process_question(document, question)
return results
### Test Code
if __name__ == "__main__":
# Sample document (an instruction manual excerpt)
sample_document = """
QUICKSTART GUIDE: MODEL X3000 COFFEE MAKER
SETUP INSTRUCTIONS:
1. Unpack the coffee maker and remove all packaging materials.
2. Rinse the water reservoir and fill with fresh, cold water up to the MAX line.
3. Insert the gold-tone filter into the filter basket.
4. Add ground coffee (1 tbsp per cup recommended).
5. Close the lid and ensure the carafe is properly positioned on the warming plate.
6. Plug in the coffee maker and press the POWER button.
7. Press the BREW button to start brewing.
FEATURES:
- Programmable timer: Set up to 24 hours in advance
- Strength control: Choose between Regular, Strong, and Bold
- Auto-shutoff: Machine turns off automatically after 2 hours
- Pause and serve: Remove carafe during brewing for up to 30 seconds
CLEANING:
- Daily: Rinse removable parts with warm water
- Weekly: Clean carafe and filter basket with mild detergent
- Monthly: Run a descaling cycle using white vinegar solution (1:2 vinegar to water)
TROUBLESHOOTING:
- Coffee not brewing: Check water reservoir and power connection
- Weak coffee: Use STRONG setting or add more coffee grounds
- Overflow: Ensure filter is properly seated and use correct amount of coffee
- Error E01: Contact customer service for heating element replacement
"""
# Sample questions
sample_questions = (
"How much coffee should I use per cup?",
"How do I clean the coffee maker?",
"What does error code E02 mean?",
"What is the auto-shutoff time?",
"How long can I remove the carafe during brewing?"
)
# Create and use the agent
agent = ClaudeDocumentQA()
# Process a single question
print("=== Single Question ===")
answer = agent.process_question(sample_document, sample_questions(0))
print(f"Q: {sample_questions(0)}")
print(f"A: {answer}\n")
# Process multiple questions
print("=== Batch Processing ===")
results = agent.batch_process(sample_document, sample_questions)
for question, answer in results.items():
print(f"Q: {question}")
print(f"A: {answer}\n")
型號的輸出
Claude Document問答:專業的LLM應用程序
該Claude文檔問答代理商展示了LLM API的實際實現,以回答上下文感知的問題。該應用程序使用擬人化的Claude API來創建一個系統,該系統嚴格將其響應基於提供的文檔內容,這是許多企業用例的重要功能。
該代理商通過將克勞德(Claude)強大的語言功能包裹在一個專業的框架中,以:
- 將參考文檔和用戶問題作為輸入
- 結構提示在文檔上下文和查詢之間劃定
- 使用系統說明來限制Claude僅使用文檔中存在的信息
- 提供明確的處理,以獲取文檔中未找到的信息
- 支持個人和批處理問題處理
這種方法對於需要與特定內容相關的高保真響應的方案特別有價值,例如客戶支持自動化,法律文檔分析,技術文檔檢索或教育應用。該實現表明了謹慎的及時工程和系統設計如何將通用LLM轉換為針對特定領域應用程序的專用工具。
通過將直接的API集成與對模型行為的周到約束相結合,此示例展示了開發人員如何構建可靠的,上下文感知的AI應用程序,而無需昂貴的微調或複雜的基礎架構。
注意:這只是文檔問題回答的基本實現,我們沒有更深入地研究特定於領域的事物的實際複雜性。
3。實施開源LLM:本地部署和適應性
開源LLMS為封閉式選項提供了靈活且可自定義的替代方案,使開發人員可以在自己的基礎架構上部署模型,並完全控制實施詳細信息。這些模型,來自Meta(Llama),Mistral AI和各種研究機構等組織,為各種部署場景提供了績效和可訪問性的平衡。
開源LLM實現的特徵是:
- 本地部署:模型可以在個人硬件或自我管理的雲基礎架構上運行
- 自定義選項:能夠微調,量化或修改特定需求的模型
- 資源擴展:可以根據可用的計算資源進行調整性能
- 隱私保護:數據仍在受控環境中,沒有外部API調用
- 成本結構:一次性計算成本,而不是直接定價
主要的開源模型系列包括:
- LLAMA/LLAMA-2:META強大的基礎模型,具有商業友好的許可
- Mistral:儘管參數計數較小,但具有強大性能的有效模型
- 獵鷹:具有TII競爭性能的培訓模型
- 畢田(Pythia):具有廣泛培訓方法文檔的研究導向模型
這些模型可以通過框架進行部署,例如擁抱面孔變壓器,Llama.cpp或Ollama,它們提供了抽象來簡化實施的同時保留本地控制的好處。儘管通常需要比基於API的替代方案更多的技術設置,但開源LLM為高容量應用程序,數據隱私和特定領域需求的定制潛力提供了成本管理的優勢。
這是 COLAB筆記本。另外,別忘了跟隨我們 嘰嘰喳喳 並加入我們 電報頻道 和 LinkedIn GrOUP。別忘了加入我們的 80k+ ml子列數。
🚨 推薦的閱讀-LG AI研究釋放Nexus:一個高級系統集成代理AI系統和數據合規性標準,以解決AI數據集中的法律問題

Asjad是Marktechpost的實習顧問。他正在Kharagpur印度理工學院的機械工程學領域掌握B.Tech。 Asjad是一種機器學習和深度學習愛好者,他一直在研究醫療保健中機器學習的應用。
🚨推薦開源AI平台:“ Intellagent是一個開源的多代理框架,可評估複雜的對話AI系統”(已晉升)