1
0
Fork 0
This commit is contained in:
t.me/xtekky 2023-04-27 19:32:39 +01:00
parent 011d0babc2
commit ac96278d74
5 changed files with 31 additions and 26 deletions

View file

@ -38,8 +38,13 @@ Please note the following:
| **Copyright** | Copyright information | [![Link to Section](https://img.shields.io/badge/Link-Go%20to%20Section-blue)](#copyright) | - | | **Copyright** | Copyright information | [![Link to Section](https://img.shields.io/badge/Link-Go%20to%20Section-blue)](#copyright) | - |
| **Star History** | Star History | [![Link to Section](https://img.shields.io/badge/Link-Go%20to%20Section-blue)](#star-history) | - | | **Star History** | Star History | [![Link to Section](https://img.shields.io/badge/Link-Go%20to%20Section-blue)](#star-history) | - |
| **Usage Examples** | | | | | **Usage Examples** | | | |
| `theb` | Example usage for theb (gpt-3.5) | [![Link to File](https://img.shields.io/badge/Link-Go%20to%20File-blue)](./theb/README.md) | ![Active](https://img.shields.io/badge/Active-brightgreen) | | |
| `forefront` | Example usage for forefront (gpt-4) | [![Link to File](https://img.shields.io/badge/Link-Go%20to%20File-blue)](./forefront/README.md) | ![Active](https://img.shields.io/badge/Active-brightgreen) | | | | `forefront` | Example usage for forefront (gpt-4) | [![Link to File](https://img.shields.io/badge/Link-Go%20to%20File-blue)](./forefront/README.md) | ![Active](https://img.shields.io/badge/Active-brightgreen) | | |
| `quora (poe)` | Example usage for quora | [![Link to File](https://img.shields.io/badge/Link-Go%20to%20File-blue)](./quora/README.md) | ![Active](https://img.shields.io/badge/Active-brightgreen) | | | `quora (poe)` | Example usage for quora | [![Link to File](https://img.shields.io/badge/Link-Go%20to%20File-blue)](./quora/README.md) | ![Active](https://img.shields.io/badge/Active-brightgreen) | |
| `you` | Example usage for you | [![Link to File](https://img.shields.io/badge/Link-Go%20to%20File-blue)](./you/README.md) | ![Active](https://img.shields.io/badge/Active-brightgreen) | | `you` | Example usage for you | [![Link to File](https://img.shields.io/badge/Link-Go%20to%20File-blue)](./you/README.md) | ![Active](https://img.shields.io/badge/Active-brightgreen) |
| **Try it Out** | | | | | **Try it Out** | | | |
| Google Colab Jupyter Notebook | Example usage for gpt4free | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DanielShemesh/gpt4free-colab/blob/main/gpt4free.ipynb) | - | | Google Colab Jupyter Notebook | Example usage for gpt4free | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DanielShemesh/gpt4free-colab/blob/main/gpt4free.ipynb) | - |

11
theb/README.md Normal file
View file

@ -0,0 +1,11 @@
### Example: `theb` (use like openai pypi package) <a name="example-theb"></a>
```python
# import library
import theb
# simple streaming completion
for token in theb.Completion.create('hello world'):
print(token, end='', flush=True)
```

View file

@ -1,11 +1,9 @@
from re import findall
from json import loads from json import loads
from queue import Queue, Empty from queue import Queue, Empty
from re import findall
from threading import Thread from threading import Thread
from curl_cffi import requests from curl_cffi import requests
class Completion: class Completion:
# experimental # experimental
part1 = '{"role":"assistant","id":"chatcmpl' part1 = '{"role":"assistant","id":"chatcmpl'
@ -16,7 +14,7 @@ class Completion:
message_queue = Queue() message_queue = Queue()
stream_completed = False stream_completed = False
def request(): def request(prompt: str):
headers = { headers = {
'authority': 'chatbot.theb.ai', 'authority': 'chatbot.theb.ai',
'content-type': 'application/json', 'content-type': 'application/json',
@ -25,24 +23,24 @@ class Completion:
} }
requests.post('https://chatbot.theb.ai/api/chat-process', headers=headers, requests.post('https://chatbot.theb.ai/api/chat-process', headers=headers,
content_callback=Completion.handle_stream_response, content_callback = Completion.handle_stream_response,
json={ json = {
'prompt': 'hello world', 'prompt': prompt,
'options': {} 'options': {}
} }
) )
Completion.stream_completed = True Completion.stream_completed = True
@staticmethod @staticmethod
def create(): def create(prompt: str):
Thread(target=Completion.request).start() Thread(target=Completion.request, args=[prompt]).start()
while Completion.stream_completed != True or not Completion.message_queue.empty(): while Completion.stream_completed != True or not Completion.message_queue.empty():
try: try:
message = Completion.message_queue.get(timeout=0.01) message = Completion.message_queue.get(timeout=0.01)
for message in findall(Completion.regex, message): for message in findall(Completion.regex, message):
yield loads(Completion.part1 + message + Completion.part2) yield loads(Completion.part1 + message + Completion.part2)['delta']
except Empty: except Empty:
pass pass
@ -50,13 +48,3 @@ class Completion:
@staticmethod @staticmethod
def handle_stream_response(response): def handle_stream_response(response):
Completion.message_queue.put(response.decode()) Completion.message_queue.put(response.decode())
def start():
for message in Completion.create():
yield message['delta']
if __name__ == '__main__':
for message in start():
print(message)

4
theb/theb_test.py Normal file
View file

@ -0,0 +1,4 @@
import theb
for token in theb.Completion.create('hello world'):
print(token, end='', flush=True)

View file

@ -1,3 +0,0 @@
https://chatbot.theb.ai/
to do:
- code refractoring