Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
tweak: and prep for mtllm moving into own repo
Browse files Browse the repository at this point in the history
  • Loading branch information
marsninja committed Jun 25, 2024
1 parent 0ca7f3d commit 2e78f5e
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run_pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -e .
pip install pytest mtllm
pip install pytest
- name: Run tests
run: pytest -x
74 changes: 62 additions & 12 deletions examples/rpg_game/jac_impl/jac_impl_3/main.jac
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ obj Game {

can postinit {
pygame.<>init();
self.font: pygame.font.Font=pygame.font.Font(GENERAL_FONT, 32);
self.font: pygame.font.Font = pygame.font.Font(GENERAL_FONT, 32);
}
# Generate the map according to the tilemap variable in map.jac

can createTilemap;
can new;
can events;
Expand Down Expand Up @@ -99,16 +100,32 @@ with entry {
keys = pygame.key.get_pressed();
if keys[pygame.K_SPACE] {
if self.player.facing == 'up' {
Attack(self, self.player.rect.x, self.player.rect.y - TILESIZE);
Attack(
self,
self.player.rect.x,
self.player.rect.y - TILESIZE
);
}
if self.player.facing == 'down' {
Attack(self, self.player.rect.x, self.player.rect.y + TILESIZE);
Attack(
self,
self.player.rect.x,
self.player.rect.y + TILESIZE
);
}
if self.player.facing == 'right' {
Attack(self, self.player.rect.x + TILESIZE, self.player.rect.y);
Attack(
self,
self.player.rect.x + TILESIZE,
self.player.rect.y
);
}
if self.player.facing == 'left' {
Attack(self, self.player.rect.x - TILESIZE, self.player.rect.y);
Attack(
self,
self.player.rect.x - TILESIZE,
self.player.rect.y
);
}
}
}
Expand Down Expand Up @@ -145,10 +162,19 @@ with entry {
# Game over screen

:obj:Game:can:game_over() {
self.score-=2;
self.score -= 2;
text = self.font.render('GaMe OvEr', True, RED);
text_rect = text.get_rect(center=(WIN_WIDTH / 2, WIN_HEIGHT / 2));
restart_button = Button(10, WIN_HEIGHT - 135, 120, 125, WHITE, BLACK, 'Restart', 32);
restart_button = Button(
10,
WIN_HEIGHT - 135,
120,
125,
WHITE,
BLACK,
'Restart',
32
);
for sprite in self.all_sprites {
sprite.kill();
}
Expand All @@ -167,7 +193,10 @@ with entry {
}
self.screen.blit(self.go_background, (0, 0));
self.screen.blit(text, text_rect);
self.screen.blit(restart_button.image, restart_button.rect);
self.screen.blit(
restart_button.image,
restart_button.rect
);
self.clock.tick(FPS);
pygame.display.update();
}
Expand All @@ -178,7 +207,16 @@ with entry {
intro = True;
title = self.font.render('Spud-nik : SOLO', True, BLUE);
title_rect = title.get_rect(x=WIN_WIDTH / 2 - 100, y=100);
play_button = Button(int(WIN_WIDTH / 2 - 50), 200, 100, 100, WHITE, BLACK, 'Play', 32);
play_button = Button(
int(WIN_WIDTH / 2 - 50),
200,
100,
100,
WHITE,
BLACK,
'Play',
32
);
while intro {
for event in pygame.event.get() {
if event.type == pygame.QUIT {
Expand All @@ -201,10 +239,19 @@ with entry {
# Game won

:obj:Game:can:game_won {
self.score+=5;
self.score += 5;
text = self.font.render('YOU WON!', True, BLUE);
text_rect = text.get_rect(center=(WIN_WIDTH / 2, WIN_HEIGHT / 2));
restart_button = Button(10, WIN_HEIGHT - 135, 120, 125, WHITE, BLACK, 'Restart', 32);
restart_button = Button(
10,
WIN_HEIGHT - 135,
120,
125,
WHITE,
BLACK,
'Restart',
32
);
for sprite in self.all_sprites {
sprite.kill();
}
Expand All @@ -222,7 +269,10 @@ with entry {
}
self.screen.blit(self.intro_background, (0, 0));
self.screen.blit(text, text_rect);
self.screen.blit(restart_button.image, restart_button.rect);
self.screen.blit(
restart_button.image,
restart_button.rect
);
self.clock.tick(FPS);
pygame.display.update();
}
Expand Down
6 changes: 5 additions & 1 deletion jaclang/compiler/absyntree.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,11 @@ def sem_token(self) -> Optional[tuple[SemTokType, SemTokMod]]:
"""Resolve semantic token."""
if isinstance(self.name_of, BuiltinType):
return SemTokType.CLASS, SemTokMod.DECLARATION
name_of = self.sym.decl.name_of if self.sym else self.name_of
name_of = (
self.sym.decl.name_of
if self.sym and not isinstance(self.sym.decl.name_of, Name)
else self.name_of
)
if isinstance(name_of, ModulePath):
return SemTokType.NAMESPACE, SemTokMod.DEFINITION
if isinstance(name_of, Architype):
Expand Down
2 changes: 1 addition & 1 deletion jaclang/core/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def jac_importer(
if isinstance(mod_bundle, str)
and mod_bundle in sys.modules
and "__jac_mod_bundle__" in sys.modules[mod_bundle].__dict__
else mod_bundle
else None
)

caller_dir = get_caller_dir(target, base_path, dir_path)
Expand Down
191 changes: 96 additions & 95 deletions jaclang/tests/test_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,104 +112,105 @@ def test_chandra_bugs2(self) -> None:
"{'a': 'apple', 'b': 'ball', 'c': 'cat', 'd': 'dog', 'e': 'elephant'}\n",
)

def test_with_llm_function(self) -> None:
"""Parse micro jac file."""
captured_output = io.StringIO()
sys.stdout = captured_output
jac_import("with_llm_function", base_path=self.fixture_abs_path("./"))
sys.stdout = sys.__stdout__
stdout_value = captured_output.getvalue()
self.assertIn("{'temperature': 0.7}", stdout_value)
self.assertIn("Emoji Representation (str)", stdout_value)
self.assertIn('Text Input (input) (str) = "Lets move to paris"', stdout_value)
self.assertIn(
' = [{"input": "I love tp drink pina coladas"',
stdout_value,
)
# TODO: Move these tests to mtllm repo
# def test_with_llm_function(self) -> None:
# """Parse micro jac file."""
# captured_output = io.StringIO()
# sys.stdout = captured_output
# jac_import("with_llm_function", base_path=self.fixture_abs_path("./"))
# sys.stdout = sys.__stdout__
# stdout_value = captured_output.getvalue()
# self.assertIn("{'temperature': 0.7}", stdout_value)
# self.assertIn("Emoji Representation (str)", stdout_value)
# self.assertIn('Text Input (input) (str) = "Lets move to paris"', stdout_value)
# self.assertIn(
# ' = [{"input": "I love tp drink pina coladas"',
# stdout_value,
# )

def test_with_llm_method(self) -> None:
"""Parse micro jac file."""
captured_output = io.StringIO()
sys.stdout = captured_output
jac_import("with_llm_method", base_path=self.fixture_abs_path("./"))
sys.stdout = sys.__stdout__
stdout_value = captured_output.getvalue()
self.assertIn("[Reasoning] <Reason>", stdout_value)
self.assertIn("(Enum) eg:- Personality.EXTROVERT ->", stdout_value)
self.assertIn(
"Personality Index of a Person (PersonalityIndex) (class) eg:- "
"PersonalityIndex(index=int) -> Personality Index (index) (int)",
stdout_value,
)
self.assertIn(
"Personality of the Person (dict[Personality,PersonalityIndex])",
stdout_value,
)
self.assertIn(
'Diary Entries (diary_entries) (list[str]) = ["I won noble prize in '
'Physics", "I am popular for my theory of relativity"]',
stdout_value,
)
# def test_with_llm_method(self) -> None:
# """Parse micro jac file."""
# captured_output = io.StringIO()
# sys.stdout = captured_output
# jac_import("with_llm_method", base_path=self.fixture_abs_path("./"))
# sys.stdout = sys.__stdout__
# stdout_value = captured_output.getvalue()
# self.assertIn("[Reasoning] <Reason>", stdout_value)
# self.assertIn("(Enum) eg:- Personality.EXTROVERT ->", stdout_value)
# self.assertIn(
# "Personality Index of a Person (PersonalityIndex) (class) eg:- "
# "PersonalityIndex(index=int) -> Personality Index (index) (int)",
# stdout_value,
# )
# self.assertIn(
# "Personality of the Person (dict[Personality,PersonalityIndex])",
# stdout_value,
# )
# self.assertIn(
# 'Diary Entries (diary_entries) (list[str]) = ["I won noble prize in '
# 'Physics", "I am popular for my theory of relativity"]',
# stdout_value,
# )

def test_with_llm_lower(self) -> None:
"""Parse micro jac file."""
captured_output = io.StringIO()
sys.stdout = captured_output
jac_import("with_llm_lower", base_path=self.fixture_abs_path("./"))
sys.stdout = sys.__stdout__
stdout_value = captured_output.getvalue()
self.assertIn("[Reasoning] <Reason>", stdout_value)
self.assertIn(
'Name of the Person (name) (str) = "Oppenheimer"',
stdout_value,
)
self.assertIn(
"Person (Person) (obj) eg:- Person(full_name=str, yod=int, personality"
"=Personality) -> Fullname of the Person (full_name) (str), Year of Death"
" (yod) (int), Personality of the Person (personality) (Personality)",
stdout_value,
)
self.assertIn(
"J. Robert Oppenheimer was a Introvert person who died in 1967",
stdout_value,
)
# def test_with_llm_lower(self) -> None:
# """Parse micro jac file."""
# captured_output = io.StringIO()
# sys.stdout = captured_output
# jac_import("with_llm_lower", base_path=self.fixture_abs_path("./"))
# sys.stdout = sys.__stdout__
# stdout_value = captured_output.getvalue()
# self.assertIn("[Reasoning] <Reason>", stdout_value)
# self.assertIn(
# 'Name of the Person (name) (str) = "Oppenheimer"',
# stdout_value,
# )
# self.assertIn(
# "Person (Person) (obj) eg:- Person(full_name=str, yod=int, personality"
# "=Personality) -> Fullname of the Person (full_name) (str), Year of Death"
# " (yod) (int), Personality of the Person (personality) (Personality)",
# stdout_value,
# )
# self.assertIn(
# "J. Robert Oppenheimer was a Introvert person who died in 1967",
# stdout_value,
# )

def test_with_llm_type(self) -> None:
"""Parse micro jac file."""
captured_output = io.StringIO()
sys.stdout = captured_output
jac_import("with_llm_type", base_path=self.fixture_abs_path("./"))
sys.stdout = sys.__stdout__
stdout_value = captured_output.getvalue()
self.assertIn("14/03/1879", stdout_value)
self.assertNotIn(
'University (University) (obj) = type(__module__="with_llm_type", __doc__=None, '
"_jac_entry_funcs_`=[`], _jac_exit_funcs_=[], __init__=function(__wrapped__=function()))",
stdout_value,
)
desired_output_count = stdout_value.count(
"Person(name='Jason Mars', dob='1994-01-01', age=30)"
)
self.assertEqual(desired_output_count, 2)

def test_with_llm_vision(self) -> None:
"""Test MTLLLM Vision Implementation."""
try:
captured_output = io.StringIO()
sys.stdout = captured_output
jac_import("with_llm_vision", base_path=self.fixture_abs_path("./"))
sys.stdout = sys.__stdout__
stdout_value = captured_output.getvalue()
self.assertIn(
"{'type': 'text', 'text': '\\n[System Prompt]\\n", stdout_value[:500]
)
self.assertNotIn(
" {'type': 'text', 'text': 'Image of the Question (question_img) (Image) = '}, "
"{'type': 'image_url', 'image_url': {'url': 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQAB",
stdout_value[:500],
)
except Exception:
self.skipTest("This test requires Pillow to be installed.")
# def test_with_llm_type(self) -> None:
# """Parse micro jac file."""
# captured_output = io.StringIO()
# sys.stdout = captured_output
# jac_import("with_llm_type", base_path=self.fixture_abs_path("./"))
# sys.stdout = sys.__stdout__
# stdout_value = captured_output.getvalue()
# self.assertIn("14/03/1879", stdout_value)
# self.assertNotIn(
# 'University (University) (obj) = type(__module__="with_llm_type", __doc__=None, '
# "_jac_entry_funcs_`=[`], _jac_exit_funcs_=[], __init__=function(__wrapped__=function()))",
# stdout_value,
# )
# desired_output_count = stdout_value.count(
# "Person(name='Jason Mars', dob='1994-01-01', age=30)"
# )
# self.assertEqual(desired_output_count, 2)

# def test_with_llm_vision(self) -> None:
# """Test MTLLLM Vision Implementation."""
# try:
# captured_output = io.StringIO()
# sys.stdout = captured_output
# jac_import("with_llm_vision", base_path=self.fixture_abs_path("./"))
# sys.stdout = sys.__stdout__
# stdout_value = captured_output.getvalue()
# self.assertIn(
# "{'type': 'text', 'text': '\\n[System Prompt]\\n", stdout_value[:500]
# )
# self.assertNotIn(
# " {'type': 'text', 'text': 'Image of the Question (question_img) (Image) = '}, "
# "{'type': 'image_url', 'image_url': {'url': 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQAB",
# stdout_value[:500],
# )
# except Exception:
# self.skipTest("This test requires Pillow to be installed.")

def test_ignore(self) -> None:
"""Parse micro jac file."""
Expand Down

0 comments on commit 2e78f5e

Please sign in to comment.