You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于text-davinci-003的PDF问答Chatbot因模型弃用报错求替代方案

解决text-davinci-003弃用导致的PDF问答Chatbot错误

原代码依赖的text-davinci-003模型已被OpenAI官方弃用,触发报错openai.error.InvalidRequestError: The model text-davinci-003 has been deprecated。只需修改LLM实例化部分,指定当前受支持的模型即可解决问题。

推荐使用gpt-3.5-turbo-instruct作为替代模型,它是OpenAI推出的兼容补全接口的模型,完全适配原有QA链的逻辑。

修改后的完整代码:

from dotenv import load_dotenv
import streamlit as st
from PyPDF2 import PdfReader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI


def main():
    load_dotenv()
    st.set_page_config(page_title="Ask your pdf", layout="centered",initial_sidebar_state="auto")
    st.header("Ask your pdf(OpenAI) 🤓")
    
    # Uploading the file
    pdf = st.file_uploader("Upload your pdf", type="pdf")
    
    # Extracting the text
    if pdf is not None:
        pdf_reader = PdfReader(pdf)
        text = ""
        for page in pdf_reader.pages:
            text += page.extract_text()

        # Split into chunks 
        text_splitter = CharacterTextSplitter(
            separator="\n", # Defines a new line 
            chunk_size = 1000,
            chunk_overlap = 200,
            length_function = len
        )
        chunks = text_splitter.split_text(text)

        # Create embeddings
        embeddings = OpenAIEmbeddings()

        # Creating an object on which we will be able to search FAISS
        knowledge_base = FAISS.from_texts(chunks, embeddings)

        # show user input
        user_question = st.text_input("Ask a question about the PDF: ")

        if st.button("Refresh Page"):
            st.caching.clear_cache()
            
        if user_question:
            docs = knowledge_base.similarity_search(user_question)

            # 修改点:指定受支持的模型gpt-3.5-turbo-instruct
            llm = OpenAI(model="gpt-3.5-turbo-instruct", temperature=0.7)
            chain = load_qa_chain(llm, chain_type="stuff")
            response = chain.run(input_documents=docs, question = user_question)

            st.write(response)

if __name__ == '__main__':
    main()

关键修改说明

  • 实例化OpenAI类时,通过model参数明确指定gpt-3.5-turbo-instruct模型,替代已弃用的text-davinci-003。
  • 可选调整temperature参数控制回答的随机性,默认值为0.7,可根据需求修改。

内容的提问来源于stack exchange,提问作者Dismay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 11:32:40