-
Notifications
You must be signed in to change notification settings - Fork 22
/
demo.py
107 lines (93 loc) · 3.01 KB
/
demo.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import json
import logging
import time
from typing import Optional
import httpx
from uuid import uuid4
from rich.console import Console
from rich.markdown import Markdown
logging.basicConfig(level=logging.INFO)
console = Console()
def prepare_payload(
message,
conversation_id: Optional[str] = None,
parent_message_id: Optional[str] = None,
):
payload = {
"action": "next",
"messages": [
{
"content": {
"content_type": "text",
"parts": [message],
},
"id": str(uuid4()),
"role": "user",
}
],
"model": "text-davinci-002-render",
}
if conversation_id:
payload["conversation_id"] = conversation_id
if parent_message_id:
payload["parent_message_id"] = parent_message_id
else:
payload["parent_message_id"] = str(uuid4())
return payload
def send_message(message, conversation_id=None, parent_message_id=None):
console.print("[bold green underline]You[/bold green underline]:", emoji=":smiley:")
console.print(
message,
emoji=":point_right:",
style="bold blue",
soft_wrap=True,
justify="left",
)
response = None
with console.status(
"[bold green]Waiting for response...",
spinner="dots",
spinner_style="bold green",
) as _status:
response = httpx.post(
"http://localhost:8080/backend-api/conversation",
json=prepare_payload(message, conversation_id, parent_message_id),
timeout=None,
)
logging.info(response)
if response is None:
raise Exception("Response is None")
if response.status_code != 200:
raise Exception("Response status code is not 200")
response_body = response.json()["data"]
markdown = Markdown(response_body["message"]["content"]["parts"][0])
console.print(
"[deep_sky_blue1 underline]ChatGPT Assistant[/deep_sky_blue1 underline]:",
emoji=":robot_face:",
)
console.print(markdown)
console.print("\n")
# print(json.dumps(response_body, indent=2, sort_keys=True))
if response_body["conversation_id"]:
conversation_id = response_body["conversation_id"]
if response_body["message"]["id"]:
parent_message_id = response_body["message"]["id"]
return conversation_id, parent_message_id
if __name__ == "__main__":
conversation_id = None
parent_message_id = None
conversation_id, parent_message_id = send_message(
"Hello, Assistant!", conversation_id, parent_message_id
)
time.sleep(1)
conversation_id, parent_message_id = send_message(
"What is the difference between functional and procedural programming languages?",
conversation_id,
parent_message_id,
)
time.sleep(1)
conversation_id, parent_message_id = send_message(
"Write a program to print the first 10 natural numbers.",
conversation_id,
parent_message_id,
)