跳转到内容

Predibase

本笔记本展示了如何在Llamaindex中使用Predibase托管的LLM。您可以将Predibase添加到现有的Llamaindex工作流中来实现:

  1. 部署和查询预训练或自定义开源大语言模型,无需繁琐操作
  2. 实现端到端检索增强生成(RAG)系统的可操作化
  3. 仅用几行代码微调您自己的大型语言模型
  1. 在此处注册免费 Predibase 账户
  2. 创建账户
  3. 前往设置 > 我的个人资料并生成新的API令牌。
%pip install llama-index-llms-predibase
!pip install llama-index --quiet
!pip install predibase --quiet
!pip install sentence-transformers --quiet
import os
os.environ["PREDIBASE_API_TOKEN"] = "{PREDIBASE_API_TOKEN}"
from llama_index.llms.predibase import PredibaseLLM

流程 1:直接查询 Predibase 大语言模型

Section titled “Flow 1: Query Predibase LLM directly”
# Predibase-hosted fine-tuned adapter example
llm = PredibaseLLM(
model_name="mistral-7b",
predibase_sdk_version=None, # optional parameter (defaults to the latest Predibase SDK version if omitted)
adapter_id="e2e_nlg", # adapter_id is optional
adapter_version=1, # optional parameter (applies to Predibase only)
api_token=None, # optional parameter for accessing services hosting adapters (e.g., HuggingFace)
max_new_tokens=512,
temperature=0.3,
)
# The `model_name` parameter is the Predibase "serverless" base_model ID
# (see https://docs.predibase.com/user-guide/inference/models for the catalog).
# You can also optionally specify a fine-tuned adapter that's hosted on Predibase or HuggingFace
# In the case of Predibase-hosted adapters, you must also specify the adapter_version
# HuggingFace-hosted fine-tuned adapter example
llm = PredibaseLLM(
model_name="mistral-7b",
predibase_sdk_version=None, # optional parameter (defaults to the latest Predibase SDK version if omitted)
adapter_id="predibase/e2e_nlg", # adapter_id is optional
api_token=os.environ.get(
"HUGGING_FACE_HUB_TOKEN"
), # optional parameter for accessing services hosting adapters (e.g., HuggingFace)
max_new_tokens=512,
temperature=0.3,
)
# The `model_name` parameter is the Predibase "serverless" base_model ID
# (see https://docs.predibase.com/user-guide/inference/models for the catalog).
# You can also optionally specify a fine-tuned adapter that's hosted on Predibase or HuggingFace
# In the case of Predibase-hosted adapters, you can also specify the adapter_version (assumed latest if omitted)
result = llm.complete("Can you recommend me a nice dry white wine?")
print(result)

流程 2:使用 Predibase LLM 的检索增强生成 (RAG)

Section titled “Flow 2: Retrieval Augmented Generation (RAG) with Predibase LLM”
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.embeddings import resolve_embed_model
from llama_index.core.node_parser import SentenceSplitter
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
documents = SimpleDirectoryReader("./data/paul_graham/").load_data()

配置 Predibase 大语言模型

Section titled “Configure Predibase LLM”
# Predibase-hosted fine-tuned adapter
llm = PredibaseLLM(
model_name="mistral-7b",
predibase_sdk_version=None, # optional parameter (defaults to the latest Predibase SDK version if omitted)
adapter_id="e2e_nlg", # adapter_id is optional
api_token=None, # optional parameter for accessing services hosting adapters (e.g., HuggingFace)
temperature=0.3,
context_window=1024,
)
# HuggingFace-hosted fine-tuned adapter
llm = PredibaseLLM(
model_name="mistral-7b",
predibase_sdk_version=None, # optional parameter (defaults to the latest Predibase SDK version if omitted)
adapter_id="predibase/e2e_nlg", # adapter_id is optional
api_token=os.environ.get(
"HUGGING_FACE_HUB_TOKEN"
), # optional parameter for accessing services hosting adapters (e.g., HuggingFace)
temperature=0.3,
context_window=1024,
)
embed_model = resolve_embed_model("local:BAAI/bge-small-en-v1.5")
splitter = SentenceSplitter(chunk_size=1024)
index = VectorStoreIndex.from_documents(
documents, transformations=[splitter], embed_model=embed_model
)
query_engine = index.as_query_engine(llm=llm)
response = query_engine.query("What did the author do growing up?")
print(response)