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

t25*_*t25 8 python openai-api gpt-3 llama-index

我正在测试几个广泛发布的 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)
Run Code Online (Sandbox Code Playgroud)

我不断收到此错误

  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'
Run Code Online (Sandbox Code Playgroud)

很难找到很多关于 llamma 索引错误的文档,希望有人能给我指出正确的方向。

Joe*_*iro 5

您需要根据此示例更改代码:LlamaIndex 使用模式

基本上,您需要将该信息作为 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)
Run Code Online (Sandbox Code Playgroud)

但网上的大部分教程都是旧版本。所以,你被误导了,我也是。

为了完成更多答案,如果您稍后需要加载创建的索引,您还必须发送 service_context:

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)
Run Code Online (Sandbox Code Playgroud)

否则,代码将在加载索引文件时中断。

  • 即使是 [llama_index README](https://github.com/jerryjliu/llama_index/blob/046183303da4161ee027026becf25fb48b67a3d2/docs/how_to/custom_llms.md#example-using-a-custom-llm-model) 在这方面也已经过时了 (3认同)