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

current state of pretty print #771

Merged
merged 17 commits into from
Oct 29, 2024
Merged

current state of pretty print #771

merged 17 commits into from
Oct 29, 2024

Conversation

DaddyWesker
Copy link
Contributor

Examples of pretty_prints in current state:

(= (abs $x) (if
		(> $x 0)
		$x (if
			(== $x 0)
			0 (* $x -1)
			)
		))

( = (response) (if
		(== (messages) (user "match world."))
		("Hi there")
		(("Bye"))
		))

(: gen_gpt_2 (-> String String Number Number Number String))
(= (gen_gpt_2 $start_text $run_name $temperature $top_k $length)
	((text-generation) "gen_gpt_2" "Query"
		(Kwargs (start_text $start_text)
			(run_name $run_name) (temperature $temperature)
			(top_k $top_k) (length $length))))

! (assertEqual
	(match &self
		(Parent Tom $x)
		$x)
	(empty)
	)

!(assertEqual
	(exercise_1_3 1 2 5)
	29)

(= (better-sqrt-iter $oldguess $guess $x)
	(if (better-good-enough? $oldguess $guess $x)
		$guess
		(better-sqrt-iter $guess (improve $guess $x) $x)
		))

(= (first-denomination $kinds-of-coins)
	(case $kinds-of-coins
		( (1 1) (2 5) (3 10) (4 25) (5 50) )
		))

(= (cc $amount $kinds-of-coins)
	(if (== $amount 0)
		1 (if
			(or (< $amount 0) (== $kinds-of-coins 0))
			0 (+ (cc $amount (- $kinds-of-coins 1))
				(cc (- $amount (first-denomination $kinds-of-coins)) $kinds-of-coins))
			)
		))

(= (timed-prime-test $n) (let*
		( (() (println! $n)) (() (let $curtime
					(timems!)
					(start-prime-test $n $curtime)
					)))
		() ))

(= (fixed-point-print $f $first-guess)
	(let $try
		(lambda2 $guess $self-try (let $next
				($f $guess)
				(let $close-enough?
					(lambda2 $v1 $v2 (< (Abs (- $v1 $v2))
							(tolerance)))
					(if
						($close-enough? $guess $next)
						$next (let*
							( (() (println! $next)) (() ($self-try $next $self-try)) )
							() )
						)
					)
				))
		($try $first-guess $try)
		))

(= (cond $com_list) (if
		(== $com_list ())
		(empty)
		(case
			(car-atom $com_list)
			( ((Else $res) $res) (($cond $res)
					(if $cond $res
						(cond (cdr-atom $com_list))
						)) )
			)
		))

Fixed return value
@DaddyWesker
Copy link
Contributor Author

DaddyWesker commented Sep 16, 2024

I've checked, code works if launched like this:

!(import! &self snet_io)

!(bind! &generative-lms
  (snet-sdk create_service_client "naint" "generative-lms"))

!(snet-sdk get_service_callers_text &generative-lms)

@@ -88,6 +88,8 @@ def __call__(self, command_a, *args_a):
self.init_sdk()
if command == 'get_service_callers':
return args[0].generate_callers()
if command == 'get_service_callers_text':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there was no need to expose this function to metta

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, i removed those lines.

def generate_callers_text(self):
# TODO: pretty print
return "\n".join([repr(e) for e in self.generate_callers()])
res = "\n".join([repr(e) for e in self.generate_callers()])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see pretty-print is used here

Copy link
Contributor Author

@DaddyWesker DaddyWesker Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad. It was used for a while, but then I've removed by mistake. Added.

