LlamaIndex:从ServiceContext迁移到Settings

云的事情不好说 2024-06-06 03:43:46

在LlamaIndex版本 v0.10.0 中引入了一个新的全局 Settings 对象,旨在取代旧的 ServiceContext 配置。

新的 Settings 对象是一个全局设置,其参数是在需要时才懒加载实例化的。像大语言模型(LLM)或嵌入模型这样的属性只有在底层模块实际需要时才会加载。

以前使用 service context 时,各个模块经常不使用它,而且它还强制在运行时将每个组件加载到内存中(即使这些组件没有被使用)。

配置全局设置意味着正在改变 LlamaIndex 中每个模块的默认值。如果不使用 OpenAI,一个示例配置可能看起来像这样:

from llama_index.llms.ollama import Ollama

from llama_index.embeddings.huggingface import HuggingFaceEmbedding

from llama_index.core import Settings

Settings.llm = Ollama(model="llama2", request_timeout=120.0)

Settings.embed_model = HuggingFaceEmbedding(

model_name="BAAI/bge-small-en-v1.5"

)

依据上述这些设置,可以确保在框架中不会使用 OpenAI 嵌入模型。Settings 对象支持与旧 ServiceConext 几乎相同的所有属性。

一、迁移示例

以下是完全从 ServiceContext 迁移到 Settings 的示例。

之前的示例代码,使用ServiceContext:

from llama_index.embeddings.openai import OpenAIEmbedding

from llama_index.core.node_parser import SentenceSplitter

from llama_index.llms.openai import OpenAI

from llama_index.core import ServiceContext, set_global_service_context

service_context = ServiceContext.from_defaults(

llm=OpenAI(model="gpt-3.5-turbo"),

embed_model=OpenAIEmbedding(model="text-embedding-3-small"),

node_parser=SentenceSplitter(chunk_size=512, chunk_overlap=20),

num_output=512,

context_window=3900,

)

set_global_service_context(service_context)

使用Settings 对象之后的代码:

from llama_index.embeddings.openai import OpenAIEmbedding

from llama_index.core.node_parser import SentenceSplitter

from llama_index.llms.openai import OpenAI

from llama_index.core import Settings

Settings.llm = OpenAI(model="gpt-3.5-turbo")

Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small")

Settings.node_parser = SentenceSplitter(chunk_size=512, chunk_overlap=20)

Settings.num_output = 512

Settings.context_window = 3900

二、本地配置

上述内容涵盖了全局配置。要针对每个模块配置设置,所有模块接口都应该更新为接受正在使用的组件的 kwargs 参数。

如果使用的是 IDE开发工具,kwargs 应该会自动填充。以下是一些示例代码。

一个向量存储索引(Vector Store Index)只需要一个嵌入模型:

# a vector store index only needs an embed model

index = VectorStoreIndex.from_documents(

documents, embed_model=embed_model, transformations=transformations

)

# ... until you create a query engine

query_engine = index.as_query_engine(llm=llm)

文档摘要索引(Document Summary Index)需要一个 llm 和嵌入模型:

# a document summary index needs both an llm and embed model

# for the constructor

index = DocumentSummaryIndex.from_documents(

documents, embed_model=embed_model, llm=llm

)

通过这些更改,LlamaIndex的用户可以更有效地管理全局和本地配置,同时减少不必要的内存占用。



0 阅读:0

云的事情不好说

简介:感谢大家的关注