Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance set method and add get_name utility function #34

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions upsonic/remote/on_prem.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,9 @@ def dump(
key:str,
value,
message:str =None,
code:str =None,
) -> None:
return self.set(key, value, message=message)
return self.set(key, value, message=message, code=code)

def load(self, key:str, version:str=None) -> any:
return self.get(key, version=version, print_exc=True)
Expand Down Expand Up @@ -717,7 +718,7 @@ def print_current_datetime(self):
current_datetime = datetime.now()
print("Current date and time:", current_datetime.strftime("%Y-%m-%d %H:%M:%S"))

def set(self, key:str, value, message:str=None) -> bool:
def set(self, key:str, value, message:str=None, code:str=None) -> bool:

if self.tester:
self.print_current_datetime()
Expand Down Expand Up @@ -771,7 +772,7 @@ def set(self, key:str, value, message:str=None) -> bool:

encryption_key = "u"

the_code = textwrap.dedent(extract_source(value, key=key))
the_code = textwrap.dedent(extract_source(value, key=key)) if code == None else code

# Preparation of Requirements
the_requirements = Upsonic_On_Prem.export_requirement()
Expand Down Expand Up @@ -1289,10 +1290,31 @@ def ai_completion(self, message:str, model: Optional[str] = None) -> dict:
def get_all_scopes_user(self)-> dict:
return self._send_request("GET", "/get_all_scopes_user")

def get_name(self, value):
# Try to use dill to get the name
name = dill.source.getname(value)
if name is not None:
return name

# For functions, methods, and classes
if inspect.isfunction(value) or inspect.isclass(value) or inspect.ismethod(value):
return value.__name__

# For instances of a class
elif hasattr(value, '__class__'):
return value.__class__.__name__

# For variables in the current scope (using globals)
globals_dict = globals()
for name, val in globals_dict.items():
if val is value:
return name

# If none of the above work
return None

def auto_dump(
self, value, ask=True, check_function=True, print_prompts=False, model:Optional[str] = None
self, value, ask=True, suggestion_only=False, check_function=True, print_prompts=False, model:Optional[str] = None
) -> None:
if model == None:
model = self.get_default_ai_model()
Expand All @@ -1308,7 +1330,10 @@ def auto_dump(

code = textwrap.dedent(extract_source(value))
all_scopes = self.get_all_scopes_user()
all_scopes = "\n".join(all_scopes)
try:
all_scopes = "\n".join(all_scopes)
except:
all_scopes = ""

prompt = f"""
You are an helpful software engineer. Help to organize library elements in a short and clear manner.
Expand All @@ -1327,14 +1352,23 @@ def auto_dump(
Your answer should be just the suggested position. Dont say any other think.


Categories include (but are not limited to):
- database.connections (e.g., database.connections.postgre, database.connections.mysql)
- data.processing (e.g., data.processing.cleaning, data.processing.transformation)
- api.integration (e.g., api.integration.rest, api.integration.graphql)
- utils.helpers (e.g., utils.helpers.date, utils.helpers.string)
- machine_learning.models (e.g., machine_learning.models.classification, machine_learning.models.regression)
- visualization.charts (e.g., visualization.charts.bar, visualization.charts.line)


Suggested Position:

"""

ai_answer = self.ai_completion(prompt, model=model)
ai_answer = ai_answer.replace("`", "").replace("\n", "")
ai_answer = ".".join(ai_answer.split(".")[:-1])
ai_answer = ai_answer + "." + dill.source.getname(value)
ai_answer = ai_answer + "." + self.get_name(value)
prompt = prompt + f"\nASSISTANT: {ai_answer}\n"

prompt = (
Expand All @@ -1352,9 +1386,14 @@ def auto_dump(
)
ai_answer = ai_answer.replace("`", "").replace("\n", "")
ai_answer = ai_answer.replace("ASSISTANT: ", "")
if suggestion_only:
print("Suggestion:", ai_answer)
return ai_answer

if ai_answer in all_scopes:
print(f"Check: similarity with the {ai_answer} is detected")
return

if ask:
print("Commands:\n(Y)es/(N)o\n")
while True:
Expand Down