# TODO: pretty print
return "\n".join([repr(e) for e in self.generate_callers()])
res = "\n".join([repr(e) for e in self.generate_callers()])
return [ValueAtom(res)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no need to wrap the result of this function into atom since it is supposed as a pure Python function, and it can be called via py-dot from metta

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

@@ -137,9 +139,105 @@ def get_service_messages(self):
def get_operation_atom(self):
return OperationAtom(self.service_details[1], self)

def __pretty_print__(self, input_str):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty-print was supposed for adding to stdlib, but maybe it can remain here for now....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I wasn't been aware of this. Where should I place it?

@@ -137,9 +139,105 @@ def get_service_messages(self):
def get_operation_atom(self):
return OperationAtom(self.service_details[1], self)

def __pretty_print__(self, input_str):
list_of_functions = ["if", "match", "let", "let*", "unify",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is special about these functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are functions which could have several inner brackets inside their body. Probably I could forget some functions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's about just nested expressions like (* (- $x $y) (- $x $y))? What is the substantial difference?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that it is problematic to detect nested brackets in your approach, because you try to reformat a textual representation, so you need to re-parse it. If you'd started with atoms, it could be simpler...

@DaddyWesker
Copy link
Contributor Author

Okay, so I've rewrote pretty_print so it takes atoms as input not string. Below are some examples of how pretty_print prints atoms into string. @Necr0x0Der check if it is fine with you (when you have time of course).

(: generate
	(-> String String))

(= (generate $request)
	(generative-lms "generate" "Query"
	 (Kwargs
			(request $request))))

(= (abs $x)
	(if (> $x 0) $x
		(if (== $x 0) 0
			(* $x -1))))

(= (response)
	(if (== (messages)
			(user "match world"))
		("Hi there")
		((Bye))))

(assertEqual
	(match &self
		(Parent Tom $x) $x)
	(empty))

(assertEqual
	(exercise_1_3 1 2 5) 29)

(= (better-sqrt-iter $oldguess $guess $x)
	(if (better-good-enough? $oldguess $guess $x) $guess
		(better-sqrt-iter $guess
			(improve $guess $x) $x)))

(= (first-denomination $kinds-of-coins)
	(case $kinds-of-coins
		((1 1) (2 5) (3 10) (4 25) (5 50))))

(= (cc $amount $kinds-of-coins)
	(if (== $amount 0) 1
		(if (or (< $amount 0)
				(== $kinds-of-coins 0)) 0
			(+ (cc $amount
					(- $kinds-of-coins 1))
				(cc (- $amount
						(first-denomination $kinds-of-coins)) $kinds-of-coins)))))

(= (timed-prime-test $n)
	(let* ((() (println! $n))
			(() (let $curtime
					(timems!)
					(start-prime-test $n $curtime))))
		()))

(= (fixed-point-print $f $first-guess)
	(let $try
		(lambda2 $guess $self-try
			(let $next
				($f $guess)
				(let $close-enough?
					(lambda2 $v1 $v2
						(< (Abs (- $v1 $v2))
							(tolerance)))
					(if ($close-enough? $guess $next) $next
						(let* ((() (println! $next))
								(() ($self-try $next $self-try)))
							()))))) ($try $first-guess $try)))

(= (cond $com_list)
	(if (== $com_list
			()) (empty) (case (car-atom $com_list)
			(((Else $res) $res)
				(($cond $res)
					(if $cond $res
						(cond (cdr-atom $com_list))))))))

@DaddyWesker DaddyWesker changed the base branch from experimental to main September 23, 2024 04:56
@@ -139,14 +143,58 @@ def get_service_messages(self):
def get_operation_atom(self):
return OperationAtom(self.service_details[1], self)

def __pretty_print_atoms__(self, input_atoms):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this function out of ServiceCall and wouldn't introduce len_threshold and current_len as its fields.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, where to move this function? As i remember, you've proposed to move it to stdlib. Should i place it there?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can keep it in the same file for now. Just move it out of the class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Necr0x0Der Done.

ValueAtom(f'unknown command {repr(command_a)}'))]

len_threshold = 50
current_len = 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen when the function is called twice? Will current_len behave correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it should be set to 0 (line 158 sets it to 0 after every subatom was processed). I'll check just to be sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Necr0x0Der I've checked (though I needed to do some changes to init_sdk since snet-sdk was updated and now its init is slightly different, probably i need to make new pr for those changes?) and current_len is 0 on the start of pretty print. And it can be seen on line 154.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, but still... Are they used only in pretty_print_atoms and its subfunctions? In this case, it's better to not declare them as global, but rather introduce them in pretty_print_atoms and use nonlocal for them in subfunctions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try this out. I haven't been aware of nonlocal. Of course it would be better to introduce them inside function I just wasn't been able to make them work correctly. If nonlocal will help, I'll push changes tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Necr0x0Der Okay, it works. Thank you. I've pushed new commit with changes regarding current_len and threshold

@Necr0x0Der Necr0x0Der merged commit 6547067 into trueagi-io:main Oct 29, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants