gpt4free 0.0.1.4版本安装指南

gpt4free很牛,用过的都知道!gpt4free已经升级到0.0.1.4版本,使用方法跟以前略有区别。首先新版本的gpt4free只支持python3.10以上的版本,所以重新安装了python3.10环境。

刚开始用源代码编译编译安装,但是走了弯路,最后用Miniconda包安装,才最终成功。以下就是安装和使用中碰到的问题记录。

pip安装:

pip install g4f

使用的时候报错:

    from google.protobuf.internal import type_checkers
  File "/home/skywalk/py10/lib/python3.10/site-packages/google/protobuf/internal/type_checkers.py", line 48, in
    import ctypes
  File "/home/skywalk/py310/lib/python3.10/ctypes/__init__.py", line 8, in
    from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'

把protobuf版本降低到3.20 ,报错:

  File "/home/skywalk/py10/lib/python3.10/site-packages/pandas/compat/compressors.py", line 7, in
    import bz2
  File "/home/skywalk/py310/lib/python3.10/bz2.py", line 17, in
    from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'

把pandas的版本升级到2.1.0rc0 ,结果又出现了ctypes报错;
  File "/home/skywalk/py10/lib/python3.10/site-packages/pandas/errors/__init__.py", line 6, in
    import ctypes
  File "/home/skywalk/py310/lib/python3.10/ctypes/__init__.py", line 8, in
    from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'

很明显很多库新版本用ctypes都有点问题啊!

最后实在搞不定,装3.10版本Miniconda环境,问题解决。

现在有了新的报错:

raise OSError(errno.ENOSPC, "inotify watch limit reached")
OSError: [Errno 28] inotify watch limit reached

这个报错是系统报错,暂时问题不大,先不管它。

总结下新版本gpt4free一个streamlit网站的步骤:

安装gpt4free pip install g4f

安装相关库:进入gpt4free目录,pip install -r requirements.txt

安装streamlit库:pip install streamlit 

安装js2py库:pip install js2py 

写app_streamlit0.1.4.py 文件:

mport os
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir))
import g4f
import streamlit as st
from g4f.Provider import DeepAi
def get_answer(question: str) -> str:
    # Set cloudflare clearance cookie and get answer from GPT-4 model
    try:
        tmp = ""
        response = g4f.ChatCompletion.create(model='gpt-3.5-turbo', provider=DeepAi, messages=[
                                                 {"role": "user", "content": question }], stream=True)
        for message in response:
                #print(message)
            tmp += message
            status.text(tmp)
        return tmp


    except Exception as e:
        # Return error message if an exception occurs
        return (
            f'An error occurred: {e}. Please make sure you are using a valid cloudflare clearance token and user agent.'
        )


# Set page configuration and add header
st.set_page_config(
    page_title="gpt4freeGUI",
    initial_sidebar_state="expanded",
    page_icon="",
    menu_items={
        'Get Help': 'https://github.com/xtekky/gpt4free/blob/main/README.md',
        'Report a bug': "https://github.com/xtekky/gpt4free/issues",
        'About': "### gptfree GUI",
    },
)
st.header('GPT4free GUI')

status = st.empty()

# Add text area for user input and button to get answer
question_text_area = st.text_area(' Ask Any Question :', placeholder='Explain quantum computing in 50 words')
if st.button(' Think'):
    answer = get_answer(question_text_area)

    # escaped = answer.encode('utf-8').decode('unicode-escape')
    escaped = answer
    # Display answer
    st.caption("Answer :")
    st.markdown(escaped)

# Hide Streamlit footer
hide_streamlit_style = """
            
            """
st.markdown(hide_streamlit_style, unsafe_allow_html=True)

启动服务:streamlit run app_streamlit0.1.4.py 

2023.10.17日补充,现在已经更新到0.1.8.3版本,已经不需要自己写代码了,直接一键启动即可,具体见新文章:https://blog.csdn.net/skywalk8163/article/details/134456740

另有时候pip 镜像更新不太好用的时候,可以使用官方地址,比如`pip install g4f -U -i https://pypi.Python.org/simple/`

相关信息:

以前版本的安装文档:gpt4free安装记录_skywalk8163的博客-CSDN博客

gpt4free需要国外的ip地址,因此使用起来也有不方便的地方。大家可以尝试下用ChatGLM进行替代,全国产、开源,可以本地部署,具体参考这个项目:人人都有大模型用!大模型ChatGLM2-6B新手速通! - 飞桨AI Studio

你可能感兴趣的:(chatgpt,python)