跳到主要內容
Open In ColabOpen on GitHub

OpenLM

OpenLM 是一個零依賴性、相容於 OpenAI 的 LLM 供應商,可以直接透過 HTTP 呼叫不同的推論端點。

它實作了 OpenAI Completion 類別,使其可以用作 OpenAI API 的直接替代品。此變更集利用 BaseOpenAI 以盡可能減少新增程式碼。

此範例說明如何使用 LangChain 與 OpenAI 和 HuggingFace 互動。您需要來自兩者的 API 金鑰。

設定

安裝依賴套件並設定 API 金鑰。

# Uncomment to install openlm and openai if you haven't already

%pip install --upgrade --quiet openlm
%pip install --upgrade --quiet langchain-openai
import os
from getpass import getpass

# Check if OPENAI_API_KEY environment variable is set
if "OPENAI_API_KEY" not in os.environ:
print("Enter your OpenAI API key:")
os.environ["OPENAI_API_KEY"] = getpass()

# Check if HF_API_TOKEN environment variable is set
if "HF_API_TOKEN" not in os.environ:
print("Enter your HuggingFace Hub API key:")
os.environ["HF_API_TOKEN"] = getpass()

搭配 OpenLM 使用 LangChain

在這裡,我們將在 LLMChain 中呼叫兩個模型,分別是來自 OpenAI 的 text-davinci-003 和 HuggingFace 上的 gpt2

from langchain.chains import LLMChain
from langchain_community.llms import OpenLM
from langchain_core.prompts import PromptTemplate
API 參考:LLMChain | OpenLM | PromptTemplate
question = "What is the capital of France?"
template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate.from_template(template)

for model in ["text-davinci-003", "huggingface.co/gpt2"]:
llm = OpenLM(model=model)
llm_chain = LLMChain(prompt=prompt, llm=llm)
result = llm_chain.run(question)
print(
"""Model: {}
Result: {}""".format(model, result)
)
Model: text-davinci-003
Result: France is a country in Europe. The capital of France is Paris.
Model: huggingface.co/gpt2
Result: Question: What is the capital of France?

Answer: Let's think step by step. I am not going to lie, this is a complicated issue, and I don't see any solutions to all this, but it is still far more

此頁面是否對您有幫助?