gpt4free还在快速迭代中,有时候安装并不那么顺利。pypi的包还是5.2日的,所以使用pip install gpt4free的方式,无法使用最新的软件包。
解决的方法就是clone到本地:'git clone https://github.com/xtekky/gpt4free'
下载之后在gpt4free目录,使用命令`python -c "import gpt4free"`进行测试
碰到如下几个问题:
1 pip install -r requirements.txt 结果streamlit-chat报错,说装不上。要注释掉第12句。https://github.com/AI-Yash/st-chat/archive/refs/pull/24/head.zip
2 typings 要手工装,motor也要手工装。接触到第4个问题,才知道不需要安装这两个库,只需要修改为`from .typings`
3 一直有提示报错:没有ffmpeg,安装:apt install ffmpeg
4 新的报错,No module named 'info',这个比较麻爪:
````
File "/home/skywalk/py39/lib/python3.9/site-packages/typings/database.py", line 2, in
from info import DATABASE_NAME, DATABASE_URI, IMDB, IMDB_TEMPLATE, MELCOW_NEW_USERS, P_TTI_SHOW_OFF, SINGLE_BUTTON, SPELL_CHECK_REPLY, PROTECT_CONTENT
ModuleNotFoundError: No module named 'info'
```
解决的方法:https://github.com/xtekky/gpt4free/pull/620
将 gpt4free/gpt4free/aicolors/__init__.py 文件中的from typings import AiColorsResponse 修改为from .typings import AiColorsResponse
自此就安装好了,测试通过!
注意,由于chatgpt的策略问题,国内可能无法使用gpt4free,在国外用即可。
import gpt4free
from gpt4free import Provider, quora, forefront
# usage You
response = gpt4free.Completion.create(Provider.You, prompt='Write a poem on Lionel Messi')
print(response)
# usage Poe
token = quora.Account.create(logging=False)
response = gpt4free.Completion.create(Provider.Poe, prompt='Write a poem on Lionel Messi', token=token, model='ChatGPT')
print(response)
# usage forefront
token = forefront.Account.create(logging=False)
response = gpt4free.Completion.create(
Provider.ForeFront, prompt='Write a poem on Lionel Messi', model='gpt-4', token=token
)
print(response)
print(f'END')
# usage theb
response = gpt4free.Completion.create(Provider.Theb, prompt='Write a poem on Lionel Messi')
print(response)
deepai测试的例子:
for chunk in deepai.Completion.create("Who are you?"):
print(chunk, end="", flush=True)
print()
或者:
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}
]
for chunk in deepai.ChatCompletion.create(messages):
print(chunk, end="", flush=True)
print()