Skip to content

Commit 3f00387

Browse files
committed
tested, debugged, nice
1 parent 4dd1930 commit 3f00387

File tree

5 files changed

+166
-87
lines changed

5 files changed

+166
-87
lines changed

interpreter/core/core.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ def __init__(
7373
skills_path=None,
7474
import_skills=False,
7575
multi_line=False,
76-
contributing_future_conversations=None,
77-
contribute_current_conversation=False
76+
contribute_conversation=False
7877
):
7978
# State
8079
self.messages = [] if messages is None else messages
@@ -125,8 +124,7 @@ def __init__(
125124

126125
self.computer.import_skills = import_skills
127126

128-
self.contributing_future_conversations = contributing_future_conversations
129-
self.contribute_current_conversation = contribute_current_conversation
127+
self.contribute_conversation = contribute_conversation
130128

131129
def server(self, *args, **kwargs):
132130
server(self, *args, **kwargs)
@@ -141,6 +139,12 @@ def wait(self):
141139
def anonymous_telemetry(self) -> bool:
142140
return not self.disable_telemetry and not self.offline
143141

142+
@property
143+
def will_contribute(self):
144+
contribute_session = self.contribute_conversation
145+
overrides = self.offline or not self.conversation_history or self.disable_telemetry
146+
return contribute_session and not overrides
147+
144148
def chat(self, message=None, display=True, stream=False, blocking=True):
145149
try:
146150
self.responding = True

interpreter/core/utils/telemetry.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,3 @@ def send_telemetry(event_name, properties=None):
6363
pass
6464

6565

66-
def contribute_conversations(conversations):
67-
url = "https://api.openinterpreter.com/v0/conversations/contribute/"
68-
version = pkg_resources.get_distribution("open-interpreter").version
69-
70-
if conversations and len(conversations) > 1:
71-
payload = {
72-
"conversations": [conv for sublist in conversations for conv in sublist],
73-
"oi_version": version
74-
}
75-
else:
76-
payload = {
77-
"conversations": [conversations[0]],
78-
"oi_version": version
79-
}
80-
try:
81-
response = requests.post(url, json=payload)
82-
if response.status_code != 200:
83-
print(f"Failed to contribute conversation: {response.status_code} {response.text}")
84-
return None
85-
else:
86-
print(f"Successfully contributed conversation!")
87-
except requests.RequestException as e:
88-
print(f"Failed to contribute conversation: {e}")
89-
return None
Lines changed: 125 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,178 @@
11
import os
22
import json
3+
import time
4+
from typing import List, TypedDict
5+
6+
import pkg_resources
7+
import requests
8+
9+
from interpreter.terminal_interface.profiles.profiles import write_key_to_profile
10+
from interpreter.terminal_interface.utils.display_markdown_message import display_markdown_message
311

412

513
contribute_cache_path = os.path.join(
614
os.path.expanduser("~"), ".cache", "open-interpreter", "contribute.json"
715
)
816

917

10-
def ask_user_to_run_contribute():
11-
print("---")
12-
print("not contributing current")
13-
print("Run --contribute_conversation to contribute the current conversation!")
14-
print()
18+
def display_contribution_message():
19+
display_markdown_message(
20+
"""
21+
---
22+
> We're training an open-source language model!
23+
24+
You can help us train it with your past, current, or future conversations by:
25+
1. Closing out of OpenInterpreter,
26+
2. Running `interpreter --contribute_conversation`.
27+
"""
28+
)
29+
time.sleep(1)
30+
31+
32+
def display_contributing_current_message():
33+
display_markdown_message(
34+
f"""
35+
---
36+
> This conversation will be used to train OpenInterpreter's language model.
37+
"""
38+
)
1539

1640

1741
def send_past_conversations(interpreter):
18-
print("sending all past conversations!")
42+
past_conversations = get_all_conversations(interpreter)
43+
if len(past_conversations) > 0:
44+
print()
45+
print("Sending all previous conversations to OpenInterpreter...")
46+
contribute_conversations(past_conversations)
47+
print()
1948

2049

2150
def set_send_future_conversations(interpreter, should_send_future):
22-
if should_send_future:
23-
print("sending!")
24-
else:
25-
print("not sending!")
51+
write_key_to_profile("contribute_conversation", should_send_future)
52+
display_markdown_message(
53+
"""
54+
> OpenInterpreter will contribute all your conversations from now on.
55+
56+
To change this, consult the documentation at [https://unassuminglink.com](https://www.readthefuckingmanual.com).
57+
"""
58+
)
2659

2760

28-
def ask_user_to_contribute_past():
29-
print("do you want to contribute all past conversations?")
61+
def user_wants_to_contribute_past():
62+
print("Would you like to contribute all past conversations?")
3063
response = input("(y/n) ")
3164
return response.lower() == "y"
3265

3366

34-
def ask_user_to_contribute_future():
35-
print("do you want to contribute all future conversations?")
67+
def user_wants_to_contribute_future():
68+
print("Would you like to contribute all future conversations?")
3669
response = input("(y/n) ")
3770
return response.lower() == "y"
3871

3972

4073
def contribute_conversation_launch_logic(interpreter):
4174
contribution_cache = get_contribute_cache_contents()
42-
displayed_contribution_message = contribution_cache["asked_to_run_contribute"]
43-
contribute_current = interpreter.contribute_conversation
4475

45-
if displayed_contribution_message:
46-
if contribute_current:
47-
# second launch, --contribute-conversation.
48-
contribute_past_and_future_logic(interpreter, contribution_cache)
49-
else:
50-
# second launch, no --contribute-conversation.
51-
# continue launching as normal!
52-
# no need to modify contribution_cache because we're not asking about
53-
# past conversations and we've already displayed the contribution message.
54-
return
55-
else:
56-
if contribute_current:
57-
# first launch, --contribute-conversation.
58-
contribute_past_and_future_logic(interpreter, contribution_cache)
59-
else:
60-
# first launch, no --contribute-conversation.
61-
ask_user_to_run_contribute()
62-
contribution_cache["asked_to_run_contribute"] = True
76+
if interpreter.will_contribute:
77+
contribute_past_and_future_logic(interpreter, contribution_cache)
78+
elif not contribution_cache["displayed_contribution_message"]:
79+
display_contribution_message()
6380

81+
# don't show the contribution message again no matter what.
82+
contribution_cache["displayed_contribution_message"] = True
6483
write_to_contribution_cache(contribution_cache)
6584

6685

86+
class ContributionCache(TypedDict):
87+
displayed_contribution_message: bool
88+
asked_to_contribute_past: bool
89+
asked_to_contribute_future: bool
90+
91+
6792
# modifies the contribution cache!
68-
def contribute_past_and_future_logic(interpreter, contribution_cache):
93+
def contribute_past_and_future_logic(
94+
interpreter, contribution_cache: ContributionCache
95+
):
6996
if not contribution_cache["asked_to_contribute_past"]:
70-
contribute_past = ask_user_to_contribute_past()
71-
if contribute_past:
97+
if user_wants_to_contribute_past():
7298
send_past_conversations(interpreter)
99+
contribution_cache["asked_to_contribute_past"] = True
73100

74-
# set asked_to_contribute_past to True no matter what!
75-
contribution_cache["asked_to_contribute_past"] = True
76-
77-
contributing_future = interpreter.contributing_future_conversations
78-
if not contributing_future:
79-
contribute_future = ask_user_to_contribute_future()
80-
if contribute_future:
101+
if not contribution_cache["asked_to_contribute_future"]:
102+
if user_wants_to_contribute_future():
81103
set_send_future_conversations(interpreter, True)
82-
else:
83-
set_send_future_conversations(interpreter, False)
104+
contribution_cache["asked_to_contribute_future"] = True
84105

106+
display_contributing_current_message()
85107

86-
# Returns a {"asked_to_run_contribute": bool, "asked_to_contribute_past": bool}.
108+
109+
# Returns a {"asked_to_run_contribute": bool, "asked_to_contribute_past": bool}
110+
# as the first part of its Tuple, a bool as a second.
87111
# Writes the contribution cache file if it doesn't already exist.
88-
def get_contribute_cache_contents():
112+
# The bool is True if the file does not already exist, False if it does.
113+
def get_contribute_cache_contents() -> ContributionCache:
89114
if not os.path.exists(contribute_cache_path):
90-
default_dict = {
115+
default_dict: ContributionCache = {
91116
"asked_to_contribute_past": False,
92-
"asked_to_run_contribute": False,
117+
"displayed_contribution_message": False,
118+
"asked_to_contribute_future": False,
93119
}
94120
with open(contribute_cache_path, "a") as file:
95121
file.write(json.dumps(default_dict))
96122
return default_dict
97-
98-
with open(contribute_cache_path, "r") as file:
99-
contribute_cache = json.load(file)
100-
return contribute_cache
123+
else:
124+
with open(contribute_cache_path, "r") as file:
125+
contribute_cache = json.load(file)
126+
return contribute_cache
101127

102128

103129
# Takes in a {"asked_to_run_contribute": bool, "asked_to_contribute_past": bool}
104-
def write_to_contribution_cache(contribution_cache):
130+
def write_to_contribution_cache(contribution_cache: ContributionCache):
105131
with open(contribute_cache_path, "w") as file:
106132
json.dump(contribution_cache, file)
107133

108134

135+
def get_all_conversations(interpreter) -> List[List]:
136+
def is_conversation_path(path: str):
137+
_, ext = os.path.splitext(path)
138+
return ext == ".json"
139+
140+
history_path = interpreter.conversation_history_path
141+
all_conversations: List[List] = []
142+
for mpath in os.listdir(history_path):
143+
if not is_conversation_path(mpath):
144+
continue
145+
full_path = os.path.join(history_path, mpath)
146+
with open(full_path, "r") as cfile:
147+
conversation = json.load(cfile)
148+
all_conversations.append(conversation)
149+
return all_conversations
150+
151+
152+
def is_list_of_lists(l):
153+
return isinstance(l, list) and all([isinstance(e, list) for e in l])
154+
155+
156+
def contribute_conversations(conversations: List[List]):
157+
if len(conversations) == 0 or len(conversations[0]) == 0:
158+
return None
159+
160+
url = "https://api.openinterpreter.com/v0/conversations/contribute/"
161+
version = pkg_resources.get_distribution("open-interpreter").version
162+
163+
payload = {"conversations": conversations, "oi_version": version}
164+
165+
assert is_list_of_lists(payload["conversations"]), "the contribution payload is not a list of lists!"
166+
167+
try:
168+
response = requests.post(url, json=payload)
169+
if response.status_code != 200:
170+
print(
171+
f"Failed to contribute conversation: {response.status_code} {response.text}"
172+
)
173+
return None
174+
else:
175+
print(f"Successfully contributed conversations!")
176+
except requests.RequestException as e:
177+
print(f"Failed to contribute conversation: {e}")
178+
return None

interpreter/terminal_interface/profiles/profiles.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,6 @@ def apply_profile_to_object(obj, profile):
564564
else:
565565
setattr(obj, key, value)
566566

567-
568567
def open_storage_dir(directory):
569568
dir = os.path.join(oi_dir, directory)
570569

@@ -758,3 +757,29 @@ def migrate_user_app_directory():
758757
elif user_version == "0.2.0":
759758
old_dir = platformdirs.user_config_dir("Open Interpreter Terminal")
760759
migrate_app_directory(old_dir, oi_dir, profile_dir)
760+
761+
762+
def write_key_to_profile(key, value):
763+
try:
764+
with open(user_default_profile_path, 'r') as file:
765+
lines = file.readlines()
766+
767+
version_line_index = None
768+
new_lines = []
769+
for index, line in enumerate(lines):
770+
if line.strip().startswith("version:"):
771+
version_line_index = index
772+
break
773+
new_lines.append(line)
774+
775+
# Insert the new key-value pair before the version line
776+
if version_line_index is not None:
777+
if f"{key}: {value}\n" not in new_lines:
778+
new_lines.append(f"{key}: {value}\n\n") # Adding a newline for separation
779+
# Append the version line and all subsequent lines
780+
new_lines.extend(lines[version_line_index:])
781+
782+
with open(user_default_profile_path, 'w') as file:
783+
file.writelines(new_lines)
784+
except Exception:
785+
pass # Fail silently

interpreter/terminal_interface/start_terminal_interface.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pkg_resources
66

7-
from interpreter.terminal_interface.contributing_conversations import contribute_conversation_launch_logic
7+
from interpreter.terminal_interface.contributing_conversations import contribute_conversation_launch_logic, contribute_conversations
88

99
from ..core.core import OpenInterpreter
1010
from .conversation_navigator import conversation_navigator
@@ -242,7 +242,8 @@ def start_terminal_interface(interpreter):
242242
{
243243
"name": "contribute_conversation",
244244
"help_text": "let Open Interpreter use the current conversation to train an Open-Source LLM",
245-
"type": bool
245+
"type": bool,
246+
"default": False
246247
}
247248
]
248249

@@ -427,7 +428,7 @@ def start_terminal_interface(interpreter):
427428
interpreter.server()
428429
return
429430

430-
interpreter.contribute_conversation = args.contribute_conversation
431+
interpreter.contribute_conversation = args.contribute_conversation or arguments
431432

432433
validate_llm_settings(interpreter)
433434

@@ -463,4 +464,7 @@ def main():
463464
except KeyboardInterrupt:
464465
pass
465466
finally:
467+
if interpreter.will_contribute:
468+
contribute_conversations([interpreter.messages])
469+
print("Thank you for contributing to our training data!")
466470
interpreter.computer.terminate()

0 commit comments

Comments
 (0)