Llama_index在ChatGPT模型Python上出现意外的关键字参数错误。

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

Llama_index unexpected keyword argument error on ChatGPT Model Python

问题

我正在测试一些广泛发布的GPT模型,尝试入门,但遇到了一个无法解决的错误。

我正在运行以下代码:

from llama_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
import gradio as gr
import sys
import os

os.environ["OPENAI_API_KEY"] = 'MYKEY'

def construct_index(directory_path):
    max_input_size = 4096
    num_outputs = 512
    max_chunk_overlap = 20
    chunk_size_limit = 600

    prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)

    llm_predictor_gpt = LLMPredictor(llm=OpenAI(temperature=0.7, model_name="text-davinci-003", max_tokens=num_outputs))

    documents = SimpleDirectoryReader(directory_path).load_data()

    index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor_gpt, prompt_helper=prompt_helper)

    index.save_to_disk('index.json')

    return index

def chatbot(input_text):
    index = GPTSimpleVectorIndex.load_from_disk('index.json')
    response = index.query(input_text, response_mode="compact")
    return response.response

iface = gr.Interface(fn=chatbot,
                     inputs=gr.inputs.Textbox(lines=7, label="Enter your text"),
                     outputs="text",
                     title="Custom-trained AI Chatbot")

index = construct_index("salesdocs")
iface.launch(share=False)

而我一直收到这个错误:

  File "C:\Users\Anonymous\anaconda3\lib\site-packages\llama_index\indices\vector_store\base.py", line 58, in __init__
    super().__init__(
TypeError: __init__() got an unexpected keyword argument 'llm_predictor'

在llama索引错误上很难找到很多文档,希望有人能指点我正确的方向。

英文:

I'm testing a couple of the widely published GPT models just trying to get my feet wet and I am running into an error that I cannot solve.

I am running this code:

from llama_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
import gradio as gr
import sys
import os

os.environ["OPENAI_API_KEY"] = 'MYKEY'

def construct_index(directory_path):
    max_input_size = 4096
    num_outputs = 512
    max_chunk_overlap = 20
    chunk_size_limit = 600

    prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)

    llm_predictor_gpt = LLMPredictor(llm=OpenAI(temperature=0.7, model_name="text-davinci-003", max_tokens=num_outputs))

    documents = SimpleDirectoryReader(directory_path).load_data()

    index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor_gpt, prompt_helper=prompt_helper)

    index.save_to_disk('index.json')

    return index

def chatbot(input_text):
    index = GPTSimpleVectorIndex.load_from_disk('index.json')
    response = index.query(input_text, response_mode="compact")
    return response.response




iface = gr.Interface(fn=chatbot,
                     inputs=gr.inputs.Textbox(lines=7, label="Enter your text"),
                     outputs="text",
                     title="Custom-trained AI Chatbot")

index = construct_index("salesdocs")
iface.launch(share=False)

And I keep getting this error

  File "C:\Users\Anonymous\anaconda3\lib\site-packages\llama_index\indices\vector_store\base.py", line 58, in __init__
    super().__init__(
TypeError: __init__() got an unexpected keyword argument 'llm_predictor'

Having a hard time finding much documentation on llamma index errors, hoping someone can point me in the right direction.

答案1

得分: 5

你需要根据这个示例更改代码:LlamaIndex usage pattern

基本上,你需要将这些信息作为ServiceContext发送:

from llama_index import ServiceContext

service_context = ServiceContext.from_defaults(
  llm_predictor=llm_predictor,
  prompt_helper=prompt_helper,
  embed_model=embedding_llm,
)

index = GPTSimpleVectorIndex(nodes, service_context=service_context)

但是,大多数在线教程是旧版本的。所以,你被误导了,我也是。

为了更完整地回答,如果以后需要加载创建的索引,你必须同时发送service_context:

index = GPTSimpleVectorIndex.load_from_disk(
       filename, service_context=service_context
    )

否则,在加载索引文件时会出现错误。

英文:

You need to change the code according to this example: LlamaIndex usage pattern

Basically, you need to send that information as a ServiceContext:

from llama_index import ServiceContext

service_context = ServiceContext.from_defaults(
  llm_predictor=llm_predictor, 
  prompt_helper=prompt_helper,
  embed_model=embedding_llm,
  )
    
index = GPTSimpleVectorIndex(nodes, service_context=service_context)

But the majority of the tutorials online are older versions. So, you were misguided, and me as well.

To complete even more the answer, if you later need to load the created index you must send the service_context as well:

index = GPTSimpleVectorIndex.load_from_disk(
           filename, service_context=service_context
        )

Otherwise, the code will break while loading the index file.

答案2

得分: 0

我也遇到了相同的错误当尝试那个例子时最简单的修复方法是使用默认值来使用 `from_document`:

index = GPTSimpleVectorIndex.from_documents(documents)

但是如果你想设置自己的提示上下文和llm_predictor,那么创建一个上下文并将其传递给 `GPTSimpleVectorIndex.from_document()` 调用:

def construct_index(directory_path):
max_input_size = 4096
num_outputs = 512
max_chunk_overlap = 20
chunk_size_limit = 600

prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)

llm_predictor_gpt = LLMPredictor(llm=OpenAI(temperature=0.7, model_name="text-davinci-003", max_tokens=num_outputs))

documents = SimpleDirectoryReader(directory_path).load_data()

service_context = ServiceContext.from_defaults(
    llm_predictor=llm_predictor, prompt_helper=prompt_helper
)

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

index.save_to_disk('index.json')

return index

考虑使用 `model_name="gpt-3.5-turbo"`,它速度更快并且成本更低。

```python
llm_predictor_gpt = LLMPredictor(llm=ChatOpenAI(temperature=0.7,
   model_name="gpt-3.5-turbo", max_tokens=num_outputs))
英文:

I got the same error When tried that example too. A simplest fix is to use from_document using defaults:

    index = GPTSimpleVectorIndex.from_documents(documents)

But if you want to set your own prompt context and llm_predictor, then create a context and pass it to GPTSimpleVectorIndex.from_document() call:

def construct_index(directory_path):
    max_input_size = 4096
    num_outputs = 512
    max_chunk_overlap = 20
    chunk_size_limit = 600

    prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)

    llm_predictor_gpt = LLMPredictor(llm=OpenAI(temperature=0.7, model_name="text-davinci-003", max_tokens=num_outputs))

    documents = SimpleDirectoryReader(directory_path).load_data()

    # index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor_gpt, prompt_helper=prompt_helper)

    service_context = ServiceContext.from_defaults(
        llm_predictor=llm_predictor, prompt_helper=prompt_helper
    )

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

    index.save_to_disk('index.json')

    return index

Consider using model_name="gpt-3.5-turbo" that is faster and costs less.

llm_predictor_gpt = LLMPredictor(llm=ChatOpenAI(temperature=0.7,
   model_name="gpt-3.5-turbo", max_tokens=num_outputs))

huangapple
  • 本文由 发表于 2023年4月4日 06:27:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924179.html
匿名

发表评论

匿名网友

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

确定