跳到主要內容
Open In ColabOpen on GitHub

Xorbits Inference (Xinference)

Xinference 是一個強大且多功能的程式庫,旨在為 LLM、語音辨識模型和多模態模型提供服務,即使在您的筆記型電腦上也能運作。它支援各種與 GGML 相容的模型,例如 chatglm、baichuan、whisper、vicuna、orca 和許多其他模型。此筆記本示範如何將 Xinference 與 LangChain 搭配使用。

安裝

透過 PyPI 安裝 Xinference

%pip install --upgrade --quiet  "xinference[all]"

在本機或分散式叢集中部署 Xinference。

對於本機部署,請執行 xinference

若要在叢集中部署 Xinference,請先使用 xinference-supervisor 啟動 Xinference 監管者。您也可以使用選項 -p 指定埠,並使用 -H 指定主機。預設埠為 9997。

然後,在您要執行 Xinference worker 的每部伺服器上,使用 xinference-worker 啟動 Xinference worker。

您可以參考 Xinference 中的 README 檔案以取得更多資訊。

包裝器

若要將 Xinference 與 LangChain 搭配使用,您需要先啟動模型。您可以使用命令列介面 (CLI) 來執行此操作

!xinference launch -n vicuna-v1.3 -f ggmlv3 -q q4_0
Model uid: 7167b2b0-2a04-11ee-83f0-d29396a3f064

系統會傳回模型 UID 供您使用。現在您可以將 Xinference 與 LangChain 搭配使用

from langchain_community.llms import Xinference

llm = Xinference(
server_url="http://0.0.0.0:9997", model_uid="7167b2b0-2a04-11ee-83f0-d29396a3f064"
)

llm(
prompt="Q: where can we visit in the capital of France? A:",
generate_config={"max_tokens": 1024, "stream": True},
)
API 參考:Xinference
' You can visit the Eiffel Tower, Notre-Dame Cathedral, the Louvre Museum, and many other historical sites in Paris, the capital of France.'

與 LLMChain 整合

from langchain.chains import LLMChain
from langchain_core.prompts import PromptTemplate

template = "Where can we visit in the capital of {country}?"

prompt = PromptTemplate.from_template(template)

llm_chain = LLMChain(prompt=prompt, llm=llm)

generated = llm_chain.run(country="France")
print(generated)
API 參考:LLMChain | PromptTemplate

A: You can visit many places in Paris, such as the Eiffel Tower, the Louvre Museum, Notre-Dame Cathedral, the Champs-Elysées, Montmartre, Sacré-Cœur, and the Palace of Versailles.

最後,當您不需要使用模型時,請終止模型

!xinference terminate --model-uid "7167b2b0-2a04-11ee-83f0-d29396a3f064"

此頁面是否對您有幫助?