generated from box-community/box-python-oauth-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample_text_gen_streamed.py
71 lines (49 loc) · 1.65 KB
/
sample_text_gen_streamed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import logging
from time import sleep
from InquirerPy import inquirer
from app.box_ai import AI, AIItem
from app.config import AppConfig
from app.box_client import get_client
from app.prompts import get_manual_context, select_file
logging.basicConfig(level=logging.INFO)
logging.getLogger("boxsdk").setLevel(logging.CRITICAL)
conf = AppConfig()
def main():
"""
Simple script to demonstrate how to use the Box SDK
with oAuth2 authentication
"""
client = get_client(conf)
print("\n---------------------------------")
print("AI Ask Demo - Text Gen - Streamed")
print("---------------------------------")
box_ai = AI(client)
dialogue_history = []
file_selection = select_file(client)
context = get_manual_context("Enter additional file description if any:")
item = AIItem(
item_id=file_selection.item_id,
item_type=file_selection.item_type,
optional_document_content=context,
)
while True:
prompt = inquirer.text(
message="What would you like to talk about?\n" + " (type stop to exit or restart to start again)\n:"
).execute()
if prompt == "stop":
break
if prompt == "restart":
main()
answers = box_ai.ask_text_gen_streamed(
prompt=prompt,
item=item,
dialogue_history=dialogue_history,
)
for answer in answers:
print(f"{answer.answer}", end="")
sleep(0.1) # just for effect ;)
dialogue_history.append(answer)
if answer.completion_reason == "done":
print("\n")
if __name__ == "__main__":
main()