我不明白 prompts 在 llama_index 中是如何工作的。

huangapple go评论110阅读模式
英文:

I don't understand how the prompts work in llama_index

问题

抱歉,以下是翻译好的部分:

我一直在尝试使用LLM查询我的本地目录中的PDF文件,我已经下载了我在本地系统中使用的LLM模型(GPT4All-13B-snoozy.ggmlv3.q4_0.bin),并尝试使用langchain和hugging face的instructor-large模型进行嵌入。我能够设置service_context然后构建索引,但我无法查询,我一直收到关于提示的错误。

> ValueError: 预期的参数 prompt 应为字符串。而找到的是 <class 'llama_index.prompts.base.Prompt'>。如果要在多个提示上运行LLM,请改用 generate

我刚刚开始学习如何使用LLM,希望社区能帮助我...

错误消息部分1

错误消息部分2

from llama_index import VectorStoreIndex, SimpleDirectoryReader
from InstructorEmbedding import INSTRUCTOR
from llama_index import PromptHelper, ServiceContext
from llama_index import LangchainEmbedding
from langchain.chat_models import ChatOpenAI
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.llms import OpenLLM
# from langchain.chat_models.human import HumanInputChatModel
from langchain import PromptTemplate, LLMChain
from langchain.llms import GPT4All
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

documents = SimpleDirectoryReader(r'C:\Users\avish.wagde\Documents\work_avish\LLM_trials\instructor_large').load_data()

model_id = 'hkunlp/instructor-large'

model_path = "..\models\GPT4All-13B-snoozy.ggmlv3.q4_0.bin"

callbacks = [StreamingStdOutCallbackHandler()]

# 详细信息需要传递给回调管理器
llm = GPT4All(model=model_path, callbacks=callbacks, verbose=True)

embed_model = LangchainEmbedding(HuggingFaceEmbeddings(model_name=model_id))

# 定义提示助手
# 设置最大输入大小
max_input_size = 4096
# 设置输出令牌数
num_output = 256
# 设置最大块重叠
max_chunk_overlap = 0.2

prompt_helper = PromptHelper(max_input_size, num_output, max_chunk_overlap)

service_context = ServiceContext.from_defaults(chunk_size=1024, llm_predictor=llm, prompt_helper=prompt_helper, embed_model=embed_model)

index = VectorStoreIndex.from_documents(documents, service_context=service_context)

query_engine = index.as_query_engine()

response = query_engine.query("What is apple's finnacial situation")
print(response)

我一直在阅读库的源代码,就像错误消息所指示的那样,但我找不到问题 😢。

英文:

I have been trying to query a pdf file in my local directory using LLM, I have downloaded the LLM model I'm using in my local system (GPT4All-13B-snoozy.ggmlv3.q4_0.bin) and trying to use langchain and hugging face's instructor-large model for embedding purpose, I was able to set the service_context and then building index but I'm not able to query , I keeping getting this error regarding prompt..

> ValueError: Argument prompt is expected to be a string. Instead found <class 'llama_index.prompts.base.Prompt'>. If you want to run the LLM on multiple prompts, use generate instead.

I'm just starting to learn how to use LLM, hope the community helps me....

error message part1

error message part2

from llama_index import VectorStoreIndex, SimpleDirectoryReader
from InstructorEmbedding import INSTRUCTOR
from llama_index import PromptHelper, ServiceContext
from llama_index import LangchainEmbedding
from langchain.chat_models import ChatOpenAI
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.llms import OpenLLM
# from langchain.chat_models.human import HumanInputChatModel
from langchain import PromptTemplate, LLMChain
from langchain.llms import GPT4All
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

documents = SimpleDirectoryReader(r&#39;C:\Users\avish.wagde\Documents\work_avish\LLM_trials\instructor_large&#39;).load_data()

model_id = &#39;hkunlp/instructor-large&#39;

model_path = &quot;..\models\GPT4All-13B-snoozy.ggmlv3.q4_0.bin&quot;

callbacks = [StreamingStdOutCallbackHandler()]

# Verbose is required to pass to the callback manager
llm = GPT4All(model = model_path, callbacks=callbacks, verbose=True)

embed_model = LangchainEmbedding(HuggingFaceEmbeddings(model_name = model_id))

# define prompt helper
# set maximum input size
max_input_size = 4096
# set number of output tokens
num_output = 256
# set maximum chunk overlap
max_chunk_overlap = 0.2

prompt_helper = PromptHelper(max_input_size, num_output, max_chunk_overlap)

service_context = ServiceContext.from_defaults(chunk_size= 1024, llm_predictor=llm, prompt_helper=prompt_helper, embed_model=embed_model)

index = VectorStoreIndex.from_documents(documents, service_context= service_context)

query_engine = index.as_query_engine()

response = query_engine.query(&quot;What is apple&#39;s finnacial situation&quot;)
print(response)

I have been going through, the source code of the library as the error message guides but I couldn't find the problem😓

答案1

得分: 1

你在这里编写的代码有点老旧/错误。但主要错误是使用llm_predictor=llm设置服务上下文。你可以直接将llm作为关键字参数传递。

在使用最新版本(撰写时的版本为v0.7.22)时,我会像这样重新编写你的服务上下文:

service_context = ServiceContext.from_defaults(
    chunk_size= 1024, 
    llm=llm, # 这里已更新
    prompt_helper=prompt_helper, 
    embed_model=embed_model
)

来源:https://gpt-index.readthedocs.io/en/stable/core_modules/model_modules/llms/usage_custom.html#example-changing-the-underlying-llm

值得注意的是,如果你像这样从langchain传递一个llm,服务上下文会检测到并为你包装成我们的langchain包装器:

from llama_index.llms import LangChainLLM

llm = LangChainLLM(langchain_llm)

这是有用的信息,因为llama-index的其他部分(agents、chat engines等)可能期望输入一个LLM对象,而不会为你包装它。

英文:

The code you have written here is a little old/erroneous. But the main error is the service context setup with llm_predictor=llm. You can just pass the llm in directly as a kwarg.

Using the latest version (v0.7.22 at the time of writing) I would re-write your service context like so:

service_context = ServiceContext.from_defaults(
    chunk_size= 1024, 
    llm=llm, # this is updated
    prompt_helper=prompt_helper, 
    embed_model=embed_model
)

Source: https://gpt-index.readthedocs.io/en/stable/core_modules/model_modules/llms/usage_custom.html#example-changing-the-underlying-llm

For reference, if you pass in an llm from langchain like this, the service context detects this and wraps it with our langchain wrapper for you:

from llama_index.llms import LangChainLLM

llm = LangChainLLM(langchain_llm)

This is useful to know, since other parts of llama-index (agents, chat engines, etc.) my expect an LLM object as the input, and won't wrap it for you.

huangapple
  • 本文由 发表于 2023年8月9日 18:13:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定