gpt4 book ai didi

python - ChatGPT API 定制训练的 AI 聊天机器人对 Python 查询应答 "None"

转载 作者:行者123 更新时间:2023-12-02 05:49:37 25 4
gpt4 key购买 nike

我正在连接到我的第一个聊天机器人。基于此处概述的流程: https://beebom.com/how-train-ai-chatbot-custom-knowledge-base-chatgpt-api/

我创建了他建议的代码来让 ChatGPT 分析我的 PDF。不过代码有点过时了,我必须做一些调整。这就是我现在拥有的:

from llama_index import *
from langchain.chat_models import ChatOpenAI
import gradio as gr
import sys
import os
import openai

os.environ["OPENAI_API_KEY"] = 'XXXX'
openai.api_key = "XXXX"

documents = ""
service_context = ""

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, chunk_overlap_ratio=0.1, chunk_size_limit=chunk_size_limit)
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
documents = SimpleDirectoryReader(directory_path).load_data()
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)

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

# apparently this saves it to disk?
index.storage_context.persist(persist_dir='docs')
storage_context = StorageContext.from_defaults(persist_dir='docs')
index = load_index_from_storage(storage_context)
return index

def chatbot(input_text):
index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context)
index.storage_context.persist(persist_dir='docs')
storage_context = StorageContext.from_defaults(persist_dir='docs')
index = load_index_from_storage(storage_context)

# tried this method as well with no success instead of above
#index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context)

query_engine = index.as_query_engine()
response = query_engine.query(input_text)

# am I returning the correct object here? I believe its supposed to be JSON?
return response

iface = gr.Interface(fn=chatbot,
inputs=gr.components.Textbox(lines=7, label="Enter your text"),
outputs="text",
title="Custom-trained AI Chatbot")
index = construct_index("docs")
iface.launch(share=True)

当我运行该程序时,没有错误,它说它在我的 Ip 上运行。当我到达聊天机器人时,一切看起来都很好,直到我提出问题。然后它一直说“无”

enter image description here

控制台中没有错误或警告,程序继续运行。每当我查询它时,它总是说“无”。我哪里出错了?顺便说一句,我并不是 100% 理解代码,这是对原始示例的大量修改,以使所有库都能正常工作。如果有人可以简单地解释发生了什么,我们将不胜感激。谢谢G

最佳答案

Chatbot 函数返回 response=None,它只是在输出文本框组件中收集。我建议在 iface.launch() 中使用 debug=True。更多信息可以在这里找到 - https://www.gradio.app/docs/blocks#blocks-launch-arguments

关于python - ChatGPT API 定制训练的 AI 聊天机器人对 Python 查询应答 "None",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76989885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com