|
1 | | -# ChatAIonStream |
| 1 | +# ChatAIStream |
| 2 | +Message broker between YouTube chat stream and ChatGPT. |
| 3 | + |
| 4 | +## The user of this library can |
| 5 | +- pick up massegase from YouTube Chat and generate answer by ChatGPT. |
| 6 | +- easily give role to ChatGPT. |
| 7 | + |
| 8 | +## Hou to install |
| 9 | + |
| 10 | +### Install from PyPI |
| 11 | +- Install package to your environment.<br> |
| 12 | + ```install |
| 13 | + $ pip install chatai-stream |
| 14 | + ``` |
| 15 | + |
| 16 | +### Install from GitHub repository |
| 17 | +- Clone this repository.<br> |
| 18 | + ```clone |
| 19 | + $ clone https://github.com/GeneralYadoc/ChatAIStream.git |
| 20 | + ``` |
| 21 | +- Change directory to the root of the repository.<br> |
| 22 | + ```cd |
| 23 | + $ cd ChatAIStream |
| 24 | + ``` |
| 25 | +- Install package to your environment.<br> |
| 26 | + ```install |
| 27 | + $ pip install . |
| 28 | + ``` |
| 29 | +## How to use |
| 30 | +- [OpenAI API Key](https://www.howtogeek.com/885918/how-to-get-an-openai-api-key/) is necessary to execute following sample. |
| 31 | + |
| 32 | +- Sample codes exist [here](samples/sample.py). |
| 33 | + ``` sample.py |
| 34 | + # To execute this sample, please install streamchat-agent from PyPI as follows. |
| 35 | + # $ pip install streamchat-agent |
| 36 | + import sys |
| 37 | + import time |
| 38 | + import math |
| 39 | + import datetime |
| 40 | + import ChatAIStream as cas |
| 41 | + |
| 42 | + # print sentencce by a character incrementally. |
| 43 | + def print_incremental(st, interval_sec): |
| 44 | + for i in range(len(st)): |
| 45 | + if not running: |
| 46 | + break |
| 47 | + print(f"{st[i]}", end='') |
| 48 | + sys.stdout.flush() |
| 49 | + interruptible_sleep(interval_sec) |
| 50 | + |
| 51 | + # Customized sleep for making available of running flag interruption. |
| 52 | + def interruptible_sleep(time_sec): |
| 53 | + counter = math.floor(time_sec / 0.01) |
| 54 | + frac = time_sec - (counter * 0.01) |
| 55 | + for i in range(counter): |
| 56 | + if not running: |
| 57 | + break |
| 58 | + time.sleep(0.01) |
| 59 | + if not running: |
| 60 | + return |
| 61 | + time.sleep(frac) |
| 62 | + |
| 63 | + # callback for getting answer of ChatGPT |
| 64 | + def answer_cb(user_message, completion): |
| 65 | + print(f"\n[{user_message.extern.author.name} {user_message.extern.datetime}] {user_message.message}\n") |
| 66 | + interruptible_sleep(3) |
| 67 | + time_str = datetime.datetime.now().strftime ('%H:%M:%S') |
| 68 | + message = completion.choices[0]["message"]["content"] |
| 69 | + print(f"[ChatGPT {time_str}] ", end='') |
| 70 | + print_incremental(message, 0.05) |
| 71 | + print("\n") |
| 72 | + interruptible_sleep(5) |
| 73 | + |
| 74 | + running = False |
| 75 | + |
| 76 | + # YouTube Video ID and ChatGPT API Key is given from command line in this example. |
| 77 | + if len(sys.argv) <= 2: |
| 78 | + exit(0) |
| 79 | + |
| 80 | + # Set params of getting messages from stream source. |
| 81 | + stream_params=cas.streamParams(video_id=sys.argv[1]) |
| 82 | + |
| 83 | + # Set params of Chat AI. |
| 84 | + ai_params=cas.aiParams( |
| 85 | + api_key = sys.argv[2], |
| 86 | + system_role = "You are a cheerful assistant who speek English and can get conversation exciting with user.", |
| 87 | + answer_cb = answer_cb |
| 88 | + ) |
| 89 | + |
| 90 | + # Create ChatAIStream instance. |
| 91 | + ai_stream = cas.ChatAIStream(cas.params(stream_params=stream_params, ai_params=ai_params)) |
| 92 | + |
| 93 | + running = True |
| 94 | + |
| 95 | + # Wake up internal thread to get chat messages from stream and ChatGPT answers. |
| 96 | + ai_stream.start() |
| 97 | + |
| 98 | + # Wait any key inputted from keyboad. |
| 99 | + input() |
| 100 | + |
| 101 | + # Turn off runnging flag in order to finish printing fung of dhit sample. |
| 102 | + running=False |
| 103 | + |
| 104 | + # Finish getting ChatGPT answers. |
| 105 | + # Internal thread will stop soon. |
| 106 | + ai_stream.disconnect() |
| 107 | + |
| 108 | + # terminating internal thread. |
| 109 | + ai_stream.join() |
| 110 | + |
| 111 | + del ai_stream |
| 112 | + |
| 113 | + ``` |
| 114 | + |
| 115 | +- Usage of the sample |
| 116 | + ```usage |
| 117 | + $ python3 ./sample2.py VIDEO-ID OpenAI-API-KEY |
| 118 | + ``` |
| 119 | +- Output of the sample<br> |
| 120 | + The outputs of the right window are provided by this sample.<br> |
| 121 | + Left outputs are also available by ChatAIStream. |
| 122 | +  |
| 123 | + |
| 124 | +## Arguments of Constructor |
| 125 | +- ChatAIStream object can be configured with following params given to constructor. |
| 126 | + |
| 127 | + ### steamParams |
| 128 | + | name | description | default | |
| 129 | + |------|------------|---------| |
| 130 | + | video_id | String following after 'v=' in url of target YouTube live | - | |
| 131 | + | get_item_cb | Chat items are thrown to this callback | None | |
| 132 | + | pre_filter_cb | Filter set before internal queue | None | |
| 133 | + | post_filter_cb | Filter set between internal queue and get_item_cb | None | |
| 134 | + | max_queue_size | Max slots of internal queue (0 is no limit) | 1000 | |
| 135 | + | interval_sec | Polling interval of picking up items from YouTube | 0.01 \[sec\] | |
| 136 | + ### aiParams |
| 137 | + |
| 138 | + |
| 139 | + | name | description | default | |
| 140 | + |------|------------|---------| |
| 141 | + | api_key | API Key string of OpenAI | - | |
| 142 | + | system_role | API Key string of OpenAI | "You are a helpful assistant." | |
| 143 | + | ask_cb | user message given to ChatGPT is thrown to this callback | None | |
| 144 | + | max_messages_in_context | Max messages in context given to ChatGPT | 20 | |
| 145 | + | answer_cb | ChatGPT answer is thrown to this callback | None | |
| 146 | + | max_queue_size | Max slots of internal queue (0 is no limit) | 10 | |
| 147 | + | model | Model of AI to be used. | None | |
| 148 | + | max_tokens_per_request | Max number of tokens which can be contained in a request. | 256 | |
| 149 | + | interval_sec | Interval of ChatGPT API call | 20.0 \[sec\] | |
| 150 | +### Notice |
| 151 | +- Please refer [pytchat README](https://github.com/taizan-hokuto/pytchat) to know the type of YouTube Chat item used by get_item_cb, pre_filter_cb and post filter_cb. |
| 152 | +- Stamps in a message and messages consisted by stamps only are removed defaultly even if user doesn't set pre_filter_cb. |
| 153 | +- Default value of interval_sec is 20.0, since free user of OpenAI API can get only 3 completions per minitue. |
| 154 | +- The system role given by user remains ever as the oldest sentence of current context even if the number of messages is reached to the maximum, so ChatGPT doesn't forgot the role while current cunversation. |
| 155 | + |
| 156 | +## Methods |
| 157 | +### start() |
| 158 | +- Start YouTube Chat polling and ChatGPT conversation, then start calling user callbacks asyncronously. |
| 159 | +- No arguments required, nothing returns. |
| 160 | + |
| 161 | +### join() |
| 162 | +- Wait terminating internal threads kicked by start(). |
| 163 | +- No arguments required, nothing returns. |
| 164 | + |
| 165 | +### connect() |
| 166 | +- Start YouTube Chat polling and ChatGPT conversation, then start calling user callbacks syncronously. |
| 167 | +- Lines following the call of the method never executen before terminate of internal threads. |
| 168 | +- No arguments required, nothing returns. |
| 169 | + |
| 170 | +### disconnect() |
| 171 | +- Request to terminate YouTube Chat polling, ChatGPT conversation and calling user callbacks. |
| 172 | +- Internal process will be terminated soon after. |
| 173 | +- No arguments required, nothing returns. |
| 174 | + |
| 175 | +And other [threading.Thread](https://docs.python.org/3/library/threading.html) public pethods are available. |
| 176 | + |
| 177 | +## Callbacks |
| 178 | +### get_item_cb |
| 179 | +- Callback for getting YouTube chat items. |
| 180 | +- You can implement several processes in it. |
| 181 | +- YouTube chat item is thrown as an argument. |
| 182 | +- It's not be assumed that any values are returned. |
| 183 | +### pre_filter_cb |
| 184 | +- pre putting queue filter. |
| 185 | +- YouTube chat item is thrown as an argument. |
| 186 | +- You can edit YouTube chat items before putting internal queue. |
| 187 | +- It's required that edited chat item is returned. |
| 188 | +- You can avoid putting internal queue by returning None. |
| 189 | +### post_filter_cb |
| 190 | +- post getting queue filter |
| 191 | +- You can edit YouTube chat items after popping internal queue. |
| 192 | +- It's required that edited chat item is returned. |
| 193 | +- You can avoid sending item to get_item_cb by returning None. |
| 194 | +### ask_cb |
| 195 | +- Callback for getting questions that actually thrown to ChatGPT. |
| 196 | +- If you register external info to user message when you put it, you can obtain the external info here. |
| 197 | +- It's not be assumed that any values are returned. |
| 198 | +### answer_cb |
| 199 | +- Callback for getting question and answer of ChatGPT |
| 200 | +- The type of completion is mentioned [here](https://platform.openai.com/docs/guides/chat). |
| 201 | +- It's not be assumed that any values are returned. |
| 202 | + |
| 203 | +## Links |
| 204 | +StreamingChaatAgent uses following libraries internally. |
| 205 | + |
| 206 | +- [streamchat-agent](https://github.com/GeneralYadoc/StreamChatAgent)<br> |
| 207 | + YouTube chat poller which can get massages very smothly by using internal queue. |
| 208 | +- [chatai-agent](https://github.com/GeneralYadoc/ChatAIAgent)<br> |
| 209 | + Message broker between user and ChatGPT. |
0 commit comments