Skip to main content

No project description provided

Project description

GPT 聊天程式碼庫 (絕對繁體中文、串流逐步回應)

  • 這是一個使用 OpenAI API 的 GPT 聊天程式碼庫,可以讓你輕鬆地與 GPT 進行對話
  • 支持 OpenAI 的 gpt-3.5-turbo 或 gpt-4 模型(可抽換),並且可以用作命令列界面或 Python 函式庫
  • 支援全自動簡體中文轉繁體中文(採用opencc)
  • 支援串流回應,用queue方式得到串流回應,回應的字可以一個一個跑出來
  • CLI可測試使用
  • import套件方式也可以運用

安裝

您可以使用以下命令快速安裝此程式碼庫:

python3 -m pip install openai-chat-thread

安裝後,您需要設定OPENAI_API_KEY環境變數。一種簡單的方法是在您的主目錄中創建一個.env檔案,並在其中添加以下行:

OPENAI_API_KEY=your_api_key_here

記得用您自己的 API 金鑰替換your_api_key_here

命令列界面

安裝後,您可以在命令列中使用openai_chat_thread命令。請使用以下命令查看可用選項:

python3 -m openai_chat_thread --help

要與 GPT 進行對話,只需提供一個提示(prompt),例如:

python3 -m openai_chat_thread "你好,GPT!"

然後 GPT 將回應您的提示。

函式庫用法

您也可以在 Python 程式中導入此庫並使用它。只需導入openai_chat_thread函數,並提供一個提示(prompt):

from openai_chat_thread import openai_chat_thread_taiwan, openai_chat_thread
q = openai_chat_thread_taiwan('妳好', model='gpt-4') # 若改用 openai_chat_thread 就不會自動轉繁體中文 
while True:
    response = q.get()
    if response is None:
        break
    print(response, end="", flush=True)

openai_chat_thread_taiwan函數將返回一個queue.Queue對象,您可以使用它來讀取 GPT 的回應。

運作原理

該程式碼庫使用 OpenAI 提供的 GPT 模型(可參數抽換模型)。當您提供一個提示(prompt)時,該程序將向 GPT 模型發送請求,並將其回應返回給您。 為了實現實時回應,我們在後台啟動一個新的執行緒,並在其中與 GPT 模型進行通信。這意味著您可以在等待 GPT 的回應時繼續執行其他任務。 該程式碼庫使用 Python 的標準庫queuethreading來實現多執行緒和通信。這使得程式碼庫易於使用,而無需依賴額外的第三方庫。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openai-chat-thread-1.1.1.tar.gz (3.2 kB view hashes)

Uploaded Source

Built Distribution

openai_chat_thread-1.1.1-py3-none-any.whl (3.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page