gpt4 book ai didi

python - LlamaIndex 与 ChatGPT 检索答案的时间过长

转载 作者:行者123 更新时间:2023-12-02 05:48:11 64 4
gpt4 key购买 nike

我目前正在为我们的网站开发一个聊天机器人,该机器人使用 LlamaIndex 和 chatGPT 提供领域知识。我们的聊天机器人使用大约 50 个文档,每个文档长约 1-2 页,包含来 self 们网站的教程和其他信息。虽然我得到的答案很好,但性能很慢。平均而言,检索答案大约需要 15-20 秒,这对于我们的网站来说不切实际。

我已经按照文档中的建议尝试使用优化器,但没有看到太大的改进。目前,我正在使用 GPTSimpleVectorIndex,尚未测试其他索引。我尝试在不同的机器上运行该机器人,但没有看到性能的显着提高,因此我认为这不是硬件限制。

我正在寻找有关如何提高机器人性能的建议,以便它能够更快地提供答案。

谢谢!

代码:

import os
import sys
import streamlit as st
from llama_index import (LLMPredictor, GPTSimpleVectorIndex,
SimpleDirectoryReader, PromptHelper, ServiceContext)
from langchain import OpenAI

os.environ["OPENAI_API_KEY"] = ...
retrain = sys.argv[1]
doc_path = 'docs'
index_file = 'index.json'
st.title("Chatbot")

def ask_ai():
st.session_state.response = index.query(st.session_state.prompt)

if retrain:
documents = SimpleDirectoryReader(doc_path).load_data()
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0, model_name="text-davinci-003", max_tokens = 128))
num_output = 256
max_chunk_overlap = 20
max_input_size = 4096
prompt_helper = PromptHelper(max_input_size, num_output, max_chunk_overlap)
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_file)

if 'response' not in st.session_state:
st.session_state.response = ''

elif os.path.exists(index_file):
index = GPTSimpleVectorIndex.load_from_disk(index_file)

if index != None:
st.text_input("Ask something: ", key='prompt')
st.button("Send", on_click=ask_ai)
if st.session_state.response:
st.subheader("Response: ")
st.success(st.session_state.response)

最佳答案

Streamlit 默认情况下是无状态的。这意味着,如果您设置重新训练参数,它将运行整个过程,包括文档加载和索引每个交互。如果您希望它仅在启动时重新索引,则需要将 retrain arg 添加到 st.session_state,然后在 retrain 函数末尾将其设置为 false。

关于python - LlamaIndex 与 ChatGPT 检索答案的时间过长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76101760/

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