Llama Packs 使用示例¶
本示例展示如何结合 VoyageAI 使用简单的 Llama Pack,主要内容包括:
- 如何下载 Llama Pack
- 如何查看其模块组成
- 如何直接运行使用
- 如何进行自定义配置
所有可用 Pack 均可在 https://llamahub.ai 查阅
数据设置¶
In [ ]:
Copied!
!wget "https://www.dropbox.com/s/f6bmb19xdg0xedm/paul_graham_essay.txt?dl=1" -O paul_graham_essay.txt
!wget "https://www.dropbox.com/s/f6bmb19xdg0xedm/paul_graham_essay.txt?dl=1" -O paul_graham_essay.txt
In [ ]:
Copied!
from llama_index.core import SimpleDirectoryReader
# load in some sample data
reader = SimpleDirectoryReader(input_files=["paul_graham_essay.txt"])
documents = reader.load_data()
from llama_index.core import SimpleDirectoryReader
# load in some sample data
reader = SimpleDirectoryReader(input_files=["paul_graham_essay.txt"])
documents = reader.load_data()
In [ ]:
Copied!
from llama_index.core.llama_pack import download_llama_pack
VoyageQueryEnginePack = download_llama_pack(
"VoyageQueryEnginePack", "./voyage_pack"
)
from llama_index.core.llama_pack import download_llama_pack
VoyageQueryEnginePack = download_llama_pack(
"VoyageQueryEnginePack", "./voyage_pack"
)
In [ ]:
Copied!
voyage_pack = VoyageQueryEnginePack(documents)
voyage_pack = VoyageQueryEnginePack(documents)
检查模块¶
In [ ]:
Copied!
modules = voyage_pack.get_modules()
display(modules)
modules = voyage_pack.get_modules()
display(modules)
{'llm': OpenAI(callback_manager=<llama_index.callbacks.base.CallbackManager object at 0x11fdaae90>, model='gpt-4', temperature=0.1, max_tokens=None, additional_kwargs={}, max_retries=3, timeout=60.0, api_key='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', api_base='https://api.openai.com/v1', api_version=''),
'index': <llama_index.indices.vector_store.base.VectorStoreIndex at 0x2bccb3b50>}
In [ ]:
Copied!
llm = modules["llm"]
vector_index = modules["index"]
llm = modules["llm"]
vector_index = modules["index"]
In [ ]:
Copied!
# try out LLM
response = llm.complete("hello world")
print(str(response))
# try out LLM
response = llm.complete("hello world")
print(str(response))
In [ ]:
Copied!
# try out retriever
retriever = vector_index.as_retriever()
results = retriever.retrieve("What did the author do growing up?")
print(str(results[0].get_content()))
# try out retriever
retriever = vector_index.as_retriever()
results = retriever.retrieve("What did the author do growing up?")
print(str(results[0].get_content()))
运行包¶
每个包都包含一个 run 函数,能够开箱即用地完成特定任务。这里我们将通过完整的 RAG 流程(使用 VoyageAI 嵌入)进行演示。
In [ ]:
Copied!
# this will run the full pack
response = voyage_pack.run(
"What did the author do growing up?", similarity_top_k=2
)
# this will run the full pack
response = voyage_pack.run(
"What did the author do growing up?", similarity_top_k=2
)
In [ ]:
Copied!
print(str(response))
print(str(response))
The author spent his time outside of school mainly writing and programming. He wrote short stories and attempted to write programs on an IBM 1401. Later, he started programming on a TRS-80, creating simple games and a word processor. He also painted still lives while studying at the Accademia.
尝试自定义 Pack¶
LlamaPacks 的一个重要特性是您可以(也应该)检查并修改代码模板!
在本示例中,我们将展示如何通过更换不同的 LLM 来定制模板(同时保留 Voyage 嵌入功能),然后重新使用它。我们将改用 Anthropic 模型。
让我们进入 voyage_pack 并创建副本:
- 出于演示目的,我们将
voyage_pack复制为voyage_pack_copy - 进入
voyage_pack_copy/base.py查看VoyageQueryEnginePack类定义。所有核心逻辑都集中在此处。如您所见,这个 pack 类本身是非常轻量的基础抽象。您可以自由复制/粘贴代码 - 找到
__init__方法中llm = OpenAI(model="gpt-4")这一行,将其改为llm = Anthropic()(默认使用 claude-2 模型) - 添加
from llama_index.llms import Anthropic导入,并确保环境变量中已设置ANTHROPIC_API_KEY - 现在即可使用!
在以下章节中,我们将直接重新导入修改后的 VoyageQueryEnginePack 并使用它。
In [ ]:
Copied!
from voyage_pack_copy.base import VoyageQueryEnginePack
voyage_pack = VoyageQueryEnginePack(documents)
from voyage_pack_copy.base import VoyageQueryEnginePack
voyage_pack = VoyageQueryEnginePack(documents)
In [ ]:
Copied!
response = voyage_pack.run("What did the author do during his time in RISD?")
print(str(response))
response = voyage_pack.run("What did the author do during his time in RISD?")
print(str(response))
Unfortunately I do not have enough context in the provided information to definitively state what the author did during his time at RISD. The passage mentions that he learned a lot in a color class he took there, that he was basically teaching himself to paint, and that in 1993 he dropped out. But there are no specific details provided about his activities or course of study during his time enrolled at RISD. I apologize that I cannot provide a more complete response.