官方教程
官方案例
在上面的鏈接注冊后,請確保設置您的環境變量以開始記錄追蹤
export LANGSMITH_TRACING="true"
export LANGSMITH_API_KEY="..."
或者,如果在筆記本中,您可以使用以下命令設置它們
import getpass
import osos.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass()
使用語言模型
pip install -qU "langchain[openai]"
import getpass
import osif not os.environ.get("OPENAI_API_KEY"):os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")from langchain.chat_models import init_chat_modelmodel = init_chat_model("gpt-4o-mini", model_provider="openai")
讓我們首先直接使用模型。ChatModels 是 LangChain Runnables 的實例,這意味著它們公開了一個用于與它們交互的標準接口。要簡單地調用模型,我們可以將 消息 列表傳遞給 .invoke 方法。
from langchain_core.messages import HumanMessage, SystemMessagemessages = [SystemMessage("Translate the following from English into Italian"),HumanMessage("hi!"),
]model.invoke(messages)
AIMessage(content='Ciao!', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 3, 'prompt_tokens': 20, 'total_tokens': 23, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-mini-2024-07-18', 'system_fingerprint': 'fp_0705bf87c0', 'finish_reason': 'stop', 'logprobs': None}, id='run-32654a56-627c-40e1-a141-ad9350bbfd3e-0', usage_metadata={'input_tokens': 20, 'output_tokens': 3, 'total_tokens': 23, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})
huggingface 模型
因為gpt要花錢,所以換了一個模型
記得獲取token
!pip install langchain
!pip install -U langchain-openai
import os
os.environ["HUGGINGFACEHUB_API_TOKEN"] = "hf_rxxxxxxxxxxxxxx"import os
from langchain_huggingface import HuggingFaceEndpoint
# from langchain_community.chat_models import ChatHuggingFace
from langchain_core.messages import HumanMessage, SystemMessage# 設置 Token
os.environ["HF_TOKEN"] = "hf_Xxxxxxxxxxxx"# 模型 ID(使用 HuggingFace 上公開的模型)
repo_id = "HuggingFaceH4/zephyr-7b-beta"# 初始化 LLM
llm = HuggingFaceEndpoint(repo_id=repo_id,max_new_tokens=512,top_k=10,top_p=0.95,typical_p=0.95,temperature=0.01,repetition_penalty=1.03,huggingfacehub_api_token=os.environ["HF_TOKEN"]
)# 封裝成聊天模型
# chat_model = ChatHuggingFace(llm=llm)# 構造消息
messages = [SystemMessage(content="Translate the following from English into Chinese"),HumanMessage(content="hi!")
]# 調用模型
response = llm.invoke(messages)
print(response)
選擇一個公開可訪問的替代模型,例如:
HuggingFaceH4/zephyr-7b-beta ? 公開
mistralai/Mistral-7B-v0.1 ? 受限
tiiuae/falcon-7b ? 公開
google/gemma-7b ? 公開
# 1. 導入必要的模塊
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
# HuggingFaceEndpoint是一個 LangChain 封裝的類,用于連接 Hugging Face 的推理 API(也就是調用遠程模型)。
# ChatHuggingFace:將 LLM 包裝成“聊天模型”的適配器。它支持像 messages = [SystemMessage(), HumanMessage()] 這樣的對話格式。
from langchain_core.messages import HumanMessage, SystemMessage
# SystemMessage:系統提示詞,告訴模型應該做什么(如翻譯、寫詩等)。
# HumanMessage:用戶輸入的內容。
# 還有 AIMessage 是模型輸出)# 2. 創建 LLM 實例,創建了一個指向 Hugging Face 模型的客戶端對象
llm = HuggingFaceEndpoint(endpoint_url="https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta", # 要調用的模型地址(即 API 地址)huggingfacehub_api_token="hf_Xopuxxxxxxxxxxxxxxx", # 替換為你的 tokentemperature=0.7, # 控制生成文本的隨機性(越高越隨機)max_new_tokens=512, # 最多生成多少個新 token(控制輸出長度)top_k=50,top_p=0.95,repetition_penalty=1.03 # 防止重復輸出相同內容
)# 3. 用 ChatHuggingFace 封裝成聊天模型
chat_model = ChatHuggingFace(llm=llm) # 這一步把原始的 LLM 封裝成一個“聊天模型”,這樣就可以使用類似 GPT 的接口(例如傳入多個角色消息)。# 4. 構造對話消息
messages = [SystemMessage(content="Translate the following from English into Italian"),HumanMessage(content="hi!")
]# 調用模型
response = chat_model.invoke(messages)
print(response.content)
有的時候,import錯誤、不兼容,也會報錯,例如
import os
from langchain_huggingface import HuggingFaceEndpoint
from langchain_community.llms import HuggingFaceEndpoint # 這里重復引用,會引發錯誤
from langchain_community.chat_models import ChatHuggingFace # 這里新舊版本不兼容
# from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint 正確的
from langchain_core.messages import HumanMessage, SystemMessage# 設置 Token
os.environ["HF_TOKEN"] = "hf_XXXXXXXXXXXXXXXX"# 使用公開可訪問的模型
repo_id = "HuggingFaceH4/zephyr-7b-beta" # 替換為你想用的公開模型llm = HuggingFaceEndpoint(repo_id=repo_id,max_new_tokens=512,top_k=10,top_p=0.95,typical_p=0.95,temperature=0.01,repetition_penalty=1.03,huggingfacehub_api_token=os.environ["HF_TOKEN"]
)# 封裝成聊天模型
chat_model = ChatHuggingFace(llm=llm)# 構造消息
messages = [SystemMessage(content="Translate the following from English into Italian"),HumanMessage(content="hi!")
]# 調用模型
response = chat_model.invoke(messages)
print(response.content)
你使用了 langchain_community.chat_models.ChatHuggingFace,但是它只接受 langchain_community.llms 中的 LLM 實例(比如舊的 HuggingFaceEndpoint),而你現在用的是新的 langchain_huggingface 中的 HuggingFaceEndpoint,所以 類型不兼容。
其他token方法:
from huggingface_hub import login
login(token="hf_XXXXXXXXXXXXXXXX") #這里替換為自己的API Token
或者:
!huggingface-cli login
其他參數
- repo_id: 這是模型在 Hugging Face Hub 上的唯一標識符。它通常由用戶名(或組織名)和模型名組成,例如 “microsoft/Phi-3-mini-4k-instruct”。這種方式更適合于本地運行或者通過 Hugging Face Inference API 直接訪問公開模型。
- task: 指定你想要執行的任務類型,比如 “text-generation”、“translation” 等等。
- endpoint_url: 這是指向 Hugging Face Inference API 的具體URL,用于直接調用遠程部署的模型服務。例如 “https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta”。
場景 | 是否需要 API Token |
---|---|
使用 repo_id 加載公開模型 | 不需要 |
使用 repo_id 加載私有或受限模型 | 需要 |
使用 endpoint_url 訪問 Hugging Face Inference API | 大多數情況下需要 |
你可以從 Hugging Face 設置頁面 獲取或生成新的API token。
如何擴展
? 1. 換一個模型(只需修改 endpoint_url
)
你可以訪問 Hugging Face 上任意公開模型頁面,復制它的推理地址。
🔁 示例:換成 TinyLlama/TinyLlama-1.1B-Chat-v1.0
這個模型很小,速度快,適合測試。
llm = HuggingFaceEndpoint(endpoint_url="https://api-inference.huggingface.co/models/TinyLlama/TinyLlama-1.1B-Chat-v1.0",huggingfacehub_api_token="你的token",temperature=0.7,max_new_tokens=512
)
🔁 示例:換成中文模型 Qwen/Qwen2-0.5B-Instruct
如果你想要中文模型:
llm = HuggingFaceEndpoint(endpoint_url="https://api-inference.huggingface.co/models/Qwen/Qwen2-0.5B-Instruct",huggingfacehub_api_token="你的token",temperature=0.7,max_new_tokens=512
)
?? 注意:有些模型需要申請權限才能使用(會報錯
GatedRepoError
),請前往對應模型頁面點擊 “Request Access”。
? 2. 改變任務(只需修改 SystemMessage
)
比如你想讓模型寫一首詩:
messages = [SystemMessage(content="You are a poet. Write a short poem about the moon in Chinese."),HumanMessage(content="Please write a poem.")
]
或者你想讓它做 QA:
messages = [SystemMessage(content="Answer the question briefly and accurately."),HumanMessage(content="What is the capital of France?")
]
📌 三、推薦常用公開模型(可直接替換)
模型名稱 | 描述 | 是否需要申請 |
---|---|---|
HuggingFaceH4/zephyr-7b-beta | 英文聊天模型 | ? 不需要 |
TinyLlama/TinyLlama-1.1B-Chat-v1.0 | 很小的英文聊天模型 | ? 不需要 |
Qwen/Qwen2-0.5B-Instruct | 阿里通義千問系列,中文友好 | ? 不需要 |
google/gemma-7b-it | Google 推出的指令微調模型 | ? 不需要 |
mistralai/Mistral-7B-Instruct-v0.2 | Mistral 系列指令模型 | ? 需要申請 |
📝 總結
功能 | 修改位置 | 示例 |
---|---|---|
換模型 | 修改 endpoint_url | https://api-inference.huggingface.co/models/用戶名/模型名 |
控制輸出 | 修改參數 | temperature=0.1 , max_new_tokens=256 |
更換任務 | 修改 SystemMessage 和 HumanMessage 內容 | “你是誰?”、“幫我翻譯”、“寫一篇作文”等 |
使用中文模型 | 選擇支持中文的模型 | 如 Qwen2-0.5B-Instruct |