Hugging Face 为 TGI 和 Inference Endpoints 推出兼容 OpenAI 的 Messages API
From OpenAI to Open LLMs with Messages API on Hugging Face
Hugging Face 为 Text Generation Inference(TGI)和 Inference Endpoints 推出 Messages API。
Hugging Face 官方给出 TGI 兼容 OpenAI 接口的迁移路径与代码示例,读者可据此评估从闭源模型切换到开源模型的改造成本。
我们很高兴推出 Messages API,为 Text Generation Inference (TGI) 和 Inference Endpoints 提供 OpenAI 兼容性。
从 1.4.0 版本开始,TGI 提供了一个与 OpenAI Chat Completion API 兼容的 API。新的 Messages API 让客户和用户能够从 OpenAI 模型无缝过渡到开放 LLM。该 API 可直接与 OpenAI 的客户端库或第三方工具(如 LangChain 或 LlamaIndex)一起使用。
“具有 OpenAI 兼容性的新 Messages API 让 Ryght 的实时 GenAI 编排平台可以轻松地将 LLM 用例从 OpenAI 切换到开放模型。我们在 Inference Endpoints 上从 GPT4 迁移到 Mixtral/Llama2 毫不费力,现在我们拥有了简化的工作流程,并且对 AI 解决方案有了更多控制权。” - Johnny Crupi,CTO,就职于 Ryght
新的 Messages API 现已在 Inference Endpoints 中提供,包括专用和 Serverless 两种类型。为了帮助您快速上手,我们提供了详细示例,说明如何:
限制: Messages API 目前不支持函数调用,并且仅适用于在其 tokenizer 配置中定义了 chat_template 的 LLM,例如 Mixtral 8x7B Instruct 的情况。
创建 Inference Endpoint
Inference Endpoints 提供了一种安全的生产级解决方案,可轻松将 Hub 中的任何机器学习模型部署到由 Hugging Face 管理的专用基础设施上。
在此示例中,我们将使用 Text Generation Inference 将 Nous-Hermes-2-Mixtral-8x7B-DPO(一个微调过的 Mixtral 模型)部署到 Inference Endpoints。
我们只需在 UI 中点击几下即可部署模型,或者利用 huggingface_hub Python 库以编程方式创建和管理 Inference Endpoints。这里我们演示如何使用 Hub 库。
在下面展示的 API 调用中,我们需要指定 endpoint 名称和模型仓库,以及 text-generation 任务。在此示例中,我们使用 protected 类型,因此访问已部署的 endpoint 需要有效的 Hugging Face token。我们还需要配置硬件要求,如供应商、区域、加速器、实例类型和大小。您可以使用此 API 调用查看可用资源选项列表,并在我们的目录中此处查看精选模型的推荐配置。
注意:您可能需要通过发送电子邮件至 [email protected] 来申请配额升级
from huggingface_hub import create_inference_endpoint
endpoint = create_inference_endpoint(
"nous-hermes-2-mixtral-8x7b-demo",
repository="NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
framework="pytorch",
task="text-generation",
accelerator="gpu",
vendor="aws",
region="us-east-1",
type="protected",
instance_type="nvidia-a100",
instance_size="x2",
custom_image={
"health_route": "/health",
"env": {
"MAX_INPUT_LENGTH": "4096",
"MAX_BATCH_PREFILL_TOKENS": "4096",
"MAX_TOTAL_TOKENS": "32000",
"MAX_BATCH_TOTAL_TOKENS": "1024000",
"MODEL_ID": "/repository",
},
"url": "ghcr.io/huggingface/text-generation-inference:sha-1734540", # use this build or newer
},
)
endpoint.wait()
print(endpoint.status)
我们的部署需要几分钟才能启动。我们可以使用 .wait() 工具来阻塞运行线程,直到 endpoint 达到最终的“running”状态。一旦运行,我们可以确认其状态,并通过 UI Playground 试用一下:
很好,我们现在有了一个可用的 endpoint!
💡 使用
huggingface_hub部署时,您的 endpoint 默认会在空闲 15 分钟后缩容至零,以在非活动期间优化成本。请查看Hub Python Library 文档,了解可用于管理 endpoint 生命周期的所有功能。
将 Inference Endpoints 与 OpenAI 客户端库一起使用
TGI 中的 Messages 支持使 Inference Endpoints 可直接兼容 OpenAI Chat Completion API。这意味着任何通过 OpenAI 客户端库使用 OpenAI 模型的现有脚本,都可以直接替换为使用运行在 TGI 端点上的任意开源 LLM!
借助这种无缝过渡,您可以立即享受开放模型带来的诸多优势:
- 对模型和数据拥有完全的控制权和透明度
- 不再需要担心速率限制
- 能够根据您的具体需求完全自定义系统
让我们看看如何操作。
使用 Python 客户端
下面的示例展示了如何使用 OpenAI Python 库 完成这一过渡。只需将 <ENDPOINT_URL> 替换为您的端点 URL(务必包含 v1/ 后缀),并将 <HF_API_TOKEN> 字段填入有效的 Hugging Face 用户令牌。<ENDPOINT_URL> 可以从 Inference Endpoints UI 中获取,也可以从我们上面用 endpoint.url 创建的端点对象中获取。
然后我们可以像往常一样使用该客户端,传入一个消息列表,从我们的 Inference Endpoint 流式获取响应。
from openai import OpenAI
# initialize the client but point it to TGI
client = OpenAI(
base_url="<ENDPOINT_URL>" + "/v1/", # replace with your endpoint url
api_key="<HF_API_TOKEN>", # replace with your token
)
chat_completion = client.chat.completions.create(
model="tgi",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Why is open-source software important?"},
],
stream=True,
max_tokens=500
)
# iterate and print stream
for message in chat_completion:
print(message.choices[0].delta.content, end="")
在幕后,TGI 的 Messages API 会使用其 聊天模板 自动将消息列表转换为模型所需的指令格式。
💡 某些 OpenAI 功能(如函数调用)与 TGI 不兼容。目前,Messages API 支持以下聊天补全参数:
stream、max_tokens、frequency_penalty、logprobs、seed、temperature和top_p。
使用 JavaScript 客户端
下面是上面同样的流式示例,但使用的是 OpenAI Javascript/Typescript 库。
import OpenAI from "openai";
const openai = new OpenAI({
baseURL: "<ENDPOINT_URL>" + "/v1/", // replace with your endpoint url
apiKey: "<HF_API_TOKEN>", // replace with your token
});
async function main() {
const stream = await openai.chat.completions.create({
model: "tgi",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Why is open-source software important?" },
],
stream: true,
max_tokens: 500,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || "");
}
}
main();
与 LangChain 和 LlamaIndex 集成
现在,让我们看看如何将新创建的端点与您偏好的 RAG 框架一起使用。
如何在 LangChain 中使用
要在 LangChain 中使用它,只需创建一个 ChatOpenAI 实例,并按如下方式传入您的 <ENDPOINT_URL> 和 <HF_API_TOKEN>:
from langchain_community.chat_models.openai import ChatOpenAI
llm = ChatOpenAI(
model_name="tgi",
openai_api_key="<HF_API_TOKEN>",
openai_api_base="<ENDPOINT_URL>" + "/v1/",
)
llm.invoke("Why is open-source software important?")
我们能够直接复用与 OpenAI 模型一起使用的同一个 ChatOpenAI 类。这样只需更改一行代码,所有先前的代码就能与我们的端点配合工作。
现在让我们在简单的 RAG 流水线中使用以这种方式声明的 LLM,来回答一个关于 HF 博客文章内容的问题。
from langchain_core.runnables import RunnableParallel
from langchain_community.embeddings import HuggingFaceEmbeddings
# Load, chunk and index the contents of the blog
loader = WebBaseLoader(
web_paths=("https://huggingface.co/blog/open-source-llms-as-agents",),
)
docs = loader.load()
# Declare an HF embedding model and vector store
hf_embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-large-en-v1.5")
text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=200)
splits = text_splitter.split_documents(docs)
vectorstore = Chroma.from_documents(documents=splits, embedding=hf_embeddings)
# Retrieve and generate using the relevant pieces of context
retriever = vectorstore.as_retriever()
prompt = hub.pull("rlm/rag-prompt")
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
rag_chain_from_docs = (
RunnablePassthrough.assign(context=(lambda x: format_docs(x["context"])))
| prompt
| llm
| StrOutputParser()
)
rag_chain_with_source = RunnableParallel(
{"context": retriever, "question": RunnablePassthrough()}
).assign(answer=rag_chain_from_docs)
rag_chain_with_source.invoke("According to this article which open-source model is the best for an agent behaviour?")
{
"context": [...],
"question": "According to this article which open-source model is the best for an agent behaviour?",
"answer": " According to the article, Mixtral-8x7B is the best open-source model for agent behavior, as it performs well and even beats GPT-3.5. The authors recommend fine-tuning Mixtral for agents to potentially surpass the next challenger, GPT-4.",
}
如何在 LlamaIndex 中使用
同样,您也可以在 LlamaIndex 中使用 TGI 端点。我们将使用 OpenAILike 类,并通过配置一些额外参数(即 is_local、is_function_calling_model、is_chat_model、context_window)来实例化它。请注意,上下文窗口参数应与您端点之前为 MAX_TOTAL_TOKENS 设置的值相匹配。
from llama_index.llms import OpenAILike
# Instantiate an OpenAILike model
llm = OpenAILike(
model="tgi",
api_key="<HF_API_TOKEN>",
api_base="<ENDPOINT_URL>" + "/v1/",
is_chat_model=True,
is_local=False,
is_function_calling_model=False,
context_window=32000,
)
# Then call it
llm.complete("Why is open-source software important?")
现在我们可以在类似的 RAG 流水线中使用它。请记住,之前在 Inference Endpoint 中对 MAX_INPUT_LENGTH 的选择将直接影响模型可以处理的检索块数量(similarity_top_k)。
from llama_index import (
ServiceContext,
VectorStoreIndex,
)
from llama_index import download_loader
from llama_index.embeddings import HuggingFaceEmbedding
from llama_index.query_engine import CitationQueryEngine
SimpleWebPageReader = download_loader("SimpleWebPageReader")
documents = SimpleWebPageReader(html_to_text=True).load_data(
["https://huggingface.co/blog/open-source-llms-as-agents"]
)
# Load embedding model
embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-large-en-v1.5")
# Pass LLM to pipeline
service_context = ServiceContext.from_defaults(embed_model=embed_model, llm=llm)
index = VectorStoreIndex.from_documents(
documents, service_context=service_context, show_progress=True
)
# Query the index
query_engine = CitationQueryEngine.from_args(
index,
similarity_top_k=2,
)
response = query_engine.query(
"According to this article which open-source model is the best for an agent behaviour?"
)
According to the article, Mixtral-8x7B is the best performing open-source model for an agent behavior [5]. It even beats GPT-3.5 in this task. However, it's worth noting that Mixtral's performance could be further improved with proper fine-tuning for function calling and task planning skills [5].
清理
完成端点使用后,您可以暂停或删除它。此步骤可以通过 UI 完成,也可以像下面这样以编程方式完成。
# pause our running endpoint
endpoint.pause()
# optionally delete
endpoint.delete()
结论
Text Generation Inference 中新的 Messages API 提供了从 OpenAI 模型到开源 LLM 的平滑过渡路径。我们迫不及待地想看到您使用运行在 TGI 上的开源 LLM 实现哪些用例!
请参阅 此 notebook 获取本文所述代码的可运行版本。
来源:Hugging Face Blog · huggingface.co
