【初學筆記】取得Hugging Face API Tokens 到執行簡單LLM問答內容生成(Inference)

Ting
5 min readApr 28, 2024

--

OpenAI公布ChatGPT後,大型語言模型(LLM, Large Language Model)的領域爆炸式成長。對LLM初學者來說,想要自學和練習RAG各類的更進階應用時,最大的門檻就是取得GPU硬體和相關環境設定,這要消耗大量的金錢和時間。還好有許多公司開始提供免費的操作環境和API,大大降低初學者開始練習的金錢和時間成本。Hugging Face就是其中一個很棒的公司。

Hugging Face是什麼

Hugging Face是一家美國公司,專門開發用於構建機器學習應用的工具。該公司的代表產品是其為自然語言處理應用構建的transformers庫,以及允許使用者共享機器學習模型和資料集的平台。(by Wikipedia)

Huggingface這平台幾乎成為LLM開發的標準平台。幾乎所有的公司如果有open source的LLM model,一定都會上傳到HuggingFace供人下載。上面也有大量的教學和論壇討論,是開發LLM時一定會用到的平台。Hugging Face也提供許多免費的服務。

取得HuggingFace API Tokens

HuggingFace免費提供Serverless Inference API。API Tokens就是一串序號,透過這串序號可以簡單在自己的電腦上使用Huggingface上的各種語言模型,而不用自己花錢買昂貴的GPU和處理麻煩的環境設定。要注意API Tokens是自己專用,不要分享給別人。

更詳情內容參考HuggingFace官網的Quicktour

步驟1

註冊(Register)或登入(Login)HuggingFace。

步驟2

步驟3:在Python上設定Token

假設上一步驟取得的Token是ABCD1234。要新創一個名為HUGGINGFACEHUB_API_TOKEN的環境變數,並將環境變數的值指定為取得的Token

方法1:

在python上設定

>>> import os

# 在下方的‘’貼上API Token
>>> os.environ[‘HUGGINGFACEHUB_API_TOKEN’] = ‘ABCD1234‘

方法2:

使用Colab, Kaggle等系統內建的環境變數設定功能。

使用API Token執行簡單的LLM生成

步驟1: 安裝langchaing-community

這是直接在程式中安裝。也可另外在python的環境設定用pip安裝

>>> !pip install langchain-community

步驟2: 設定使用的LLM

使用HuggingfaceEndpoint來設定LLM模型。這次選擇使用Mistral-7B-Instruct-v0.2。可以參考Hugging Face的網站選擇其他想用的LLM模型

>>> from langchain_community.llms import HuggingFaceEndpoint
>>> repo_id = "mistralai/Mistral-7B-Instruct-v0.2"
>>> llm_try = HuggingFaceEndpoint(repo_id=repo_id)
Token is valid (permission: read).
Your token has been saved to /root/.cache/huggingface/token
Login successful

步驟3: 詢問LLM問題

>>> print(llm_try.invoke("What is Deep Learning?"))

Deep Learning is a subset of Machine Learning which uses Artificial
Neural Networks (ANNs) to model and solve complex problems. Deep Le
arning models consist of multiple hidden layers which learn represe
ntations of the data. Deep Learning models can learn unsupervised f
rom unstructured or unlabelled data and supervised from labelled da
ta.

參考資料

LangChain Manuel :Huggingface Endpoints

Getting started with LLMs in Hugging Face

Appendix: 完整Code

## 安裝
!pip install langchain-community

import os
from langchain_community.llms import HuggingFaceEndpoint

os.environ['HUGGINGFACEHUB_API_TOKEN'] = 'ABCD1234' #改為自己的API Token
repo_id = "mistralai/Mistral-7B-Instruct-v0.2"
llm_try = HuggingFaceEndpoint(repo_id=repo_id)

print(llm_try.invoke("What is Deep Learning?"))

--

--

Ting

An engineer. Specializes in Optics and Data Analysis. Sharing some notes on daily life in Japan and self-studied interests. 旅日工程師。專精於光學量測系統優化和資料分析。紀錄在日生活和自學筆記