-
Notifications
You must be signed in to change notification settings - Fork 1
/
monolyth.py
65 lines (59 loc) · 2.15 KB
/
monolyth.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
import json
import requests
import os
'''
HELPER FUNCTIONS
Mostly for chat history, retrieval.
'''
def append_chat_history(file_path, chat_list):
# Step 2: Open the JSON file and read its content
try:
with open(file_path, 'r') as file:
data = json.load(file) # Load the data as a Python list
except json.JSONDecodeError:
# If the file is empty and causes a JSONDecodeError, start with an empty list
data = []
# Step 3: Append the new dictionary to the list
data.extend(chat_list)
# Step 4: Write the updated list back to the JSON file
with open(file_path, 'w') as file:
json.dump(data, file, indent=4) # Use `indent` for pretty-printing
def get_chat_history(file_path):
try:
with open(file_path, 'r') as file:
history = json.load(file)
except json.JSONDecodeError:
history = []
return history
def monolyth_generator(input_prompt, modelname="soliloquy-l3"):
API = os.getenv('MONOLYTH_API_KEY')
response = requests.post(
url="https://api.monolyth.ai/v1/chat/completions",
headers={
"Authorization": f"Bearer {API}",
},
data=json.dumps({
"model": f"{modelname}",
"messages": input_prompt,
"repeat_penalty": 1,
"temperature" : 0.7,
"max_tokens" : 800,
"stop" : ["<|eot_id|>", "user:" ]
})
).json()
return response['choices'][0]['message']
def chat_loop():
history = get_chat_history("./chat_history/chat.json")
with open('./sysprompt/hatsune_miku.json', 'r', encoding='utf-8') as file:
system_prompt = file.read()
try:
while True:
user_input = input("You: ")
response = monolyth_generator(user_prompt=user_input, system_prompt=system_prompt, chat_history=history[-300:])
print("AI:", response['content'])
# Append current chat to history and file
current_chat = [{"role": "user", "content": user_input}, response]
history.extend(current_chat)
append_chat_history("./chat_history/chat.json", current_chat)
except KeyboardInterrupt:
print("Chat stopped")