Skip to content

Commit

Permalink
Merge pull request xtekky#335 from sagadav/main
Browse files Browse the repository at this point in the history
Typo correction in forefront README, theb update
  • Loading branch information
xtekky authored May 1, 2023
2 parents 72dbed8 + fb05440 commit a13c00f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
9 changes: 6 additions & 3 deletions gpt4free/forefront/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ from gpt4free import forefront
token = forefront.Account.create(logging=False)
print(token)
# get a response
for response in forefront.StreamingCompletion.create(token=token,
prompt='hello world', model='gpt-4'):
print(response.completion.choices[0].text, end='')
for response in forefront.StreamingCompletion.create(
token=token,
prompt='hello world',
model='gpt-4'
):
print(response.choices[0].text, end='')
print("")
```
9 changes: 6 additions & 3 deletions gpt4free/theb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from gpt4free import theb

# simple streaming completion
for token in theb.Completion.create('hello world'):
print(token, end='', flush=True)
print("")

while True:
x = input()
for token in theb.Completion.create(x):
print(token, end='', flush=True)
print("")
```
14 changes: 11 additions & 3 deletions gpt4free/theb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Completion:
timer = None
message_queue = Queue()
stream_completed = False
last_msg_id = None

@staticmethod
def request(prompt: str, proxy: Optional[str]=None):
Expand All @@ -28,26 +29,33 @@ def request(prompt: str, proxy: Optional[str]=None):
}

proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else None


options = {}
if Completion.last_msg_id:
options['parentMessageId'] = Completion.last_msg_id

requests.post(
'https://chatbot.theb.ai/api/chat-process',
headers=headers,
proxies=proxies,
content_callback=Completion.handle_stream_response,
json={'prompt': prompt, 'options': {}},
json={'prompt': prompt, 'options': options},
)

Completion.stream_completed = True

@staticmethod
def create(prompt: str, proxy: Optional[str]=None) -> Generator[str, None, None]:
Completion.stream_completed = False
Thread(target=Completion.request, args=[prompt, proxy]).start()

while not Completion.stream_completed or not Completion.message_queue.empty():
try:
message = Completion.message_queue.get(timeout=0.01)
for message in findall(Completion.regex, message):
yield loads(Completion.part1 + message + Completion.part2)['delta']
message_json = loads(Completion.part1 + message + Completion.part2)
Completion.last_msg_id = message_json['id']
yield message_json['delta']

except Empty:
pass
Expand Down

0 comments on commit a13c00f

Please sign in to comment.