cleverbotfree
1.0.0
Cleverbot.com 曾經為其聊天機器人應用程式提供免費的 API。他們有
刪除了免費 API,取而代之的是分層訂閱 API 服務。
smartbotfree 是使用無頭 Firefox 的 API 的免費替代品
瀏覽器與聊天機器人應用程式進行通訊。您可以使用這個模組
建立向 Cleverbot 發送和接收訊息的應用程式/機器人
聊天機器人應用程式。
滿足要求後,您可以透過 pip 安裝該庫。
pip install cleverbotfree
該庫使用 Playwright 庫來連接 Cleverbot 網站
使用無頭 Firefox 瀏覽器。
要下載 Playwright Firefox 瀏覽器二進位文件,只需在
安裝 smartbotfree:
playwright install firefox
範例
建立持久聊天會話直到關閉的簡單 CLI 腳本範例。
import asyncio
import cleverbotfree
def chat ():
"""Example code using cleverbotfree sync api."""
with cleverbotfree . sync_playwright () as p_w :
c_b = cleverbotfree . Cleverbot ( p_w )
while True :
user_input = input ( "User: " )
if user_input == 'quit' :
break
bot = c_b . single_exchange ( user_input )
print ( 'Cleverbot:' , bot )
c_b . close ()
chat ()
async def async_chat ():
"""Example code using cleverbotfree async api."""
async with cleverbotfree . async_playwright () as p_w :
c_b = await cleverbotfree . CleverbotAsync ( p_w )
while True :
user_input = input ( "User: " )
if user_input == 'quit' :
break
bot = await c_b . single_exchange ( user_input )
print ( 'Cleverbot:' , bot )
await c_b . close ()
asyncio . run ( async_chat ())
使用類別裝飾器的簡單 CLI 腳本範例。
import asyncio
from cleverbotfree import CleverbotAsync
from cleverbotfree import Cleverbot
@ Cleverbot . connect
def chat ( bot , user_prompt , bot_prompt ):
"""Example code using cleverbotfree sync api with decorator."""
while True :
user_input = input ( user_prompt )
if user_input == "quit" :
break
reply = bot . single_exchange ( user_input )
print ( bot_prompt , reply )
bot . close ()
chat ( "User: " , "Cleverbot:" )
@ CleverbotAsync . connect
async def async_chat ( bot , user_prompt , bot_prompt ):
"""Example code using cleverbotfree async api with decorator."""
while True :
user_input = input ( user_prompt )
if user_input == "quit" :
break
reply = await bot . single_exchange ( user_input )
print ( bot_prompt , reply )
await bot . close ()
asyncio . run ( async_chat ( "User: " , "Cleverbot:" ))