Issue: Langchain ChatOpenAI chat_model.complete error message: 'chatopenai' object has no attribute 'complete'
Labels: name: auto:bug, name: auto:question, name: area: models

I am new to Langchain and OpenAI models. I am creating a custom PDF reading in Python using Langchain ChatOpenAI model to interact with the chat completion endpoint. I keep getting this error message and I don't know what to do. Please help. All suggestions are welcome!

Here is a part of my code where the error message was generated. 

# User Input
        current_prompt = st.session_state.get('user_input', '')

        prompt_placeholder = st.empty()

        # Check if a submission has been made
        if 'submitted' in st.session_state and st.session_state.submitted:
            prompt = prompt_placeholder.text_area("Ask questions about your PDF:", value='', placeholder="Send a message", key="new_user_input")
        else:
            prompt = prompt_placeholder.text_area("Ask questions about your PDF:", value=current_prompt, placeholder="Send a message", key="user_input")

        submit_button = st.button("Submit")

        if submit_button and prompt:
            # Indicate that a submission has been made
            st.session_state.submitted = True

            # Update the last input in session state
            st.session_state.last_input = prompt

            # Process user message
            user_message = HumanMessage(content=prompt)
            st.session_state.chat_history.append(user_message)

            try:
                # Similarity check
                docs = VectorStore.similarity_search(query=prompt, k=3)
                
                # Initialize chat model
                chat_model = ChatOpenAI(model_name="gpt-3.5-turbo")

                # Add a system message to the chat history to define the role
                system_message = SystemMessage(content="You're a helpful assistant")
                st.session_state.chat_history.append(system_message)

                # Get a response from the chat model
                completion_response = chat_model.complete(
                    messages=st.session_state.chat_history,
                    temperature=0.9  # Adjust temperature as needed
                )
                response_content = completion_response.choices[0].message['content']

                # Process AI message using AIMessage
                assistant_message = AIMessage(content=response_content)
                st.session_state.chat_history.append(assistant_message)

                # Load the question-answering chain
                llm = OpenAI(model_name='gpt-3.5-turbo')
                chain = load_qa_chain(llm=llm, chain_type="stuff")

                # Run the question-answering chain with the documents and the user's prompt
                with get_openai_callback() as cb:
                    response = chain.run(input_documents=docs, question=prompt)
                    # This print statement is for debugging purposes
                    print(cb)
                # st.write(response)
                # st.write(docs)

                # Append the QA chain response as an AI message
                qa_response_message = AIMessage(content=response)
                st.session_state.chat_history.append(qa_response_message)
                
            except Exception as e:
                st.error(f"An error occurred: {e}")
            
            # Clear the input after processing
            prompt_placeholder.text_area("Ask questions about your PDF:", value='', placeholder="Send a message", key="pdf_prompt") 

        # Save chat history
        with open(chat_history_file, "wb") as f:
            pickle.dump(st.session_state.chat_history, f)
            
        # Display the entire chat 
        chat_content = ""
        for message in st.session_state.chat_history:
            if isinstance(message, HumanMessage):
                role = "User"  # Set the role manually for HumanMessage
                content = message.content  # Access the content attribute directly
            elif isinstance(message, AIMessage):
                role = "AI"  # Set the role manually for AIMessage
                content = message.content  # Access the content attribute directly
            else:
                # Handle other types of messages or raise an error
                role = "Unknown"
                content = "Unsupported message type"

            chat_content += f"<div style='background-color: #222222; color: white; padding: 10px;'>**{role}:** {content}</div>"
        st.markdown(chat_content, unsafe_allow_html=True)

if __name__ == '__main__':
    main()