跳至主要內容

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 supervisor。 您也可以使用選項 -p 指定連接埠,使用 -H 指定主機。 預設連接埠為 9997。

然後,在您想要執行它們的每個伺服器上使用 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"

此頁面有幫助嗎?