-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_chat_with_faker.py
57 lines (43 loc) · 1.6 KB
/
random_chat_with_faker.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
import requests
import random
import logging
import time
from faker import Faker
from datetime import datetime
node_url = "SUBDOMAIN" # "https://NODE_ID.us.gaianet.network/v1/chat/completions"
faker = Faker()
headers = {
"accept": "application/json",
"Content-Type": "application/json"
}
logging.basicConfig(filename='chat_log.txt', level=logging.INFO, format='%(asctime)s - %(message)s')
def log_message(node, message):
logging.info(f"{node}: {message}")
def send_message(node_url, message):
try:
response = requests.post(node_url, json=message, headers=headers)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Failed to get response from API: {e}")
return None
def extract_reply(response):
if response and 'choices' in response:
return response['choices'][0]['message']['content']
return ""
while True:
random_question = faker.sentence(nb_words=10)
message = {
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": random_question}
]
}
question_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
response = send_message(node_url, message)
reply = extract_reply(response)
reply_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_message("Node replied", f"Q ({question_time}): {random_question} A ({reply_time}): {reply}")
print(f"Q ({question_time}): {random_question}\nA ({reply_time}): {reply}")
delay = random.randint(60, 180)
time.sleep(delay)