-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #671 from astroseger/pyfuns
Implementing py-tuple py-list py-dict py-chain
- Loading branch information
Showing
7 changed files
with
112 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
!(import! &self simple_import) | ||
|
||
!(import_from example_01 import simple_fun) | ||
!(import_from example_01 import SimpleObject) | ||
!(bind! simple_fun (py-atom example_01.simple_fun)) | ||
!(bind! SimpleObject (py-atom example_01.SimpleObject)) | ||
|
||
!(bind! so (SimpleObject)) | ||
|
||
|
||
; it is important that obj will have type SimpleObject when passed to simple_fun! | ||
!(simple_fun 1 2 "3" (kwarg1 2) (obj so) ) | ||
!(simple_fun 1 2 "3" (Kwargs (kwarg1 2) (obj so)) ) | ||
|
||
!(call_dot so method "arg1" "arg2" (arg3 3)) | ||
!( (py-dot so method) "arg1" "arg2" (Kwargs (arg3 3)) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
!(import! &self simple_import) | ||
!(bind! np (py-atom numpy)) | ||
|
||
!(import_as numpy as np) | ||
!(bind! a1 ( (py-dot np array) (py-atom (py-tuple (1 2 3)) ))) | ||
!(bind! a2 ( (py-dot a1 __mul__) 3)) | ||
!(bind! a3 ( (py-dot a1 __add__) a2)) | ||
|
||
!(bind! a1 (call_dot np array (ptuple 1 2 3) )) | ||
!(bind! a2 (call_dot a1 __mul__ 3)) | ||
!(bind! a3 (call_dot a1 __add__ a2)) | ||
|
||
!(a1) | ||
!(a2) | ||
!(a3) | ||
|
||
!(__unwrap a1) | ||
!(__unwrap a2) | ||
!(__unwrap a3) | ||
!(bind! m1 ((py-dot np array) (py-atom (py-list ((1 2 3) (py-list (4 4 5)) (py-tuple (6 7 8))) )))) | ||
!(bind! linalg (py-atom numpy.linalg)) | ||
!(bind! m1_inv ( (py-dot linalg inv) m1)) | ||
|
||
!(bind! m1 (call_dot np array (ptuple (1 2 3) (4 4 5) (6 7 8)) )) | ||
!(import_as numpy.linalg as linalg) | ||
!(bind! m1_inv (call_dot linalg inv m1)) | ||
|
||
!(__unwrap (call_dot np matmul m1 m1_inv)) | ||
!( (py-dot np matmul) m1 m1_inv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,18 @@ | ||
!(import! &self simple_import) | ||
!(bind! ChatOpenAI (py-atom langchain_openai.ChatOpenAI)) | ||
!(bind! ChatPromptTemplate (py-atom langchain_core.prompts.ChatPromptTemplate)) | ||
!(bind! StrOutputParser (py-atom langchain_core.output_parsers.StrOutputParser)) | ||
|
||
!(import_from langchain_openai import ChatOpenAI) | ||
!(import_from langchain_core.prompts import ChatPromptTemplate) | ||
!(import_from langchain_core.output_parsers import StrOutputParser) | ||
!(bind! model (ChatOpenAI (Kwargs (temperature 0) (model "gpt-3.5-turbo")))) | ||
|
||
!(bind! prompt ( (py-dot ChatPromptTemplate from_template) "tell me a joke about cat")) | ||
|
||
!(bind! model (ChatOpenAI (temperature 0) (model "gpt-3.5-turbo"))) | ||
!(bind! chain1 (py-chain (prompt model (StrOutputParser)) )) | ||
|
||
!(bind! prompt (call_dot ChatPromptTemplate from_template "tell me a joke about cat")) | ||
!( (py-dot chain1 invoke) (py-dict ())) | ||
|
||
!(bind! chain1 (chain prompt model (StrOutputParser) )) | ||
!(bind! prompt2 ( (py-dot ChatPromptTemplate from_messages ) (py-tuple (("system" "You are very funny") ("user" "tell me joke about {foo}"))))) | ||
|
||
!(__unwrap(call_dot chain1 invoke (pdict))) | ||
!(bind! chain2 (py-chain (prompt2 model (StrOutputParser)) )) | ||
|
||
!(bind! prompt2 (call_dot ChatPromptTemplate from_messages (ptuple ("system" "You are very funny") ("user" "tell me joke about {foo}")))) | ||
|
||
!(bind! chain2 (chain prompt2 model (StrOutputParser) )) | ||
|
||
!(__unwrap(call_dot chain2 invoke (pdict (foo "dogs") ))) | ||
!((py-dot chain2 invoke) (py-dict (("foo" "dogs")))) | ||
|
18 changes: 5 additions & 13 deletions
18
python/sandbox/simple_import/example_04_numpy_simple_import.metta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,6 @@ | ||
!(import! &self simple_import) | ||
!(bind! linalg (py-atom numpy.linalg)) | ||
!(bind! numpy (py-atom numpy)) | ||
|
||
; with simple "import" it is rather common that we import something | ||
; twice because of submodules in python | ||
; So let's import twice to make sure that it does not cause any problems | ||
!(import numpy) | ||
!(import numpy) | ||
!(import numpy.linalg) | ||
|
||
|
||
!(bind! m1 (call_dot2 numpy random rand 3 3 )) | ||
!(bind! m1_inv (call_dot2 numpy linalg inv m1)) | ||
|
||
!(__unwrap (call_dot numpy matmul m1 m1_inv)) | ||
!(bind! m1 ((py-dot numpy random.rand) 3 3 )) | ||
!(bind! m1_inv ( (py-dot linalg inv) m1)) | ||
!( (py-dot numpy matmul) m1 m1_inv) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters