Llama Packs 示例¶
本示例展示了如何使用一个简单的 Llama Pack 和 VoyageAI。我们将展示以下内容:
- 如何下载 Llama Pack
- 如何检查其模块
- 如何直接运行它
- 如何自定义它。
您可以在这里找到所有 Pack: https://llamahub.ai
设置数据¶
In [ ]
已复制!
!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 [ ]
已复制!
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 # 加载一些示例数据 reader = SimpleDirectoryReader(input_files=["paul_graham_essay.txt"]) documents = reader.load_data()
In [ ]
已复制!
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 [ ]
已复制!
voyage_pack = VoyageQueryEnginePack(documents)
voyage_pack = VoyageQueryEnginePack(documents)
检查模块¶
In [ ]
已复制!
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 [ ]
已复制!
llm = modules["llm"]
vector_index = modules["index"]
llm = modules["llm"] vector_index = modules["index"]
In [ ]
已复制!
# try out LLM
response = llm.complete("hello world")
print(str(response))
# 尝试 LLM response = llm.complete("hello world") print(str(response))
In [ ]
已复制!
# try out retriever
retriever = vector_index.as_retriever()
results = retriever.retrieve("What did the author do growing up?")
print(str(results[0].get_content()))
# 尝试检索器 retriever = vector_index.as_retriever() results = retriever.retrieve("What did the author do growing up?") print(str(results[0].get_content()))
运行 Pack¶
每个 Pack 都有一个 run
函数,可以直接完成特定任务。这里我们将通过 VoyageAI 嵌入来完成完整的 RAG 流程。
In [ ]
已复制!
# this will run the full pack
response = voyage_pack.run(
"What did the author do growing up?", similarity_top_k=2
)
# 这将运行整个 Pack response = voyage_pack.run( "What did the author do growing up?", similarity_top_k=2 )
In [ ]
已复制!
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 的一个主要特点是您可以并且应该检查和修改代码模板!
在本示例中,我们将展示如何在保留 Voyage 嵌入的同时,使用不同的 LLM 来自定义模板,然后重新使用它。我们将改用 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 [ ]
已复制!
from voyage_pack_copy.base import VoyageQueryEnginePack
voyage_pack = VoyageQueryEnginePack(documents)
from voyage_pack_copy.base import VoyageQueryEnginePack voyage_pack = VoyageQueryEnginePack(documents)
In [ ]
已复制!
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.