mirror of
https://github.com/hwchase17/langchain.git
synced 2026-04-18 08:03:52 +00:00
Compare commits
531 Commits
erick/pyte
...
wfh/may3/h
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3584285f76 | ||
|
|
c9e9470c5a | ||
|
|
ee1adaacaa | ||
|
|
9639457222 | ||
|
|
3ef8b24277 | ||
|
|
36c2ca3c8b | ||
|
|
6e1e0c7d5c | ||
|
|
26a37dce0a | ||
|
|
335bd01e45 | ||
|
|
23a05c3986 | ||
|
|
6da3d92b42 | ||
|
|
d6e34f9ee5 | ||
|
|
487aff7e46 | ||
|
|
ba4a309d98 | ||
|
|
66a1e3f083 | ||
|
|
0989c48028 | ||
|
|
2fbe82f5e6 | ||
|
|
3a8d1d8838 | ||
|
|
64e17bd793 | ||
|
|
c3d169ab00 | ||
|
|
70bde15480 | ||
|
|
67a5cc34c6 | ||
|
|
c1eb95b967 | ||
|
|
47ce8d5a57 | ||
|
|
6ac6158a07 | ||
|
|
aa9faa8512 | ||
|
|
c1aa237bc2 | ||
|
|
eb0a2fd53a | ||
|
|
2d77e5e3a1 | ||
|
|
1ebb5a70ad | ||
|
|
683fb45c6b | ||
|
|
b00fd1dbde | ||
|
|
ee2c55ca09 | ||
|
|
7d451d0041 | ||
|
|
d297d90ad9 | ||
|
|
663747b730 | ||
|
|
df49404794 | ||
|
|
bd5d2c2674 | ||
|
|
3cd7fced5f | ||
|
|
b5c3a04e4b | ||
|
|
c9119b0e75 | ||
|
|
c306364b06 | ||
|
|
cd4c54282a | ||
|
|
6fa8626e2f | ||
|
|
94a838740e | ||
|
|
23fdd320bc | ||
|
|
9e53fa7d2e | ||
|
|
0694538c39 | ||
|
|
44602bdc20 | ||
|
|
9932f49b3e | ||
|
|
57e8e70daa | ||
|
|
2914abd747 | ||
|
|
23c5d87311 | ||
|
|
bec3eee3fa | ||
|
|
43110daea5 | ||
|
|
59f10ab3e0 | ||
|
|
2f709d94d7 | ||
|
|
7230e430db | ||
|
|
daab9789a8 | ||
|
|
642975dd9f | ||
|
|
7a39fe60da | ||
|
|
b879184595 | ||
|
|
8b4b75e543 | ||
|
|
0e5bf16d00 | ||
|
|
bd38073d76 | ||
|
|
4d1c21d97d | ||
|
|
2a6f78a53f | ||
|
|
1ce1a10f2b | ||
|
|
aa0bc7467c | ||
|
|
86ff8a3fb4 | ||
|
|
d640605694 | ||
|
|
2b10c4dd52 | ||
|
|
2fcab9acd9 | ||
|
|
ab55f6996d | ||
|
|
86fe484e24 | ||
|
|
8a01760a0f | ||
|
|
12b1caf295 | ||
|
|
45ddf4d26f | ||
|
|
3b473d10f2 | ||
|
|
1fdf63fa6c | ||
|
|
7860e4c649 | ||
|
|
67e6744e0f | ||
|
|
5a8909440b | ||
|
|
a36935b520 | ||
|
|
c9e96bb5e2 | ||
|
|
8d2909ee25 | ||
|
|
bef50ded63 | ||
|
|
0f7f448603 | ||
|
|
3441a11b21 | ||
|
|
6d3e9eaf84 | ||
|
|
14422a4220 | ||
|
|
6c938da302 | ||
|
|
5f8a307565 | ||
|
|
bf95414758 | ||
|
|
e4f51f59a2 | ||
|
|
9e788f09c6 | ||
|
|
3853fe9f64 | ||
|
|
8658d52587 | ||
|
|
9b6d04a187 | ||
|
|
aec13a6123 | ||
|
|
8a62fb0570 | ||
|
|
2407c353be | ||
|
|
dbdfa3d34e | ||
|
|
fd94aa8366 | ||
|
|
3e749369ef | ||
|
|
f1c3687aa5 | ||
|
|
b0b1a67771 | ||
|
|
b9c53e95b7 | ||
|
|
3c064a757f | ||
|
|
8f38b7a725 | ||
|
|
748f2ba9ea | ||
|
|
efe27ef849 | ||
|
|
c8f18a2524 | ||
|
|
5c63ac3dd7 | ||
|
|
845d8e0025 | ||
|
|
5c77f45b06 | ||
|
|
d6e9bd3011 | ||
|
|
db14d4326d | ||
|
|
fa4124b821 | ||
|
|
d7e12750df | ||
|
|
ea43c669f2 | ||
|
|
28b0b0d863 | ||
|
|
a7a4630bf4 | ||
|
|
c172611647 | ||
|
|
08d08d7c83 | ||
|
|
85094cbb3a | ||
|
|
90f19028e5 | ||
|
|
cc6191cb90 | ||
|
|
1a2ff56cd8 | ||
|
|
f479a337cc | ||
|
|
82d4afcac0 | ||
|
|
854ae3e1de | ||
|
|
3e241956d3 | ||
|
|
29493bb598 | ||
|
|
aab78a37f3 | ||
|
|
ce89b34fc0 | ||
|
|
b3efa38cc0 | ||
|
|
3331865f6b | ||
|
|
b3e74f2b98 | ||
|
|
4822beb298 | ||
|
|
a64a1943fd | ||
|
|
4f4ee8e2cf | ||
|
|
67428c4052 | ||
|
|
dc70c23a11 | ||
|
|
8b59bddc03 | ||
|
|
1fad39be1c | ||
|
|
d36332476c | ||
|
|
dc7c06bc07 | ||
|
|
2ddac9a7c3 | ||
|
|
d781560722 | ||
|
|
5e0b6b3e75 | ||
|
|
07ce39bfe7 | ||
|
|
17bbb7d2a5 | ||
|
|
804390ba4b | ||
|
|
6342217b93 | ||
|
|
8097bec472 | ||
|
|
9ec7151317 | ||
|
|
9fa9f05e5d | ||
|
|
2aca7fcdcf | ||
|
|
0e917e319b | ||
|
|
45092a36a2 | ||
|
|
e818c75f8a | ||
|
|
f931a9ce60 | ||
|
|
e57cf73cf5 | ||
|
|
40b2e2916b | ||
|
|
955cf186d2 | ||
|
|
790ea75cf7 | ||
|
|
8bbdb4f6a0 | ||
|
|
61f14f00d7 | ||
|
|
8c085fc697 | ||
|
|
893a924b90 | ||
|
|
d4befd0cfb | ||
|
|
8ed150b2fe | ||
|
|
989e4a92c2 | ||
|
|
2fa0ff1a2d | ||
|
|
078c5d9bc6 | ||
|
|
d4aec8fc8f | ||
|
|
bf16cefd18 | ||
|
|
38eccab3ae | ||
|
|
e1c2e2fdfa | ||
|
|
84b8e67c9c | ||
|
|
465fbaa30b | ||
|
|
12c906f6ce | ||
|
|
5653f36adc | ||
|
|
fe1304afc4 | ||
|
|
6598757037 | ||
|
|
d95e9fb67f | ||
|
|
9281841cfe | ||
|
|
7d8d0229fa | ||
|
|
4c437ebb9c | ||
|
|
891ae37437 | ||
|
|
28df4750ef | ||
|
|
5f1d1666e3 | ||
|
|
b54b19ba1c | ||
|
|
5e60d65917 | ||
|
|
898362de81 | ||
|
|
63a07f52df | ||
|
|
fd1061e7bf | ||
|
|
dc921f0823 | ||
|
|
a5028b6356 | ||
|
|
1202017c56 | ||
|
|
f386f71bb3 | ||
|
|
05ae8ca7d4 | ||
|
|
fdabd3cdf5 | ||
|
|
6986e44959 | ||
|
|
b8db73233c | ||
|
|
52896258ee | ||
|
|
520972fd0f | ||
|
|
748a6ae609 | ||
|
|
37cbbc00a9 | ||
|
|
a6b8ff23bd | ||
|
|
eca3640af7 | ||
|
|
82b5bdc7a1 | ||
|
|
baefbfb14e | ||
|
|
92969d49cb | ||
|
|
53bb7dbd29 | ||
|
|
ed26149a29 | ||
|
|
5b83130855 | ||
|
|
540f384197 | ||
|
|
ffad3985a1 | ||
|
|
6ccecf2363 | ||
|
|
9e694963a4 | ||
|
|
5da9dd1195 | ||
|
|
7c5063ef60 | ||
|
|
c2d09a5186 | ||
|
|
a936f696a6 | ||
|
|
2cd907ad7e | ||
|
|
2968f20970 | ||
|
|
481d3855dc | ||
|
|
9b7fb381a4 | ||
|
|
9e983c9500 | ||
|
|
6353991498 | ||
|
|
a9e2e98708 | ||
|
|
1aef8116de | ||
|
|
c8fd51e8c8 | ||
|
|
5ecebf168c | ||
|
|
243ba71b28 | ||
|
|
43c041cda5 | ||
|
|
a1614b88ac | ||
|
|
493afe4d8d | ||
|
|
9efab3ed66 | ||
|
|
13751c3297 | ||
|
|
0186e4e633 | ||
|
|
12e5ec6de3 | ||
|
|
8c95ac3145 | ||
|
|
477eb1745c | ||
|
|
a9c7d47c03 | ||
|
|
5ab3f9a995 | ||
|
|
70ae59bcfe | ||
|
|
8d1167b32f | ||
|
|
87d31a3ec0 | ||
|
|
d8aa72f51d | ||
|
|
3bcfbcc871 | ||
|
|
30e48c9878 | ||
|
|
6debadaa70 | ||
|
|
7984206c95 | ||
|
|
9111d3a636 | ||
|
|
06b04b80b8 | ||
|
|
5a3c65a756 | ||
|
|
ddc2274aea | ||
|
|
6622829c67 | ||
|
|
a7c347ab35 | ||
|
|
72f720fa38 | ||
|
|
42de5168b1 | ||
|
|
30c7951505 | ||
|
|
5560cc448c | ||
|
|
1c89e45c14 | ||
|
|
ad6b5f84e5 | ||
|
|
4f67ce485a | ||
|
|
a2cc9b55ba | ||
|
|
9428923bab | ||
|
|
645b1e142e | ||
|
|
7a922f3e48 | ||
|
|
b481b73805 | ||
|
|
ed980601e1 | ||
|
|
be51cd3bc9 | ||
|
|
c807f0a6dd | ||
|
|
dc61e23886 | ||
|
|
6a0d44d632 | ||
|
|
fa4d6f9f8b | ||
|
|
0ae5027d98 | ||
|
|
eb18f4e155 | ||
|
|
2a11a30572 | ||
|
|
936c6cc74a | ||
|
|
38adbfdf34 | ||
|
|
ce23f8293a | ||
|
|
c010ec8b71 | ||
|
|
939d113d10 | ||
|
|
bb69819267 | ||
|
|
1c7b3c75a7 | ||
|
|
d0cee65cdc | ||
|
|
5ae738c4fe | ||
|
|
cb6e5e56c2 | ||
|
|
c909ae0152 | ||
|
|
06d18c106d | ||
|
|
d6470aab60 | ||
|
|
3a750e130c | ||
|
|
f111efeb6e | ||
|
|
e5f5d9ff56 | ||
|
|
75ffe51bbe | ||
|
|
73809817ff | ||
|
|
e4b38e2822 | ||
|
|
3d9b26fc28 | ||
|
|
8c08cf4619 | ||
|
|
940242c1ec | ||
|
|
3dd6266bcc | ||
|
|
6a97448928 | ||
|
|
c5aab9afe3 | ||
|
|
36084e7500 | ||
|
|
beebd73f95 | ||
|
|
0b99e9201d | ||
|
|
27a4682415 | ||
|
|
53ae77b13e | ||
|
|
baedc3ec0a | ||
|
|
6d530481c1 | ||
|
|
764871f97d | ||
|
|
5c216ad08f | ||
|
|
48307e46a3 | ||
|
|
1cbab0ebda | ||
|
|
dd5139e304 | ||
|
|
d5c22b80a5 | ||
|
|
726234eee5 | ||
|
|
3425988de7 | ||
|
|
7d0a008744 | ||
|
|
e3c2431c5b | ||
|
|
6f0d4f3f09 | ||
|
|
27370b679e | ||
|
|
718c9cbe3a | ||
|
|
e3bd521654 | ||
|
|
c0548eb632 | ||
|
|
95dc90609e | ||
|
|
2542a09abc | ||
|
|
520ef24fb9 | ||
|
|
8f0b5687a3 | ||
|
|
d2d01370bc | ||
|
|
8c29b7bf35 | ||
|
|
66fb0b1f35 | ||
|
|
e786da7774 | ||
|
|
2d6d796040 | ||
|
|
9c1d7f2405 | ||
|
|
9c175bc618 | ||
|
|
7a884eb416 | ||
|
|
697d98cac9 | ||
|
|
c897264b9b | ||
|
|
25c4c24e89 | ||
|
|
8bad536c6c | ||
|
|
cea379e7c7 | ||
|
|
084bedd16e | ||
|
|
e7e94b37f1 | ||
|
|
e395115807 | ||
|
|
f09bd0b75b | ||
|
|
11c9ed3362 | ||
|
|
54e9271504 | ||
|
|
719da8746e | ||
|
|
8b09e81496 | ||
|
|
a22da4315b | ||
|
|
75733c5cc1 | ||
|
|
463160c3f6 | ||
|
|
984e7e36c2 | ||
|
|
ecd19a9e58 | ||
|
|
80679ab906 | ||
|
|
55dd349472 | ||
|
|
1e3b07aae2 | ||
|
|
2238490069 | ||
|
|
7a7851aa06 | ||
|
|
7917e2c418 | ||
|
|
4a17951900 | ||
|
|
f257909699 | ||
|
|
3f156e0ece | ||
|
|
b34f1086fe | ||
|
|
c05c379b26 | ||
|
|
479be3cc91 | ||
|
|
2cbfc94bcb | ||
|
|
475892ca0e | ||
|
|
b78ede2f96 | ||
|
|
3729bec1a2 | ||
|
|
a7c5e41443 | ||
|
|
7824291252 | ||
|
|
295b9b704b | ||
|
|
bce69ae43d | ||
|
|
54d388d898 | ||
|
|
0c95ddbcd8 | ||
|
|
57b226532d | ||
|
|
e9fc87aab1 | ||
|
|
a892f985d3 | ||
|
|
e7fe5f7d3f | ||
|
|
f74d5d642e | ||
|
|
96d8769eae | ||
|
|
6adca37eb7 | ||
|
|
22da9f5f3f | ||
|
|
806a54908c | ||
|
|
f3aa26d6bf | ||
|
|
d6d559d50d | ||
|
|
07f23bd4ff | ||
|
|
45d045b2c5 | ||
|
|
77eba10f47 | ||
|
|
5acc7ba622 | ||
|
|
aab075345e | ||
|
|
9fd36efdb5 | ||
|
|
b48add4353 | ||
|
|
c50099161b | ||
|
|
f7667c614b | ||
|
|
86cf1d3ee1 | ||
|
|
90184255f8 | ||
|
|
7997f3b7f8 | ||
|
|
97b2191e99 | ||
|
|
30b00090ef | ||
|
|
cc3c343673 | ||
|
|
7ea80bcb22 | ||
|
|
60c7a17781 | ||
|
|
4dd05791a2 | ||
|
|
d55a365c6c | ||
|
|
3cbc4693f5 | ||
|
|
676c68d318 | ||
|
|
b66a4f48fa | ||
|
|
450c458f8f | ||
|
|
ece008f117 | ||
|
|
c8391d4ff1 | ||
|
|
4be7ca7b4c | ||
|
|
7d7a08e458 | ||
|
|
0758da8940 | ||
|
|
b507cd222b | ||
|
|
160bcaeb93 | ||
|
|
4b6b0a87b6 | ||
|
|
6dc4f592ba | ||
|
|
38faa74c23 | ||
|
|
3a068b26f3 | ||
|
|
f1248f8d9a | ||
|
|
4808441d29 | ||
|
|
4f75b230ed | ||
|
|
e512d3c6a6 | ||
|
|
d83b720c40 | ||
|
|
525226fb0b | ||
|
|
e7b1a44c5b | ||
|
|
1b272fa2f4 | ||
|
|
93caa568f9 | ||
|
|
ad04585e30 | ||
|
|
a1b105ac00 | ||
|
|
20f5cd7c95 | ||
|
|
6786fa9186 | ||
|
|
9317df7f16 | ||
|
|
57bb940c17 | ||
|
|
fad0962643 | ||
|
|
5395c409cb | ||
|
|
6470b30173 | ||
|
|
b65a1d4cfd | ||
|
|
29282371db | ||
|
|
e6806a08d4 | ||
|
|
f78564d75c | ||
|
|
bac9fb9a7c | ||
|
|
cb29b42285 | ||
|
|
204a16addc | ||
|
|
7cf2d2759d | ||
|
|
2900720cd3 | ||
|
|
d2f4153fe6 | ||
|
|
eafd8c580b | ||
|
|
ec0273fc92 | ||
|
|
a889cd14f3 | ||
|
|
9d302c1b57 | ||
|
|
da707d0755 | ||
|
|
de938a4451 | ||
|
|
56fe4ab382 | ||
|
|
43a98592c1 | ||
|
|
562b546bcc | ||
|
|
2c4741b5ed | ||
|
|
f02e55aaf7 | ||
|
|
6608089030 | ||
|
|
0e74fb4ec1 | ||
|
|
653489a1a9 | ||
|
|
799714c629 | ||
|
|
e72330aacc | ||
|
|
795c728f71 | ||
|
|
22fd844e8a | ||
|
|
f02f708f52 | ||
|
|
f709ab4cdf | ||
|
|
c706689413 | ||
|
|
e936fba428 | ||
|
|
cb25fa0d55 | ||
|
|
03b247cca1 | ||
|
|
0fa551c278 | ||
|
|
16f8fff14f | ||
|
|
991fd82532 | ||
|
|
f8a54d1d73 | ||
|
|
eef19954f3 | ||
|
|
39471a9c87 | ||
|
|
15271ac832 | ||
|
|
4cb5f4c353 | ||
|
|
12190ad728 | ||
|
|
21c1ce0bc1 | ||
|
|
9eb6f538f0 | ||
|
|
0d0458d1a7 | ||
|
|
e4046939d0 | ||
|
|
a8eb0f5b1b | ||
|
|
a43b9e4f33 | ||
|
|
9514bc4d67 | ||
|
|
00552918ac | ||
|
|
2d83505be9 | ||
|
|
f06cb59ab9 | ||
|
|
ad3f1a9e85 | ||
|
|
0b2f0307d7 | ||
|
|
4b84c9b28c | ||
|
|
74d04a4e80 | ||
|
|
e5913c8758 | ||
|
|
e39fdfddf1 | ||
|
|
a07238d14e | ||
|
|
806d4ae48f | ||
|
|
230376f183 | ||
|
|
fe35e13083 | ||
|
|
b972f394c8 | ||
|
|
add31f46d0 | ||
|
|
37a9e23c05 | ||
|
|
f43b48aebc | ||
|
|
19001e6cb9 | ||
|
|
855ba46f80 | ||
|
|
9b5cae045c | ||
|
|
7cfb643a1c | ||
|
|
2fa7266ebb | ||
|
|
a682f0d12b | ||
|
|
6c11c8dac6 | ||
|
|
0394c6e126 | ||
|
|
301dc3dfd2 | ||
|
|
0c848a25ad | ||
|
|
ac42e96e4c | ||
|
|
1a34c65e01 | ||
|
|
1b480914b4 | ||
|
|
97d91ec17c | ||
|
|
cd7abc495a | ||
|
|
ad9750403b | ||
|
|
1f9f4d8742 | ||
|
|
6baeaf4802 | ||
|
|
2f8dd1a161 |
@@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
// The optional 'workspaceFolder' property is the path VS Code should open by default when
|
// The optional 'workspaceFolder' property is the path VS Code should open by default when
|
||||||
// connected. This is typically a file mount in .devcontainer/docker-compose.yml
|
// connected. This is typically a file mount in .devcontainer/docker-compose.yml
|
||||||
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
|
"workspaceFolder": "/workspaces/langchain",
|
||||||
|
|
||||||
// Prevent the container from shutting down
|
// Prevent the container from shutting down
|
||||||
"overrideCommand": true
|
"overrideCommand": true
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ services:
|
|||||||
context: ..
|
context: ..
|
||||||
volumes:
|
volumes:
|
||||||
# Update this to wherever you want VS Code to mount the folder of your project
|
# Update this to wherever you want VS Code to mount the folder of your project
|
||||||
- ..:/workspaces:cached
|
- ..:/workspaces/langchain:cached
|
||||||
networks:
|
networks:
|
||||||
- langchain-network
|
- langchain-network
|
||||||
# environment:
|
# environment:
|
||||||
|
|||||||
15
.github/scripts/check_diff.py
vendored
15
.github/scripts/check_diff.py
vendored
@@ -19,6 +19,7 @@ if __name__ == "__main__":
|
|||||||
"test": set(),
|
"test": set(),
|
||||||
"extended-test": set(),
|
"extended-test": set(),
|
||||||
}
|
}
|
||||||
|
docs_edited = False
|
||||||
|
|
||||||
if len(files) == 300:
|
if len(files) == 300:
|
||||||
# max diff length is 300 files - there are likely files missing
|
# max diff length is 300 files - there are likely files missing
|
||||||
@@ -47,6 +48,17 @@ if __name__ == "__main__":
|
|||||||
found = True
|
found = True
|
||||||
if found:
|
if found:
|
||||||
dirs_to_run["extended-test"].add(dir_)
|
dirs_to_run["extended-test"].add(dir_)
|
||||||
|
elif file.startswith("libs/standard-tests"):
|
||||||
|
# TODO: update to include all packages that rely on standard-tests (all partner packages)
|
||||||
|
# note: won't run on external repo partners
|
||||||
|
dirs_to_run["lint"].add("libs/standard-tests")
|
||||||
|
dirs_to_run["test"].add("libs/partners/mistralai")
|
||||||
|
dirs_to_run["test"].add("libs/partners/openai")
|
||||||
|
dirs_to_run["test"].add("libs/partners/anthropic")
|
||||||
|
dirs_to_run["test"].add("libs/partners/ai21")
|
||||||
|
dirs_to_run["test"].add("libs/partners/fireworks")
|
||||||
|
dirs_to_run["test"].add("libs/partners/groq")
|
||||||
|
|
||||||
elif file.startswith("libs/cli"):
|
elif file.startswith("libs/cli"):
|
||||||
# todo: add cli makefile
|
# todo: add cli makefile
|
||||||
pass
|
pass
|
||||||
@@ -65,6 +77,8 @@ if __name__ == "__main__":
|
|||||||
"an update for this new library!"
|
"an update for this new library!"
|
||||||
)
|
)
|
||||||
elif any(file.startswith(p) for p in ["docs/", "templates/", "cookbook/"]):
|
elif any(file.startswith(p) for p in ["docs/", "templates/", "cookbook/"]):
|
||||||
|
if file.startswith("docs/"):
|
||||||
|
docs_edited = True
|
||||||
dirs_to_run["lint"].add(".")
|
dirs_to_run["lint"].add(".")
|
||||||
|
|
||||||
outputs = {
|
outputs = {
|
||||||
@@ -73,6 +87,7 @@ if __name__ == "__main__":
|
|||||||
),
|
),
|
||||||
"dirs-to-test": list(dirs_to_run["test"] | dirs_to_run["extended-test"]),
|
"dirs-to-test": list(dirs_to_run["test"] | dirs_to_run["extended-test"]),
|
||||||
"dirs-to-extended-test": list(dirs_to_run["extended-test"]),
|
"dirs-to-extended-test": list(dirs_to_run["extended-test"]),
|
||||||
|
"docs-edited": "true" if docs_edited else "",
|
||||||
}
|
}
|
||||||
for key, value in outputs.items():
|
for key, value in outputs.items():
|
||||||
json_output = json.dumps(value)
|
json_output = json.dumps(value)
|
||||||
|
|||||||
12
.github/scripts/get_min_versions.py
vendored
12
.github/scripts/get_min_versions.py
vendored
@@ -13,13 +13,16 @@ MIN_VERSION_LIBS = [
|
|||||||
|
|
||||||
|
|
||||||
def get_min_version(version: str) -> str:
|
def get_min_version(version: str) -> str:
|
||||||
|
# base regex for x.x.x with cases for rc/post/etc
|
||||||
|
# valid strings: https://peps.python.org/pep-0440/#public-version-identifiers
|
||||||
|
vstring = r"\d+(?:\.\d+){0,2}(?:(?:a|b|rc|\.post|\.dev)\d+)?"
|
||||||
# case ^x.x.x
|
# case ^x.x.x
|
||||||
_match = re.match(r"^\^(\d+(?:\.\d+){0,2})$", version)
|
_match = re.match(f"^\\^({vstring})$", version)
|
||||||
if _match:
|
if _match:
|
||||||
return _match.group(1)
|
return _match.group(1)
|
||||||
|
|
||||||
# case >=x.x.x,<y.y.y
|
# case >=x.x.x,<y.y.y
|
||||||
_match = re.match(r"^>=(\d+(?:\.\d+){0,2}),<(\d+(?:\.\d+){0,2})$", version)
|
_match = re.match(f"^>=({vstring}),<({vstring})$", version)
|
||||||
if _match:
|
if _match:
|
||||||
_min = _match.group(1)
|
_min = _match.group(1)
|
||||||
_max = _match.group(2)
|
_max = _match.group(2)
|
||||||
@@ -27,7 +30,7 @@ def get_min_version(version: str) -> str:
|
|||||||
return _min
|
return _min
|
||||||
|
|
||||||
# case x.x.x
|
# case x.x.x
|
||||||
_match = re.match(r"^(\d+(?:\.\d+){0,2})$", version)
|
_match = re.match(f"^({vstring})$", version)
|
||||||
if _match:
|
if _match:
|
||||||
return _match.group(1)
|
return _match.group(1)
|
||||||
|
|
||||||
@@ -52,6 +55,9 @@ def get_min_version_from_toml(toml_path: str):
|
|||||||
# Get the version string
|
# Get the version string
|
||||||
version_string = dependencies[lib]
|
version_string = dependencies[lib]
|
||||||
|
|
||||||
|
if isinstance(version_string, dict):
|
||||||
|
version_string = version_string["version"]
|
||||||
|
|
||||||
# Use parse_version to get the minimum supported version from version_string
|
# Use parse_version to get the minimum supported version from version_string
|
||||||
min_version = get_min_version(version_string)
|
min_version = get_min_version(version_string)
|
||||||
|
|
||||||
|
|||||||
2
.github/workflows/_integration_test.yml
vendored
2
.github/workflows/_integration_test.yml
vendored
@@ -58,6 +58,7 @@ jobs:
|
|||||||
MISTRAL_API_KEY: ${{ secrets.MISTRAL_API_KEY }}
|
MISTRAL_API_KEY: ${{ secrets.MISTRAL_API_KEY }}
|
||||||
TOGETHER_API_KEY: ${{ secrets.TOGETHER_API_KEY }}
|
TOGETHER_API_KEY: ${{ secrets.TOGETHER_API_KEY }}
|
||||||
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
||||||
|
GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }}
|
||||||
NVIDIA_API_KEY: ${{ secrets.NVIDIA_API_KEY }}
|
NVIDIA_API_KEY: ${{ secrets.NVIDIA_API_KEY }}
|
||||||
GOOGLE_SEARCH_API_KEY: ${{ secrets.GOOGLE_SEARCH_API_KEY }}
|
GOOGLE_SEARCH_API_KEY: ${{ secrets.GOOGLE_SEARCH_API_KEY }}
|
||||||
GOOGLE_CSE_ID: ${{ secrets.GOOGLE_CSE_ID }}
|
GOOGLE_CSE_ID: ${{ secrets.GOOGLE_CSE_ID }}
|
||||||
@@ -77,6 +78,7 @@ jobs:
|
|||||||
MONGODB_ATLAS_URI: ${{ secrets.MONGODB_ATLAS_URI }}
|
MONGODB_ATLAS_URI: ${{ secrets.MONGODB_ATLAS_URI }}
|
||||||
VOYAGE_API_KEY: ${{ secrets.VOYAGE_API_KEY }}
|
VOYAGE_API_KEY: ${{ secrets.VOYAGE_API_KEY }}
|
||||||
COHERE_API_KEY: ${{ secrets.COHERE_API_KEY }}
|
COHERE_API_KEY: ${{ secrets.COHERE_API_KEY }}
|
||||||
|
UPSTAGE_API_KEY: ${{ secrets.UPSTAGE_API_KEY }}
|
||||||
run: |
|
run: |
|
||||||
make integration_tests
|
make integration_tests
|
||||||
|
|
||||||
|
|||||||
26
.github/workflows/_release.yml
vendored
26
.github/workflows/_release.yml
vendored
@@ -13,6 +13,11 @@ on:
|
|||||||
required: true
|
required: true
|
||||||
type: string
|
type: string
|
||||||
default: 'libs/langchain'
|
default: 'libs/langchain'
|
||||||
|
dangerous-nonmaster-release:
|
||||||
|
required: false
|
||||||
|
type: boolean
|
||||||
|
default: false
|
||||||
|
description: "Release from a non-master branch (danger!)"
|
||||||
|
|
||||||
env:
|
env:
|
||||||
PYTHON_VERSION: "3.11"
|
PYTHON_VERSION: "3.11"
|
||||||
@@ -20,7 +25,7 @@ env:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
if: github.ref == 'refs/heads/master'
|
if: github.ref == 'refs/heads/master' || inputs.dangerous-nonmaster-release
|
||||||
environment: Scheduled testing
|
environment: Scheduled testing
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
@@ -75,6 +80,7 @@ jobs:
|
|||||||
./.github/workflows/_test_release.yml
|
./.github/workflows/_test_release.yml
|
||||||
with:
|
with:
|
||||||
working-directory: ${{ inputs.working-directory }}
|
working-directory: ${{ inputs.working-directory }}
|
||||||
|
dangerous-nonmaster-release: ${{ inputs.dangerous-nonmaster-release }}
|
||||||
secrets: inherit
|
secrets: inherit
|
||||||
|
|
||||||
pre-release-checks:
|
pre-release-checks:
|
||||||
@@ -112,7 +118,7 @@ jobs:
|
|||||||
PKG_NAME: ${{ needs.build.outputs.pkg-name }}
|
PKG_NAME: ${{ needs.build.outputs.pkg-name }}
|
||||||
VERSION: ${{ needs.build.outputs.version }}
|
VERSION: ${{ needs.build.outputs.version }}
|
||||||
# Here we use:
|
# Here we use:
|
||||||
# - The default regular PyPI index as the *primary* index, meaning
|
# - The default regular PyPI index as the *primary* index, meaning
|
||||||
# that it takes priority (https://pypi.org/simple)
|
# that it takes priority (https://pypi.org/simple)
|
||||||
# - The test PyPI index as an extra index, so that any dependencies that
|
# - The test PyPI index as an extra index, so that any dependencies that
|
||||||
# are not found on test PyPI can be resolved and installed anyway.
|
# are not found on test PyPI can be resolved and installed anyway.
|
||||||
@@ -171,7 +177,7 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
MIN_VERSIONS: ${{ steps.min-version.outputs.min-versions }}
|
MIN_VERSIONS: ${{ steps.min-version.outputs.min-versions }}
|
||||||
run: |
|
run: |
|
||||||
poetry run pip install $MIN_VERSIONS
|
poetry run pip install --force-reinstall $MIN_VERSIONS
|
||||||
make tests
|
make tests
|
||||||
working-directory: ${{ inputs.working-directory }}
|
working-directory: ${{ inputs.working-directory }}
|
||||||
|
|
||||||
@@ -215,6 +221,7 @@ jobs:
|
|||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # for airbyte
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # for airbyte
|
||||||
MONGODB_ATLAS_URI: ${{ secrets.MONGODB_ATLAS_URI }}
|
MONGODB_ATLAS_URI: ${{ secrets.MONGODB_ATLAS_URI }}
|
||||||
VOYAGE_API_KEY: ${{ secrets.VOYAGE_API_KEY }}
|
VOYAGE_API_KEY: ${{ secrets.VOYAGE_API_KEY }}
|
||||||
|
UPSTAGE_API_KEY: ${{ secrets.UPSTAGE_API_KEY }}
|
||||||
run: make integration_tests
|
run: make integration_tests
|
||||||
working-directory: ${{ inputs.working-directory }}
|
working-directory: ${{ inputs.working-directory }}
|
||||||
|
|
||||||
@@ -290,14 +297,13 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
name: dist
|
name: dist
|
||||||
path: ${{ inputs.working-directory }}/dist/
|
path: ${{ inputs.working-directory }}/dist/
|
||||||
|
|
||||||
- name: Create Release
|
- name: Create Tag
|
||||||
uses: ncipollo/release-action@v1
|
uses: ncipollo/release-action@v1
|
||||||
if: ${{ inputs.working-directory == 'libs/langchain' }}
|
|
||||||
with:
|
with:
|
||||||
artifacts: "dist/*"
|
artifacts: "dist/*"
|
||||||
token: ${{ secrets.GITHUB_TOKEN }}
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
draft: false
|
generateReleaseNotes: false
|
||||||
generateReleaseNotes: true
|
tag: ${{needs.build.outputs.pkg-name}}==${{ needs.build.outputs.version }}
|
||||||
tag: v${{ needs.build.outputs.version }}
|
body: "# Release ${{needs.build.outputs.pkg-name}}==${{ needs.build.outputs.version }}\n\nPackage-specific release note generation coming soon."
|
||||||
commit: master
|
commit: ${{ github.sha }}
|
||||||
|
|||||||
7
.github/workflows/_test_release.yml
vendored
7
.github/workflows/_test_release.yml
vendored
@@ -7,6 +7,11 @@ on:
|
|||||||
required: true
|
required: true
|
||||||
type: string
|
type: string
|
||||||
description: "From which folder this pipeline executes"
|
description: "From which folder this pipeline executes"
|
||||||
|
dangerous-nonmaster-release:
|
||||||
|
required: false
|
||||||
|
type: boolean
|
||||||
|
default: false
|
||||||
|
description: "Release from a non-master branch (danger!)"
|
||||||
|
|
||||||
env:
|
env:
|
||||||
POETRY_VERSION: "1.7.1"
|
POETRY_VERSION: "1.7.1"
|
||||||
@@ -14,7 +19,7 @@ env:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
if: github.ref == 'refs/heads/master'
|
if: github.ref == 'refs/heads/master' || inputs.dangerous-nonmaster-release
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
outputs:
|
outputs:
|
||||||
|
|||||||
7
.github/workflows/check_diffs.yml
vendored
7
.github/workflows/check_diffs.yml
vendored
@@ -36,6 +36,7 @@ jobs:
|
|||||||
dirs-to-lint: ${{ steps.set-matrix.outputs.dirs-to-lint }}
|
dirs-to-lint: ${{ steps.set-matrix.outputs.dirs-to-lint }}
|
||||||
dirs-to-test: ${{ steps.set-matrix.outputs.dirs-to-test }}
|
dirs-to-test: ${{ steps.set-matrix.outputs.dirs-to-test }}
|
||||||
dirs-to-extended-test: ${{ steps.set-matrix.outputs.dirs-to-extended-test }}
|
dirs-to-extended-test: ${{ steps.set-matrix.outputs.dirs-to-extended-test }}
|
||||||
|
docs-edited: ${{ steps.set-matrix.outputs.docs-edited }}
|
||||||
lint:
|
lint:
|
||||||
name: cd ${{ matrix.working-directory }}
|
name: cd ${{ matrix.working-directory }}
|
||||||
needs: [ build ]
|
needs: [ build ]
|
||||||
@@ -60,9 +61,9 @@ jobs:
|
|||||||
working-directory: ${{ matrix.working-directory }}
|
working-directory: ${{ matrix.working-directory }}
|
||||||
secrets: inherit
|
secrets: inherit
|
||||||
|
|
||||||
test_doc_imports:
|
test-doc-imports:
|
||||||
needs: [ build ]
|
needs: [ build ]
|
||||||
if: ${{ needs.build.outputs.dirs-to-test != '[]' }}
|
if: ${{ needs.build.outputs.dirs-to-test != '[]' || needs.build.outputs.docs-edited }}
|
||||||
uses: ./.github/workflows/_test_doc_imports.yml
|
uses: ./.github/workflows/_test_doc_imports.yml
|
||||||
secrets: inherit
|
secrets: inherit
|
||||||
|
|
||||||
@@ -140,7 +141,7 @@ jobs:
|
|||||||
echo "$STATUS" | grep 'nothing to commit, working tree clean'
|
echo "$STATUS" | grep 'nothing to commit, working tree clean'
|
||||||
ci_success:
|
ci_success:
|
||||||
name: "CI Success"
|
name: "CI Success"
|
||||||
needs: [build, lint, test, compile-integration-tests, dependencies, extended-tests]
|
needs: [build, lint, test, compile-integration-tests, dependencies, extended-tests, test-doc-imports]
|
||||||
if: |
|
if: |
|
||||||
always()
|
always()
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|||||||
42
.github/workflows/scheduled_test.yml
vendored
42
.github/workflows/scheduled_test.yml
vendored
@@ -10,19 +10,21 @@ env:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
defaults:
|
|
||||||
run:
|
|
||||||
working-directory: libs/langchain
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
environment: Scheduled testing
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
python-version:
|
python-version:
|
||||||
- "3.8"
|
- "3.8"
|
||||||
- "3.9"
|
|
||||||
- "3.10"
|
|
||||||
- "3.11"
|
- "3.11"
|
||||||
name: Python ${{ matrix.python-version }}
|
working-directory:
|
||||||
|
- "libs/partners/openai"
|
||||||
|
- "libs/partners/anthropic"
|
||||||
|
- "libs/partners/ai21"
|
||||||
|
- "libs/partners/fireworks"
|
||||||
|
- "libs/partners/groq"
|
||||||
|
- "libs/partners/mistralai"
|
||||||
|
- "libs/partners/together"
|
||||||
|
name: Python ${{ matrix.python-version }} - ${{ matrix.working-directory }}
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
@@ -31,7 +33,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
python-version: ${{ matrix.python-version }}
|
python-version: ${{ matrix.python-version }}
|
||||||
poetry-version: ${{ env.POETRY_VERSION }}
|
poetry-version: ${{ env.POETRY_VERSION }}
|
||||||
working-directory: libs/langchain
|
working-directory: ${{ matrix.working-directory }}
|
||||||
cache-key: scheduled
|
cache-key: scheduled
|
||||||
|
|
||||||
- name: 'Authenticate to Google Cloud'
|
- name: 'Authenticate to Google Cloud'
|
||||||
@@ -40,26 +42,15 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'
|
credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'
|
||||||
|
|
||||||
- name: Configure AWS Credentials
|
|
||||||
uses: aws-actions/configure-aws-credentials@v4
|
|
||||||
with:
|
|
||||||
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
|
|
||||||
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
|
||||||
aws-region: ${{ vars.AWS_REGION }}
|
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
working-directory: libs/langchain
|
working-directory: ${{ matrix.working-directory }}
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
echo "Running scheduled tests, installing dependencies with poetry..."
|
echo "Running scheduled tests, installing dependencies with poetry..."
|
||||||
poetry install --with=test_integration,test
|
poetry install --with=test_integration,test
|
||||||
|
|
||||||
- name: Install deps outside pyproject
|
- name: Run integration tests
|
||||||
if: ${{ startsWith(inputs.working-directory, 'libs/community/') }}
|
working-directory: ${{ matrix.working-directory }}
|
||||||
shell: bash
|
|
||||||
run: poetry run pip install "boto3<2" "google-cloud-aiplatform<2"
|
|
||||||
|
|
||||||
- name: Run tests
|
|
||||||
shell: bash
|
shell: bash
|
||||||
env:
|
env:
|
||||||
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
||||||
@@ -70,11 +61,16 @@ jobs:
|
|||||||
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME: ${{ secrets.AZURE_OPENAI_CHAT_DEPLOYMENT_NAME }}
|
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME: ${{ secrets.AZURE_OPENAI_CHAT_DEPLOYMENT_NAME }}
|
||||||
AZURE_OPENAI_LLM_DEPLOYMENT_NAME: ${{ secrets.AZURE_OPENAI_LLM_DEPLOYMENT_NAME }}
|
AZURE_OPENAI_LLM_DEPLOYMENT_NAME: ${{ secrets.AZURE_OPENAI_LLM_DEPLOYMENT_NAME }}
|
||||||
AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME: ${{ secrets.AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME }}
|
AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME: ${{ secrets.AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME }}
|
||||||
|
AI21_API_KEY: ${{ secrets.AI21_API_KEY }}
|
||||||
FIREWORKS_API_KEY: ${{ secrets.FIREWORKS_API_KEY }}
|
FIREWORKS_API_KEY: ${{ secrets.FIREWORKS_API_KEY }}
|
||||||
|
GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }}
|
||||||
|
MISTRAL_API_KEY: ${{ secrets.MISTRAL_API_KEY }}
|
||||||
|
TOGETHER_API_KEY: ${{ secrets.TOGETHER_API_KEY }}
|
||||||
run: |
|
run: |
|
||||||
make scheduled_tests
|
make integration_test
|
||||||
|
|
||||||
- name: Ensure the tests did not create any additional files
|
- name: Ensure the tests did not create any additional files
|
||||||
|
working-directory: ${{ matrix.working-directory }}
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
set -eu
|
set -eu
|
||||||
|
|||||||
15
Makefile
15
Makefile
@@ -17,16 +17,11 @@ clean: docs_clean api_docs_clean
|
|||||||
|
|
||||||
## docs_build: Build the documentation.
|
## docs_build: Build the documentation.
|
||||||
docs_build:
|
docs_build:
|
||||||
docs/.local_build.sh
|
cd docs && make build-local
|
||||||
|
|
||||||
## docs_clean: Clean the documentation build artifacts.
|
## docs_clean: Clean the documentation build artifacts.
|
||||||
docs_clean:
|
docs_clean:
|
||||||
@if [ -d _dist ]; then \
|
cd docs && make clean
|
||||||
rm -r _dist; \
|
|
||||||
echo "Directory _dist has been cleaned."; \
|
|
||||||
else \
|
|
||||||
echo "Nothing to clean."; \
|
|
||||||
fi
|
|
||||||
|
|
||||||
## docs_linkcheck: Run linkchecker on the documentation.
|
## docs_linkcheck: Run linkchecker on the documentation.
|
||||||
docs_linkcheck:
|
docs_linkcheck:
|
||||||
@@ -60,12 +55,12 @@ spell_fix:
|
|||||||
|
|
||||||
## lint: Run linting on the project.
|
## lint: Run linting on the project.
|
||||||
lint lint_package lint_tests:
|
lint lint_package lint_tests:
|
||||||
poetry run ruff docs templates cookbook
|
poetry run ruff check docs templates cookbook
|
||||||
poetry run ruff format docs templates cookbook --diff
|
poetry run ruff format docs templates cookbook --diff
|
||||||
poetry run ruff --select I docs templates cookbook
|
poetry run ruff check --select I docs templates cookbook
|
||||||
git grep 'from langchain import' docs/docs templates cookbook | grep -vE 'from langchain import (hub)' && exit 1 || exit 0
|
git grep 'from langchain import' docs/docs templates cookbook | grep -vE 'from langchain import (hub)' && exit 1 || exit 0
|
||||||
|
|
||||||
## format: Format the project files.
|
## format: Format the project files.
|
||||||
format format_diff:
|
format format_diff:
|
||||||
poetry run ruff format docs templates cookbook
|
poetry run ruff format docs templates cookbook
|
||||||
poetry run ruff --select I --fix docs templates cookbook
|
poetry run ruff check --select I --fix docs templates cookbook
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ For these applications, LangChain simplifies the entire application lifecycle:
|
|||||||
- **`langchain-community`**: Third party integrations.
|
- **`langchain-community`**: Third party integrations.
|
||||||
- Some integrations have been further split into **partner packages** that only rely on **`langchain-core`**. Examples include **`langchain_openai`** and **`langchain_anthropic`**.
|
- Some integrations have been further split into **partner packages** that only rely on **`langchain-core`**. Examples include **`langchain_openai`** and **`langchain_anthropic`**.
|
||||||
- **`langchain`**: Chains, agents, and retrieval strategies that make up an application's cognitive architecture.
|
- **`langchain`**: Chains, agents, and retrieval strategies that make up an application's cognitive architecture.
|
||||||
- **`[LangGraph](https://python.langchain.com/docs/langgraph)`**: A library for building robust and stateful multi-actor applications with LLMs by modeling steps as edges and nodes in a graph.
|
- **[`LangGraph`](https://python.langchain.com/docs/langgraph)**: A library for building robust and stateful multi-actor applications with LLMs by modeling steps as edges and nodes in a graph.
|
||||||
|
|
||||||
### Productionization:
|
### Productionization:
|
||||||
- **[LangSmith](https://python.langchain.com/docs/langsmith)**: A developer platform that lets you debug, test, evaluate, and monitor chains built on any LLM framework and seamlessly integrates with LangChain.
|
- **[LangSmith](https://python.langchain.com/docs/langsmith)**: A developer platform that lets you debug, test, evaluate, and monitor chains built on any LLM framework and seamlessly integrates with LangChain.
|
||||||
|
|||||||
@@ -464,8 +464,8 @@
|
|||||||
" Check if the base64 data is an image by looking at the start of the data\n",
|
" Check if the base64 data is an image by looking at the start of the data\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" image_signatures = {\n",
|
" image_signatures = {\n",
|
||||||
" b\"\\xFF\\xD8\\xFF\": \"jpg\",\n",
|
" b\"\\xff\\xd8\\xff\": \"jpg\",\n",
|
||||||
" b\"\\x89\\x50\\x4E\\x47\\x0D\\x0A\\x1A\\x0A\": \"png\",\n",
|
" b\"\\x89\\x50\\x4e\\x47\\x0d\\x0a\\x1a\\x0a\": \"png\",\n",
|
||||||
" b\"\\x47\\x49\\x46\\x38\": \"gif\",\n",
|
" b\"\\x47\\x49\\x46\\x38\": \"gif\",\n",
|
||||||
" b\"\\x52\\x49\\x46\\x46\": \"webp\",\n",
|
" b\"\\x52\\x49\\x46\\x46\": \"webp\",\n",
|
||||||
" }\n",
|
" }\n",
|
||||||
@@ -604,7 +604,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# Check retrieval\n",
|
"# Check retrieval\n",
|
||||||
"query = \"Give me company names that are interesting investments based on EV / NTM and NTM rev growth. Consider EV / NTM multiples vs historical?\"\n",
|
"query = \"Give me company names that are interesting investments based on EV / NTM and NTM rev growth. Consider EV / NTM multiples vs historical?\"\n",
|
||||||
"docs = retriever_multi_vector_img.get_relevant_documents(query, limit=6)\n",
|
"docs = retriever_multi_vector_img.invoke(query, limit=6)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# We get 4 docs\n",
|
"# We get 4 docs\n",
|
||||||
"len(docs)"
|
"len(docs)"
|
||||||
@@ -630,7 +630,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# Check retrieval\n",
|
"# Check retrieval\n",
|
||||||
"query = \"What are the EV / NTM and NTM rev growth for MongoDB, Cloudflare, and Datadog?\"\n",
|
"query = \"What are the EV / NTM and NTM rev growth for MongoDB, Cloudflare, and Datadog?\"\n",
|
||||||
"docs = retriever_multi_vector_img.get_relevant_documents(query, limit=6)\n",
|
"docs = retriever_multi_vector_img.invoke(query, limit=6)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# We get 4 docs\n",
|
"# We get 4 docs\n",
|
||||||
"len(docs)"
|
"len(docs)"
|
||||||
|
|||||||
@@ -185,7 +185,7 @@
|
|||||||
" )\n",
|
" )\n",
|
||||||
" # Text summary chain\n",
|
" # Text summary chain\n",
|
||||||
" model = VertexAI(\n",
|
" model = VertexAI(\n",
|
||||||
" temperature=0, model_name=\"gemini-pro\", max_output_tokens=1024\n",
|
" temperature=0, model_name=\"gemini-pro\", max_tokens=1024\n",
|
||||||
" ).with_fallbacks([empty_response])\n",
|
" ).with_fallbacks([empty_response])\n",
|
||||||
" summarize_chain = {\"element\": lambda x: x} | prompt | model | StrOutputParser()\n",
|
" summarize_chain = {\"element\": lambda x: x} | prompt | model | StrOutputParser()\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -254,9 +254,9 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"def image_summarize(img_base64, prompt):\n",
|
"def image_summarize(img_base64, prompt):\n",
|
||||||
" \"\"\"Make image summary\"\"\"\n",
|
" \"\"\"Make image summary\"\"\"\n",
|
||||||
" model = ChatVertexAI(model_name=\"gemini-pro-vision\", max_output_tokens=1024)\n",
|
" model = ChatVertexAI(model=\"gemini-pro-vision\", max_tokens=1024)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" msg = model(\n",
|
" msg = model.invoke(\n",
|
||||||
" [\n",
|
" [\n",
|
||||||
" HumanMessage(\n",
|
" HumanMessage(\n",
|
||||||
" content=[\n",
|
" content=[\n",
|
||||||
@@ -462,8 +462,8 @@
|
|||||||
" Check if the base64 data is an image by looking at the start of the data\n",
|
" Check if the base64 data is an image by looking at the start of the data\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" image_signatures = {\n",
|
" image_signatures = {\n",
|
||||||
" b\"\\xFF\\xD8\\xFF\": \"jpg\",\n",
|
" b\"\\xff\\xd8\\xff\": \"jpg\",\n",
|
||||||
" b\"\\x89\\x50\\x4E\\x47\\x0D\\x0A\\x1A\\x0A\": \"png\",\n",
|
" b\"\\x89\\x50\\x4e\\x47\\x0d\\x0a\\x1a\\x0a\": \"png\",\n",
|
||||||
" b\"\\x47\\x49\\x46\\x38\": \"gif\",\n",
|
" b\"\\x47\\x49\\x46\\x38\": \"gif\",\n",
|
||||||
" b\"\\x52\\x49\\x46\\x46\": \"webp\",\n",
|
" b\"\\x52\\x49\\x46\\x46\": \"webp\",\n",
|
||||||
" }\n",
|
" }\n",
|
||||||
@@ -553,9 +553,7 @@
|
|||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # Multi-modal LLM\n",
|
" # Multi-modal LLM\n",
|
||||||
" model = ChatVertexAI(\n",
|
" model = ChatVertexAI(temperature=0, model_name=\"gemini-pro-vision\", max_tokens=1024)\n",
|
||||||
" temperature=0, model_name=\"gemini-pro-vision\", max_output_tokens=1024\n",
|
|
||||||
" )\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" # RAG pipeline\n",
|
" # RAG pipeline\n",
|
||||||
" chain = (\n",
|
" chain = (\n",
|
||||||
@@ -604,7 +602,7 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"query = \"What are the EV / NTM and NTM rev growth for MongoDB, Cloudflare, and Datadog?\"\n",
|
"query = \"What are the EV / NTM and NTM rev growth for MongoDB, Cloudflare, and Datadog?\"\n",
|
||||||
"docs = retriever_multi_vector_img.get_relevant_documents(query, limit=1)\n",
|
"docs = retriever_multi_vector_img.invoke(query, limit=1)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# We get 2 docs\n",
|
"# We get 2 docs\n",
|
||||||
"len(docs)"
|
"len(docs)"
|
||||||
|
|||||||
@@ -535,9 +535,9 @@
|
|||||||
" print(f\"--Generated {len(all_clusters)} clusters--\")\n",
|
" print(f\"--Generated {len(all_clusters)} clusters--\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # Summarization\n",
|
" # Summarization\n",
|
||||||
" template = \"\"\"Here is a sub-set of LangChain Expression Langauge doc. \n",
|
" template = \"\"\"Here is a sub-set of LangChain Expression Language doc. \n",
|
||||||
" \n",
|
" \n",
|
||||||
" LangChain Expression Langauge provides a way to compose chain in LangChain.\n",
|
" LangChain Expression Language provides a way to compose chain in LangChain.\n",
|
||||||
" \n",
|
" \n",
|
||||||
" Give a detailed summary of the documentation provided.\n",
|
" Give a detailed summary of the documentation provided.\n",
|
||||||
" \n",
|
" \n",
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ Notebook | Description
|
|||||||
[press_releases.ipynb](https://github.com/langchain-ai/langchain/tree/master/cookbook/press_releases.ipynb) | Retrieve and query company press release data powered by [Kay.ai](https://kay.ai).
|
[press_releases.ipynb](https://github.com/langchain-ai/langchain/tree/master/cookbook/press_releases.ipynb) | Retrieve and query company press release data powered by [Kay.ai](https://kay.ai).
|
||||||
[program_aided_language_model.i...](https://github.com/langchain-ai/langchain/tree/master/cookbook/program_aided_language_model.ipynb) | Implement program-aided language models as described in the provided research paper.
|
[program_aided_language_model.i...](https://github.com/langchain-ai/langchain/tree/master/cookbook/program_aided_language_model.ipynb) | Implement program-aided language models as described in the provided research paper.
|
||||||
[qa_citations.ipynb](https://github.com/langchain-ai/langchain/tree/master/cookbook/qa_citations.ipynb) | Different ways to get a model to cite its sources.
|
[qa_citations.ipynb](https://github.com/langchain-ai/langchain/tree/master/cookbook/qa_citations.ipynb) | Different ways to get a model to cite its sources.
|
||||||
|
[rag_upstage_layout_analysis_groundedness_check.ipynb](https://github.com/langchain-ai/langchain/tree/master/cookbook/rag_upstage_layout_analysis_groundedness_check.ipynb) | End-to-end RAG example using Upstage Layout Analysis and Groundedness Check.
|
||||||
[retrieval_in_sql.ipynb](https://github.com/langchain-ai/langchain/tree/master/cookbook/retrieval_in_sql.ipynb) | Perform retrieval-augmented-generation (rag) on a PostgreSQL database using pgvector.
|
[retrieval_in_sql.ipynb](https://github.com/langchain-ai/langchain/tree/master/cookbook/retrieval_in_sql.ipynb) | Perform retrieval-augmented-generation (rag) on a PostgreSQL database using pgvector.
|
||||||
[sales_agent_with_context.ipynb](https://github.com/langchain-ai/langchain/tree/master/cookbook/sales_agent_with_context.ipynb) | Implement a context-aware ai sales agent, salesgpt, that can have natural sales conversations, interact with other systems, and use a product knowledge base to discuss a company's offerings.
|
[sales_agent_with_context.ipynb](https://github.com/langchain-ai/langchain/tree/master/cookbook/sales_agent_with_context.ipynb) | Implement a context-aware ai sales agent, salesgpt, that can have natural sales conversations, interact with other systems, and use a product knowledge base to discuss a company's offerings.
|
||||||
[self_query_hotel_search.ipynb](https://github.com/langchain-ai/langchain/tree/master/cookbook/self_query_hotel_search.ipynb) | Build a hotel room search feature with self-querying retrieval, using a specific hotel recommendation dataset.
|
[self_query_hotel_search.ipynb](https://github.com/langchain-ai/langchain/tree/master/cookbook/self_query_hotel_search.ipynb) | Build a hotel room search feature with self-querying retrieval, using a specific hotel recommendation dataset.
|
||||||
|
|||||||
@@ -75,7 +75,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"Apply to the [`LLaMA2`](https://arxiv.org/pdf/2307.09288.pdf) paper. \n",
|
"Apply to the [`LLaMA2`](https://arxiv.org/pdf/2307.09288.pdf) paper. \n",
|
||||||
"\n",
|
"\n",
|
||||||
"We use the Unstructured [`partition_pdf`](https://unstructured-io.github.io/unstructured/bricks/partition.html#partition-pdf), which segments a PDF document by using a layout model. \n",
|
"We use the Unstructured [`partition_pdf`](https://unstructured-io.github.io/unstructured/core/partition.html#partition-pdf), which segments a PDF document by using a layout model. \n",
|
||||||
"\n",
|
"\n",
|
||||||
"This layout model makes it possible to extract elements, such as tables, from pdfs. \n",
|
"This layout model makes it possible to extract elements, such as tables, from pdfs. \n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -562,9 +562,7 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# We can retrieve this table\n",
|
"# We can retrieve this table\n",
|
||||||
"retriever.get_relevant_documents(\n",
|
"retriever.invoke(\"What are results for LLaMA across across domains / subjects?\")[1]"
|
||||||
" \"What are results for LLaMA across across domains / subjects?\"\n",
|
|
||||||
")[1]"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -614,9 +612,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"retriever.get_relevant_documents(\"Images / figures with playful and creative examples\")[\n",
|
"retriever.invoke(\"Images / figures with playful and creative examples\")[1]"
|
||||||
" 1\n",
|
|
||||||
"]"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -501,9 +501,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"retriever.get_relevant_documents(\"Images / figures with playful and creative examples\")[\n",
|
"retriever.invoke(\"Images / figures with playful and creative examples\")[0]"
|
||||||
" 0\n",
|
|
||||||
"]"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -342,7 +342,7 @@
|
|||||||
"# Testing on retrieval\n",
|
"# Testing on retrieval\n",
|
||||||
"query = \"What percentage of CPI is dedicated to Housing, and how does it compare to the combined percentage of Medical Care, Apparel, and Other Goods and Services?\"\n",
|
"query = \"What percentage of CPI is dedicated to Housing, and how does it compare to the combined percentage of Medical Care, Apparel, and Other Goods and Services?\"\n",
|
||||||
"suffix_for_images = \" Include any pie charts, graphs, or tables.\"\n",
|
"suffix_for_images = \" Include any pie charts, graphs, or tables.\"\n",
|
||||||
"docs = retriever_multi_vector_img.get_relevant_documents(query + suffix_for_images)"
|
"docs = retriever_multi_vector_img.invoke(query + suffix_for_images)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -532,8 +532,8 @@
|
|||||||
"def is_image_data(b64data):\n",
|
"def is_image_data(b64data):\n",
|
||||||
" \"\"\"Check if the base64 data is an image by looking at the start of the data.\"\"\"\n",
|
" \"\"\"Check if the base64 data is an image by looking at the start of the data.\"\"\"\n",
|
||||||
" image_signatures = {\n",
|
" image_signatures = {\n",
|
||||||
" b\"\\xFF\\xD8\\xFF\": \"jpg\",\n",
|
" b\"\\xff\\xd8\\xff\": \"jpg\",\n",
|
||||||
" b\"\\x89\\x50\\x4E\\x47\\x0D\\x0A\\x1A\\x0A\": \"png\",\n",
|
" b\"\\x89\\x50\\x4e\\x47\\x0d\\x0a\\x1a\\x0a\": \"png\",\n",
|
||||||
" b\"\\x47\\x49\\x46\\x38\": \"gif\",\n",
|
" b\"\\x47\\x49\\x46\\x38\": \"gif\",\n",
|
||||||
" b\"\\x52\\x49\\x46\\x46\": \"webp\",\n",
|
" b\"\\x52\\x49\\x46\\x46\": \"webp\",\n",
|
||||||
" }\n",
|
" }\n",
|
||||||
|
|||||||
@@ -90,7 +90,7 @@
|
|||||||
" ) -> AIMessage:\n",
|
" ) -> AIMessage:\n",
|
||||||
" messages = self.update_messages(input_message)\n",
|
" messages = self.update_messages(input_message)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" output_message = self.model(messages)\n",
|
" output_message = self.model.invoke(messages)\n",
|
||||||
" self.update_messages(output_message)\n",
|
" self.update_messages(output_message)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" return output_message"
|
" return output_message"
|
||||||
|
|||||||
557
cookbook/cql_agent.ipynb
Normal file
557
cookbook/cql_agent.ipynb
Normal file
@@ -0,0 +1,557 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Setup Environment"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Python Modules"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Install the following Python modules:\n",
|
||||||
|
"\n",
|
||||||
|
"```bash\n",
|
||||||
|
"pip install ipykernel python-dotenv cassio pandas langchain_openai langchain langchain-community langchainhub langchain_experimental openai-multi-tool-use-parallel-patch\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Load the `.env` File"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Connection is via `cassio` using `auto=True` parameter, and the notebook uses OpenAI. You should create a `.env` file accordingly.\n",
|
||||||
|
"\n",
|
||||||
|
"For Casssandra, set:\n",
|
||||||
|
"```bash\n",
|
||||||
|
"CASSANDRA_CONTACT_POINTS\n",
|
||||||
|
"CASSANDRA_USERNAME\n",
|
||||||
|
"CASSANDRA_PASSWORD\n",
|
||||||
|
"CASSANDRA_KEYSPACE\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"For Astra, set:\n",
|
||||||
|
"```bash\n",
|
||||||
|
"ASTRA_DB_APPLICATION_TOKEN\n",
|
||||||
|
"ASTRA_DB_DATABASE_ID\n",
|
||||||
|
"ASTRA_DB_KEYSPACE\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"For example:\n",
|
||||||
|
"\n",
|
||||||
|
"```bash\n",
|
||||||
|
"# Connection to Astra:\n",
|
||||||
|
"ASTRA_DB_DATABASE_ID=a1b2c3d4-...\n",
|
||||||
|
"ASTRA_DB_APPLICATION_TOKEN=AstraCS:...\n",
|
||||||
|
"ASTRA_DB_KEYSPACE=notebooks\n",
|
||||||
|
"\n",
|
||||||
|
"# Also set \n",
|
||||||
|
"OPENAI_API_KEY=sk-....\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"(You may also modify the below code to directly connect with `cassio`.)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from dotenv import load_dotenv\n",
|
||||||
|
"\n",
|
||||||
|
"load_dotenv(override=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Connect to Cassandra"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"\n",
|
||||||
|
"import cassio\n",
|
||||||
|
"\n",
|
||||||
|
"cassio.init(auto=True)\n",
|
||||||
|
"session = cassio.config.resolve_session()\n",
|
||||||
|
"if not session:\n",
|
||||||
|
" raise Exception(\n",
|
||||||
|
" \"Check environment configuration or manually configure cassio connection parameters\"\n",
|
||||||
|
" )\n",
|
||||||
|
"\n",
|
||||||
|
"keyspace = os.environ.get(\n",
|
||||||
|
" \"ASTRA_DB_KEYSPACE\", os.environ.get(\"CASSANDRA_KEYSPACE\", None)\n",
|
||||||
|
")\n",
|
||||||
|
"if not keyspace:\n",
|
||||||
|
" raise ValueError(\"a KEYSPACE environment variable must be set\")\n",
|
||||||
|
"\n",
|
||||||
|
"session.set_keyspace(keyspace)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Setup Database"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"This needs to be done one time only!"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Download Data"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"The dataset used is from Kaggle, the [Environmental Sensor Telemetry Data](https://www.kaggle.com/datasets/garystafford/environmental-sensor-data-132k?select=iot_telemetry_data.csv). The next cell will download and unzip the data into a Pandas dataframe. The following cell is instructions to download manually. \n",
|
||||||
|
"\n",
|
||||||
|
"The net result of this section is you should have a Pandas dataframe variable `df`."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"#### Download Automatically"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from io import BytesIO\n",
|
||||||
|
"from zipfile import ZipFile\n",
|
||||||
|
"\n",
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"import requests\n",
|
||||||
|
"\n",
|
||||||
|
"datasetURL = \"https://storage.googleapis.com/kaggle-data-sets/788816/1355729/bundle/archive.zip?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=gcp-kaggle-com%40kaggle-161607.iam.gserviceaccount.com%2F20240404%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20240404T115828Z&X-Goog-Expires=259200&X-Goog-SignedHeaders=host&X-Goog-Signature=2849f003b100eb9dcda8dd8535990f51244292f67e4f5fad36f14aa67f2d4297672d8fe6ff5a39f03a29cda051e33e95d36daab5892b8874dcd5a60228df0361fa26bae491dd4371f02dd20306b583a44ba85a4474376188b1f84765147d3b4f05c57345e5de883c2c29653cce1f3755cd8e645c5e952f4fb1c8a735b22f0c811f97f7bce8d0235d0d3731ca8ab4629ff381f3bae9e35fc1b181c1e69a9c7913a5e42d9d52d53e5f716467205af9c8a3cc6746fc5352e8fbc47cd7d18543626bd67996d18c2045c1e475fc136df83df352fa747f1a3bb73e6ba3985840792ec1de407c15836640ec96db111b173bf16115037d53fdfbfd8ac44145d7f9a546aa\"\n",
|
||||||
|
"\n",
|
||||||
|
"response = requests.get(datasetURL)\n",
|
||||||
|
"if response.status_code == 200:\n",
|
||||||
|
" zip_file = ZipFile(BytesIO(response.content))\n",
|
||||||
|
" csv_file_name = zip_file.namelist()[0]\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\"Failed to download the file\")\n",
|
||||||
|
"\n",
|
||||||
|
"with zip_file.open(csv_file_name) as csv_file:\n",
|
||||||
|
" df = pd.read_csv(csv_file)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"#### Download Manually"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"You can download the `.zip` file and unpack the `.csv` contained within. Comment in the next line, and adjust the path to this `.csv` file appropriately."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# df = pd.read_csv(\"/path/to/iot_telemetry_data.csv\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Load Data into Cassandra"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"This section assumes the existence of a dataframe `df`, the following cell validates its structure. The Download section above creates this object."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"assert df is not None, \"Dataframe 'df' must be set\"\n",
|
||||||
|
"expected_columns = [\n",
|
||||||
|
" \"ts\",\n",
|
||||||
|
" \"device\",\n",
|
||||||
|
" \"co\",\n",
|
||||||
|
" \"humidity\",\n",
|
||||||
|
" \"light\",\n",
|
||||||
|
" \"lpg\",\n",
|
||||||
|
" \"motion\",\n",
|
||||||
|
" \"smoke\",\n",
|
||||||
|
" \"temp\",\n",
|
||||||
|
"]\n",
|
||||||
|
"assert all(\n",
|
||||||
|
" [column in df.columns for column in expected_columns]\n",
|
||||||
|
"), \"DataFrame does not have the expected columns\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Create and load tables:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from datetime import UTC, datetime\n",
|
||||||
|
"\n",
|
||||||
|
"from cassandra.query import BatchStatement\n",
|
||||||
|
"\n",
|
||||||
|
"# Create sensors table\n",
|
||||||
|
"table_query = \"\"\"\n",
|
||||||
|
"CREATE TABLE IF NOT EXISTS iot_sensors (\n",
|
||||||
|
" device text,\n",
|
||||||
|
" conditions text,\n",
|
||||||
|
" room text,\n",
|
||||||
|
" PRIMARY KEY (device)\n",
|
||||||
|
")\n",
|
||||||
|
"WITH COMMENT = 'Environmental IoT room sensor metadata.';\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
"session.execute(table_query)\n",
|
||||||
|
"\n",
|
||||||
|
"pstmt = session.prepare(\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
"INSERT INTO iot_sensors (device, conditions, room)\n",
|
||||||
|
"VALUES (?, ?, ?)\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"devices = [\n",
|
||||||
|
" (\"00:0f:00:70:91:0a\", \"stable conditions, cooler and more humid\", \"room 1\"),\n",
|
||||||
|
" (\"1c:bf:ce:15:ec:4d\", \"highly variable temperature and humidity\", \"room 2\"),\n",
|
||||||
|
" (\"b8:27:eb:bf:9d:51\", \"stable conditions, warmer and dryer\", \"room 3\"),\n",
|
||||||
|
"]\n",
|
||||||
|
"\n",
|
||||||
|
"for device, conditions, room in devices:\n",
|
||||||
|
" session.execute(pstmt, (device, conditions, room))\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"Sensors inserted successfully.\")\n",
|
||||||
|
"\n",
|
||||||
|
"# Create data table\n",
|
||||||
|
"table_query = \"\"\"\n",
|
||||||
|
"CREATE TABLE IF NOT EXISTS iot_data (\n",
|
||||||
|
" day text,\n",
|
||||||
|
" device text,\n",
|
||||||
|
" ts timestamp,\n",
|
||||||
|
" co double,\n",
|
||||||
|
" humidity double,\n",
|
||||||
|
" light boolean,\n",
|
||||||
|
" lpg double,\n",
|
||||||
|
" motion boolean,\n",
|
||||||
|
" smoke double,\n",
|
||||||
|
" temp double,\n",
|
||||||
|
" PRIMARY KEY ((day, device), ts)\n",
|
||||||
|
")\n",
|
||||||
|
"WITH COMMENT = 'Data from environmental IoT room sensors. Columns include device identifier, timestamp (ts) of the data collection, carbon monoxide level (co), relative humidity, light presence, LPG concentration, motion detection, smoke concentration, and temperature (temp). Data is partitioned by day and device.';\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
"session.execute(table_query)\n",
|
||||||
|
"\n",
|
||||||
|
"pstmt = session.prepare(\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
"INSERT INTO iot_data (day, device, ts, co, humidity, light, lpg, motion, smoke, temp)\n",
|
||||||
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def insert_data_batch(name, group):\n",
|
||||||
|
" batch = BatchStatement()\n",
|
||||||
|
" day, device = name\n",
|
||||||
|
" print(f\"Inserting batch for day: {day}, device: {device}\")\n",
|
||||||
|
"\n",
|
||||||
|
" for _, row in group.iterrows():\n",
|
||||||
|
" timestamp = datetime.fromtimestamp(row[\"ts\"], UTC)\n",
|
||||||
|
" batch.add(\n",
|
||||||
|
" pstmt,\n",
|
||||||
|
" (\n",
|
||||||
|
" day,\n",
|
||||||
|
" row[\"device\"],\n",
|
||||||
|
" timestamp,\n",
|
||||||
|
" row[\"co\"],\n",
|
||||||
|
" row[\"humidity\"],\n",
|
||||||
|
" row[\"light\"],\n",
|
||||||
|
" row[\"lpg\"],\n",
|
||||||
|
" row[\"motion\"],\n",
|
||||||
|
" row[\"smoke\"],\n",
|
||||||
|
" row[\"temp\"],\n",
|
||||||
|
" ),\n",
|
||||||
|
" )\n",
|
||||||
|
"\n",
|
||||||
|
" session.execute(batch)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"# Convert columns to appropriate types\n",
|
||||||
|
"df[\"light\"] = df[\"light\"] == \"true\"\n",
|
||||||
|
"df[\"motion\"] = df[\"motion\"] == \"true\"\n",
|
||||||
|
"df[\"ts\"] = df[\"ts\"].astype(float)\n",
|
||||||
|
"df[\"day\"] = df[\"ts\"].apply(\n",
|
||||||
|
" lambda x: datetime.fromtimestamp(x, UTC).strftime(\"%Y-%m-%d\")\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"grouped_df = df.groupby([\"day\", \"device\"])\n",
|
||||||
|
"\n",
|
||||||
|
"for name, group in grouped_df:\n",
|
||||||
|
" insert_data_batch(name, group)\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"Data load complete\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"print(session.keyspace)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Load the Tools"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Python `import` statements for the demo:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain.agents import AgentExecutor, create_openai_tools_agent\n",
|
||||||
|
"from langchain_community.agent_toolkits.cassandra_database.toolkit import (\n",
|
||||||
|
" CassandraDatabaseToolkit,\n",
|
||||||
|
")\n",
|
||||||
|
"from langchain_community.tools.cassandra_database.prompt import QUERY_PATH_PROMPT\n",
|
||||||
|
"from langchain_community.tools.cassandra_database.tool import (\n",
|
||||||
|
" GetSchemaCassandraDatabaseTool,\n",
|
||||||
|
" GetTableDataCassandraDatabaseTool,\n",
|
||||||
|
" QueryCassandraDatabaseTool,\n",
|
||||||
|
")\n",
|
||||||
|
"from langchain_community.utilities.cassandra_database import CassandraDatabase\n",
|
||||||
|
"from langchain_openai import ChatOpenAI"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"The `CassandraDatabase` object is loaded from `cassio`, though it does accept a `Session`-type parameter as an alternative."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Create a CassandraDatabase instance\n",
|
||||||
|
"db = CassandraDatabase(include_tables=[\"iot_sensors\", \"iot_data\"])\n",
|
||||||
|
"\n",
|
||||||
|
"# Create the Cassandra Database tools\n",
|
||||||
|
"query_tool = QueryCassandraDatabaseTool(db=db)\n",
|
||||||
|
"schema_tool = GetSchemaCassandraDatabaseTool(db=db)\n",
|
||||||
|
"select_data_tool = GetTableDataCassandraDatabaseTool(db=db)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"The tools can be invoked directly:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Test the tools\n",
|
||||||
|
"print(\"Executing a CQL query:\")\n",
|
||||||
|
"query = \"SELECT * FROM iot_sensors LIMIT 5;\"\n",
|
||||||
|
"result = query_tool.run({\"query\": query})\n",
|
||||||
|
"print(result)\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"\\nGetting the schema for a keyspace:\")\n",
|
||||||
|
"schema = schema_tool.run({\"keyspace\": keyspace})\n",
|
||||||
|
"print(schema)\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"\\nGetting data from a table:\")\n",
|
||||||
|
"table = \"iot_data\"\n",
|
||||||
|
"predicate = \"day = '2020-07-14' and device = 'b8:27:eb:bf:9d:51'\"\n",
|
||||||
|
"data = select_data_tool.run(\n",
|
||||||
|
" {\"keyspace\": keyspace, \"table\": table, \"predicate\": predicate, \"limit\": 5}\n",
|
||||||
|
")\n",
|
||||||
|
"print(data)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Agent Configuration"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain.agents import Tool\n",
|
||||||
|
"from langchain_experimental.utilities import PythonREPL\n",
|
||||||
|
"\n",
|
||||||
|
"python_repl = PythonREPL()\n",
|
||||||
|
"\n",
|
||||||
|
"repl_tool = Tool(\n",
|
||||||
|
" name=\"python_repl\",\n",
|
||||||
|
" description=\"A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.\",\n",
|
||||||
|
" func=python_repl.run,\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain import hub\n",
|
||||||
|
"\n",
|
||||||
|
"llm = ChatOpenAI(temperature=0, model=\"gpt-4-1106-preview\")\n",
|
||||||
|
"toolkit = CassandraDatabaseToolkit(db=db)\n",
|
||||||
|
"\n",
|
||||||
|
"# context = toolkit.get_context()\n",
|
||||||
|
"# tools = toolkit.get_tools()\n",
|
||||||
|
"tools = [schema_tool, select_data_tool, repl_tool]\n",
|
||||||
|
"\n",
|
||||||
|
"input = (\n",
|
||||||
|
" QUERY_PATH_PROMPT\n",
|
||||||
|
" + f\"\"\"\n",
|
||||||
|
"\n",
|
||||||
|
"Here is your task: In the {keyspace} keyspace, find the total number of times the temperature of each device has exceeded 23 degrees on July 14, 2020.\n",
|
||||||
|
" Create a summary report including the name of the room. Use Pandas if helpful.\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"prompt = hub.pull(\"hwchase17/openai-tools-agent\")\n",
|
||||||
|
"\n",
|
||||||
|
"# messages = [\n",
|
||||||
|
"# HumanMessagePromptTemplate.from_template(input),\n",
|
||||||
|
"# AIMessage(content=QUERY_PATH_PROMPT),\n",
|
||||||
|
"# MessagesPlaceholder(variable_name=\"agent_scratchpad\"),\n",
|
||||||
|
"# ]\n",
|
||||||
|
"\n",
|
||||||
|
"# prompt = ChatPromptTemplate.from_messages(messages)\n",
|
||||||
|
"# print(prompt)\n",
|
||||||
|
"\n",
|
||||||
|
"# Choose the LLM that will drive the agent\n",
|
||||||
|
"# Only certain models support this\n",
|
||||||
|
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-1106\", temperature=0)\n",
|
||||||
|
"\n",
|
||||||
|
"# Construct the OpenAI Tools agent\n",
|
||||||
|
"agent = create_openai_tools_agent(llm, tools, prompt)\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"Available tools:\")\n",
|
||||||
|
"for tool in tools:\n",
|
||||||
|
" print(\"\\t\" + tool.name + \" - \" + tool.description + \" - \" + str(tool))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)\n",
|
||||||
|
"\n",
|
||||||
|
"response = agent_executor.invoke({\"input\": input})\n",
|
||||||
|
"\n",
|
||||||
|
"print(response[\"output\"])"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.9.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
||||||
@@ -169,7 +169,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"def get_tools(query):\n",
|
"def get_tools(query):\n",
|
||||||
" # Get documents, which contain the Plugins to use\n",
|
" # Get documents, which contain the Plugins to use\n",
|
||||||
" docs = retriever.get_relevant_documents(query)\n",
|
" docs = retriever.invoke(query)\n",
|
||||||
" # Get the toolkits, one for each plugin\n",
|
" # Get the toolkits, one for each plugin\n",
|
||||||
" tool_kits = [toolkits_dict[d.metadata[\"plugin_name\"]] for d in docs]\n",
|
" tool_kits = [toolkits_dict[d.metadata[\"plugin_name\"]] for d in docs]\n",
|
||||||
" # Get the tools: a separate NLAChain for each endpoint\n",
|
" # Get the tools: a separate NLAChain for each endpoint\n",
|
||||||
|
|||||||
@@ -193,7 +193,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"def get_tools(query):\n",
|
"def get_tools(query):\n",
|
||||||
" # Get documents, which contain the Plugins to use\n",
|
" # Get documents, which contain the Plugins to use\n",
|
||||||
" docs = retriever.get_relevant_documents(query)\n",
|
" docs = retriever.invoke(query)\n",
|
||||||
" # Get the toolkits, one for each plugin\n",
|
" # Get the toolkits, one for each plugin\n",
|
||||||
" tool_kits = [toolkits_dict[d.metadata[\"plugin_name\"]] for d in docs]\n",
|
" tool_kits = [toolkits_dict[d.metadata[\"plugin_name\"]] for d in docs]\n",
|
||||||
" # Get the tools: a separate NLAChain for each endpoint\n",
|
" # Get the tools: a separate NLAChain for each endpoint\n",
|
||||||
|
|||||||
@@ -142,7 +142,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def get_tools(query):\n",
|
"def get_tools(query):\n",
|
||||||
" docs = retriever.get_relevant_documents(query)\n",
|
" docs = retriever.invoke(query)\n",
|
||||||
" return [ALL_TOOLS[d.metadata[\"index\"]] for d in docs]"
|
" return [ALL_TOOLS[d.metadata[\"index\"]] for d in docs]"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -362,7 +362,7 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"llm = OpenAI()\n",
|
"llm = OpenAI()\n",
|
||||||
"llm(query)"
|
"llm.invoke(query)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -108,7 +108,7 @@
|
|||||||
" return obs_message\n",
|
" return obs_message\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def _act(self):\n",
|
" def _act(self):\n",
|
||||||
" act_message = self.model(self.message_history)\n",
|
" act_message = self.model.invoke(self.message_history)\n",
|
||||||
" self.message_history.append(act_message)\n",
|
" self.message_history.append(act_message)\n",
|
||||||
" action = int(self.action_parser.parse(act_message.content)[\"action\"])\n",
|
" action = int(self.action_parser.parse(act_message.content)[\"action\"])\n",
|
||||||
" return action\n",
|
" return action\n",
|
||||||
|
|||||||
@@ -206,7 +206,7 @@
|
|||||||
" print(\"---RETRIEVE---\")\n",
|
" print(\"---RETRIEVE---\")\n",
|
||||||
" state_dict = state[\"keys\"]\n",
|
" state_dict = state[\"keys\"]\n",
|
||||||
" question = state_dict[\"question\"]\n",
|
" question = state_dict[\"question\"]\n",
|
||||||
" documents = retriever.get_relevant_documents(question)\n",
|
" documents = retriever.invoke(question)\n",
|
||||||
" return {\"keys\": {\"documents\": documents, \"question\": question}}\n",
|
" return {\"keys\": {\"documents\": documents, \"question\": question}}\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -213,7 +213,7 @@
|
|||||||
" print(\"---RETRIEVE---\")\n",
|
" print(\"---RETRIEVE---\")\n",
|
||||||
" state_dict = state[\"keys\"]\n",
|
" state_dict = state[\"keys\"]\n",
|
||||||
" question = state_dict[\"question\"]\n",
|
" question = state_dict[\"question\"]\n",
|
||||||
" documents = retriever.get_relevant_documents(question)\n",
|
" documents = retriever.invoke(question)\n",
|
||||||
" return {\"keys\": {\"documents\": documents, \"question\": question}}\n",
|
" return {\"keys\": {\"documents\": documents, \"question\": question}}\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -435,7 +435,7 @@
|
|||||||
" display(HTML(image_html))\n",
|
" display(HTML(image_html))\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"docs = retriever.get_relevant_documents(\"Woman with children\", k=10)\n",
|
"docs = retriever.invoke(\"Woman with children\", k=10)\n",
|
||||||
"for doc in docs:\n",
|
"for doc in docs:\n",
|
||||||
" if is_base64(doc.page_content):\n",
|
" if is_base64(doc.page_content):\n",
|
||||||
" plt_img_base64(doc.page_content)\n",
|
" plt_img_base64(doc.page_content)\n",
|
||||||
|
|||||||
@@ -443,7 +443,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"query = \"Woman with children\"\n",
|
"query = \"Woman with children\"\n",
|
||||||
"docs = retriever.get_relevant_documents(query, k=10)\n",
|
"docs = retriever.invoke(query, k=10)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"for doc in docs:\n",
|
"for doc in docs:\n",
|
||||||
" if is_base64(doc.page_content):\n",
|
" if is_base64(doc.page_content):\n",
|
||||||
|
|||||||
@@ -74,7 +74,7 @@
|
|||||||
" Applies the chatmodel to the message history\n",
|
" Applies the chatmodel to the message history\n",
|
||||||
" and returns the message string\n",
|
" and returns the message string\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" message = self.model(\n",
|
" message = self.model.invoke(\n",
|
||||||
" [\n",
|
" [\n",
|
||||||
" self.system_message,\n",
|
" self.system_message,\n",
|
||||||
" HumanMessage(content=\"\\n\".join(self.message_history + [self.prefix])),\n",
|
" HumanMessage(content=\"\\n\".join(self.message_history + [self.prefix])),\n",
|
||||||
|
|||||||
@@ -79,7 +79,7 @@
|
|||||||
" Applies the chatmodel to the message history\n",
|
" Applies the chatmodel to the message history\n",
|
||||||
" and returns the message string\n",
|
" and returns the message string\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" message = self.model(\n",
|
" message = self.model.invoke(\n",
|
||||||
" [\n",
|
" [\n",
|
||||||
" self.system_message,\n",
|
" self.system_message,\n",
|
||||||
" HumanMessage(content=\"\\n\".join(self.message_history + [self.prefix])),\n",
|
" HumanMessage(content=\"\\n\".join(self.message_history + [self.prefix])),\n",
|
||||||
@@ -234,7 +234,7 @@
|
|||||||
" termination_clause=self.termination_clause if self.stop else \"\",\n",
|
" termination_clause=self.termination_clause if self.stop else \"\",\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
"\n",
|
"\n",
|
||||||
" self.response = self.model(\n",
|
" self.response = self.model.invoke(\n",
|
||||||
" [\n",
|
" [\n",
|
||||||
" self.system_message,\n",
|
" self.system_message,\n",
|
||||||
" HumanMessage(content=response_prompt),\n",
|
" HumanMessage(content=response_prompt),\n",
|
||||||
@@ -263,7 +263,7 @@
|
|||||||
" speaker_names=speaker_names,\n",
|
" speaker_names=speaker_names,\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
"\n",
|
"\n",
|
||||||
" choice_string = self.model(\n",
|
" choice_string = self.model.invoke(\n",
|
||||||
" [\n",
|
" [\n",
|
||||||
" self.system_message,\n",
|
" self.system_message,\n",
|
||||||
" HumanMessage(content=choice_prompt),\n",
|
" HumanMessage(content=choice_prompt),\n",
|
||||||
@@ -299,7 +299,7 @@
|
|||||||
" ),\n",
|
" ),\n",
|
||||||
" next_speaker=self.next_speaker,\n",
|
" next_speaker=self.next_speaker,\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
" message = self.model(\n",
|
" message = self.model.invoke(\n",
|
||||||
" [\n",
|
" [\n",
|
||||||
" self.system_message,\n",
|
" self.system_message,\n",
|
||||||
" HumanMessage(content=next_prompt),\n",
|
" HumanMessage(content=next_prompt),\n",
|
||||||
|
|||||||
@@ -71,7 +71,7 @@
|
|||||||
" Applies the chatmodel to the message history\n",
|
" Applies the chatmodel to the message history\n",
|
||||||
" and returns the message string\n",
|
" and returns the message string\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" message = self.model(\n",
|
" message = self.model.invoke(\n",
|
||||||
" [\n",
|
" [\n",
|
||||||
" self.system_message,\n",
|
" self.system_message,\n",
|
||||||
" HumanMessage(content=\"\\n\".join(self.message_history + [self.prefix])),\n",
|
" HumanMessage(content=\"\\n\".join(self.message_history + [self.prefix])),\n",
|
||||||
@@ -164,7 +164,7 @@
|
|||||||
" message_history=\"\\n\".join(self.message_history),\n",
|
" message_history=\"\\n\".join(self.message_history),\n",
|
||||||
" recent_message=self.message_history[-1],\n",
|
" recent_message=self.message_history[-1],\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
" bid_string = self.model([SystemMessage(content=prompt)]).content\n",
|
" bid_string = self.model.invoke([SystemMessage(content=prompt)]).content\n",
|
||||||
" return bid_string"
|
" return bid_string"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -129,7 +129,7 @@
|
|||||||
" return obs_message\n",
|
" return obs_message\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def _act(self):\n",
|
" def _act(self):\n",
|
||||||
" act_message = self.model(self.message_history)\n",
|
" act_message = self.model.invoke(self.message_history)\n",
|
||||||
" self.message_history.append(act_message)\n",
|
" self.message_history.append(act_message)\n",
|
||||||
" action = int(self.action_parser.parse(act_message.content)[\"action\"])\n",
|
" action = int(self.action_parser.parse(act_message.content)[\"action\"])\n",
|
||||||
" return action\n",
|
" return action\n",
|
||||||
|
|||||||
@@ -168,7 +168,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"retriever = vector_store.as_retriever(search_type=\"similarity\", search_kwargs={\"k\": 3})\n",
|
"retriever = vector_store.as_retriever(search_type=\"similarity\", search_kwargs={\"k\": 3})\n",
|
||||||
"\n",
|
"\n",
|
||||||
"retrieved_docs = retriever.get_relevant_documents(\"<your question>\")\n",
|
"retrieved_docs = retriever.invoke(\"<your question>\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(retrieved_docs[0].page_content)\n",
|
"print(retrieved_docs[0].page_content)\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# RAG using Upstage Layout Analysis and Groundedness Check\n",
|
||||||
|
"This example illustrates RAG using [Upstage](https://python.langchain.com/docs/integrations/providers/upstage/) Layout Analysis and Groundedness Check."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from typing import List\n",
|
||||||
|
"\n",
|
||||||
|
"from langchain_community.vectorstores import DocArrayInMemorySearch\n",
|
||||||
|
"from langchain_core.output_parsers import StrOutputParser\n",
|
||||||
|
"from langchain_core.prompts import ChatPromptTemplate\n",
|
||||||
|
"from langchain_core.runnables import RunnablePassthrough\n",
|
||||||
|
"from langchain_core.runnables.base import RunnableSerializable\n",
|
||||||
|
"from langchain_upstage import (\n",
|
||||||
|
" ChatUpstage,\n",
|
||||||
|
" UpstageEmbeddings,\n",
|
||||||
|
" UpstageGroundednessCheck,\n",
|
||||||
|
" UpstageLayoutAnalysisLoader,\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"model = ChatUpstage()\n",
|
||||||
|
"\n",
|
||||||
|
"files = [\"/PATH/TO/YOUR/FILE.pdf\", \"/PATH/TO/YOUR/FILE2.pdf\"]\n",
|
||||||
|
"\n",
|
||||||
|
"loader = UpstageLayoutAnalysisLoader(file_path=files, split=\"element\")\n",
|
||||||
|
"\n",
|
||||||
|
"docs = loader.load()\n",
|
||||||
|
"\n",
|
||||||
|
"vectorstore = DocArrayInMemorySearch.from_documents(docs, embedding=UpstageEmbeddings())\n",
|
||||||
|
"retriever = vectorstore.as_retriever()\n",
|
||||||
|
"\n",
|
||||||
|
"template = \"\"\"Answer the question based only on the following context:\n",
|
||||||
|
"{context}\n",
|
||||||
|
"\n",
|
||||||
|
"Question: {question}\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
"prompt = ChatPromptTemplate.from_template(template)\n",
|
||||||
|
"output_parser = StrOutputParser()\n",
|
||||||
|
"\n",
|
||||||
|
"retrieved_docs = retriever.get_relevant_documents(\"How many parameters in SOLAR model?\")\n",
|
||||||
|
"\n",
|
||||||
|
"groundedness_check = UpstageGroundednessCheck()\n",
|
||||||
|
"groundedness = \"\"\n",
|
||||||
|
"while groundedness != \"grounded\":\n",
|
||||||
|
" chain: RunnableSerializable = RunnablePassthrough() | prompt | model | output_parser\n",
|
||||||
|
"\n",
|
||||||
|
" result = chain.invoke(\n",
|
||||||
|
" {\n",
|
||||||
|
" \"context\": retrieved_docs,\n",
|
||||||
|
" \"question\": \"How many parameters in SOLAR model?\",\n",
|
||||||
|
" }\n",
|
||||||
|
" )\n",
|
||||||
|
"\n",
|
||||||
|
" groundedness = groundedness_check.invoke(\n",
|
||||||
|
" {\n",
|
||||||
|
" \"context\": retrieved_docs,\n",
|
||||||
|
" \"answer\": result,\n",
|
||||||
|
" }\n",
|
||||||
|
" )"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"language_info": {
|
||||||
|
"name": "python"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 2
|
||||||
|
}
|
||||||
@@ -355,15 +355,15 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"attribute_info[-2][\n",
|
"attribute_info[-2][\"description\"] += (\n",
|
||||||
" \"description\"\n",
|
" f\". Valid values are {sorted(latest_price['starrating'].value_counts().index.tolist())}\"\n",
|
||||||
"] += f\". Valid values are {sorted(latest_price['starrating'].value_counts().index.tolist())}\"\n",
|
")\n",
|
||||||
"attribute_info[3][\n",
|
"attribute_info[3][\"description\"] += (\n",
|
||||||
" \"description\"\n",
|
" f\". Valid values are {sorted(latest_price['maxoccupancy'].value_counts().index.tolist())}\"\n",
|
||||||
"] += f\". Valid values are {sorted(latest_price['maxoccupancy'].value_counts().index.tolist())}\"\n",
|
")\n",
|
||||||
"attribute_info[-3][\n",
|
"attribute_info[-3][\"description\"] += (\n",
|
||||||
" \"description\"\n",
|
" f\". Valid values are {sorted(latest_price['country'].value_counts().index.tolist())}\"\n",
|
||||||
"] += f\". Valid values are {sorted(latest_price['country'].value_counts().index.tolist())}\""
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -688,9 +688,9 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"attribute_info[-3][\n",
|
"attribute_info[-3][\"description\"] += (\n",
|
||||||
" \"description\"\n",
|
" \". NOTE: Only use the 'eq' operator if a specific country is mentioned. If a region is mentioned, include all relevant countries in filter.\"\n",
|
||||||
"] += \". NOTE: Only use the 'eq' operator if a specific country is mentioned. If a region is mentioned, include all relevant countries in filter.\"\n",
|
")\n",
|
||||||
"chain = load_query_constructor_runnable(\n",
|
"chain = load_query_constructor_runnable(\n",
|
||||||
" ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0),\n",
|
" ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0),\n",
|
||||||
" doc_contents,\n",
|
" doc_contents,\n",
|
||||||
@@ -1227,7 +1227,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"results = retriever.get_relevant_documents(\n",
|
"results = retriever.invoke(\n",
|
||||||
" \"I want to stay somewhere highly rated along the coast. I want a room with a patio and a fireplace.\"\n",
|
" \"I want to stay somewhere highly rated along the coast. I want a room with a patio and a fireplace.\"\n",
|
||||||
")\n",
|
")\n",
|
||||||
"for res in results:\n",
|
"for res in results:\n",
|
||||||
|
|||||||
@@ -22,7 +22,8 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import AgentExecutor, Tool, ZeroShotAgent\n",
|
"from langchain import hub\n",
|
||||||
|
"from langchain.agents import AgentExecutor, Tool, ZeroShotAgent, create_react_agent\n",
|
||||||
"from langchain.chains import LLMChain\n",
|
"from langchain.chains import LLMChain\n",
|
||||||
"from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory\n",
|
"from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory\n",
|
||||||
"from langchain.prompts import PromptTemplate\n",
|
"from langchain.prompts import PromptTemplate\n",
|
||||||
@@ -84,19 +85,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"prefix = \"\"\"Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:\"\"\"\n",
|
"prompt = hub.pull(\"hwchase17/react\")"
|
||||||
"suffix = \"\"\"Begin!\"\n",
|
|
||||||
"\n",
|
|
||||||
"{chat_history}\n",
|
|
||||||
"Question: {input}\n",
|
|
||||||
"{agent_scratchpad}\"\"\"\n",
|
|
||||||
"\n",
|
|
||||||
"prompt = ZeroShotAgent.create_prompt(\n",
|
|
||||||
" tools,\n",
|
|
||||||
" prefix=prefix,\n",
|
|
||||||
" suffix=suffix,\n",
|
|
||||||
" input_variables=[\"input\", \"chat_history\", \"agent_scratchpad\"],\n",
|
|
||||||
")"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -114,16 +103,14 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)\n",
|
"model = OpenAI()\n",
|
||||||
"agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)\n",
|
"agent = create_react_agent(model, tools, prompt)\n",
|
||||||
"agent_chain = AgentExecutor.from_agent_and_tools(\n",
|
"agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory)"
|
||||||
" agent=agent, tools=tools, verbose=True, memory=memory\n",
|
|
||||||
")"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": 36,
|
||||||
"id": "ca4bc1fb",
|
"id": "ca4bc1fb",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
@@ -133,15 +120,15 @@
|
|||||||
"text": [
|
"text": [
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
"\u001B[1m> Entering new AgentExecutor chain...\u001B[0m\n",
|
||||||
"\u001b[32;1m\u001b[1;3mThought: I should research ChatGPT to answer this question.\n",
|
"\u001B[32;1m\u001B[1;3mThought: I should research ChatGPT to answer this question.\n",
|
||||||
"Action: Search\n",
|
"Action: Search\n",
|
||||||
"Action Input: \"ChatGPT\"\u001b[0m\n",
|
"Action Input: \"ChatGPT\"\u001B[0m\n",
|
||||||
"Observation: \u001b[36;1m\u001b[1;3mNov 30, 2022 ... We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... ChatGPT. We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... Feb 2, 2023 ... ChatGPT, the popular chatbot from OpenAI, is estimated to have reached 100 million monthly active users in January, just two months after ... 2 days ago ... ChatGPT recently launched a new version of its own plagiarism detection tool, with hopes that it will squelch some of the criticism around how ... An API for accessing new AI models developed by OpenAI. Feb 19, 2023 ... ChatGPT is an AI chatbot system that OpenAI released in November to show off and test what a very large, powerful AI system can accomplish. You ... ChatGPT is fine-tuned from GPT-3.5, a language model trained to produce text. ChatGPT was optimized for dialogue by using Reinforcement Learning with Human ... 3 days ago ... Visual ChatGPT connects ChatGPT and a series of Visual Foundation Models to enable sending and receiving images during chatting. Dec 1, 2022 ... ChatGPT is a natural language processing tool driven by AI technology that allows you to have human-like conversations and much more with a ...\u001b[0m\n",
|
"Observation: \u001B[36;1m\u001B[1;3mNov 30, 2022 ... We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... ChatGPT. We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... Feb 2, 2023 ... ChatGPT, the popular chatbot from OpenAI, is estimated to have reached 100 million monthly active users in January, just two months after ... 2 days ago ... ChatGPT recently launched a new version of its own plagiarism detection tool, with hopes that it will squelch some of the criticism around how ... An API for accessing new AI models developed by OpenAI. Feb 19, 2023 ... ChatGPT is an AI chatbot system that OpenAI released in November to show off and test what a very large, powerful AI system can accomplish. You ... ChatGPT is fine-tuned from GPT-3.5, a language model trained to produce text. ChatGPT was optimized for dialogue by using Reinforcement Learning with Human ... 3 days ago ... Visual ChatGPT connects ChatGPT and a series of Visual Foundation Models to enable sending and receiving images during chatting. Dec 1, 2022 ... ChatGPT is a natural language processing tool driven by AI technology that allows you to have human-like conversations and much more with a ...\u001B[0m\n",
|
||||||
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
|
"Thought:\u001B[32;1m\u001B[1;3m I now know the final answer.\n",
|
||||||
"Final Answer: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\u001b[0m\n",
|
"Final Answer: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\u001B[0m\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
"\u001B[1m> Finished chain.\u001B[0m\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -153,10 +140,40 @@
|
|||||||
"execution_count": 6,
|
"execution_count": 6,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ename": "KeyboardInterrupt",
|
||||||
|
"evalue": "",
|
||||||
|
"output_type": "error",
|
||||||
|
"traceback": [
|
||||||
|
"\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
|
||||||
|
"\u001B[0;31mKeyboardInterrupt\u001B[0m Traceback (most recent call last)",
|
||||||
|
"Cell \u001B[0;32mIn[36], line 1\u001B[0m\n\u001B[0;32m----> 1\u001B[0m \u001B[43magent_executor\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43minvoke\u001B[49m\u001B[43m(\u001B[49m\u001B[43m{\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43minput\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m:\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mWhat is ChatGPT?\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m}\u001B[49m\u001B[43m)\u001B[49m\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/libs/langchain/langchain/chains/base.py:163\u001B[0m, in \u001B[0;36mChain.invoke\u001B[0;34m(self, input, config, **kwargs)\u001B[0m\n\u001B[1;32m 161\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mBaseException\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m e:\n\u001B[1;32m 162\u001B[0m run_manager\u001B[38;5;241m.\u001B[39mon_chain_error(e)\n\u001B[0;32m--> 163\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m e\n\u001B[1;32m 164\u001B[0m run_manager\u001B[38;5;241m.\u001B[39mon_chain_end(outputs)\n\u001B[1;32m 166\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m include_run_info:\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/libs/langchain/langchain/chains/base.py:153\u001B[0m, in \u001B[0;36mChain.invoke\u001B[0;34m(self, input, config, **kwargs)\u001B[0m\n\u001B[1;32m 150\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[1;32m 151\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_validate_inputs(inputs)\n\u001B[1;32m 152\u001B[0m outputs \u001B[38;5;241m=\u001B[39m (\n\u001B[0;32m--> 153\u001B[0m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_call\u001B[49m\u001B[43m(\u001B[49m\u001B[43minputs\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mrun_manager\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mrun_manager\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 154\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m new_arg_supported\n\u001B[1;32m 155\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_call(inputs)\n\u001B[1;32m 156\u001B[0m )\n\u001B[1;32m 158\u001B[0m final_outputs: Dict[\u001B[38;5;28mstr\u001B[39m, Any] \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mprep_outputs(\n\u001B[1;32m 159\u001B[0m inputs, outputs, return_only_outputs\n\u001B[1;32m 160\u001B[0m )\n\u001B[1;32m 161\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mBaseException\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m e:\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/libs/langchain/langchain/agents/agent.py:1432\u001B[0m, in \u001B[0;36mAgentExecutor._call\u001B[0;34m(self, inputs, run_manager)\u001B[0m\n\u001B[1;32m 1430\u001B[0m \u001B[38;5;66;03m# We now enter the agent loop (until it returns something).\u001B[39;00m\n\u001B[1;32m 1431\u001B[0m \u001B[38;5;28;01mwhile\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_should_continue(iterations, time_elapsed):\n\u001B[0;32m-> 1432\u001B[0m next_step_output \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_take_next_step\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m 1433\u001B[0m \u001B[43m \u001B[49m\u001B[43mname_to_tool_map\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 1434\u001B[0m \u001B[43m \u001B[49m\u001B[43mcolor_mapping\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 1435\u001B[0m \u001B[43m \u001B[49m\u001B[43minputs\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 1436\u001B[0m \u001B[43m \u001B[49m\u001B[43mintermediate_steps\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 1437\u001B[0m \u001B[43m \u001B[49m\u001B[43mrun_manager\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mrun_manager\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 1438\u001B[0m \u001B[43m \u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 1439\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(next_step_output, AgentFinish):\n\u001B[1;32m 1440\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_return(\n\u001B[1;32m 1441\u001B[0m next_step_output, intermediate_steps, run_manager\u001B[38;5;241m=\u001B[39mrun_manager\n\u001B[1;32m 1442\u001B[0m )\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/libs/langchain/langchain/agents/agent.py:1138\u001B[0m, in \u001B[0;36mAgentExecutor._take_next_step\u001B[0;34m(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager)\u001B[0m\n\u001B[1;32m 1129\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m_take_next_step\u001B[39m(\n\u001B[1;32m 1130\u001B[0m \u001B[38;5;28mself\u001B[39m,\n\u001B[1;32m 1131\u001B[0m name_to_tool_map: Dict[\u001B[38;5;28mstr\u001B[39m, BaseTool],\n\u001B[0;32m (...)\u001B[0m\n\u001B[1;32m 1135\u001B[0m run_manager: Optional[CallbackManagerForChainRun] \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m,\n\u001B[1;32m 1136\u001B[0m ) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m Union[AgentFinish, List[Tuple[AgentAction, \u001B[38;5;28mstr\u001B[39m]]]:\n\u001B[1;32m 1137\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_consume_next_step(\n\u001B[0;32m-> 1138\u001B[0m [\n\u001B[1;32m 1139\u001B[0m a\n\u001B[1;32m 1140\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m a \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_iter_next_step(\n\u001B[1;32m 1141\u001B[0m name_to_tool_map,\n\u001B[1;32m 1142\u001B[0m color_mapping,\n\u001B[1;32m 1143\u001B[0m inputs,\n\u001B[1;32m 1144\u001B[0m intermediate_steps,\n\u001B[1;32m 1145\u001B[0m run_manager,\n\u001B[1;32m 1146\u001B[0m )\n\u001B[1;32m 1147\u001B[0m ]\n\u001B[1;32m 1148\u001B[0m )\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/libs/langchain/langchain/agents/agent.py:1138\u001B[0m, in \u001B[0;36m<listcomp>\u001B[0;34m(.0)\u001B[0m\n\u001B[1;32m 1129\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m_take_next_step\u001B[39m(\n\u001B[1;32m 1130\u001B[0m \u001B[38;5;28mself\u001B[39m,\n\u001B[1;32m 1131\u001B[0m name_to_tool_map: Dict[\u001B[38;5;28mstr\u001B[39m, BaseTool],\n\u001B[0;32m (...)\u001B[0m\n\u001B[1;32m 1135\u001B[0m run_manager: Optional[CallbackManagerForChainRun] \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m,\n\u001B[1;32m 1136\u001B[0m ) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m Union[AgentFinish, List[Tuple[AgentAction, \u001B[38;5;28mstr\u001B[39m]]]:\n\u001B[1;32m 1137\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_consume_next_step(\n\u001B[0;32m-> 1138\u001B[0m [\n\u001B[1;32m 1139\u001B[0m a\n\u001B[1;32m 1140\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m a \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_iter_next_step(\n\u001B[1;32m 1141\u001B[0m name_to_tool_map,\n\u001B[1;32m 1142\u001B[0m color_mapping,\n\u001B[1;32m 1143\u001B[0m inputs,\n\u001B[1;32m 1144\u001B[0m intermediate_steps,\n\u001B[1;32m 1145\u001B[0m run_manager,\n\u001B[1;32m 1146\u001B[0m )\n\u001B[1;32m 1147\u001B[0m ]\n\u001B[1;32m 1148\u001B[0m )\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/libs/langchain/langchain/agents/agent.py:1223\u001B[0m, in \u001B[0;36mAgentExecutor._iter_next_step\u001B[0;34m(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager)\u001B[0m\n\u001B[1;32m 1221\u001B[0m \u001B[38;5;28;01myield\u001B[39;00m agent_action\n\u001B[1;32m 1222\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m agent_action \u001B[38;5;129;01min\u001B[39;00m actions:\n\u001B[0;32m-> 1223\u001B[0m \u001B[38;5;28;01myield\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_perform_agent_action\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m 1224\u001B[0m \u001B[43m \u001B[49m\u001B[43mname_to_tool_map\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mcolor_mapping\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43magent_action\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mrun_manager\u001B[49m\n\u001B[1;32m 1225\u001B[0m \u001B[43m \u001B[49m\u001B[43m)\u001B[49m\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/libs/langchain/langchain/agents/agent.py:1245\u001B[0m, in \u001B[0;36mAgentExecutor._perform_agent_action\u001B[0;34m(self, name_to_tool_map, color_mapping, agent_action, run_manager)\u001B[0m\n\u001B[1;32m 1243\u001B[0m tool_run_kwargs[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mllm_prefix\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m 1244\u001B[0m \u001B[38;5;66;03m# We then call the tool on the tool input to get an observation\u001B[39;00m\n\u001B[0;32m-> 1245\u001B[0m observation \u001B[38;5;241m=\u001B[39m \u001B[43mtool\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mrun\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m 1246\u001B[0m \u001B[43m \u001B[49m\u001B[43magent_action\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mtool_input\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 1247\u001B[0m \u001B[43m \u001B[49m\u001B[43mverbose\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mverbose\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 1248\u001B[0m \u001B[43m \u001B[49m\u001B[43mcolor\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mcolor\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 1249\u001B[0m \u001B[43m \u001B[49m\u001B[43mcallbacks\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mrun_manager\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mget_child\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43;01mif\u001B[39;49;00m\u001B[43m \u001B[49m\u001B[43mrun_manager\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43;01melse\u001B[39;49;00m\u001B[43m \u001B[49m\u001B[38;5;28;43;01mNone\u001B[39;49;00m\u001B[43m,\u001B[49m\n\u001B[1;32m 1250\u001B[0m \u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mtool_run_kwargs\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 1251\u001B[0m \u001B[43m \u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 1252\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m 1253\u001B[0m tool_run_kwargs \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39magent\u001B[38;5;241m.\u001B[39mtool_run_logging_kwargs()\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/libs/core/langchain_core/tools.py:422\u001B[0m, in \u001B[0;36mBaseTool.run\u001B[0;34m(self, tool_input, verbose, start_color, color, callbacks, tags, metadata, run_name, run_id, **kwargs)\u001B[0m\n\u001B[1;32m 420\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m (\u001B[38;5;167;01mException\u001B[39;00m, \u001B[38;5;167;01mKeyboardInterrupt\u001B[39;00m) \u001B[38;5;28;01mas\u001B[39;00m e:\n\u001B[1;32m 421\u001B[0m run_manager\u001B[38;5;241m.\u001B[39mon_tool_error(e)\n\u001B[0;32m--> 422\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m e\n\u001B[1;32m 423\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m 424\u001B[0m run_manager\u001B[38;5;241m.\u001B[39mon_tool_end(observation, color\u001B[38;5;241m=\u001B[39mcolor, name\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mname, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/libs/core/langchain_core/tools.py:381\u001B[0m, in \u001B[0;36mBaseTool.run\u001B[0;34m(self, tool_input, verbose, start_color, color, callbacks, tags, metadata, run_name, run_id, **kwargs)\u001B[0m\n\u001B[1;32m 378\u001B[0m parsed_input \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_parse_input(tool_input)\n\u001B[1;32m 379\u001B[0m tool_args, tool_kwargs \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_to_args_and_kwargs(parsed_input)\n\u001B[1;32m 380\u001B[0m observation \u001B[38;5;241m=\u001B[39m (\n\u001B[0;32m--> 381\u001B[0m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_run\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mtool_args\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mrun_manager\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mrun_manager\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mtool_kwargs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 382\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m new_arg_supported\n\u001B[1;32m 383\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_run(\u001B[38;5;241m*\u001B[39mtool_args, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mtool_kwargs)\n\u001B[1;32m 384\u001B[0m )\n\u001B[1;32m 385\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m ValidationError \u001B[38;5;28;01mas\u001B[39;00m e:\n\u001B[1;32m 386\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mhandle_validation_error:\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/libs/core/langchain_core/tools.py:588\u001B[0m, in \u001B[0;36mTool._run\u001B[0;34m(self, run_manager, *args, **kwargs)\u001B[0m\n\u001B[1;32m 579\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mfunc:\n\u001B[1;32m 580\u001B[0m new_argument_supported \u001B[38;5;241m=\u001B[39m signature(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mfunc)\u001B[38;5;241m.\u001B[39mparameters\u001B[38;5;241m.\u001B[39mget(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mcallbacks\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[1;32m 581\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m (\n\u001B[1;32m 582\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mfunc(\n\u001B[1;32m 583\u001B[0m \u001B[38;5;241m*\u001B[39margs,\n\u001B[1;32m 584\u001B[0m callbacks\u001B[38;5;241m=\u001B[39mrun_manager\u001B[38;5;241m.\u001B[39mget_child() \u001B[38;5;28;01mif\u001B[39;00m run_manager \u001B[38;5;28;01melse\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m,\n\u001B[1;32m 585\u001B[0m \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs,\n\u001B[1;32m 586\u001B[0m )\n\u001B[1;32m 587\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m new_argument_supported\n\u001B[0;32m--> 588\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mfunc\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43margs\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 589\u001B[0m )\n\u001B[1;32m 590\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mNotImplementedError\u001B[39;00m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mTool does not support sync\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/libs/community/langchain_community/utilities/google_search.py:94\u001B[0m, in \u001B[0;36mGoogleSearchAPIWrapper.run\u001B[0;34m(self, query)\u001B[0m\n\u001B[1;32m 92\u001B[0m \u001B[38;5;250m\u001B[39m\u001B[38;5;124;03m\"\"\"Run query through GoogleSearch and parse result.\"\"\"\u001B[39;00m\n\u001B[1;32m 93\u001B[0m snippets \u001B[38;5;241m=\u001B[39m []\n\u001B[0;32m---> 94\u001B[0m results \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_google_search_results\u001B[49m\u001B[43m(\u001B[49m\u001B[43mquery\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mnum\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mk\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 95\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mlen\u001B[39m(results) \u001B[38;5;241m==\u001B[39m \u001B[38;5;241m0\u001B[39m:\n\u001B[1;32m 96\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mNo good Google Search Result was found\u001B[39m\u001B[38;5;124m\"\u001B[39m\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/libs/community/langchain_community/utilities/google_search.py:62\u001B[0m, in \u001B[0;36mGoogleSearchAPIWrapper._google_search_results\u001B[0;34m(self, search_term, **kwargs)\u001B[0m\n\u001B[1;32m 60\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39msiterestrict:\n\u001B[1;32m 61\u001B[0m cse \u001B[38;5;241m=\u001B[39m cse\u001B[38;5;241m.\u001B[39msiterestrict()\n\u001B[0;32m---> 62\u001B[0m res \u001B[38;5;241m=\u001B[39m \u001B[43mcse\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mlist\u001B[49m\u001B[43m(\u001B[49m\u001B[43mq\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43msearch_term\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mcx\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mgoogle_cse_id\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mexecute\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 63\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m res\u001B[38;5;241m.\u001B[39mget(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mitems\u001B[39m\u001B[38;5;124m\"\u001B[39m, [])\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/.venv/lib/python3.10/site-packages/googleapiclient/_helpers.py:130\u001B[0m, in \u001B[0;36mpositional.<locals>.positional_decorator.<locals>.positional_wrapper\u001B[0;34m(*args, **kwargs)\u001B[0m\n\u001B[1;32m 128\u001B[0m \u001B[38;5;28;01melif\u001B[39;00m positional_parameters_enforcement \u001B[38;5;241m==\u001B[39m POSITIONAL_WARNING:\n\u001B[1;32m 129\u001B[0m logger\u001B[38;5;241m.\u001B[39mwarning(message)\n\u001B[0;32m--> 130\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43mwrapped\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43margs\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/.venv/lib/python3.10/site-packages/googleapiclient/http.py:923\u001B[0m, in \u001B[0;36mHttpRequest.execute\u001B[0;34m(self, http, num_retries)\u001B[0m\n\u001B[1;32m 920\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mheaders[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mcontent-length\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mstr\u001B[39m(\u001B[38;5;28mlen\u001B[39m(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mbody))\n\u001B[1;32m 922\u001B[0m \u001B[38;5;66;03m# Handle retries for server-side errors.\u001B[39;00m\n\u001B[0;32m--> 923\u001B[0m resp, content \u001B[38;5;241m=\u001B[39m \u001B[43m_retry_request\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m 924\u001B[0m \u001B[43m \u001B[49m\u001B[43mhttp\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 925\u001B[0m \u001B[43m \u001B[49m\u001B[43mnum_retries\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 926\u001B[0m \u001B[43m \u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mrequest\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\n\u001B[1;32m 927\u001B[0m \u001B[43m \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_sleep\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 928\u001B[0m \u001B[43m \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_rand\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 929\u001B[0m \u001B[43m \u001B[49m\u001B[38;5;28;43mstr\u001B[39;49m\u001B[43m(\u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43muri\u001B[49m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 930\u001B[0m \u001B[43m \u001B[49m\u001B[43mmethod\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mstr\u001B[39;49m\u001B[43m(\u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mmethod\u001B[49m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 931\u001B[0m \u001B[43m \u001B[49m\u001B[43mbody\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mbody\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 932\u001B[0m \u001B[43m \u001B[49m\u001B[43mheaders\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mheaders\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 933\u001B[0m \u001B[43m\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 935\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m callback \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mresponse_callbacks:\n\u001B[1;32m 936\u001B[0m callback(resp)\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/.venv/lib/python3.10/site-packages/googleapiclient/http.py:191\u001B[0m, in \u001B[0;36m_retry_request\u001B[0;34m(http, num_retries, req_type, sleep, rand, uri, method, *args, **kwargs)\u001B[0m\n\u001B[1;32m 189\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[1;32m 190\u001B[0m exception \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[0;32m--> 191\u001B[0m resp, content \u001B[38;5;241m=\u001B[39m \u001B[43mhttp\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mrequest\u001B[49m\u001B[43m(\u001B[49m\u001B[43muri\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mmethod\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43margs\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 192\u001B[0m \u001B[38;5;66;03m# Retry on SSL errors and socket timeout errors.\u001B[39;00m\n\u001B[1;32m 193\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m _ssl_SSLError \u001B[38;5;28;01mas\u001B[39;00m ssl_error:\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/.venv/lib/python3.10/site-packages/httplib2/__init__.py:1724\u001B[0m, in \u001B[0;36mHttp.request\u001B[0;34m(self, uri, method, body, headers, redirections, connection_type)\u001B[0m\n\u001B[1;32m 1722\u001B[0m content \u001B[38;5;241m=\u001B[39m \u001B[38;5;124mb\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m 1723\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[0;32m-> 1724\u001B[0m (response, content) \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_request\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m 1725\u001B[0m \u001B[43m \u001B[49m\u001B[43mconn\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mauthority\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43muri\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mrequest_uri\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mmethod\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mbody\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mheaders\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mredirections\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mcachekey\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 1726\u001B[0m \u001B[43m \u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 1727\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mException\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m e:\n\u001B[1;32m 1728\u001B[0m is_timeout \u001B[38;5;241m=\u001B[39m \u001B[38;5;28misinstance\u001B[39m(e, socket\u001B[38;5;241m.\u001B[39mtimeout)\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/.venv/lib/python3.10/site-packages/httplib2/__init__.py:1444\u001B[0m, in \u001B[0;36mHttp._request\u001B[0;34m(self, conn, host, absolute_uri, request_uri, method, body, headers, redirections, cachekey)\u001B[0m\n\u001B[1;32m 1441\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m auth:\n\u001B[1;32m 1442\u001B[0m auth\u001B[38;5;241m.\u001B[39mrequest(method, request_uri, headers, body)\n\u001B[0;32m-> 1444\u001B[0m (response, content) \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_conn_request\u001B[49m\u001B[43m(\u001B[49m\u001B[43mconn\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mrequest_uri\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mmethod\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mbody\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mheaders\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 1446\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m auth:\n\u001B[1;32m 1447\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m auth\u001B[38;5;241m.\u001B[39mresponse(response, body):\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/.venv/lib/python3.10/site-packages/httplib2/__init__.py:1366\u001B[0m, in \u001B[0;36mHttp._conn_request\u001B[0;34m(self, conn, request_uri, method, body, headers)\u001B[0m\n\u001B[1;32m 1364\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[1;32m 1365\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m conn\u001B[38;5;241m.\u001B[39msock \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[0;32m-> 1366\u001B[0m \u001B[43mconn\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mconnect\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 1367\u001B[0m conn\u001B[38;5;241m.\u001B[39mrequest(method, request_uri, body, headers)\n\u001B[1;32m 1368\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m socket\u001B[38;5;241m.\u001B[39mtimeout:\n",
|
||||||
|
"File \u001B[0;32m~/code/langchain/.venv/lib/python3.10/site-packages/httplib2/__init__.py:1156\u001B[0m, in \u001B[0;36mHTTPSConnectionWithTimeout.connect\u001B[0;34m(self)\u001B[0m\n\u001B[1;32m 1154\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m has_timeout(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mtimeout):\n\u001B[1;32m 1155\u001B[0m sock\u001B[38;5;241m.\u001B[39msettimeout(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mtimeout)\n\u001B[0;32m-> 1156\u001B[0m \u001B[43msock\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mconnect\u001B[49m\u001B[43m(\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mhost\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mport\u001B[49m\u001B[43m)\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 1158\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39msock \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_context\u001B[38;5;241m.\u001B[39mwrap_socket(sock, server_hostname\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mhost)\n\u001B[1;32m 1160\u001B[0m \u001B[38;5;66;03m# Python 3.3 compatibility: emulate the check_hostname behavior\u001B[39;00m\n",
|
||||||
|
"\u001B[0;31mKeyboardInterrupt\u001B[0m: "
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"agent_chain.run(input=\"What is ChatGPT?\")"
|
"agent_executor.invoke({\"input\": \"What is ChatGPT?\"})"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -179,15 +196,15 @@
|
|||||||
"text": [
|
"text": [
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
"\u001B[1m> Entering new AgentExecutor chain...\u001B[0m\n",
|
||||||
"\u001b[32;1m\u001b[1;3mThought: I need to find out who developed ChatGPT\n",
|
"\u001B[32;1m\u001B[1;3mThought: I need to find out who developed ChatGPT\n",
|
||||||
"Action: Search\n",
|
"Action: Search\n",
|
||||||
"Action Input: Who developed ChatGPT\u001b[0m\n",
|
"Action Input: Who developed ChatGPT\u001B[0m\n",
|
||||||
"Observation: \u001b[36;1m\u001b[1;3mChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... Feb 15, 2023 ... Who owns Chat GPT? Chat GPT is owned and developed by AI research and deployment company, OpenAI. The organization is headquartered in San ... Feb 8, 2023 ... ChatGPT is an AI chatbot developed by San Francisco-based startup OpenAI. OpenAI was co-founded in 2015 by Elon Musk and Sam Altman and is ... Dec 7, 2022 ... ChatGPT is an AI chatbot designed and developed by OpenAI. The bot works by generating text responses based on human-user input, like questions ... Jan 12, 2023 ... In 2019, Microsoft invested $1 billion in OpenAI, the tiny San Francisco company that designed ChatGPT. And in the years since, it has quietly ... Jan 25, 2023 ... The inside story of ChatGPT: How OpenAI founder Sam Altman built the world's hottest technology with billions from Microsoft. Dec 3, 2022 ... ChatGPT went viral on social media for its ability to do anything from code to write essays. · The company that created the AI chatbot has a ... Jan 17, 2023 ... While many Americans were nursing hangovers on New Year's Day, 22-year-old Edward Tian was working feverishly on a new app to combat misuse ... ChatGPT is a language model created by OpenAI, an artificial intelligence research laboratory consisting of a team of researchers and engineers focused on ... 1 day ago ... Everyone is talking about ChatGPT, developed by OpenAI. This is such a great tool that has helped to make AI more accessible to a wider ...\u001b[0m\n",
|
"Observation: \u001B[36;1m\u001B[1;3mChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... Feb 15, 2023 ... Who owns Chat GPT? Chat GPT is owned and developed by AI research and deployment company, OpenAI. The organization is headquartered in San ... Feb 8, 2023 ... ChatGPT is an AI chatbot developed by San Francisco-based startup OpenAI. OpenAI was co-founded in 2015 by Elon Musk and Sam Altman and is ... Dec 7, 2022 ... ChatGPT is an AI chatbot designed and developed by OpenAI. The bot works by generating text responses based on human-user input, like questions ... Jan 12, 2023 ... In 2019, Microsoft invested $1 billion in OpenAI, the tiny San Francisco company that designed ChatGPT. And in the years since, it has quietly ... Jan 25, 2023 ... The inside story of ChatGPT: How OpenAI founder Sam Altman built the world's hottest technology with billions from Microsoft. Dec 3, 2022 ... ChatGPT went viral on social media for its ability to do anything from code to write essays. · The company that created the AI chatbot has a ... Jan 17, 2023 ... While many Americans were nursing hangovers on New Year's Day, 22-year-old Edward Tian was working feverishly on a new app to combat misuse ... ChatGPT is a language model created by OpenAI, an artificial intelligence research laboratory consisting of a team of researchers and engineers focused on ... 1 day ago ... Everyone is talking about ChatGPT, developed by OpenAI. This is such a great tool that has helped to make AI more accessible to a wider ...\u001B[0m\n",
|
||||||
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
|
"Thought:\u001B[32;1m\u001B[1;3m I now know the final answer\n",
|
||||||
"Final Answer: ChatGPT was developed by OpenAI.\u001b[0m\n",
|
"Final Answer: ChatGPT was developed by OpenAI.\u001B[0m\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
"\u001B[1m> Finished chain.\u001B[0m\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -202,7 +219,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"agent_chain.run(input=\"Who developed it?\")"
|
"agent_executor.invoke({\"input\": \"Who developed it?\"})"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -217,14 +234,14 @@
|
|||||||
"text": [
|
"text": [
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
"\u001B[1m> Entering new AgentExecutor chain...\u001B[0m\n",
|
||||||
"\u001b[32;1m\u001b[1;3mThought: I need to simplify the conversation for a 5 year old.\n",
|
"\u001B[32;1m\u001B[1;3mThought: I need to simplify the conversation for a 5 year old.\n",
|
||||||
"Action: Summary\n",
|
"Action: Summary\n",
|
||||||
"Action Input: My daughter 5 years old\u001b[0m\n",
|
"Action Input: My daughter 5 years old\u001B[0m\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
|
"\u001B[1m> Entering new LLMChain chain...\u001B[0m\n",
|
||||||
"Prompt after formatting:\n",
|
"Prompt after formatting:\n",
|
||||||
"\u001b[32;1m\u001b[1;3mThis is a conversation between a human and a bot:\n",
|
"\u001B[32;1m\u001B[1;3mThis is a conversation between a human and a bot:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Human: What is ChatGPT?\n",
|
"Human: What is ChatGPT?\n",
|
||||||
"AI: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\n",
|
"AI: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\n",
|
||||||
@@ -232,16 +249,16 @@
|
|||||||
"AI: ChatGPT was developed by OpenAI.\n",
|
"AI: ChatGPT was developed by OpenAI.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Write a summary of the conversation for My daughter 5 years old:\n",
|
"Write a summary of the conversation for My daughter 5 years old:\n",
|
||||||
"\u001b[0m\n",
|
"\u001B[0m\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Finished chain.\u001b[0m\n",
|
"\u001B[1m> Finished chain.\u001B[0m\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Observation: \u001b[33;1m\u001b[1;3m\n",
|
"Observation: \u001B[33;1m\u001B[1;3m\n",
|
||||||
"The conversation was about ChatGPT, an artificial intelligence chatbot. It was created by OpenAI and can send and receive images while chatting.\u001b[0m\n",
|
"The conversation was about ChatGPT, an artificial intelligence chatbot. It was created by OpenAI and can send and receive images while chatting.\u001B[0m\n",
|
||||||
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
|
"Thought:\u001B[32;1m\u001B[1;3m I now know the final answer.\n",
|
||||||
"Final Answer: ChatGPT is an artificial intelligence chatbot created by OpenAI that can send and receive images while chatting.\u001b[0m\n",
|
"Final Answer: ChatGPT is an artificial intelligence chatbot created by OpenAI that can send and receive images while chatting.\u001B[0m\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
"\u001B[1m> Finished chain.\u001B[0m\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -256,8 +273,8 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"agent_chain.run(\n",
|
"agent_executor.invoke(\n",
|
||||||
" input=\"Thanks. Summarize the conversation, for my daughter 5 years old.\"\n",
|
" {\"input\": \"Thanks. Summarize the conversation, for my daughter 5 years old.\"}\n",
|
||||||
")"
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -289,9 +306,17 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"print(agent_chain.memory.buffer)"
|
"print(agent_executor.memory.buffer)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "84ca95c30e262e00",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "cc3d0aa4",
|
"id": "cc3d0aa4",
|
||||||
@@ -340,25 +365,9 @@
|
|||||||
" ),\n",
|
" ),\n",
|
||||||
"]\n",
|
"]\n",
|
||||||
"\n",
|
"\n",
|
||||||
"prefix = \"\"\"Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:\"\"\"\n",
|
"prompt = hub.pull(\"hwchase17/react\")\n",
|
||||||
"suffix = \"\"\"Begin!\"\n",
|
"agent = create_react_agent(model, tools, prompt)\n",
|
||||||
"\n",
|
"agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory)"
|
||||||
"{chat_history}\n",
|
|
||||||
"Question: {input}\n",
|
|
||||||
"{agent_scratchpad}\"\"\"\n",
|
|
||||||
"\n",
|
|
||||||
"prompt = ZeroShotAgent.create_prompt(\n",
|
|
||||||
" tools,\n",
|
|
||||||
" prefix=prefix,\n",
|
|
||||||
" suffix=suffix,\n",
|
|
||||||
" input_variables=[\"input\", \"chat_history\", \"agent_scratchpad\"],\n",
|
|
||||||
")\n",
|
|
||||||
"\n",
|
|
||||||
"llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)\n",
|
|
||||||
"agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)\n",
|
|
||||||
"agent_chain = AgentExecutor.from_agent_and_tools(\n",
|
|
||||||
" agent=agent, tools=tools, verbose=True, memory=memory\n",
|
|
||||||
")"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -373,15 +382,15 @@
|
|||||||
"text": [
|
"text": [
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
"\u001B[1m> Entering new AgentExecutor chain...\u001B[0m\n",
|
||||||
"\u001b[32;1m\u001b[1;3mThought: I should research ChatGPT to answer this question.\n",
|
"\u001B[32;1m\u001B[1;3mThought: I should research ChatGPT to answer this question.\n",
|
||||||
"Action: Search\n",
|
"Action: Search\n",
|
||||||
"Action Input: \"ChatGPT\"\u001b[0m\n",
|
"Action Input: \"ChatGPT\"\u001B[0m\n",
|
||||||
"Observation: \u001b[36;1m\u001b[1;3mNov 30, 2022 ... We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... ChatGPT. We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... Feb 2, 2023 ... ChatGPT, the popular chatbot from OpenAI, is estimated to have reached 100 million monthly active users in January, just two months after ... 2 days ago ... ChatGPT recently launched a new version of its own plagiarism detection tool, with hopes that it will squelch some of the criticism around how ... An API for accessing new AI models developed by OpenAI. Feb 19, 2023 ... ChatGPT is an AI chatbot system that OpenAI released in November to show off and test what a very large, powerful AI system can accomplish. You ... ChatGPT is fine-tuned from GPT-3.5, a language model trained to produce text. ChatGPT was optimized for dialogue by using Reinforcement Learning with Human ... 3 days ago ... Visual ChatGPT connects ChatGPT and a series of Visual Foundation Models to enable sending and receiving images during chatting. Dec 1, 2022 ... ChatGPT is a natural language processing tool driven by AI technology that allows you to have human-like conversations and much more with a ...\u001b[0m\n",
|
"Observation: \u001B[36;1m\u001B[1;3mNov 30, 2022 ... We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... ChatGPT. We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... Feb 2, 2023 ... ChatGPT, the popular chatbot from OpenAI, is estimated to have reached 100 million monthly active users in January, just two months after ... 2 days ago ... ChatGPT recently launched a new version of its own plagiarism detection tool, with hopes that it will squelch some of the criticism around how ... An API for accessing new AI models developed by OpenAI. Feb 19, 2023 ... ChatGPT is an AI chatbot system that OpenAI released in November to show off and test what a very large, powerful AI system can accomplish. You ... ChatGPT is fine-tuned from GPT-3.5, a language model trained to produce text. ChatGPT was optimized for dialogue by using Reinforcement Learning with Human ... 3 days ago ... Visual ChatGPT connects ChatGPT and a series of Visual Foundation Models to enable sending and receiving images during chatting. Dec 1, 2022 ... ChatGPT is a natural language processing tool driven by AI technology that allows you to have human-like conversations and much more with a ...\u001B[0m\n",
|
||||||
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
|
"Thought:\u001B[32;1m\u001B[1;3m I now know the final answer.\n",
|
||||||
"Final Answer: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\u001b[0m\n",
|
"Final Answer: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\u001B[0m\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
"\u001B[1m> Finished chain.\u001B[0m\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -396,7 +405,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"agent_chain.run(input=\"What is ChatGPT?\")"
|
"agent_executor.invoke({\"input\": \"What is ChatGPT?\"})"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -411,15 +420,15 @@
|
|||||||
"text": [
|
"text": [
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
"\u001B[1m> Entering new AgentExecutor chain...\u001B[0m\n",
|
||||||
"\u001b[32;1m\u001b[1;3mThought: I need to find out who developed ChatGPT\n",
|
"\u001B[32;1m\u001B[1;3mThought: I need to find out who developed ChatGPT\n",
|
||||||
"Action: Search\n",
|
"Action: Search\n",
|
||||||
"Action Input: Who developed ChatGPT\u001b[0m\n",
|
"Action Input: Who developed ChatGPT\u001B[0m\n",
|
||||||
"Observation: \u001b[36;1m\u001b[1;3mChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... Feb 15, 2023 ... Who owns Chat GPT? Chat GPT is owned and developed by AI research and deployment company, OpenAI. The organization is headquartered in San ... Feb 8, 2023 ... ChatGPT is an AI chatbot developed by San Francisco-based startup OpenAI. OpenAI was co-founded in 2015 by Elon Musk and Sam Altman and is ... Dec 7, 2022 ... ChatGPT is an AI chatbot designed and developed by OpenAI. The bot works by generating text responses based on human-user input, like questions ... Jan 12, 2023 ... In 2019, Microsoft invested $1 billion in OpenAI, the tiny San Francisco company that designed ChatGPT. And in the years since, it has quietly ... Jan 25, 2023 ... The inside story of ChatGPT: How OpenAI founder Sam Altman built the world's hottest technology with billions from Microsoft. Dec 3, 2022 ... ChatGPT went viral on social media for its ability to do anything from code to write essays. · The company that created the AI chatbot has a ... Jan 17, 2023 ... While many Americans were nursing hangovers on New Year's Day, 22-year-old Edward Tian was working feverishly on a new app to combat misuse ... ChatGPT is a language model created by OpenAI, an artificial intelligence research laboratory consisting of a team of researchers and engineers focused on ... 1 day ago ... Everyone is talking about ChatGPT, developed by OpenAI. This is such a great tool that has helped to make AI more accessible to a wider ...\u001b[0m\n",
|
"Observation: \u001B[36;1m\u001B[1;3mChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... Feb 15, 2023 ... Who owns Chat GPT? Chat GPT is owned and developed by AI research and deployment company, OpenAI. The organization is headquartered in San ... Feb 8, 2023 ... ChatGPT is an AI chatbot developed by San Francisco-based startup OpenAI. OpenAI was co-founded in 2015 by Elon Musk and Sam Altman and is ... Dec 7, 2022 ... ChatGPT is an AI chatbot designed and developed by OpenAI. The bot works by generating text responses based on human-user input, like questions ... Jan 12, 2023 ... In 2019, Microsoft invested $1 billion in OpenAI, the tiny San Francisco company that designed ChatGPT. And in the years since, it has quietly ... Jan 25, 2023 ... The inside story of ChatGPT: How OpenAI founder Sam Altman built the world's hottest technology with billions from Microsoft. Dec 3, 2022 ... ChatGPT went viral on social media for its ability to do anything from code to write essays. · The company that created the AI chatbot has a ... Jan 17, 2023 ... While many Americans were nursing hangovers on New Year's Day, 22-year-old Edward Tian was working feverishly on a new app to combat misuse ... ChatGPT is a language model created by OpenAI, an artificial intelligence research laboratory consisting of a team of researchers and engineers focused on ... 1 day ago ... Everyone is talking about ChatGPT, developed by OpenAI. This is such a great tool that has helped to make AI more accessible to a wider ...\u001B[0m\n",
|
||||||
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
|
"Thought:\u001B[32;1m\u001B[1;3m I now know the final answer\n",
|
||||||
"Final Answer: ChatGPT was developed by OpenAI.\u001b[0m\n",
|
"Final Answer: ChatGPT was developed by OpenAI.\u001B[0m\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
"\u001B[1m> Finished chain.\u001B[0m\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -434,7 +443,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"agent_chain.run(input=\"Who developed it?\")"
|
"agent_executor.invoke({\"input\": \"Who developed it?\"})"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -449,14 +458,14 @@
|
|||||||
"text": [
|
"text": [
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
"\u001B[1m> Entering new AgentExecutor chain...\u001B[0m\n",
|
||||||
"\u001b[32;1m\u001b[1;3mThought: I need to simplify the conversation for a 5 year old.\n",
|
"\u001B[32;1m\u001B[1;3mThought: I need to simplify the conversation for a 5 year old.\n",
|
||||||
"Action: Summary\n",
|
"Action: Summary\n",
|
||||||
"Action Input: My daughter 5 years old\u001b[0m\n",
|
"Action Input: My daughter 5 years old\u001B[0m\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
|
"\u001B[1m> Entering new LLMChain chain...\u001B[0m\n",
|
||||||
"Prompt after formatting:\n",
|
"Prompt after formatting:\n",
|
||||||
"\u001b[32;1m\u001b[1;3mThis is a conversation between a human and a bot:\n",
|
"\u001B[32;1m\u001B[1;3mThis is a conversation between a human and a bot:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Human: What is ChatGPT?\n",
|
"Human: What is ChatGPT?\n",
|
||||||
"AI: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\n",
|
"AI: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.\n",
|
||||||
@@ -464,16 +473,16 @@
|
|||||||
"AI: ChatGPT was developed by OpenAI.\n",
|
"AI: ChatGPT was developed by OpenAI.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Write a summary of the conversation for My daughter 5 years old:\n",
|
"Write a summary of the conversation for My daughter 5 years old:\n",
|
||||||
"\u001b[0m\n",
|
"\u001B[0m\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Finished chain.\u001b[0m\n",
|
"\u001B[1m> Finished chain.\u001B[0m\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Observation: \u001b[33;1m\u001b[1;3m\n",
|
"Observation: \u001B[33;1m\u001B[1;3m\n",
|
||||||
"The conversation was about ChatGPT, an artificial intelligence chatbot developed by OpenAI. It is designed to have conversations with humans and can also send and receive images.\u001b[0m\n",
|
"The conversation was about ChatGPT, an artificial intelligence chatbot developed by OpenAI. It is designed to have conversations with humans and can also send and receive images.\u001B[0m\n",
|
||||||
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
|
"Thought:\u001B[32;1m\u001B[1;3m I now know the final answer.\n",
|
||||||
"Final Answer: ChatGPT is an artificial intelligence chatbot developed by OpenAI that can have conversations with humans and send and receive images.\u001b[0m\n",
|
"Final Answer: ChatGPT is an artificial intelligence chatbot developed by OpenAI that can have conversations with humans and send and receive images.\u001B[0m\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
"\u001B[1m> Finished chain.\u001B[0m\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -488,8 +497,8 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"agent_chain.run(\n",
|
"agent_executor.invoke(\n",
|
||||||
" input=\"Thanks. Summarize the conversation, for my daughter 5 years old.\"\n",
|
" {\"input\": \"Thanks. Summarize the conversation, for my daughter 5 years old.\"}\n",
|
||||||
")"
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -524,7 +533,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"print(agent_chain.memory.buffer)"
|
"print(agent_executor.memory.buffer)"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|||||||
199
cookbook/tool_call_messages.ipynb
Normal file
199
cookbook/tool_call_messages.ipynb
Normal file
@@ -0,0 +1,199 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "c48812ed-35bd-4fbe-9a2c-6c7335e5645e",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain_anthropic import ChatAnthropic\n",
|
||||||
|
"from langchain_core.runnables import ConfigurableField\n",
|
||||||
|
"from langchain_core.tools import tool\n",
|
||||||
|
"from langchain_openai import ChatOpenAI\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"@tool\n",
|
||||||
|
"def multiply(x: float, y: float) -> float:\n",
|
||||||
|
" \"\"\"Multiply 'x' times 'y'.\"\"\"\n",
|
||||||
|
" return x * y\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"@tool\n",
|
||||||
|
"def exponentiate(x: float, y: float) -> float:\n",
|
||||||
|
" \"\"\"Raise 'x' to the 'y'.\"\"\"\n",
|
||||||
|
" return x**y\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"@tool\n",
|
||||||
|
"def add(x: float, y: float) -> float:\n",
|
||||||
|
" \"\"\"Add 'x' and 'y'.\"\"\"\n",
|
||||||
|
" return x + y\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"tools = [multiply, exponentiate, add]\n",
|
||||||
|
"\n",
|
||||||
|
"gpt35 = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0).bind_tools(tools)\n",
|
||||||
|
"claude3 = ChatAnthropic(model=\"claude-3-sonnet-20240229\").bind_tools(tools)\n",
|
||||||
|
"llm_with_tools = gpt35.configurable_alternatives(\n",
|
||||||
|
" ConfigurableField(id=\"llm\"), default_key=\"gpt35\", claude3=claude3\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "9c186263-1b98-4cb2-b6d1-71f65eb0d811",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# LangGraph"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "28fc2c60-7dbc-428a-8983-1a6a15ea30d2",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import operator\n",
|
||||||
|
"from typing import Annotated, Sequence, TypedDict\n",
|
||||||
|
"\n",
|
||||||
|
"from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, ToolMessage\n",
|
||||||
|
"from langchain_core.runnables import RunnableLambda\n",
|
||||||
|
"from langgraph.graph import END, StateGraph\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class AgentState(TypedDict):\n",
|
||||||
|
" messages: Annotated[Sequence[BaseMessage], operator.add]\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def should_continue(state):\n",
|
||||||
|
" return \"continue\" if state[\"messages\"][-1].tool_calls else \"end\"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def call_model(state, config):\n",
|
||||||
|
" return {\"messages\": [llm_with_tools.invoke(state[\"messages\"], config=config)]}\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def _invoke_tool(tool_call):\n",
|
||||||
|
" tool = {tool.name: tool for tool in tools}[tool_call[\"name\"]]\n",
|
||||||
|
" return ToolMessage(tool.invoke(tool_call[\"args\"]), tool_call_id=tool_call[\"id\"])\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"tool_executor = RunnableLambda(_invoke_tool)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def call_tools(state):\n",
|
||||||
|
" last_message = state[\"messages\"][-1]\n",
|
||||||
|
" return {\"messages\": tool_executor.batch(last_message.tool_calls)}\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"workflow = StateGraph(AgentState)\n",
|
||||||
|
"workflow.add_node(\"agent\", call_model)\n",
|
||||||
|
"workflow.add_node(\"action\", call_tools)\n",
|
||||||
|
"workflow.set_entry_point(\"agent\")\n",
|
||||||
|
"workflow.add_conditional_edges(\n",
|
||||||
|
" \"agent\",\n",
|
||||||
|
" should_continue,\n",
|
||||||
|
" {\n",
|
||||||
|
" \"continue\": \"action\",\n",
|
||||||
|
" \"end\": END,\n",
|
||||||
|
" },\n",
|
||||||
|
")\n",
|
||||||
|
"workflow.add_edge(\"action\", \"agent\")\n",
|
||||||
|
"graph = workflow.compile()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "3710e724-2595-4625-ba3a-effb81e66e4a",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"{'messages': [HumanMessage(content=\"what's 3 plus 5 raised to the 2.743. also what's 17.24 - 918.1241\"),\n",
|
||||||
|
" AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_6yMU2WsS4Bqgi1WxFHxtfJRc', 'function': {'arguments': '{\"x\": 8, \"y\": 2.743}', 'name': 'exponentiate'}, 'type': 'function'}, {'id': 'call_GAL3dQiKFF9XEV0RrRLPTvVp', 'function': {'arguments': '{\"x\": 17.24, \"y\": -918.1241}', 'name': 'add'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 58, 'prompt_tokens': 168, 'total_tokens': 226}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': 'fp_b28b39ffa8', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-528302fc-7acf-4c11-82c4-119ccf40c573-0', tool_calls=[{'name': 'exponentiate', 'args': {'x': 8, 'y': 2.743}, 'id': 'call_6yMU2WsS4Bqgi1WxFHxtfJRc'}, {'name': 'add', 'args': {'x': 17.24, 'y': -918.1241}, 'id': 'call_GAL3dQiKFF9XEV0RrRLPTvVp'}]),\n",
|
||||||
|
" ToolMessage(content='300.03770462067547', tool_call_id='call_6yMU2WsS4Bqgi1WxFHxtfJRc'),\n",
|
||||||
|
" ToolMessage(content='-900.8841', tool_call_id='call_GAL3dQiKFF9XEV0RrRLPTvVp'),\n",
|
||||||
|
" AIMessage(content='The result of \\\\(3 + 5^{2.743}\\\\) is approximately 300.04, and the result of \\\\(17.24 - 918.1241\\\\) is approximately -900.88.', response_metadata={'token_usage': {'completion_tokens': 44, 'prompt_tokens': 251, 'total_tokens': 295}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': 'fp_b28b39ffa8', 'finish_reason': 'stop', 'logprobs': None}, id='run-d1161669-ed09-4b18-94bd-6d8530df5aa8-0')]}"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"graph.invoke(\n",
|
||||||
|
" {\n",
|
||||||
|
" \"messages\": [\n",
|
||||||
|
" HumanMessage(\n",
|
||||||
|
" \"what's 3 plus 5 raised to the 2.743. also what's 17.24 - 918.1241\"\n",
|
||||||
|
" )\n",
|
||||||
|
" ]\n",
|
||||||
|
" }\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "073c074e-d722-42e0-85ec-c62c079207e4",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"{'messages': [HumanMessage(content=\"what's 3 plus 5 raised to the 2.743. also what's 17.24 - 918.1241\"),\n",
|
||||||
|
" AIMessage(content=[{'text': \"Okay, let's break this down into two parts:\", 'type': 'text'}, {'id': 'toolu_01DEhqcXkXTtzJAiZ7uMBeDC', 'input': {'x': 3, 'y': 5}, 'name': 'add', 'type': 'tool_use'}], response_metadata={'id': 'msg_01AkLGH8sxMHaH15yewmjwkF', 'model': 'claude-3-sonnet-20240229', 'stop_reason': 'tool_use', 'stop_sequence': None, 'usage': {'input_tokens': 450, 'output_tokens': 81}}, id='run-f35bfae8-8ded-4f8a-831b-0940d6ad16b6-0', tool_calls=[{'name': 'add', 'args': {'x': 3, 'y': 5}, 'id': 'toolu_01DEhqcXkXTtzJAiZ7uMBeDC'}]),\n",
|
||||||
|
" ToolMessage(content='8.0', tool_call_id='toolu_01DEhqcXkXTtzJAiZ7uMBeDC'),\n",
|
||||||
|
" AIMessage(content=[{'id': 'toolu_013DyMLrvnrto33peAKMGMr1', 'input': {'x': 8.0, 'y': 2.743}, 'name': 'exponentiate', 'type': 'tool_use'}], response_metadata={'id': 'msg_015Fmp8aztwYcce2JDAFfce3', 'model': 'claude-3-sonnet-20240229', 'stop_reason': 'tool_use', 'stop_sequence': None, 'usage': {'input_tokens': 545, 'output_tokens': 75}}, id='run-48aaeeeb-a1e5-48fd-a57a-6c3da2907b47-0', tool_calls=[{'name': 'exponentiate', 'args': {'x': 8.0, 'y': 2.743}, 'id': 'toolu_013DyMLrvnrto33peAKMGMr1'}]),\n",
|
||||||
|
" ToolMessage(content='300.03770462067547', tool_call_id='toolu_013DyMLrvnrto33peAKMGMr1'),\n",
|
||||||
|
" AIMessage(content=[{'text': 'So 3 plus 5 raised to the 2.743 power is 300.04.\\n\\nFor the second part:', 'type': 'text'}, {'id': 'toolu_01UTmMrGTmLpPrPCF1rShN46', 'input': {'x': 17.24, 'y': -918.1241}, 'name': 'add', 'type': 'tool_use'}], response_metadata={'id': 'msg_015TkhfRBENPib2RWAxkieH6', 'model': 'claude-3-sonnet-20240229', 'stop_reason': 'tool_use', 'stop_sequence': None, 'usage': {'input_tokens': 638, 'output_tokens': 105}}, id='run-45fb62e3-d102-4159-881d-241c5dbadeed-0', tool_calls=[{'name': 'add', 'args': {'x': 17.24, 'y': -918.1241}, 'id': 'toolu_01UTmMrGTmLpPrPCF1rShN46'}]),\n",
|
||||||
|
" ToolMessage(content='-900.8841', tool_call_id='toolu_01UTmMrGTmLpPrPCF1rShN46'),\n",
|
||||||
|
" AIMessage(content='Therefore, 17.24 - 918.1241 = -900.8841', response_metadata={'id': 'msg_01LgKnRuUcSyADCpxv9tPoYD', 'model': 'claude-3-sonnet-20240229', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 759, 'output_tokens': 24}}, id='run-1008254e-ccd1-497c-8312-9550dd77bd08-0')]}"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"graph.invoke(\n",
|
||||||
|
" {\n",
|
||||||
|
" \"messages\": [\n",
|
||||||
|
" HumanMessage(\n",
|
||||||
|
" \"what's 3 plus 5 raised to the 2.743. also what's 17.24 - 918.1241\"\n",
|
||||||
|
" )\n",
|
||||||
|
" ]\n",
|
||||||
|
" },\n",
|
||||||
|
" config={\"configurable\": {\"llm\": \"claude3\"}},\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.10.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
||||||
@@ -84,7 +84,7 @@
|
|||||||
" Applies the chatmodel to the message history\n",
|
" Applies the chatmodel to the message history\n",
|
||||||
" and returns the message string\n",
|
" and returns the message string\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" message = self.model(\n",
|
" message = self.model.invoke(\n",
|
||||||
" [\n",
|
" [\n",
|
||||||
" self.system_message,\n",
|
" self.system_message,\n",
|
||||||
" HumanMessage(content=\"\\n\".join(self.message_history + [self.prefix])),\n",
|
" HumanMessage(content=\"\\n\".join(self.message_history + [self.prefix])),\n",
|
||||||
|
|||||||
@@ -70,7 +70,7 @@
|
|||||||
" Applies the chatmodel to the message history\n",
|
" Applies the chatmodel to the message history\n",
|
||||||
" and returns the message string\n",
|
" and returns the message string\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" message = self.model(\n",
|
" message = self.model.invoke(\n",
|
||||||
" [\n",
|
" [\n",
|
||||||
" self.system_message,\n",
|
" self.system_message,\n",
|
||||||
" HumanMessage(content=\"\\n\".join(self.message_history + [self.prefix])),\n",
|
" HumanMessage(content=\"\\n\".join(self.message_history + [self.prefix])),\n",
|
||||||
|
|||||||
1
docs/.gitignore
vendored
1
docs/.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
/.quarto/
|
/.quarto/
|
||||||
src/supabase.d.ts
|
src/supabase.d.ts
|
||||||
|
build
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
set -o errexit
|
|
||||||
set -o nounset
|
|
||||||
set -o pipefail
|
|
||||||
set -o xtrace
|
|
||||||
|
|
||||||
SCRIPT_DIR="$(cd "$(dirname "$0")"; pwd)"
|
|
||||||
cd "${SCRIPT_DIR}"
|
|
||||||
|
|
||||||
mkdir -p ../_dist
|
|
||||||
rsync -ruv --exclude node_modules --exclude api_reference --exclude .venv --exclude .docusaurus . ../_dist
|
|
||||||
cd ../_dist
|
|
||||||
poetry run python scripts/model_feat_table.py
|
|
||||||
cp ../cookbook/README.md src/pages/cookbook.mdx
|
|
||||||
mkdir -p docs/templates
|
|
||||||
cp ../templates/docs/INDEX.md docs/templates/index.md
|
|
||||||
poetry run python scripts/copy_templates.py
|
|
||||||
wget -q https://raw.githubusercontent.com/langchain-ai/langserve/main/README.md -O docs/langserve.md
|
|
||||||
wget -q https://raw.githubusercontent.com/langchain-ai/langgraph/main/README.md -O docs/langgraph.md
|
|
||||||
|
|
||||||
yarn
|
|
||||||
|
|
||||||
poetry run quarto preview docs
|
|
||||||
80
docs/Makefile
Normal file
80
docs/Makefile
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
# we build the docs in these stages:
|
||||||
|
# 1. install quarto and python dependencies
|
||||||
|
# 2. copy files from "source dir" to "intermediate dir"
|
||||||
|
# 2. generate files like model feat table, etc in "intermediate dir"
|
||||||
|
# 3. copy files to their right spots (e.g. langserve readme) in "intermediate dir"
|
||||||
|
# 4. build the docs from "intermediate dir" to "output dir"
|
||||||
|
|
||||||
|
SOURCE_DIR = docs/
|
||||||
|
INTERMEDIATE_DIR = build/intermediate/docs
|
||||||
|
OUTPUT_DIR = build/output
|
||||||
|
OUTPUT_DOCS_DIR = $(OUTPUT_DIR)/docs
|
||||||
|
|
||||||
|
PYTHON = .venv/bin/python
|
||||||
|
|
||||||
|
QUARTO_CMD ?= quarto
|
||||||
|
|
||||||
|
PARTNER_DEPS_LIST := $(shell ls -1 ../libs/partners | grep -vE "airbyte|ibm" | xargs -I {} echo "../libs/partners/{}" | tr '\n' ' ')
|
||||||
|
|
||||||
|
PORT ?= 3001
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf build
|
||||||
|
|
||||||
|
install-vercel-deps:
|
||||||
|
yum -y update
|
||||||
|
yum install gcc bzip2-devel libffi-devel zlib-devel wget tar gzip rsync -y
|
||||||
|
|
||||||
|
wget -q https://github.com/quarto-dev/quarto-cli/releases/download/v1.3.450/quarto-1.3.450-linux-amd64.tar.gz
|
||||||
|
tar -xzf quarto-1.3.450-linux-amd64.tar.gz
|
||||||
|
|
||||||
|
install-py-deps:
|
||||||
|
python3 -m venv .venv
|
||||||
|
$(PYTHON) -m pip install --upgrade pip
|
||||||
|
$(PYTHON) -m pip install --upgrade uv
|
||||||
|
$(PYTHON) -m uv pip install -r vercel_requirements.txt
|
||||||
|
$(PYTHON) -m uv pip install --editable $(PARTNER_DEPS_LIST)
|
||||||
|
|
||||||
|
generate-files:
|
||||||
|
mkdir -p $(INTERMEDIATE_DIR)
|
||||||
|
cp -r $(SOURCE_DIR)/* $(INTERMEDIATE_DIR)
|
||||||
|
mkdir -p $(INTERMEDIATE_DIR)/templates
|
||||||
|
cp ../templates/docs/INDEX.md $(INTERMEDIATE_DIR)/templates/index.md
|
||||||
|
cp ../cookbook/README.md $(INTERMEDIATE_DIR)/cookbook.mdx
|
||||||
|
|
||||||
|
$(PYTHON) scripts/model_feat_table.py $(INTERMEDIATE_DIR)
|
||||||
|
|
||||||
|
$(PYTHON) scripts/copy_templates.py $(INTERMEDIATE_DIR)
|
||||||
|
|
||||||
|
wget -q https://raw.githubusercontent.com/langchain-ai/langserve/main/README.md -O $(INTERMEDIATE_DIR)/langserve.md
|
||||||
|
$(PYTHON) scripts/resolve_local_links.py $(INTERMEDIATE_DIR)/langserve.md https://github.com/langchain-ai/langserve/tree/main/
|
||||||
|
|
||||||
|
wget -q https://raw.githubusercontent.com/langchain-ai/langgraph/main/README.md -O $(INTERMEDIATE_DIR)/langgraph.md
|
||||||
|
$(PYTHON) scripts/resolve_local_links.py $(INTERMEDIATE_DIR)/langgraph.md https://github.com/langchain-ai/langgraph/tree/main/
|
||||||
|
|
||||||
|
$(PYTHON) scripts/generate_api_reference_links.py --docs_dir $(INTERMEDIATE_DIR)
|
||||||
|
|
||||||
|
copy-infra:
|
||||||
|
mkdir -p $(OUTPUT_DIR)
|
||||||
|
cp -r src $(OUTPUT_DIR)
|
||||||
|
cp vercel.json $(OUTPUT_DIR)
|
||||||
|
cp babel.config.js $(OUTPUT_DIR)
|
||||||
|
cp -r data $(OUTPUT_DIR)
|
||||||
|
cp docusaurus.config.js $(OUTPUT_DIR)
|
||||||
|
cp package.json $(OUTPUT_DIR)
|
||||||
|
cp sidebars.js $(OUTPUT_DIR)
|
||||||
|
cp -r static $(OUTPUT_DIR)
|
||||||
|
cp yarn.lock $(OUTPUT_DIR)
|
||||||
|
|
||||||
|
quarto-render:
|
||||||
|
$(QUARTO_CMD) render $(INTERMEDIATE_DIR) --output-dir $(OUTPUT_DOCS_DIR) --no-execute
|
||||||
|
mv $(OUTPUT_DOCS_DIR)/$(INTERMEDIATE_DIR)/* $(OUTPUT_DOCS_DIR)
|
||||||
|
rm -rf $(OUTPUT_DOCS_DIR)/build
|
||||||
|
|
||||||
|
md-sync:
|
||||||
|
rsync -avm --include="*/" --include="*.mdx" --include="*.md" --exclude="*" $(INTERMEDIATE_DIR)/ $(OUTPUT_DOCS_DIR)
|
||||||
|
|
||||||
|
build: install-py-deps generate-files copy-infra quarto-render md-sync
|
||||||
|
|
||||||
|
start:
|
||||||
|
cd $(OUTPUT_DIR) && yarn && yarn start --port=$(PORT)
|
||||||
File diff suppressed because one or more lines are too long
@@ -1,76 +0,0 @@
|
|||||||
/* eslint-disable prefer-template */
|
|
||||||
/* eslint-disable no-param-reassign */
|
|
||||||
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
||||||
const babel = require("@babel/core");
|
|
||||||
const path = require("path");
|
|
||||||
const fs = require("fs");
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {string|Buffer} content Content of the resource file
|
|
||||||
* @param {object} [map] SourceMap data consumable by https://github.com/mozilla/source-map
|
|
||||||
* @param {any} [meta] Meta data, could be anything
|
|
||||||
*/
|
|
||||||
async function webpackLoader(content, map, meta) {
|
|
||||||
const cb = this.async();
|
|
||||||
|
|
||||||
if (!this.resourcePath.endsWith(".ts")) {
|
|
||||||
cb(null, JSON.stringify({ content, imports: [] }), map, meta);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const result = await babel.parseAsync(content, {
|
|
||||||
sourceType: "module",
|
|
||||||
filename: this.resourcePath,
|
|
||||||
});
|
|
||||||
|
|
||||||
const imports = [];
|
|
||||||
|
|
||||||
result.program.body.forEach((node) => {
|
|
||||||
if (node.type === "ImportDeclaration") {
|
|
||||||
const source = node.source.value;
|
|
||||||
|
|
||||||
if (!source.startsWith("langchain")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
node.specifiers.forEach((specifier) => {
|
|
||||||
if (specifier.type === "ImportSpecifier") {
|
|
||||||
const local = specifier.local.name;
|
|
||||||
const imported = specifier.imported.name;
|
|
||||||
imports.push({ local, imported, source });
|
|
||||||
} else {
|
|
||||||
throw new Error("Unsupported import type");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
imports.forEach((imp) => {
|
|
||||||
const { imported, source } = imp;
|
|
||||||
const moduleName = source.split("/").slice(1).join("_");
|
|
||||||
const docsPath = path.resolve(__dirname, "docs", "api", moduleName);
|
|
||||||
const available = fs.readdirSync(docsPath, { withFileTypes: true });
|
|
||||||
const found = available.find(
|
|
||||||
(dirent) =>
|
|
||||||
dirent.isDirectory() &&
|
|
||||||
fs.existsSync(path.resolve(docsPath, dirent.name, imported + ".md"))
|
|
||||||
);
|
|
||||||
if (found) {
|
|
||||||
imp.docs =
|
|
||||||
"/" + path.join("docs", "api", moduleName, found.name, imported);
|
|
||||||
} else {
|
|
||||||
throw new Error(
|
|
||||||
`Could not find docs for ${source}.${imported} in docs/api/`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
cb(null, JSON.stringify({ content, imports }), map, meta);
|
|
||||||
} catch (err) {
|
|
||||||
cb(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = webpackLoader;
|
|
||||||
1356
docs/data/people.yml
1356
docs/data/people.yml
File diff suppressed because it is too large
Load Diff
@@ -9,6 +9,10 @@
|
|||||||
|
|
||||||
## Tutorials
|
## Tutorials
|
||||||
|
|
||||||
|
### [LangChain v 0.1 by LangChain.ai](https://www.youtube.com/playlist?list=PLfaIDFEXuae0gBSJ9T0w7cu7iJZbH3T31)
|
||||||
|
### [Build with Langchain - Advanced by LangChain.ai](https://www.youtube.com/playlist?list=PLfaIDFEXuae06tclDATrMYY0idsTdLg9v)
|
||||||
|
### [LangGraph by LangChain.ai](https://www.youtube.com/playlist?list=PLfaIDFEXuae16n2TWUkKq5PgJ0w6Pkwtg)
|
||||||
|
|
||||||
### [by Greg Kamradt](https://www.youtube.com/playlist?list=PLqZXAkvF1bPNQER9mLmDbntNfSpzdDIU5)
|
### [by Greg Kamradt](https://www.youtube.com/playlist?list=PLqZXAkvF1bPNQER9mLmDbntNfSpzdDIU5)
|
||||||
### [by Sam Witteveen](https://www.youtube.com/playlist?list=PL8motc6AQftk1Bs42EW45kwYbyJ4jOdiZ)
|
### [by Sam Witteveen](https://www.youtube.com/playlist?list=PL8motc6AQftk1Bs42EW45kwYbyJ4jOdiZ)
|
||||||
### [by James Briggs](https://www.youtube.com/playlist?list=PLIUOU7oqGTLieV9uTIFMm6_4PXg-hlN6F)
|
### [by James Briggs](https://www.youtube.com/playlist?list=PLIUOU7oqGTLieV9uTIFMm6_4PXg-hlN6F)
|
||||||
@@ -35,6 +39,7 @@
|
|||||||
- [Udacity](https://www.udacity.com/catalog/all/any-price/any-school/any-skill/any-difficulty/any-duration/any-type/relevance/page-1?searchValue=langchain)
|
- [Udacity](https://www.udacity.com/catalog/all/any-price/any-school/any-skill/any-difficulty/any-duration/any-type/relevance/page-1?searchValue=langchain)
|
||||||
- [LinkedIn Learning](https://www.linkedin.com/search/results/learning/?keywords=langchain)
|
- [LinkedIn Learning](https://www.linkedin.com/search/results/learning/?keywords=langchain)
|
||||||
- [edX](https://www.edx.org/search?q=langchain)
|
- [edX](https://www.edx.org/search?q=langchain)
|
||||||
|
- [freeCodeCamp](https://www.youtube.com/@freecodecamp/search?query=langchain)
|
||||||
|
|
||||||
## Short Tutorials
|
## Short Tutorials
|
||||||
|
|
||||||
|
|||||||
@@ -190,12 +190,9 @@ Maintainer steps (Contributors should **not** do these):
|
|||||||
|
|
||||||
## Partner package in external repo
|
## Partner package in external repo
|
||||||
|
|
||||||
If you are creating a partner package in an external repo, you should follow the same steps as above,
|
Partner packages in external repos must be coordinated between the LangChain team and
|
||||||
but you will need to set up your own CI/CD and package management.
|
the partner organization to ensure that they are maintained and updated.
|
||||||
|
|
||||||
Name your package as `langchain-{partner}-{integration}`.
|
If you're interested in creating a partner package in an external repo, please start
|
||||||
|
with one in the LangChain repo, and then reach out to the LangChain team to discuss
|
||||||
Still, you have to create the `libs/partners/{partner}-{integration}` folder in the `LangChain` monorepo
|
how to move it to an external repo.
|
||||||
and add a `README.md` file with a link to the external repo.
|
|
||||||
See this [example](https://github.com/langchain-ai/langchain/tree/master/libs/partners/google-genai).
|
|
||||||
This allows keeping track of all the partner packages in the `LangChain` documentation.
|
|
||||||
|
|||||||
@@ -440,7 +440,7 @@
|
|||||||
"id": "e6833844-f1c4-444c-a3d2-31b3c6b31d46",
|
"id": "e6833844-f1c4-444c-a3d2-31b3c6b31d46",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"We then use the `RunnableParallel` to prepare the expected inputs into the prompt by using the entries for the retrieved documents as well as the original user question, using the retriever for document search, and RunnablePassthrough to pass the user’s question:"
|
"We then use the `RunnableParallel` to prepare the expected inputs into the prompt by using the entries for the retrieved documents as well as the original user question, using the retriever for document search, and `RunnablePassthrough` to pass the user’s question:"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -29,9 +29,9 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.prompts import ChatPromptTemplate\n",
|
|
||||||
"from langchain_community.vectorstores import FAISS\n",
|
"from langchain_community.vectorstores import FAISS\n",
|
||||||
"from langchain_core.output_parsers import StrOutputParser\n",
|
"from langchain_core.output_parsers import StrOutputParser\n",
|
||||||
|
"from langchain_core.prompts import ChatPromptTemplate\n",
|
||||||
"from langchain_core.runnables import RunnablePassthrough\n",
|
"from langchain_core.runnables import RunnablePassthrough\n",
|
||||||
"from langchain_openai import ChatOpenAI, OpenAIEmbeddings"
|
"from langchain_openai import ChatOpenAI, OpenAIEmbeddings"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ LCEL was designed from day 1 to **support putting prototypes in production, with
|
|||||||
When you build your chains with LCEL you get the best possible time-to-first-token (time elapsed until the first chunk of output comes out). For some chains this means eg. we stream tokens straight from an LLM to a streaming output parser, and you get back parsed, incremental chunks of output at the same rate as the LLM provider outputs the raw tokens.
|
When you build your chains with LCEL you get the best possible time-to-first-token (time elapsed until the first chunk of output comes out). For some chains this means eg. we stream tokens straight from an LLM to a streaming output parser, and you get back parsed, incremental chunks of output at the same rate as the LLM provider outputs the raw tokens.
|
||||||
|
|
||||||
[**Async support**](/docs/expression_language/interface)
|
[**Async support**](/docs/expression_language/interface)
|
||||||
Any chain built with LCEL can be called both with the synchronous API (eg. in your Jupyter notebook while prototyping) as well as with the asynchronous API (eg. in a [LangServe](/docs/langsmith) server). This enables using the same code for prototypes and in production, with great performance, and the ability to handle many concurrent requests in the same server.
|
Any chain built with LCEL can be called both with the synchronous API (eg. in your Jupyter notebook while prototyping) as well as with the asynchronous API (eg. in a [LangServe](/docs/langserve) server). This enables using the same code for prototypes and in production, with great performance, and the ability to handle many concurrent requests in the same server.
|
||||||
|
|
||||||
[**Optimized parallel execution**](/docs/expression_language/primitives/parallel)
|
[**Optimized parallel execution**](/docs/expression_language/primitives/parallel)
|
||||||
Whenever your LCEL chains have steps that can be executed in parallel (eg if you fetch documents from multiple retrievers) we automatically do it, both in the sync and the async interfaces, for the smallest possible latency.
|
Whenever your LCEL chains have steps that can be executed in parallel (eg if you fetch documents from multiple retrievers) we automatically do it, both in the sync and the async interfaces, for the smallest possible latency.
|
||||||
|
|||||||
@@ -63,7 +63,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.prompts import PromptTemplate\n",
|
"from langchain_core.prompts import PromptTemplate\n",
|
||||||
"from langchain_core.runnables import ConfigurableField\n",
|
"from langchain_core.runnables import ConfigurableField\n",
|
||||||
"from langchain_openai import ChatOpenAI\n",
|
"from langchain_openai import ChatOpenAI\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -285,8 +285,8 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.prompts import PromptTemplate\n",
|
|
||||||
"from langchain_community.chat_models import ChatAnthropic\n",
|
"from langchain_community.chat_models import ChatAnthropic\n",
|
||||||
|
"from langchain_core.prompts import PromptTemplate\n",
|
||||||
"from langchain_core.runnables import ConfigurableField\n",
|
"from langchain_core.runnables import ConfigurableField\n",
|
||||||
"from langchain_openai import ChatOpenAI"
|
"from langchain_openai import ChatOpenAI"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -194,7 +194,7 @@ Prompt templates convert raw user input to better input to the LLM.
|
|||||||
```python
|
```python
|
||||||
from langchain_core.prompts import ChatPromptTemplate
|
from langchain_core.prompts import ChatPromptTemplate
|
||||||
prompt = ChatPromptTemplate.from_messages([
|
prompt = ChatPromptTemplate.from_messages([
|
||||||
("system", "You are world class technical documentation writer."),
|
("system", "You are a world class technical documentation writer."),
|
||||||
("user", "{input}")
|
("user", "{input}")
|
||||||
])
|
])
|
||||||
```
|
```
|
||||||
@@ -293,7 +293,7 @@ embeddings = OllamaEmbeddings()
|
|||||||
Make sure you have the `cohere` package installed and the appropriate environment variables set (these are the same as needed for the LLM).
|
Make sure you have the `cohere` package installed and the appropriate environment variables set (these are the same as needed for the LLM).
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from langchain_community.embeddings import CohereEmbeddings
|
from langchain_cohere.embeddings import CohereEmbeddings
|
||||||
|
|
||||||
embeddings = CohereEmbeddings()
|
embeddings = CohereEmbeddings()
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook shows how to prevent prompt injection attacks using the text classification model from `HuggingFace`.\n",
|
"This notebook shows how to prevent prompt injection attacks using the text classification model from `HuggingFace`.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"By default, it uses a *[laiyer/deberta-v3-base-prompt-injection](https://huggingface.co/laiyer/deberta-v3-base-prompt-injection)* model trained to identify prompt injections. \n",
|
"By default, it uses a *[protectai/deberta-v3-base-prompt-injection-v2](https://huggingface.co/protectai/deberta-v3-base-prompt-injection-v2)* model trained to identify prompt injections. \n",
|
||||||
"\n",
|
"\n",
|
||||||
"In this notebook, we will use the ONNX version of the model to speed up the inference. "
|
"In this notebook, we will use the ONNX version of the model to speed up the inference. "
|
||||||
]
|
]
|
||||||
@@ -49,11 +49,15 @@
|
|||||||
"from optimum.onnxruntime import ORTModelForSequenceClassification\n",
|
"from optimum.onnxruntime import ORTModelForSequenceClassification\n",
|
||||||
"from transformers import AutoTokenizer, pipeline\n",
|
"from transformers import AutoTokenizer, pipeline\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Using https://huggingface.co/laiyer/deberta-v3-base-prompt-injection\n",
|
"# Using https://huggingface.co/protectai/deberta-v3-base-prompt-injection-v2\n",
|
||||||
"model_path = \"laiyer/deberta-v3-base-prompt-injection\"\n",
|
"model_path = \"laiyer/deberta-v3-base-prompt-injection-v2\"\n",
|
||||||
"tokenizer = AutoTokenizer.from_pretrained(model_path)\n",
|
"revision = None # We recommend specifiying the revision to avoid breaking changes or supply chain attacks\n",
|
||||||
"tokenizer.model_input_names = [\"input_ids\", \"attention_mask\"] # Hack to run the model\n",
|
"tokenizer = AutoTokenizer.from_pretrained(\n",
|
||||||
"model = ORTModelForSequenceClassification.from_pretrained(model_path, subfolder=\"onnx\")\n",
|
" model_path, revision=revision, model_input_names=[\"input_ids\", \"attention_mask\"]\n",
|
||||||
|
")\n",
|
||||||
|
"model = ORTModelForSequenceClassification.from_pretrained(\n",
|
||||||
|
" model_path, revision=revision, subfolder=\"onnx\"\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"classifier = pipeline(\n",
|
"classifier = pipeline(\n",
|
||||||
" \"text-classification\",\n",
|
" \"text-classification\",\n",
|
||||||
|
|||||||
@@ -137,7 +137,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.prompts.prompt import PromptTemplate\n",
|
"from langchain_core.prompts.prompt import PromptTemplate\n",
|
||||||
"from langchain_openai import ChatOpenAI\n",
|
"from langchain_openai import ChatOpenAI\n",
|
||||||
"\n",
|
"\n",
|
||||||
"anonymizer = PresidioAnonymizer()\n",
|
"anonymizer = PresidioAnonymizer()\n",
|
||||||
|
|||||||
@@ -878,8 +878,8 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.prompts.prompt import PromptTemplate\n",
|
|
||||||
"from langchain_core.prompts import format_document\n",
|
"from langchain_core.prompts import format_document\n",
|
||||||
|
"from langchain_core.prompts.prompt import PromptTemplate\n",
|
||||||
"\n",
|
"\n",
|
||||||
"DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template=\"{page_content}\")\n",
|
"DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template=\"{page_content}\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -207,7 +207,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.prompts.prompt import PromptTemplate\n",
|
"from langchain_core.prompts.prompt import PromptTemplate\n",
|
||||||
"from langchain_openai import ChatOpenAI\n",
|
"from langchain_openai import ChatOpenAI\n",
|
||||||
"\n",
|
"\n",
|
||||||
"anonymizer = PresidioReversibleAnonymizer()\n",
|
"anonymizer = PresidioReversibleAnonymizer()\n",
|
||||||
|
|||||||
@@ -278,8 +278,8 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.chains import LLMChain\n",
|
"from langchain.chains import LLMChain\n",
|
||||||
"from langchain.prompts import PromptTemplate\n",
|
|
||||||
"from langchain_core.callbacks.stdout import StdOutCallbackHandler\n",
|
"from langchain_core.callbacks.stdout import StdOutCallbackHandler\n",
|
||||||
|
"from langchain_core.prompts import PromptTemplate\n",
|
||||||
"from langchain_openai import OpenAI\n",
|
"from langchain_openai import OpenAI\n",
|
||||||
"\n",
|
"\n",
|
||||||
"argilla_callback = ArgillaCallbackHandler(\n",
|
"argilla_callback = ArgillaCallbackHandler(\n",
|
||||||
|
|||||||
@@ -42,7 +42,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%pip install --upgrade --quiet langchain langchain-openai deepeval"
|
"%pip install --upgrade --quiet langchain langchain-openai deepeval langchain-chroma"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -215,8 +215,8 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"import requests\n",
|
"import requests\n",
|
||||||
"from langchain.chains import RetrievalQA\n",
|
"from langchain.chains import RetrievalQA\n",
|
||||||
|
"from langchain_chroma import Chroma\n",
|
||||||
"from langchain_community.document_loaders import TextLoader\n",
|
"from langchain_community.document_loaders import TextLoader\n",
|
||||||
"from langchain_community.vectorstores import Chroma\n",
|
|
||||||
"from langchain_openai import OpenAI, OpenAIEmbeddings\n",
|
"from langchain_openai import OpenAI, OpenAIEmbeddings\n",
|
||||||
"from langchain_text_splitters import CharacterTextSplitter\n",
|
"from langchain_text_splitters import CharacterTextSplitter\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -170,8 +170,8 @@
|
|||||||
"import os\n",
|
"import os\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from langchain.chains import LLMChain\n",
|
"from langchain.chains import LLMChain\n",
|
||||||
"from langchain.prompts import PromptTemplate\n",
|
"from langchain_core.prompts import PromptTemplate\n",
|
||||||
"from langchain.prompts.chat import (\n",
|
"from langchain_core.prompts.chat import (\n",
|
||||||
" ChatPromptTemplate,\n",
|
" ChatPromptTemplate,\n",
|
||||||
" HumanMessagePromptTemplate,\n",
|
" HumanMessagePromptTemplate,\n",
|
||||||
")\n",
|
")\n",
|
||||||
|
|||||||
@@ -151,7 +151,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.prompts import (\n",
|
"from langchain_core.prompts import (\n",
|
||||||
" ChatPromptTemplate,\n",
|
" ChatPromptTemplate,\n",
|
||||||
" FewShotChatMessagePromptTemplate,\n",
|
" FewShotChatMessagePromptTemplate,\n",
|
||||||
")\n",
|
")\n",
|
||||||
|
|||||||
@@ -194,7 +194,7 @@
|
|||||||
"llm = OpenAI(\n",
|
"llm = OpenAI(\n",
|
||||||
" temperature=0, callbacks=[LabelStudioCallbackHandler(project_name=\"My Project\")]\n",
|
" temperature=0, callbacks=[LabelStudioCallbackHandler(project_name=\"My Project\")]\n",
|
||||||
")\n",
|
")\n",
|
||||||
"print(llm(\"Tell me a joke\"))"
|
"print(llm.invoke(\"Tell me a joke\"))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -270,7 +270,7 @@
|
|||||||
" )\n",
|
" )\n",
|
||||||
" ]\n",
|
" ]\n",
|
||||||
")\n",
|
")\n",
|
||||||
"llm_results = chat_llm(\n",
|
"llm_results = chat_llm.invoke(\n",
|
||||||
" [\n",
|
" [\n",
|
||||||
" SystemMessage(content=\"Always use a lot of emojis\"),\n",
|
" SystemMessage(content=\"Always use a lot of emojis\"),\n",
|
||||||
" HumanMessage(content=\"Tell me a joke\"),\n",
|
" HumanMessage(content=\"Tell me a joke\"),\n",
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ User tracking allows you to identify your users, track their cost, conversations
|
|||||||
from langchain_community.callbacks.llmonitor_callback import LLMonitorCallbackHandler, identify
|
from langchain_community.callbacks.llmonitor_callback import LLMonitorCallbackHandler, identify
|
||||||
|
|
||||||
with identify("user-123"):
|
with identify("user-123"):
|
||||||
llm("Tell me a joke")
|
llm.invoke("Tell me a joke")
|
||||||
|
|
||||||
with identify("user-456", user_props={"email": "user456@test.com"}):
|
with identify("user-456", user_props={"email": "user456@test.com"}):
|
||||||
agen.run("Who is Leo DiCaprio's girlfriend?")
|
agen.run("Who is Leo DiCaprio's girlfriend?")
|
||||||
|
|||||||
@@ -103,7 +103,7 @@
|
|||||||
" temperature=0,\n",
|
" temperature=0,\n",
|
||||||
" callbacks=[PromptLayerCallbackHandler(pl_tags=[\"chatopenai\"])],\n",
|
" callbacks=[PromptLayerCallbackHandler(pl_tags=[\"chatopenai\"])],\n",
|
||||||
")\n",
|
")\n",
|
||||||
"llm_results = chat_llm(\n",
|
"llm_results = chat_llm.invoke(\n",
|
||||||
" [\n",
|
" [\n",
|
||||||
" HumanMessage(content=\"What comes after 1,2,3 ?\"),\n",
|
" HumanMessage(content=\"What comes after 1,2,3 ?\"),\n",
|
||||||
" HumanMessage(content=\"Tell me another joke?\"),\n",
|
" HumanMessage(content=\"Tell me another joke?\"),\n",
|
||||||
@@ -129,10 +129,11 @@
|
|||||||
"from langchain_community.llms import GPT4All\n",
|
"from langchain_community.llms import GPT4All\n",
|
||||||
"\n",
|
"\n",
|
||||||
"model = GPT4All(model=\"./models/gpt4all-model.bin\", n_ctx=512, n_threads=8)\n",
|
"model = GPT4All(model=\"./models/gpt4all-model.bin\", n_ctx=512, n_threads=8)\n",
|
||||||
|
"callbacks = [PromptLayerCallbackHandler(pl_tags=[\"langchain\", \"gpt4all\"])]\n",
|
||||||
"\n",
|
"\n",
|
||||||
"response = model(\n",
|
"response = model.invoke(\n",
|
||||||
" \"Once upon a time, \",\n",
|
" \"Once upon a time, \",\n",
|
||||||
" callbacks=[PromptLayerCallbackHandler(pl_tags=[\"langchain\", \"gpt4all\"])],\n",
|
" config={\"callbacks\": callbacks},\n",
|
||||||
")"
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -181,7 +182,7 @@
|
|||||||
")\n",
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"example_prompt = promptlayer.prompts.get(\"example\", version=1, langchain=True)\n",
|
"example_prompt = promptlayer.prompts.get(\"example\", version=1, langchain=True)\n",
|
||||||
"openai_llm(example_prompt.format(product=\"toasters\"))"
|
"openai_llm.invoke(example_prompt.format(product=\"toasters\"))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -91,7 +91,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import initialize_agent, load_tools\n",
|
"from langchain.agents import initialize_agent, load_tools\n",
|
||||||
"from langchain.chains import LLMChain, SimpleSequentialChain\n",
|
"from langchain.chains import LLMChain, SimpleSequentialChain\n",
|
||||||
"from langchain.prompts import PromptTemplate\n",
|
"from langchain_core.prompts import PromptTemplate\n",
|
||||||
"from langchain_openai import OpenAI\n",
|
"from langchain_openai import OpenAI\n",
|
||||||
"from sagemaker.analytics import ExperimentAnalytics\n",
|
"from sagemaker.analytics import ExperimentAnalytics\n",
|
||||||
"from sagemaker.experiments.run import Run\n",
|
"from sagemaker.experiments.run import Run\n",
|
||||||
|
|||||||
@@ -315,7 +315,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"chat_res = chat_llm(\n",
|
"chat_res = chat_llm.invoke(\n",
|
||||||
" [\n",
|
" [\n",
|
||||||
" SystemMessage(content=\"Every answer of yours must be about OpenAI.\"),\n",
|
" SystemMessage(content=\"Every answer of yours must be about OpenAI.\"),\n",
|
||||||
" HumanMessage(content=\"Tell me a joke\"),\n",
|
" HumanMessage(content=\"Tell me a joke\"),\n",
|
||||||
|
|||||||
503
docs/docs/integrations/callbacks/uptrain.ipynb
Normal file
503
docs/docs/integrations/callbacks/uptrain.ipynb
Normal file
@@ -0,0 +1,503 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<a target=\"_blank\" href=\"https://colab.research.google.com/github/langchain-ai/langchain/blob/master/docs/docs/integrations/callbacks/uptrain.ipynb\">\n",
|
||||||
|
" <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
|
||||||
|
"</a>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# UpTrain\n",
|
||||||
|
"\n",
|
||||||
|
"> UpTrain [[github](https://github.com/uptrain-ai/uptrain) || [website](https://uptrain.ai/) || [docs](https://docs.uptrain.ai/getting-started/introduction)] is an open-source platform to evaluate and improve LLM applications. It provides grades for 20+ preconfigured checks (covering language, code, embedding use cases), performs root cause analyses on instances of failure cases and provides guidance for resolving them."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## UpTrain Callback Handler\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook showcases the UpTrain callback handler seamlessly integrating into your pipeline, facilitating diverse evaluations. We have chosen a few evaluations that we deemed apt for evaluating the chains. These evaluations run automatically, with results displayed in the output. More details on UpTrain's evaluations can be found [here](https://github.com/uptrain-ai/uptrain?tab=readme-ov-file#pre-built-evaluations-we-offer-). \n",
|
||||||
|
"\n",
|
||||||
|
"Selected retievers from Langchain are highlighted for demonstration:\n",
|
||||||
|
"\n",
|
||||||
|
"### 1. **Vanilla RAG**:\n",
|
||||||
|
"RAG plays a crucial role in retrieving context and generating responses. To ensure its performance and response quality, we conduct the following evaluations:\n",
|
||||||
|
"\n",
|
||||||
|
"- **[Context Relevance](https://docs.uptrain.ai/predefined-evaluations/context-awareness/context-relevance)**: Determines if the context extracted from the query is relevant to the response.\n",
|
||||||
|
"- **[Factual Accuracy](https://docs.uptrain.ai/predefined-evaluations/context-awareness/factual-accuracy)**: Assesses if the LLM is hallcuinating or providing incorrect information.\n",
|
||||||
|
"- **[Response Completeness](https://docs.uptrain.ai/predefined-evaluations/response-quality/response-completeness)**: Checks if the response contains all the information requested by the query.\n",
|
||||||
|
"\n",
|
||||||
|
"### 2. **Multi Query Generation**:\n",
|
||||||
|
"MultiQueryRetriever creates multiple variants of a question having a similar meaning to the original question. Given the complexity, we include the previous evaluations and add:\n",
|
||||||
|
"\n",
|
||||||
|
"- **[Multi Query Accuracy](https://docs.uptrain.ai/predefined-evaluations/query-quality/multi-query-accuracy)**: Assures that the multi-queries generated mean the same as the original query.\n",
|
||||||
|
"\n",
|
||||||
|
"### 3. **Context Compression and Reranking**:\n",
|
||||||
|
"Re-ranking involves reordering nodes based on relevance to the query and choosing top n nodes. Since the number of nodes can reduce once the re-ranking is complete, we perform the following evaluations:\n",
|
||||||
|
"\n",
|
||||||
|
"- **[Context Reranking](https://docs.uptrain.ai/predefined-evaluations/context-awareness/context-reranking)**: Checks if the order of re-ranked nodes is more relevant to the query than the original order.\n",
|
||||||
|
"- **[Context Conciseness](https://docs.uptrain.ai/predefined-evaluations/context-awareness/context-conciseness)**: Examines whether the reduced number of nodes still provides all the required information.\n",
|
||||||
|
"\n",
|
||||||
|
"These evaluations collectively ensure the robustness and effectiveness of the RAG, MultiQueryRetriever, and the Reranking process in the chain."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Install Dependencies"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 22,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
|
||||||
|
"To disable this warning, you can either:\n",
|
||||||
|
"\t- Avoid using `tokenizers` before the fork if possible\n",
|
||||||
|
"\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[33mWARNING: There was an error checking the latest version of pip.\u001b[0m\u001b[33m\n",
|
||||||
|
"\u001b[0mNote: you may need to restart the kernel to use updated packages.\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"%pip install -qU langchain langchain_openai uptrain faiss-cpu flashrank"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"NOTE: that you can also install `faiss-gpu` instead of `faiss-cpu` if you want to use the GPU enabled version of the library."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Import Libraries"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 23,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from getpass import getpass\n",
|
||||||
|
"\n",
|
||||||
|
"from langchain.chains import RetrievalQA\n",
|
||||||
|
"from langchain.retrievers import ContextualCompressionRetriever\n",
|
||||||
|
"from langchain.retrievers.document_compressors import FlashrankRerank\n",
|
||||||
|
"from langchain.retrievers.multi_query import MultiQueryRetriever\n",
|
||||||
|
"from langchain_community.callbacks.uptrain_callback import UpTrainCallbackHandler\n",
|
||||||
|
"from langchain_community.document_loaders import TextLoader\n",
|
||||||
|
"from langchain_community.vectorstores import FAISS\n",
|
||||||
|
"from langchain_core.output_parsers.string import StrOutputParser\n",
|
||||||
|
"from langchain_core.prompts.chat import ChatPromptTemplate\n",
|
||||||
|
"from langchain_core.runnables.passthrough import RunnablePassthrough\n",
|
||||||
|
"from langchain_openai import ChatOpenAI, OpenAIEmbeddings\n",
|
||||||
|
"from langchain_text_splitters import (\n",
|
||||||
|
" RecursiveCharacterTextSplitter,\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Load the documents"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 24,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"loader = TextLoader(\"../../modules/state_of_the_union.txt\")\n",
|
||||||
|
"documents = loader.load()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Split the document into chunks"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 25,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
|
||||||
|
"chunks = text_splitter.split_documents(documents)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Create the retriever"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 26,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"embeddings = OpenAIEmbeddings()\n",
|
||||||
|
"db = FAISS.from_documents(chunks, embeddings)\n",
|
||||||
|
"retriever = db.as_retriever()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Define the LLM"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 27,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"llm = ChatOpenAI(temperature=0, model=\"gpt-4\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Set the openai API key\n",
|
||||||
|
"This key is required to perform the evaluations. UpTrain uses the GPT models to evaluate the responses generated by the LLM."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 28,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"OPENAI_API_KEY = getpass()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Setup\n",
|
||||||
|
"\n",
|
||||||
|
"For each of the retrievers below, it is better to define the callback handler again to avoid interference. You can choose between the following options for evaluating using UpTrain:\n",
|
||||||
|
"\n",
|
||||||
|
"### 1. **UpTrain's Open-Source Software (OSS)**: \n",
|
||||||
|
"You can use the open-source evaluation service to evaluate your model.\n",
|
||||||
|
"In this case, you will need to provie an OpenAI API key. You can get yours [here](https://platform.openai.com/account/api-keys).\n",
|
||||||
|
"\n",
|
||||||
|
"Parameters:\n",
|
||||||
|
"- key_type=\"openai\"\n",
|
||||||
|
"- api_key=\"OPENAI_API_KEY\"\n",
|
||||||
|
"- project_name_prefix=\"PROJECT_NAME_PREFIX\"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"### 2. **UpTrain Managed Service and Dashboards**: \n",
|
||||||
|
"You can create a free UpTrain account [here](https://uptrain.ai/) and get free trial credits. If you want more trial credits, [book a call with the maintainers of UpTrain here](https://calendly.com/uptrain-sourabh/30min).\n",
|
||||||
|
"\n",
|
||||||
|
"UpTrain Managed service provides:\n",
|
||||||
|
"1. Dashboards with advanced drill-down and filtering options\n",
|
||||||
|
"1. Insights and common topics among failing cases\n",
|
||||||
|
"1. Observability and real-time monitoring of production data\n",
|
||||||
|
"1. Regression testing via seamless integration with your CI/CD pipelines\n",
|
||||||
|
"\n",
|
||||||
|
"The notebook contains some screenshots of the dashboards and the insights that you can get from the UpTrain managed service.\n",
|
||||||
|
"\n",
|
||||||
|
"Parameters:\n",
|
||||||
|
"- key_type=\"uptrain\"\n",
|
||||||
|
"- api_key=\"UPTRAIN_API_KEY\"\n",
|
||||||
|
"- project_name_prefix=\"PROJECT_NAME_PREFIX\"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"**Note:** The `project_name_prefix` will be used as prefix for the project names in the UpTrain dashboard. These will be different for different types of evals. For example, if you set project_name_prefix=\"langchain\" and perform the multi_query evaluation, the project name will be \"langchain_multi_query\"."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 1. Vanilla RAG"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"UpTrain callback handler will automatically capture the query, context and response once generated and will run the following three evaluations *(Graded from 0 to 1)* on the response:\n",
|
||||||
|
"- **[Context Relevance](https://docs.uptrain.ai/predefined-evaluations/context-awareness/context-relevance)**: Check if the context extractedfrom the query is relevant to the response.\n",
|
||||||
|
"- **[Factual Accuracy](https://docs.uptrain.ai/predefined-evaluations/context-awareness/factual-accuracy)**: Check how factually accurate the response is.\n",
|
||||||
|
"- **[Response Completeness](https://docs.uptrain.ai/predefined-evaluations/response-quality/response-completeness)**: Check if the response contains all the information that the query is asking for."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 29,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[32m2024-04-17 17:03:44.969\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate_on_server\u001b[0m:\u001b[36m378\u001b[0m - \u001b[1mSending evaluation request for rows 0 to <50 to the Uptrain\u001b[0m\n",
|
||||||
|
"\u001b[32m2024-04-17 17:04:05.809\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate\u001b[0m:\u001b[36m367\u001b[0m - \u001b[1mLocal server not running, start the server to log data and visualize in the dashboard!\u001b[0m\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"Question: What did the president say about Ketanji Brown Jackson\n",
|
||||||
|
"Response: The president mentioned that he had nominated Ketanji Brown Jackson to serve on the United States Supreme Court 4 days ago. He described her as one of the nation's top legal minds who will continue Justice Breyer’s legacy of excellence. He also mentioned that she is a former top litigator in private practice, a former federal public defender, and comes from a family of public school educators and police officers. He described her as a consensus builder and noted that since her nomination, she has received a broad range of support from various groups, including the Fraternal Order of Police and former judges appointed by both Democrats and Republicans.\n",
|
||||||
|
"\n",
|
||||||
|
"Context Relevance Score: 1.0\n",
|
||||||
|
"Factual Accuracy Score: 1.0\n",
|
||||||
|
"Response Completeness Score: 1.0\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"# Create the RAG prompt\n",
|
||||||
|
"template = \"\"\"Answer the question based only on the following context, which can include text and tables:\n",
|
||||||
|
"{context}\n",
|
||||||
|
"Question: {question}\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
"rag_prompt_text = ChatPromptTemplate.from_template(template)\n",
|
||||||
|
"\n",
|
||||||
|
"# Create the chain\n",
|
||||||
|
"chain = (\n",
|
||||||
|
" {\"context\": retriever, \"question\": RunnablePassthrough()}\n",
|
||||||
|
" | rag_prompt_text\n",
|
||||||
|
" | llm\n",
|
||||||
|
" | StrOutputParser()\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"# Create the uptrain callback handler\n",
|
||||||
|
"uptrain_callback = UpTrainCallbackHandler(key_type=\"openai\", api_key=OPENAI_API_KEY)\n",
|
||||||
|
"config = {\"callbacks\": [uptrain_callback]}\n",
|
||||||
|
"\n",
|
||||||
|
"# Invoke the chain with a query\n",
|
||||||
|
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
|
||||||
|
"docs = chain.invoke(query, config=config)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 2. Multi Query Generation\n",
|
||||||
|
"\n",
|
||||||
|
"The **MultiQueryRetriever** is used to tackle the problem that the RAG pipeline might not return the best set of documents based on the query. It generates multiple queries that mean the same as the original query and then fetches documents for each.\n",
|
||||||
|
"\n",
|
||||||
|
"To evluate this retriever, UpTrain will run the following evaluation:\n",
|
||||||
|
"- **[Multi Query Accuracy](https://docs.uptrain.ai/predefined-evaluations/query-quality/multi-query-accuracy)**: Checks if the multi-queries generated mean the same as the original query."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 30,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[32m2024-04-17 17:04:10.675\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate_on_server\u001b[0m:\u001b[36m378\u001b[0m - \u001b[1mSending evaluation request for rows 0 to <50 to the Uptrain\u001b[0m\n",
|
||||||
|
"\u001b[32m2024-04-17 17:04:16.804\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate\u001b[0m:\u001b[36m367\u001b[0m - \u001b[1mLocal server not running, start the server to log data and visualize in the dashboard!\u001b[0m\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"Question: What did the president say about Ketanji Brown Jackson\n",
|
||||||
|
"Multi Queries:\n",
|
||||||
|
" - How did the president comment on Ketanji Brown Jackson?\n",
|
||||||
|
" - What were the president's remarks regarding Ketanji Brown Jackson?\n",
|
||||||
|
" - What statements has the president made about Ketanji Brown Jackson?\n",
|
||||||
|
"\n",
|
||||||
|
"Multi Query Accuracy Score: 0.5\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[32m2024-04-17 17:04:22.027\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate_on_server\u001b[0m:\u001b[36m378\u001b[0m - \u001b[1mSending evaluation request for rows 0 to <50 to the Uptrain\u001b[0m\n",
|
||||||
|
"\u001b[32m2024-04-17 17:04:44.033\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate\u001b[0m:\u001b[36m367\u001b[0m - \u001b[1mLocal server not running, start the server to log data and visualize in the dashboard!\u001b[0m\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"Question: What did the president say about Ketanji Brown Jackson\n",
|
||||||
|
"Response: The president mentioned that he had nominated Circuit Court of Appeals Judge Ketanji Brown Jackson to serve on the United States Supreme Court 4 days ago. He described her as one of the nation's top legal minds who will continue Justice Breyer’s legacy of excellence. He also mentioned that since her nomination, she has received a broad range of support—from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.\n",
|
||||||
|
"\n",
|
||||||
|
"Context Relevance Score: 1.0\n",
|
||||||
|
"Factual Accuracy Score: 1.0\n",
|
||||||
|
"Response Completeness Score: 1.0\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"# Create the retriever\n",
|
||||||
|
"multi_query_retriever = MultiQueryRetriever.from_llm(retriever=retriever, llm=llm)\n",
|
||||||
|
"\n",
|
||||||
|
"# Create the uptrain callback\n",
|
||||||
|
"uptrain_callback = UpTrainCallbackHandler(key_type=\"openai\", api_key=OPENAI_API_KEY)\n",
|
||||||
|
"config = {\"callbacks\": [uptrain_callback]}\n",
|
||||||
|
"\n",
|
||||||
|
"# Create the RAG prompt\n",
|
||||||
|
"template = \"\"\"Answer the question based only on the following context, which can include text and tables:\n",
|
||||||
|
"{context}\n",
|
||||||
|
"Question: {question}\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
"rag_prompt_text = ChatPromptTemplate.from_template(template)\n",
|
||||||
|
"\n",
|
||||||
|
"chain = (\n",
|
||||||
|
" {\"context\": multi_query_retriever, \"question\": RunnablePassthrough()}\n",
|
||||||
|
" | rag_prompt_text\n",
|
||||||
|
" | llm\n",
|
||||||
|
" | StrOutputParser()\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"# Invoke the chain with a query\n",
|
||||||
|
"question = \"What did the president say about Ketanji Brown Jackson\"\n",
|
||||||
|
"docs = chain.invoke(question, config=config)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 3. Context Compression and Reranking\n",
|
||||||
|
"\n",
|
||||||
|
"The reranking process involves reordering nodes based on relevance to the query and choosing the top n nodes. Since the number of nodes can reduce once the reranking is complete, we perform the following evaluations:\n",
|
||||||
|
"- **[Context Reranking](https://docs.uptrain.ai/predefined-evaluations/context-awareness/context-reranking)**: Check if the order of re-ranked nodes is more relevant to the query than the original order.\n",
|
||||||
|
"- **[Context Conciseness](https://docs.uptrain.ai/predefined-evaluations/context-awareness/context-conciseness)**: Check if the reduced number of nodes still provides all the required information."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 31,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[32m2024-04-17 17:04:46.462\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate_on_server\u001b[0m:\u001b[36m378\u001b[0m - \u001b[1mSending evaluation request for rows 0 to <50 to the Uptrain\u001b[0m\n",
|
||||||
|
"\u001b[32m2024-04-17 17:04:53.561\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate\u001b[0m:\u001b[36m367\u001b[0m - \u001b[1mLocal server not running, start the server to log data and visualize in the dashboard!\u001b[0m\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"Question: What did the president say about Ketanji Brown Jackson\n",
|
||||||
|
"\n",
|
||||||
|
"Context Conciseness Score: 0.0\n",
|
||||||
|
"Context Reranking Score: 1.0\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[32m2024-04-17 17:04:56.947\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate_on_server\u001b[0m:\u001b[36m378\u001b[0m - \u001b[1mSending evaluation request for rows 0 to <50 to the Uptrain\u001b[0m\n",
|
||||||
|
"\u001b[32m2024-04-17 17:05:16.551\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate\u001b[0m:\u001b[36m367\u001b[0m - \u001b[1mLocal server not running, start the server to log data and visualize in the dashboard!\u001b[0m\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"Question: What did the president say about Ketanji Brown Jackson\n",
|
||||||
|
"Response: The President mentioned that he nominated Circuit Court of Appeals Judge Ketanji Brown Jackson to serve on the United States Supreme Court 4 days ago. He described her as one of the nation's top legal minds who will continue Justice Breyer’s legacy of excellence.\n",
|
||||||
|
"\n",
|
||||||
|
"Context Relevance Score: 1.0\n",
|
||||||
|
"Factual Accuracy Score: 1.0\n",
|
||||||
|
"Response Completeness Score: 0.5\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"# Create the retriever\n",
|
||||||
|
"compressor = FlashrankRerank()\n",
|
||||||
|
"compression_retriever = ContextualCompressionRetriever(\n",
|
||||||
|
" base_compressor=compressor, base_retriever=retriever\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"# Create the chain\n",
|
||||||
|
"chain = RetrievalQA.from_chain_type(llm=llm, retriever=compression_retriever)\n",
|
||||||
|
"\n",
|
||||||
|
"# Create the uptrain callback\n",
|
||||||
|
"uptrain_callback = UpTrainCallbackHandler(key_type=\"openai\", api_key=OPENAI_API_KEY)\n",
|
||||||
|
"config = {\"callbacks\": [uptrain_callback]}\n",
|
||||||
|
"\n",
|
||||||
|
"# Invoke the chain with a query\n",
|
||||||
|
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
|
||||||
|
"result = chain.invoke(query, config=config)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": ".venv",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.11.7"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 2
|
||||||
|
}
|
||||||
@@ -72,7 +72,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"output = chat([HumanMessage(content=\"write a funny joke\")])\n",
|
"output = chat.invoke([HumanMessage(content=\"write a funny joke\")])\n",
|
||||||
"print(\"output:\", output)"
|
"print(\"output:\", output)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -90,7 +90,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"kwargs = {\"temperature\": 0.8, \"top_p\": 0.8, \"top_k\": 5}\n",
|
"kwargs = {\"temperature\": 0.8, \"top_p\": 0.8, \"top_k\": 5}\n",
|
||||||
"output = chat([HumanMessage(content=\"write a funny joke\")], **kwargs)\n",
|
"output = chat.invoke([HumanMessage(content=\"write a funny joke\")], **kwargs)\n",
|
||||||
"print(\"output:\", output)"
|
"print(\"output:\", output)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -19,59 +19,85 @@
|
|||||||
"\n",
|
"\n",
|
||||||
">[Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/ai-services/openai/overview) provides REST API access to OpenAI's powerful language models including the GPT-4, GPT-3.5-Turbo, and Embeddings model series. These models can be easily adapted to your specific task including but not limited to content generation, summarization, semantic search, and natural language to code translation. Users can access the service through REST APIs, Python SDK, or a web-based interface in the Azure OpenAI Studio.\n",
|
">[Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/ai-services/openai/overview) provides REST API access to OpenAI's powerful language models including the GPT-4, GPT-3.5-Turbo, and Embeddings model series. These models can be easily adapted to your specific task including but not limited to content generation, summarization, semantic search, and natural language to code translation. Users can access the service through REST APIs, Python SDK, or a web-based interface in the Azure OpenAI Studio.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"This notebook goes over how to connect to an Azure-hosted OpenAI endpoint. We recommend having version `openai>=1` installed."
|
"This notebook goes over how to connect to an Azure-hosted OpenAI endpoint. First, we need to install the `langchain-openai` package."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "raw",
|
||||||
|
"id": "d83ba7de",
|
||||||
|
"metadata": {
|
||||||
|
"vscode": {
|
||||||
|
"languageId": "raw"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"%pip install -qU langchain-openai"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "e39133c8",
|
||||||
|
"metadata": {
|
||||||
|
"vscode": {
|
||||||
|
"languageId": "raw"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Next, let's set some environment variables to help us connect to the Azure OpenAI service. You can find these values in the Azure portal."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": null,
|
||||||
"id": "96164b42",
|
"id": "1d8d73bd",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import os\n",
|
"import os\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from langchain_core.messages import HumanMessage\n",
|
"os.environ[\"AZURE_OPENAI_API_KEY\"] = \"...\"\n",
|
||||||
"from langchain_openai import AzureChatOpenAI"
|
"os.environ[\"AZURE_OPENAI_ENDPOINT\"] = \"https://<your-endpoint>.openai.azure.com/\"\n",
|
||||||
|
"os.environ[\"AZURE_OPENAI_API_VERSION\"] = \"2023-06-01-preview\"\n",
|
||||||
|
"os.environ[\"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME\"] = \"chat\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "e7b160f8",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Next, let's construct our model and chat with it:"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 3,
|
||||||
"id": "cbe4bb58-ba13-4355-8af9-cd990dc47a64",
|
"id": "cbe4bb58-ba13-4355-8af9-cd990dc47a64",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"os.environ[\"AZURE_OPENAI_API_KEY\"] = \"...\"\n",
|
"from langchain_core.messages import HumanMessage\n",
|
||||||
"os.environ[\"AZURE_OPENAI_ENDPOINT\"] = \"https://<your-endpoint>.openai.azure.com/\""
|
"from langchain_openai import AzureChatOpenAI\n",
|
||||||
]
|
"\n",
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 14,
|
|
||||||
"id": "8161278f",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"model = AzureChatOpenAI(\n",
|
"model = AzureChatOpenAI(\n",
|
||||||
" openai_api_version=\"2023-05-15\",\n",
|
" openai_api_version=os.environ[\"AZURE_OPENAI_API_VERSION\"],\n",
|
||||||
" azure_deployment=\"your-deployment-name\",\n",
|
" azure_deployment=os.environ[\"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME\"],\n",
|
||||||
")"
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 15,
|
"execution_count": 4,
|
||||||
"id": "99509140",
|
"id": "99509140",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"AIMessage(content=\"J'adore la programmation.\")"
|
"AIMessage(content=\"J'adore programmer.\", response_metadata={'token_usage': {'completion_tokens': 6, 'prompt_tokens': 19, 'total_tokens': 25}, 'model_name': 'gpt-35-turbo', 'system_fingerprint': None, 'prompt_filter_results': [{'prompt_index': 0, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}], 'finish_reason': 'stop', 'logprobs': None, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}, id='run-25ed88db-38f2-4b0c-a943-a03f217711a9-0')"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 15,
|
"execution_count": 4,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@@ -80,7 +106,7 @@
|
|||||||
"message = HumanMessage(\n",
|
"message = HumanMessage(\n",
|
||||||
" content=\"Translate this sentence from English to French. I love programming.\"\n",
|
" content=\"Translate this sentence from English to French. I love programming.\"\n",
|
||||||
")\n",
|
")\n",
|
||||||
"model([message])"
|
"model.invoke([message])"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -96,7 +122,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 8,
|
"execution_count": 5,
|
||||||
"id": "0531798a",
|
"id": "0531798a",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -106,19 +132,29 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 7,
|
||||||
"id": "aceddb72",
|
"id": "aceddb72",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"scrolled": true
|
"scrolled": true
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Total Cost (USD): $0.000041\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"model = AzureChatOpenAI(\n",
|
"model = AzureChatOpenAI(\n",
|
||||||
" openai_api_version=\"2023-05-15\",\n",
|
" openai_api_version=os.environ[\"AZURE_OPENAI_API_VERSION\"],\n",
|
||||||
" azure_deployment=\"gpt-35-turbo\", # in Azure, this deployment has version 0613 - input and output tokens are counted separately\n",
|
" azure_deployment=os.environ[\n",
|
||||||
|
" \"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME\"\n",
|
||||||
|
" ], # in Azure, this deployment has version 0613 - input and output tokens are counted separately\n",
|
||||||
")\n",
|
")\n",
|
||||||
"with get_openai_callback() as cb:\n",
|
"with get_openai_callback() as cb:\n",
|
||||||
" model([message])\n",
|
" model.invoke([message])\n",
|
||||||
" print(\n",
|
" print(\n",
|
||||||
" f\"Total Cost (USD): ${format(cb.total_cost, '.6f')}\"\n",
|
" f\"Total Cost (USD): ${format(cb.total_cost, '.6f')}\"\n",
|
||||||
" ) # without specifying the model version, flat-rate 0.002 USD per 1k input and output tokens is used"
|
" ) # without specifying the model version, flat-rate 0.002 USD per 1k input and output tokens is used"
|
||||||
@@ -134,7 +170,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 17,
|
"execution_count": 11,
|
||||||
"id": "8d5e54e9",
|
"id": "8d5e54e9",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
@@ -147,13 +183,13 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"model0613 = AzureChatOpenAI(\n",
|
"model0301 = AzureChatOpenAI(\n",
|
||||||
" openai_api_version=\"2023-05-15\",\n",
|
" openai_api_version=os.environ[\"AZURE_OPENAI_API_VERSION\"],\n",
|
||||||
" deployment_name=\"gpt-35-turbo\",\n",
|
" azure_deployment=os.environ[\"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME\"],\n",
|
||||||
" model_version=\"0613\",\n",
|
" model_version=\"0301\",\n",
|
||||||
")\n",
|
")\n",
|
||||||
"with get_openai_callback() as cb:\n",
|
"with get_openai_callback() as cb:\n",
|
||||||
" model0613([message])\n",
|
" model0301.invoke([message])\n",
|
||||||
" print(f\"Total Cost (USD): ${format(cb.total_cost, '.6f')}\")"
|
" print(f\"Total Cost (USD): ${format(cb.total_cost, '.6f')}\")"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -174,7 +210,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.10.12"
|
"version": "3.11.4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -3,10 +3,14 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "raw",
|
"cell_type": "raw",
|
||||||
"id": "fbc66410",
|
"id": "fbc66410",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"vscode": {
|
||||||
|
"languageId": "raw"
|
||||||
|
}
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"---\n",
|
"---\n",
|
||||||
"sidebar_label: Bedrock Chat\n",
|
"sidebar_label: Bedrock\n",
|
||||||
"---"
|
"---"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -15,7 +19,7 @@
|
|||||||
"id": "bf733a38-db84-4363-89e2-de6735c37230",
|
"id": "bf733a38-db84-4363-89e2-de6735c37230",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"# BedrockChat\n",
|
"# ChatBedrock\n",
|
||||||
"\n",
|
"\n",
|
||||||
">[Amazon Bedrock](https://aws.amazon.com/bedrock/) is a fully managed service that offers a choice of \n",
|
">[Amazon Bedrock](https://aws.amazon.com/bedrock/) is a fully managed service that offers a choice of \n",
|
||||||
"> high-performing foundation models (FMs) from leading AI companies like `AI21 Labs`, `Anthropic`, `Cohere`, \n",
|
"> high-performing foundation models (FMs) from leading AI companies like `AI21 Labs`, `Anthropic`, `Cohere`, \n",
|
||||||
@@ -30,42 +34,53 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 2,
|
||||||
"id": "d51edc81",
|
"id": "d51edc81",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Note: you may need to restart the kernel to use updated packages.\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%pip install --upgrade --quiet boto3"
|
"%pip install --upgrade --quiet langchain-aws"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 1,
|
||||||
"id": "d4a7c55d-b235-4ca4-a579-c90cc9570da9",
|
"id": "d4a7c55d-b235-4ca4-a579-c90cc9570da9",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain_community.chat_models import BedrockChat\n",
|
"from langchain_aws import ChatBedrock\n",
|
||||||
"from langchain_core.messages import HumanMessage"
|
"from langchain_core.messages import HumanMessage"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 11,
|
||||||
"id": "70cf04e8-423a-4ff6-8b09-f11fb711c817",
|
"id": "70cf04e8-423a-4ff6-8b09-f11fb711c817",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"chat = BedrockChat(model_id=\"anthropic.claude-v2\", model_kwargs={\"temperature\": 0.1})"
|
"chat = ChatBedrock(\n",
|
||||||
|
" model_id=\"anthropic.claude-3-sonnet-20240229-v1:0\",\n",
|
||||||
|
" model_kwargs={\"temperature\": 0.1},\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 12,
|
||||||
"id": "8199ef8f-eb8b-4253-9ea0-6c24a013ca4c",
|
"id": "8199ef8f-eb8b-4253-9ea0-6c24a013ca4c",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
@@ -74,10 +89,10 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"AIMessage(content=\" Voici la traduction en français : J'adore programmer.\", additional_kwargs={}, example=False)"
|
"AIMessage(content=\"Voici la traduction en français :\\n\\nJ'aime la programmation.\", additional_kwargs={'usage': {'prompt_tokens': 20, 'completion_tokens': 21, 'total_tokens': 41}}, response_metadata={'model_id': 'anthropic.claude-3-sonnet-20240229-v1:0', 'usage': {'prompt_tokens': 20, 'completion_tokens': 21, 'total_tokens': 41}}, id='run-994f0362-0e50-4524-afad-3c4f5bb11328-0')"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 3,
|
"execution_count": 12,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@@ -88,7 +103,7 @@
|
|||||||
" content=\"Translate this sentence from English to French. I love programming.\"\n",
|
" content=\"Translate this sentence from English to French. I love programming.\"\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
"]\n",
|
"]\n",
|
||||||
"chat(messages)"
|
"chat.invoke(messages)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -97,39 +112,30 @@
|
|||||||
"id": "a4a4f4d4",
|
"id": "a4a4f4d4",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### For BedrockChat with Streaming"
|
"### Streaming\n",
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "c253883f",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"chat = BedrockChat(\n",
|
"To stream responses, you can use the runnable `.stream()` method."
|
||||||
" model_id=\"anthropic.claude-v2\",\n",
|
|
||||||
" streaming=True,\n",
|
|
||||||
" callbacks=[StreamingStdOutCallbackHandler()],\n",
|
|
||||||
" model_kwargs={\"temperature\": 0.1},\n",
|
|
||||||
")"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 14,
|
||||||
"id": "d9e52838",
|
"id": "d9e52838",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Voici la traduction en français :\n",
|
||||||
|
"\n",
|
||||||
|
"J'aime la programmation."
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"messages = [\n",
|
"for chunk in chat.stream(messages):\n",
|
||||||
" HumanMessage(\n",
|
" print(chunk.content, end=\"\", flush=True)"
|
||||||
" content=\"Translate this sentence from English to French. I love programming.\"\n",
|
|
||||||
" )\n",
|
|
||||||
"]\n",
|
|
||||||
"chat(messages)"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -149,7 +155,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.10.12"
|
"version": "3.11.4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
181
docs/docs/integrations/chat/coze.ipynb
Normal file
181
docs/docs/integrations/chat/coze.ipynb
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "raw",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"---\n",
|
||||||
|
"sidebar_label: Coze Chat\n",
|
||||||
|
"---"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Chat with Coze Bot\n",
|
||||||
|
"\n",
|
||||||
|
"ChatCoze chat models API by coze.com. For more information, see [https://www.coze.com/open/docs/chat](https://www.coze.com/open/docs/chat)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2024-04-25T15:14:24.186131Z",
|
||||||
|
"start_time": "2024-04-25T15:14:23.831767Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain_community.chat_models import ChatCoze\n",
|
||||||
|
"from langchain_core.messages import HumanMessage"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2024-04-25T15:14:24.191123Z",
|
||||||
|
"start_time": "2024-04-25T15:14:24.186330Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"chat = ChatCoze(\n",
|
||||||
|
" coze_api_base=\"YOUR_API_BASE\",\n",
|
||||||
|
" coze_api_key=\"YOUR_API_KEY\",\n",
|
||||||
|
" bot_id=\"YOUR_BOT_ID\",\n",
|
||||||
|
" user=\"YOUR_USER_ID\",\n",
|
||||||
|
" conversation_id=\"YOUR_CONVERSATION_ID\",\n",
|
||||||
|
" streaming=False,\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Alternatively, you can set your API key and API base with:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"\n",
|
||||||
|
"os.environ[\"COZE_API_KEY\"] = \"YOUR_API_KEY\"\n",
|
||||||
|
"os.environ[\"COZE_API_BASE\"] = \"YOUR_API_BASE\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2024-04-25T15:14:25.853218Z",
|
||||||
|
"start_time": "2024-04-25T15:14:24.192408Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"AIMessage(content='为你找到关于coze的信息如下:\n\nCoze是一个由字节跳动推出的AI聊天机器人和应用程序编辑开发平台。\n\n用户无论是否有编程经验,都可以通过该平台快速创建各种类型的聊天机器人、智能体、AI应用和插件,并将其部署在社交平台和即时聊天应用程序中。\n\n国际版使用的模型比国内版更强大。')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"chat([HumanMessage(content=\"什么是扣子(coze)\")])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Chat with Coze Streaming"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2024-04-25T15:14:25.870044Z",
|
||||||
|
"start_time": "2024-04-25T15:14:25.863381Z"
|
||||||
|
},
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"chat = ChatCoze(\n",
|
||||||
|
" coze_api_base=\"YOUR_API_BASE\",\n",
|
||||||
|
" coze_api_key=\"YOUR_API_KEY\",\n",
|
||||||
|
" bot_id=\"YOUR_BOT_ID\",\n",
|
||||||
|
" user=\"YOUR_USER_ID\",\n",
|
||||||
|
" conversation_id=\"YOUR_CONVERSATION_ID\",\n",
|
||||||
|
" streaming=True,\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2024-04-25T15:14:27.153546Z",
|
||||||
|
"start_time": "2024-04-25T15:14:25.868470Z"
|
||||||
|
},
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"AIMessageChunk(content='为你查询到Coze是一个由字节跳动推出的AI聊天机器人和应用程序编辑开发平台。')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"chat([HumanMessage(content=\"什么是扣子(coze)\")])"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.11.4"
|
||||||
|
},
|
||||||
|
"orig_nbformat": 4
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 2
|
||||||
|
}
|
||||||
@@ -24,19 +24,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
" ········\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"# get a new token: https://deepinfra.com/login?from=%2Fdash\n",
|
"# get a new token: https://deepinfra.com/login?from=%2Fdash\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -47,7 +39,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 7,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
@@ -61,7 +53,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 3,
|
||||||
"id": "d4a7c55d-b235-4ca4-a579-c90cc9570da9",
|
"id": "d4a7c55d-b235-4ca4-a579-c90cc9570da9",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
@@ -74,7 +66,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 4,
|
||||||
"id": "70cf04e8-423a-4ff6-8b09-f11fb711c817",
|
"id": "70cf04e8-423a-4ff6-8b09-f11fb711c817",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
@@ -109,7 +101,7 @@
|
|||||||
" content=\"Translate this sentence from English to French. I love programming.\"\n",
|
" content=\"Translate this sentence from English to French. I love programming.\"\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
"]\n",
|
"]\n",
|
||||||
"chat(messages)"
|
"chat.invoke(messages)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -123,7 +115,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 5,
|
||||||
"id": "93a21c5c-6ef9-4688-be60-b2e1f94842fb",
|
"id": "93a21c5c-6ef9-4688-be60-b2e1f94842fb",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
@@ -188,16 +180,8 @@
|
|||||||
" verbose=True,\n",
|
" verbose=True,\n",
|
||||||
" callbacks=[StreamingStdOutCallbackHandler()],\n",
|
" callbacks=[StreamingStdOutCallbackHandler()],\n",
|
||||||
")\n",
|
")\n",
|
||||||
"chat(messages)"
|
"chat.invoke(messages)"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "c253883f",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": []
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|||||||
@@ -51,7 +51,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 1,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -114,7 +114,7 @@
|
|||||||
"human = \"Translate this sentence from English to French. I love programming.\"\n",
|
"human = \"Translate this sentence from English to French. I love programming.\"\n",
|
||||||
"prompt = ChatPromptTemplate.from_messages([(\"system\", system), (\"human\", human)])\n",
|
"prompt = ChatPromptTemplate.from_messages([(\"system\", system), (\"human\", human)])\n",
|
||||||
"\n",
|
"\n",
|
||||||
"chat = ChatVertexAI(model_name=\"gemini-pro\", convert_system_message_to_human=True)\n",
|
"chat = ChatVertexAI(model=\"gemini-pro\", convert_system_message_to_human=True)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"chain = prompt | chat\n",
|
"chain = prompt | chat\n",
|
||||||
"chain.invoke({})"
|
"chain.invoke({})"
|
||||||
@@ -233,9 +233,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"chat = ChatVertexAI(\n",
|
"chat = ChatVertexAI(model=\"codechat-bison\", max_tokens=1000, temperature=0.5)\n",
|
||||||
" model_name=\"codechat-bison\", max_output_tokens=1000, temperature=0.5\n",
|
|
||||||
")\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"message = chat.invoke(\"Write a Python function generating all prime numbers\")\n",
|
"message = chat.invoke(\"Write a Python function generating all prime numbers\")\n",
|
||||||
"print(message.content)"
|
"print(message.content)"
|
||||||
@@ -261,31 +259,46 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from pprint import pprint\n",
|
||||||
|
"\n",
|
||||||
|
"from langchain_core.messages import HumanMessage\n",
|
||||||
|
"from langchain_google_vertexai import HarmBlockThreshold, HarmCategory"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"{'is_blocked': False,\n",
|
"{'citation_metadata': None,\n",
|
||||||
" 'safety_ratings': [{'category': 'HARM_CATEGORY_HARASSMENT',\n",
|
" 'is_blocked': False,\n",
|
||||||
|
" 'safety_ratings': [{'blocked': False,\n",
|
||||||
|
" 'category': 'HARM_CATEGORY_HATE_SPEECH',\n",
|
||||||
" 'probability_label': 'NEGLIGIBLE'},\n",
|
" 'probability_label': 'NEGLIGIBLE'},\n",
|
||||||
" {'category': 'HARM_CATEGORY_HATE_SPEECH',\n",
|
" {'blocked': False,\n",
|
||||||
|
" 'category': 'HARM_CATEGORY_DANGEROUS_CONTENT',\n",
|
||||||
" 'probability_label': 'NEGLIGIBLE'},\n",
|
" 'probability_label': 'NEGLIGIBLE'},\n",
|
||||||
" {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT',\n",
|
" {'blocked': False,\n",
|
||||||
|
" 'category': 'HARM_CATEGORY_HARASSMENT',\n",
|
||||||
" 'probability_label': 'NEGLIGIBLE'},\n",
|
" 'probability_label': 'NEGLIGIBLE'},\n",
|
||||||
" {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT',\n",
|
" {'blocked': False,\n",
|
||||||
" 'probability_label': 'NEGLIGIBLE'}]}\n"
|
" 'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT',\n",
|
||||||
|
" 'probability_label': 'NEGLIGIBLE'}],\n",
|
||||||
|
" 'usage_metadata': {'candidates_token_count': 6,\n",
|
||||||
|
" 'prompt_token_count': 12,\n",
|
||||||
|
" 'total_token_count': 18}}\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"from pprint import pprint\n",
|
|
||||||
"\n",
|
|
||||||
"from langchain_core.messages import HumanMessage\n",
|
|
||||||
"from langchain_google_vertexai import ChatVertexAI, HarmBlockThreshold, HarmCategory\n",
|
|
||||||
"\n",
|
|
||||||
"human = \"Translate this sentence from English to French. I love programming.\"\n",
|
"human = \"Translate this sentence from English to French. I love programming.\"\n",
|
||||||
"messages = [HumanMessage(content=human)]\n",
|
"messages = [HumanMessage(content=human)]\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -315,18 +328,21 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 6,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"{'is_blocked': False,\n",
|
"{'errors': (),\n",
|
||||||
" 'safety_attributes': {'Derogatory': 0.1,\n",
|
" 'grounding_metadata': {'citations': [], 'search_queries': []},\n",
|
||||||
" 'Finance': 0.3,\n",
|
" 'is_blocked': False,\n",
|
||||||
" 'Insult': 0.1,\n",
|
" 'safety_attributes': [{'Derogatory': 0.1, 'Insult': 0.1, 'Sexual': 0.2}],\n",
|
||||||
" 'Sexual': 0.1}}\n"
|
" 'usage_metadata': {'candidates_billable_characters': 88.0,\n",
|
||||||
|
" 'candidates_token_count': 24.0,\n",
|
||||||
|
" 'prompt_billable_characters': 58.0,\n",
|
||||||
|
" 'prompt_token_count': 12.0}}\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -341,40 +357,149 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Function Calling with Gemini\n",
|
"## Tool calling (a.k.a. function calling) with Gemini\n",
|
||||||
"\n",
|
"\n",
|
||||||
"We can call Gemini models with tools."
|
"We can pass tool definitions to Gemini models to get the model to invoke those tools when appropriate. This is useful not only for LLM-powered tool use but also for getting structured outputs out of models more generally.\n",
|
||||||
|
"\n",
|
||||||
|
"With `ChatVertexAI.bind_tools()`, we can easily pass in Pydantic classes, dict schemas, LangChain tools, or even functions as tools to the model. Under the hood these are converted to a Gemini tool schema, which looks like:\n",
|
||||||
|
"```python\n",
|
||||||
|
"{\n",
|
||||||
|
" \"name\": \"...\", # tool name\n",
|
||||||
|
" \"description\": \"...\", # tool description\n",
|
||||||
|
" \"parameters\": {...} # tool input schema as JSONSchema\n",
|
||||||
|
"}\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"AIMessage(content='', additional_kwargs={'function_call': {'name': 'GetWeather', 'arguments': '{\"location\": \"San Francisco, CA\"}'}}, response_metadata={'is_blocked': False, 'safety_ratings': [{'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}], 'citation_metadata': None, 'usage_metadata': {'prompt_token_count': 41, 'candidates_token_count': 7, 'total_token_count': 48}}, id='run-05e760dc-0682-4286-88e1-5b23df69b083-0', tool_calls=[{'name': 'GetWeather', 'args': {'location': 'San Francisco, CA'}, 'id': 'cd2499c4-4513-4059-bfff-5321b6e922d0'}])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"from langchain.pydantic_v1 import BaseModel, Field\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class GetWeather(BaseModel):\n",
|
||||||
|
" \"\"\"Get the current weather in a given location\"\"\"\n",
|
||||||
|
"\n",
|
||||||
|
" location: str = Field(..., description=\"The city and state, e.g. San Francisco, CA\")\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"llm = ChatVertexAI(model=\"gemini-pro\", temperature=0)\n",
|
||||||
|
"llm_with_tools = llm.bind_tools([GetWeather])\n",
|
||||||
|
"ai_msg = llm_with_tools.invoke(\n",
|
||||||
|
" \"what is the weather like in San Francisco\",\n",
|
||||||
|
")\n",
|
||||||
|
"ai_msg"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"The tool calls can be access via the `AIMessage.tool_calls` attribute, where they are extracted in a model-agnostic format:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"[{'name': 'GetWeather',\n",
|
||||||
|
" 'args': {'location': 'San Francisco, CA'},\n",
|
||||||
|
" 'id': 'cd2499c4-4513-4059-bfff-5321b6e922d0'}]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"ai_msg.tool_calls"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"For a complete guide on tool calling [head here](/docs/modules/model_io/chat/function_calling/)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Structured outputs\n",
|
||||||
|
"\n",
|
||||||
|
"Many applications require structured model outputs. Tool calling makes it much easier to do this reliably. The [with_structured_outputs](https://api.python.langchain.com/en/latest/chat_models/langchain_google_vertexai.chat_models.ChatVertexAI.html) constructor provides a simple interface built on top of tool calling for getting structured outputs out of a model. For a complete guide on structured outputs [head here](/docs/modules/model_io/chat/structured_output/).\n",
|
||||||
|
"\n",
|
||||||
|
"### ChatVertexAI.with_structured_outputs()\n",
|
||||||
|
"\n",
|
||||||
|
"To get structured outputs from our Gemini model all we need to do is to specify a desired schema, either as a Pydantic class or as a JSON schema, "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"Person(name='Stefan', age=13)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"class Person(BaseModel):\n",
|
||||||
|
" \"\"\"Save information about a person.\"\"\"\n",
|
||||||
|
"\n",
|
||||||
|
" name: str = Field(..., description=\"The person's name.\")\n",
|
||||||
|
" age: int = Field(..., description=\"The person's age.\")\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"structured_llm = llm.with_structured_output(Person)\n",
|
||||||
|
"structured_llm.invoke(\"Stefan is already 13 years old\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### [Legacy] Using `create_structured_runnable()`\n",
|
||||||
|
"\n",
|
||||||
|
"The legacy wasy to get structured outputs is using the `create_structured_runnable` constructor:"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"MyModel(name='Erick', age=27)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.pydantic_v1 import BaseModel\n",
|
|
||||||
"from langchain_google_vertexai import create_structured_runnable\n",
|
"from langchain_google_vertexai import create_structured_runnable\n",
|
||||||
"\n",
|
"\n",
|
||||||
"llm = ChatVertexAI(model_name=\"gemini-pro\")\n",
|
"chain = create_structured_runnable(Person, llm)\n",
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"class MyModel(BaseModel):\n",
|
|
||||||
" name: str\n",
|
|
||||||
" age: int\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"chain = create_structured_runnable(MyModel, llm)\n",
|
|
||||||
"chain.invoke(\"My name is Erick and I'm 27 years old\")"
|
"chain.invoke(\"My name is Erick and I'm 27 years old\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -424,7 +549,7 @@
|
|||||||
"human = \"{text}\"\n",
|
"human = \"{text}\"\n",
|
||||||
"prompt = ChatPromptTemplate.from_messages([(\"system\", system), (\"human\", human)])\n",
|
"prompt = ChatPromptTemplate.from_messages([(\"system\", system), (\"human\", human)])\n",
|
||||||
"\n",
|
"\n",
|
||||||
"chat = ChatVertexAI(model_name=\"chat-bison\", max_output_tokens=1000, temperature=0.5)\n",
|
"chat = ChatVertexAI(model=\"chat-bison\", max_tokens=1000, temperature=0.5)\n",
|
||||||
"chain = prompt | chat\n",
|
"chain = prompt | chat\n",
|
||||||
"\n",
|
"\n",
|
||||||
"asyncio.run(\n",
|
"asyncio.run(\n",
|
||||||
@@ -484,11 +609,21 @@
|
|||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "",
|
"display_name": "poetry-venv-2",
|
||||||
"name": ""
|
"language": "python",
|
||||||
|
"name": "poetry-venv-2"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"name": "python"
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.9.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -19,13 +19,13 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.prompts.chat import (\n",
|
"from langchain_community.chat_models import JinaChat\n",
|
||||||
|
"from langchain_core.messages import HumanMessage, SystemMessage\n",
|
||||||
|
"from langchain_core.prompts.chat import (\n",
|
||||||
" ChatPromptTemplate,\n",
|
" ChatPromptTemplate,\n",
|
||||||
" HumanMessagePromptTemplate,\n",
|
" HumanMessagePromptTemplate,\n",
|
||||||
" SystemMessagePromptTemplate,\n",
|
" SystemMessagePromptTemplate,\n",
|
||||||
")\n",
|
")"
|
||||||
"from langchain_community.chat_models import JinaChat\n",
|
|
||||||
"from langchain_core.messages import HumanMessage, SystemMessage"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -49,12 +49,12 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.prompts.chat import (\n",
|
"from langchain_core.messages import SystemMessage\n",
|
||||||
|
"from langchain_core.prompts.chat import (\n",
|
||||||
" ChatPromptTemplate,\n",
|
" ChatPromptTemplate,\n",
|
||||||
" HumanMessagePromptTemplate,\n",
|
" HumanMessagePromptTemplate,\n",
|
||||||
" MessagesPlaceholder,\n",
|
" MessagesPlaceholder,\n",
|
||||||
")\n",
|
")\n",
|
||||||
"from langchain_core.messages import SystemMessage\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"template_messages = [\n",
|
"template_messages = [\n",
|
||||||
" SystemMessage(content=\"You are a helpful assistant.\"),\n",
|
" SystemMessage(content=\"You are a helpful assistant.\"),\n",
|
||||||
|
|||||||
@@ -62,7 +62,7 @@
|
|||||||
"messages = [system_message, user_message]\n",
|
"messages = [system_message, user_message]\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# chat with wasm-chat service\n",
|
"# chat with wasm-chat service\n",
|
||||||
"response = chat(messages)\n",
|
"response = chat.invoke(messages)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"print(f\"[Bot] {response.content}\")"
|
"print(f\"[Bot] {response.content}\")"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"!pip install langchain langchain-core langchain-community"
|
"!pip install langchain langchain-core langchain-community httpx"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -60,9 +60,9 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.prompts.chat import ChatPromptTemplate\n",
|
|
||||||
"from langchain_community.chat_models import ChatMaritalk\n",
|
"from langchain_community.chat_models import ChatMaritalk\n",
|
||||||
"from langchain_core.output_parsers import StrOutputParser\n",
|
"from langchain_core.output_parsers import StrOutputParser\n",
|
||||||
|
"from langchain_core.prompts.chat import ChatPromptTemplate\n",
|
||||||
"\n",
|
"\n",
|
||||||
"llm = ChatMaritalk(\n",
|
"llm = ChatMaritalk(\n",
|
||||||
" model=\"sabia-2-medium\", # Available models: sabia-2-small and sabia-2-medium\n",
|
" model=\"sabia-2-medium\", # Available models: sabia-2-small and sabia-2-medium\n",
|
||||||
@@ -89,6 +89,58 @@
|
|||||||
"print(response) # should answer something like \"1. Max\\n2. Bella\\n3. Charlie\\n4. Rocky\""
|
"print(response) # should answer something like \"1. Max\\n2. Bella\\n3. Charlie\\n4. Rocky\""
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"attachments": {},
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Stream Generation\n",
|
||||||
|
"\n",
|
||||||
|
"For tasks involving the generation of long text, such as creating an extensive article or translating a large document, it can be advantageous to receive the response in parts, as the text is generated, instead of waiting for the complete text. This makes the application more responsive and efficient, especially when the generated text is extensive. We offer two approaches to meet this need: one synchronous and another asynchronous.\n",
|
||||||
|
"\n",
|
||||||
|
"#### Synchronous:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain_core.messages import HumanMessage\n",
|
||||||
|
"\n",
|
||||||
|
"messages = [HumanMessage(content=\"Suggest 3 names for my dog\")]\n",
|
||||||
|
"\n",
|
||||||
|
"for chunk in llm.stream(messages):\n",
|
||||||
|
" print(chunk.content, end=\"\", flush=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"attachments": {},
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"#### Asynchronous:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain_core.messages import HumanMessage\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"async def async_invoke_chain(animal: str):\n",
|
||||||
|
" messages = [HumanMessage(content=f\"Suggest 3 names for my {animal}\")]\n",
|
||||||
|
" async for chunk in llm._astream(messages):\n",
|
||||||
|
" print(chunk.message.content, end=\"\", flush=True)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"await async_invoke_chain(\"dog\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
@@ -184,7 +236,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"query = \"Qual o tempo máximo para realização da prova?\"\n",
|
"query = \"Qual o tempo máximo para realização da prova?\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"docs = retriever.get_relevant_documents(query)\n",
|
"docs = retriever.invoke(query)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"chain.invoke(\n",
|
"chain.invoke(\n",
|
||||||
" {\"input_documents\": docs, \"query\": query}\n",
|
" {\"input_documents\": docs, \"query\": query}\n",
|
||||||
|
|||||||
217
docs/docs/integrations/chat/mlx.ipynb
Normal file
217
docs/docs/integrations/chat/mlx.ipynb
Normal file
@@ -0,0 +1,217 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# MLX\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook shows how to get started using `MLX` LLM's as chat models.\n",
|
||||||
|
"\n",
|
||||||
|
"In particular, we will:\n",
|
||||||
|
"1. Utilize the [MLXPipeline](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/llms/mlx_pipelines.py), \n",
|
||||||
|
"2. Utilize the `ChatMLX` class to enable any of these LLMs to interface with LangChain's [Chat Messages](https://python.langchain.com/docs/modules/model_io/chat/#messages) abstraction.\n",
|
||||||
|
"3. Demonstrate how to use an open-source LLM to power an `ChatAgent` pipeline\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%pip install --upgrade --quiet mlx-lm transformers huggingface_hub"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 1. Instantiate an LLM\n",
|
||||||
|
"\n",
|
||||||
|
"There are three LLM options to choose from."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain_community.llms.mlx_pipeline import MLXPipeline\n",
|
||||||
|
"\n",
|
||||||
|
"llm = MLXPipeline.from_model_id(\n",
|
||||||
|
" \"mlx-community/quantized-gemma-2b-it\",\n",
|
||||||
|
" pipeline_kwargs={\"max_tokens\": 10, \"temp\": 0.1},\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 2. Instantiate the `ChatMLX` to apply chat templates"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Instantiate the chat model and some messages to pass."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain.schema import (\n",
|
||||||
|
" HumanMessage,\n",
|
||||||
|
")\n",
|
||||||
|
"from langchain_community.chat_models.mlx import ChatMLX\n",
|
||||||
|
"\n",
|
||||||
|
"messages = [\n",
|
||||||
|
" HumanMessage(\n",
|
||||||
|
" content=\"What happens when an unstoppable force meets an immovable object?\"\n",
|
||||||
|
" ),\n",
|
||||||
|
"]\n",
|
||||||
|
"\n",
|
||||||
|
"chat_model = ChatMLX(llm=llm)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Inspect how the chat messages are formatted for the LLM call."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"chat_model._to_chat_prompt(messages)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Call the model."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"res = chat_model.invoke(messages)\n",
|
||||||
|
"print(res.content)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## 3. Take it for a spin as an agent!\n",
|
||||||
|
"\n",
|
||||||
|
"Here we'll test out `gemma-2b-it` as a zero-shot `ReAct` Agent. The example below is taken from [here](https://python.langchain.com/docs/modules/agents/agent_types/react#using-chat-models).\n",
|
||||||
|
"\n",
|
||||||
|
"> Note: To run this section, you'll need to have a [SerpAPI Token](https://serpapi.com/) saved as an environment variable: `SERPAPI_API_KEY`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain import hub\n",
|
||||||
|
"from langchain.agents import AgentExecutor, load_tools\n",
|
||||||
|
"from langchain.agents.format_scratchpad import format_log_to_str\n",
|
||||||
|
"from langchain.agents.output_parsers import (\n",
|
||||||
|
" ReActJsonSingleInputOutputParser,\n",
|
||||||
|
")\n",
|
||||||
|
"from langchain.tools.render import render_text_description\n",
|
||||||
|
"from langchain_community.utilities import SerpAPIWrapper"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Configure the agent with a `react-json` style prompt and access to a search engine and calculator."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# setup tools\n",
|
||||||
|
"tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)\n",
|
||||||
|
"\n",
|
||||||
|
"# setup ReAct style prompt\n",
|
||||||
|
"prompt = hub.pull(\"hwchase17/react-json\")\n",
|
||||||
|
"prompt = prompt.partial(\n",
|
||||||
|
" tools=render_text_description(tools),\n",
|
||||||
|
" tool_names=\", \".join([t.name for t in tools]),\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"# define the agent\n",
|
||||||
|
"chat_model_with_stop = chat_model.bind(stop=[\"\\nObservation\"])\n",
|
||||||
|
"agent = (\n",
|
||||||
|
" {\n",
|
||||||
|
" \"input\": lambda x: x[\"input\"],\n",
|
||||||
|
" \"agent_scratchpad\": lambda x: format_log_to_str(x[\"intermediate_steps\"]),\n",
|
||||||
|
" }\n",
|
||||||
|
" | prompt\n",
|
||||||
|
" | chat_model_with_stop\n",
|
||||||
|
" | ReActJsonSingleInputOutputParser()\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"# instantiate AgentExecutor\n",
|
||||||
|
"agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"agent_executor.invoke(\n",
|
||||||
|
" {\n",
|
||||||
|
" \"input\": \"Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?\"\n",
|
||||||
|
" }\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.9.18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
||||||
@@ -12,7 +12,7 @@
|
|||||||
"The `ChatNVIDIA` class is a LangChain chat model that connects to [NVIDIA AI Foundation Endpoints](https://www.nvidia.com/en-us/ai-data-science/foundation-models/).\n",
|
"The `ChatNVIDIA` class is a LangChain chat model that connects to [NVIDIA AI Foundation Endpoints](https://www.nvidia.com/en-us/ai-data-science/foundation-models/).\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"> [NVIDIA AI Foundation Endpoints](https://www.nvidia.com/en-us/ai-data-science/foundation-models/) give users easy access to NVIDIA hosted API endpoints for NVIDIA AI Foundation Models like Mixtral 8x7B, Llama 2, Stable Diffusion, etc. These models, hosted on the [NVIDIA NGC catalog](https://catalog.ngc.nvidia.com/ai-foundation-models), are optimized, tested, and hosted on the NVIDIA AI platform, making them fast and easy to evaluate, further customize, and seamlessly run at peak performance on any accelerated stack.\n",
|
"> [NVIDIA AI Foundation Endpoints](https://www.nvidia.com/en-us/ai-data-science/foundation-models/) give users easy access to NVIDIA hosted API endpoints for NVIDIA AI Foundation Models like Mixtral 8x7B, Llama 2, Stable Diffusion, etc. These models, hosted on the [NVIDIA API catalog](https://build.nvidia.com/), are optimized, tested, and hosted on the NVIDIA AI platform, making them fast and easy to evaluate, further customize, and seamlessly run at peak performance on any accelerated stack.\n",
|
||||||
"> \n",
|
"> \n",
|
||||||
"> With [NVIDIA AI Foundation Endpoints](https://www.nvidia.com/en-us/ai-data-science/foundation-models/), you can get quick results from a fully accelerated stack running on [NVIDIA DGX Cloud](https://www.nvidia.com/en-us/data-center/dgx-cloud/). Once customized, these models can be deployed anywhere with enterprise-grade security, stability, and support using [NVIDIA AI Enterprise](https://www.nvidia.com/en-us/data-center/products/ai-enterprise/).\n",
|
"> With [NVIDIA AI Foundation Endpoints](https://www.nvidia.com/en-us/ai-data-science/foundation-models/), you can get quick results from a fully accelerated stack running on [NVIDIA DGX Cloud](https://www.nvidia.com/en-us/data-center/dgx-cloud/). Once customized, these models can be deployed anywhere with enterprise-grade security, stability, and support using [NVIDIA AI Enterprise](https://www.nvidia.com/en-us/data-center/products/ai-enterprise/).\n",
|
||||||
"> \n",
|
"> \n",
|
||||||
@@ -58,13 +58,13 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"**To get started:**\n",
|
"**To get started:**\n",
|
||||||
"\n",
|
"\n",
|
||||||
"1. Create a free account with the [NVIDIA NGC](https://catalog.ngc.nvidia.com/) service, which hosts AI solution catalogs, containers, models, etc.\n",
|
"1. Create a free account with [NVIDIA](https://build.nvidia.com/), which hosts NVIDIA AI Foundation models\n",
|
||||||
"\n",
|
"\n",
|
||||||
"2. Navigate to `Catalog > AI Foundation Models > (Model with API endpoint)`.\n",
|
"2. Click on your model of choice\n",
|
||||||
"\n",
|
"\n",
|
||||||
"3. Select the `API` option and click `Generate Key`.\n",
|
"3. Under `Input` select the `Python` tab, and click `Get API Key`. Then click `Generate Key`.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"4. Save the generated key as `NVIDIA_API_KEY`. From there, you should have access to the endpoints."
|
"4. Copy and save the generated key as `NVIDIA_API_KEY`. From there, you should have access to the endpoints."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -311,7 +311,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"Some model types support unique prompting techniques and chat messages. We will review a few important ones below.\n",
|
"Some model types support unique prompting techniques and chat messages. We will review a few important ones below.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"**To find out more about a specific model, please navigate to the API section of an AI Foundation model [as linked here](https://catalog.ngc.nvidia.com/orgs/nvidia/teams/ai-foundation/models/codellama-13b/api).**"
|
"**To find out more about a specific model, please navigate to the API section of an AI Foundation model [as linked here](https://build.nvidia.com/).**"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
112
docs/docs/integrations/chat/octoai.ipynb
Normal file
112
docs/docs/integrations/chat/octoai.ipynb
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# ChatOctoAI\n",
|
||||||
|
"\n",
|
||||||
|
"[OctoAI](https://docs.octoai.cloud/docs) offers easy access to efficient compute and enables users to integrate their choice of AI models into applications. The `OctoAI` compute service helps you run, tune, and scale AI applications easily.\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook demonstrates the use of `langchain.chat_models.ChatOctoAI` for [OctoAI endpoints](https://octoai.cloud/text).\n",
|
||||||
|
"\n",
|
||||||
|
"## Setup\n",
|
||||||
|
"\n",
|
||||||
|
"To run our example app, there are two simple steps to take:\n",
|
||||||
|
"\n",
|
||||||
|
"1. Get an API Token from [your OctoAI account page](https://octoai.cloud/settings).\n",
|
||||||
|
" \n",
|
||||||
|
"2. Paste your API token in in the code cell below or use the `octoai_api_token` keyword argument.\n",
|
||||||
|
"\n",
|
||||||
|
"Note: If you want to use a different model than the [available models](https://octoai.cloud/text?selectedTags=Chat), you can containerize the model and make a custom OctoAI endpoint yourself, by following [Build a Container from Python](https://octo.ai/docs/bring-your-own-model/advanced-build-a-container-from-scratch-in-python) and [Create a Custom Endpoint from a Container](https://octo.ai/docs/bring-your-own-model/create-custom-endpoints-from-a-container/create-custom-endpoints-from-a-container) and then updating your `OCTOAI_API_BASE` environment variable.\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"\n",
|
||||||
|
"os.environ[\"OCTOAI_API_TOKEN\"] = \"OCTOAI_API_TOKEN\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain_community.chat_models import ChatOctoAI\n",
|
||||||
|
"from langchain_core.messages import HumanMessage, SystemMessage"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Example"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"chat = ChatOctoAI(max_tokens=300, model_name=\"mixtral-8x7b-instruct\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"messages = [\n",
|
||||||
|
" SystemMessage(content=\"You are a helpful assistant.\"),\n",
|
||||||
|
" HumanMessage(content=\"Tell me about Leonardo da Vinci briefly.\"),\n",
|
||||||
|
"]\n",
|
||||||
|
"print(chat(messages).content)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Leonardo da Vinci (1452-1519) was an Italian polymath who is often considered one of the greatest painters in history. However, his genius extended far beyond art. He was also a scientist, inventor, mathematician, engineer, anatomist, geologist, and cartographer.\n",
|
||||||
|
"\n",
|
||||||
|
"Da Vinci is best known for his paintings such as the Mona Lisa, The Last Supper, and The Virgin of the Rocks. His scientific studies were ahead of his time, and his notebooks contain detailed drawings and descriptions of various machines, human anatomy, and natural phenomena.\n",
|
||||||
|
"\n",
|
||||||
|
"Despite never receiving a formal education, da Vinci's insatiable curiosity and observational skills made him a pioneer in many fields. His work continues to inspire and influence artists, scientists, and thinkers today."
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.11.7"
|
||||||
|
},
|
||||||
|
"vscode": {
|
||||||
|
"interpreter": {
|
||||||
|
"hash": "97697b63fdcee0a640856f91cb41326ad601964008c341809e43189d1cab1047"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
||||||
@@ -30,7 +30,7 @@
|
|||||||
"* [Download](https://ollama.ai/download) and install Ollama onto the available supported platforms (including Windows Subsystem for Linux)\n",
|
"* [Download](https://ollama.ai/download) and install Ollama onto the available supported platforms (including Windows Subsystem for Linux)\n",
|
||||||
"* Fetch available LLM model via `ollama pull <name-of-model>`\n",
|
"* Fetch available LLM model via `ollama pull <name-of-model>`\n",
|
||||||
" * View a list of available models via the [model library](https://ollama.ai/library)\n",
|
" * View a list of available models via the [model library](https://ollama.ai/library)\n",
|
||||||
" * e.g., for `Llama-7b`: `ollama pull llama2`\n",
|
" * e.g., `ollama pull llama3`\n",
|
||||||
"* This will download the default tagged version of the model. Typically, the default points to the latest, smallest sized-parameter model.\n",
|
"* This will download the default tagged version of the model. Typically, the default points to the latest, smallest sized-parameter model.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"> On Mac, the models will be download to `~/.ollama/models`\n",
|
"> On Mac, the models will be download to `~/.ollama/models`\n",
|
||||||
@@ -46,7 +46,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"You can see a full list of supported parameters on the [API reference page](https://api.python.langchain.com/en/latest/llms/langchain.llms.ollama.Ollama.html).\n",
|
"You can see a full list of supported parameters on the [API reference page](https://api.python.langchain.com/en/latest/llms/langchain.llms.ollama.Ollama.html).\n",
|
||||||
"\n",
|
"\n",
|
||||||
"If you are using a LLaMA `chat` model (e.g., `ollama pull llama2:7b-chat`) then you can use the `ChatOllama` interface.\n",
|
"If you are using a LLaMA `chat` model (e.g., `ollama pull llama3`) then you can use the `ChatOllama` interface.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"This includes [special tokens](https://huggingface.co/blog/llama2#how-to-prompt-llama-2) for system message and user input.\n",
|
"This includes [special tokens](https://huggingface.co/blog/llama2#how-to-prompt-llama-2) for system message and user input.\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -65,7 +65,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"```bash\n",
|
"```bash\n",
|
||||||
"curl http://localhost:11434/api/generate -d '{\n",
|
"curl http://localhost:11434/api/generate -d '{\n",
|
||||||
" \"model\": \"llama2\",\n",
|
" \"model\": \"llama3\",\n",
|
||||||
" \"prompt\":\"Why is the sky blue?\"\n",
|
" \"prompt\":\"Why is the sky blue?\"\n",
|
||||||
"}'\n",
|
"}'\n",
|
||||||
"```\n",
|
"```\n",
|
||||||
@@ -86,11 +86,9 @@
|
|||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
" Sure, here's a fun space-themed joke for you:\n",
|
"Why did the astronaut break up with his girlfriend?\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Why don't astronauts like broccoli? \n",
|
"Because he needed space!\n"
|
||||||
"Because it has too many \"crisps\" in it!\n",
|
|
||||||
"\n"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -102,7 +100,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"# supports many more optional parameters. Hover on your `ChatOllama(...)`\n",
|
"# supports many more optional parameters. Hover on your `ChatOllama(...)`\n",
|
||||||
"# class to view the latest available supported parameters\n",
|
"# class to view the latest available supported parameters\n",
|
||||||
"llm = ChatOllama(model=\"llama2\")\n",
|
"llm = ChatOllama(model=\"llama3\")\n",
|
||||||
"prompt = ChatPromptTemplate.from_template(\"Tell me a short joke about {topic}\")\n",
|
"prompt = ChatPromptTemplate.from_template(\"Tell me a short joke about {topic}\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# using LangChain Expressive Language chain syntax\n",
|
"# using LangChain Expressive Language chain syntax\n",
|
||||||
@@ -125,21 +123,14 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 12,
|
"execution_count": 6,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
" Sure\n",
|
"Why\n",
|
||||||
",\n",
|
|
||||||
" here\n",
|
|
||||||
"'s\n",
|
|
||||||
" a\n",
|
|
||||||
" joke\n",
|
|
||||||
":\n",
|
|
||||||
" Why\n",
|
|
||||||
" did\n",
|
" did\n",
|
||||||
" the\n",
|
" the\n",
|
||||||
" astronaut\n",
|
" astronaut\n",
|
||||||
@@ -148,17 +139,18 @@
|
|||||||
" with\n",
|
" with\n",
|
||||||
" his\n",
|
" his\n",
|
||||||
" girlfriend\n",
|
" girlfriend\n",
|
||||||
|
" before\n",
|
||||||
|
" going\n",
|
||||||
|
" to\n",
|
||||||
|
" Mars\n",
|
||||||
"?\n",
|
"?\n",
|
||||||
" Because\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"Because\n",
|
||||||
" he\n",
|
" he\n",
|
||||||
" needed\n",
|
" needed\n",
|
||||||
" more\n",
|
|
||||||
" space\n",
|
" space\n",
|
||||||
" to\n",
|
"!\n",
|
||||||
" explore\n",
|
|
||||||
".\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"\n"
|
"\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -179,51 +171,9 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 13,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
" Sure\n",
|
|
||||||
",\n",
|
|
||||||
" here\n",
|
|
||||||
"'s\n",
|
|
||||||
" a\n",
|
|
||||||
" little\n",
|
|
||||||
" one\n",
|
|
||||||
":\n",
|
|
||||||
" Why\n",
|
|
||||||
" did\n",
|
|
||||||
" the\n",
|
|
||||||
" rocket\n",
|
|
||||||
" scientist\n",
|
|
||||||
" break\n",
|
|
||||||
" up\n",
|
|
||||||
" with\n",
|
|
||||||
" her\n",
|
|
||||||
" partner\n",
|
|
||||||
"?\n",
|
|
||||||
" Because\n",
|
|
||||||
" he\n",
|
|
||||||
" couldn\n",
|
|
||||||
"'t\n",
|
|
||||||
" handle\n",
|
|
||||||
" all\n",
|
|
||||||
" her\n",
|
|
||||||
" \"\n",
|
|
||||||
"space\n",
|
|
||||||
"y\n",
|
|
||||||
"\"\n",
|
|
||||||
" jokes\n",
|
|
||||||
".\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"topic = {\"topic\": \"Space travel\"}\n",
|
"topic = {\"topic\": \"Space travel\"}\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -255,13 +205,13 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": 4,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain_community.chat_models import ChatOllama\n",
|
"from langchain_community.chat_models import ChatOllama\n",
|
||||||
"\n",
|
"\n",
|
||||||
"llm = ChatOllama(model=\"llama2\", format=\"json\", temperature=0)"
|
"llm = ChatOllama(model=\"llama3\", format=\"json\", temperature=0)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -273,7 +223,7 @@
|
|||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"content='{\\n\"morning\": {\\n\"color\": \"light blue\"\\n},\\n\"noon\": {\\n\"color\": \"blue\"\\n},\\n\"afternoon\": {\\n\"color\": \"grayish-blue\"\\n},\\n\"evening\": {\\n\"color\": \"pinkish-orange\"\\n}\\n}'\n"
|
"content='{ \"morning\": \"blue\", \"noon\": \"clear blue\", \"afternoon\": \"hazy yellow\", \"evening\": \"orange-red\" }\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n ' id='run-e893700f-e2d0-4df8-ad86-17525dcee318-0'\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -292,7 +242,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 53,
|
"execution_count": 8,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
@@ -300,13 +250,9 @@
|
|||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"\n",
|
"\n",
|
||||||
"{\n",
|
"Name: John\n",
|
||||||
"\"name\": \"John\",\n",
|
"Age: 35\n",
|
||||||
"\"age\": 35,\n",
|
"Likes: Pizza\n"
|
||||||
"\"interests\": [\n",
|
|
||||||
"\"pizza\"\n",
|
|
||||||
"]\n",
|
|
||||||
"}\n"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -516,7 +462,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.9.1"
|
"version": "3.11.8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook shows how to use an experimental wrapper around Ollama that gives it the same API as OpenAI Functions.\n",
|
"This notebook shows how to use an experimental wrapper around Ollama that gives it the same API as OpenAI Functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Note that more powerful and capable models will perform better with complex schema and/or multiple functions. The examples below use Mistral.\n",
|
"Note that more powerful and capable models will perform better with complex schema and/or multiple functions. The examples below use llama3 and phi3 models.\n",
|
||||||
"For a complete list of supported models and model variants, see the [Ollama model library](https://ollama.ai/library).\n",
|
"For a complete list of supported models and model variants, see the [Ollama model library](https://ollama.ai/library).\n",
|
||||||
"\n",
|
"\n",
|
||||||
"## Setup\n",
|
"## Setup\n",
|
||||||
@@ -32,12 +32,18 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2024-04-28T00:53:25.276543Z",
|
||||||
|
"start_time": "2024-04-28T00:53:24.881202Z"
|
||||||
|
},
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain_experimental.llms.ollama_functions import OllamaFunctions\n",
|
"from langchain_experimental.llms.ollama_functions import OllamaFunctions\n",
|
||||||
"\n",
|
"\n",
|
||||||
"model = OllamaFunctions(model=\"mistral\")"
|
"model = OllamaFunctions(model=\"llama3\", format=\"json\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -50,11 +56,16 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2024-04-26T04:59:17.270931Z",
|
||||||
|
"start_time": "2024-04-26T04:59:17.263347Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"model = model.bind(\n",
|
"model = model.bind_tools(\n",
|
||||||
" functions=[\n",
|
" tools=[\n",
|
||||||
" {\n",
|
" {\n",
|
||||||
" \"name\": \"get_current_weather\",\n",
|
" \"name\": \"get_current_weather\",\n",
|
||||||
" \"description\": \"Get the current weather in a given location\",\n",
|
" \"description\": \"Get the current weather in a given location\",\n",
|
||||||
@@ -88,12 +99,17 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2024-04-26T04:59:26.092428Z",
|
||||||
|
"start_time": "2024-04-26T04:59:17.272627Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_current_weather', 'arguments': '{\"location\": \"Boston, MA\", \"unit\": \"celsius\"}'}})"
|
"AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_current_weather', 'arguments': '{\"location\": \"Boston, MA\"}'}}, id='run-1791f9fe-95ad-4ca4-bdf7-9f73eab31e6f-0')"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
@@ -111,54 +127,119 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Using for extraction\n",
|
"## Structured Output\n",
|
||||||
"\n",
|
"\n",
|
||||||
"One useful thing you can do with function calling here is extracting properties from a given input in a structured format:"
|
"One useful thing you can do with function calling using `with_structured_output()` function is extracting properties from a given input in a structured format:"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2024-04-26T04:59:26.098828Z",
|
||||||
|
"start_time": "2024-04-26T04:59:26.094021Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain_core.prompts import PromptTemplate\n",
|
||||||
|
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"# Schema for structured response\n",
|
||||||
|
"class Person(BaseModel):\n",
|
||||||
|
" name: str = Field(description=\"The person's name\", required=True)\n",
|
||||||
|
" height: float = Field(description=\"The person's height\", required=True)\n",
|
||||||
|
" hair_color: str = Field(description=\"The person's hair color\")\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"# Prompt template\n",
|
||||||
|
"prompt = PromptTemplate.from_template(\n",
|
||||||
|
" \"\"\"Alex is 5 feet tall. \n",
|
||||||
|
"Claudia is 1 feet taller than Alex and jumps higher than him. \n",
|
||||||
|
"Claudia is a brunette and Alex is blonde.\n",
|
||||||
|
"\n",
|
||||||
|
"Human: {question}\n",
|
||||||
|
"AI: \"\"\"\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"# Chain\n",
|
||||||
|
"llm = OllamaFunctions(model=\"phi3\", format=\"json\", temperature=0)\n",
|
||||||
|
"structured_llm = llm.with_structured_output(Person)\n",
|
||||||
|
"chain = prompt | structured_llm"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Extracting data about Alex"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2024-04-26T04:59:30.164955Z",
|
||||||
|
"start_time": "2024-04-26T04:59:26.099790Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"[{'name': 'Alex', 'height': 5, 'hair_color': 'blonde'},\n",
|
"Person(name='Alex', height=5.0, hair_color='blonde')"
|
||||||
" {'name': 'Claudia', 'height': 6, 'hair_color': 'brunette'}]"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 4,
|
"execution_count": 5,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.chains import create_extraction_chain\n",
|
"alex = chain.invoke(\"Describe Alex\")\n",
|
||||||
"\n",
|
"alex"
|
||||||
"# Schema\n",
|
]
|
||||||
"schema = {\n",
|
},
|
||||||
" \"properties\": {\n",
|
{
|
||||||
" \"name\": {\"type\": \"string\"},\n",
|
"cell_type": "markdown",
|
||||||
" \"height\": {\"type\": \"integer\"},\n",
|
"metadata": {},
|
||||||
" \"hair_color\": {\"type\": \"string\"},\n",
|
"source": [
|
||||||
" },\n",
|
"### Extracting data about Claudia"
|
||||||
" \"required\": [\"name\", \"height\"],\n",
|
]
|
||||||
"}\n",
|
},
|
||||||
"\n",
|
{
|
||||||
"# Input\n",
|
"cell_type": "code",
|
||||||
"input = \"\"\"Alex is 5 feet tall. Claudia is 1 feet taller than Alex and jumps higher than him. Claudia is a brunette and Alex is blonde.\"\"\"\n",
|
"execution_count": 6,
|
||||||
"\n",
|
"metadata": {
|
||||||
"# Run chain\n",
|
"ExecuteTime": {
|
||||||
"llm = OllamaFunctions(model=\"mistral\", temperature=0)\n",
|
"end_time": "2024-04-26T04:59:31.509846Z",
|
||||||
"chain = create_extraction_chain(schema, llm)\n",
|
"start_time": "2024-04-26T04:59:30.165662Z"
|
||||||
"chain.run(input)"
|
}
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"Person(name='Claudia', height=6.0, hair_color='brunette')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"claudia = chain.invoke(\"Describe Claudia\")\n",
|
||||||
|
"claudia"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": ".venv",
|
"display_name": "Python 3 (ipykernel)",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
@@ -172,9 +253,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.10.5"
|
"version": "3.9.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 2
|
"nbformat_minor": 4
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 3,
|
||||||
"id": "522686de",
|
"id": "522686de",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
@@ -30,24 +30,20 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain_core.messages import HumanMessage, SystemMessage\n",
|
"from langchain_core.messages import HumanMessage, SystemMessage\n",
|
||||||
"from langchain_core.prompts.chat import (\n",
|
"from langchain_core.prompts import ChatPromptTemplate\n",
|
||||||
" ChatPromptTemplate,\n",
|
|
||||||
" HumanMessagePromptTemplate,\n",
|
|
||||||
" SystemMessagePromptTemplate,\n",
|
|
||||||
")\n",
|
|
||||||
"from langchain_openai import ChatOpenAI"
|
"from langchain_openai import ChatOpenAI"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 4,
|
||||||
"id": "62e0dbc3",
|
"id": "62e0dbc3",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"chat = ChatOpenAI(temperature=0)"
|
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -58,14 +54,14 @@
|
|||||||
"The above cell assumes that your OpenAI API key is set in your environment variables. If you would rather manually specify your API key and/or organization ID, use the following code:\n",
|
"The above cell assumes that your OpenAI API key is set in your environment variables. If you would rather manually specify your API key and/or organization ID, use the following code:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"```python\n",
|
"```python\n",
|
||||||
"chat = ChatOpenAI(temperature=0, openai_api_key=\"YOUR_API_KEY\", openai_organization=\"YOUR_ORGANIZATION_ID\")\n",
|
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0, api_key=\"YOUR_API_KEY\", openai_organization=\"YOUR_ORGANIZATION_ID\")\n",
|
||||||
"```\n",
|
"```\n",
|
||||||
"Remove the openai_organization parameter should it not apply to you."
|
"Remove the openai_organization parameter should it not apply to you."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 5,
|
||||||
"id": "ce16ad78-8e6f-48cd-954e-98be75eb5836",
|
"id": "ce16ad78-8e6f-48cd-954e-98be75eb5836",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
@@ -74,63 +70,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"AIMessage(content=\"J'adore la programmation.\", additional_kwargs={}, example=False)"
|
"AIMessage(content=\"J'adore programmer.\", response_metadata={'token_usage': {'completion_tokens': 6, 'prompt_tokens': 34, 'total_tokens': 40}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': 'fp_b28b39ffa8', 'finish_reason': 'stop', 'logprobs': None}, id='run-8591eae1-b42b-402b-a23a-dfdb0cd151bd-0')"
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 3,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
|
||||||
"messages = [\n",
|
|
||||||
" SystemMessage(\n",
|
|
||||||
" content=\"You are a helpful assistant that translates English to French.\"\n",
|
|
||||||
" ),\n",
|
|
||||||
" HumanMessage(\n",
|
|
||||||
" content=\"Translate this sentence from English to French. I love programming.\"\n",
|
|
||||||
" ),\n",
|
|
||||||
"]\n",
|
|
||||||
"chat.invoke(messages)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"id": "778f912a-66ea-4a5d-b3de-6c7db4baba26",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"You can make use of templating by using a `MessagePromptTemplate`. You can build a `ChatPromptTemplate` from one or more `MessagePromptTemplates`. You can use `ChatPromptTemplate`'s `format_prompt` -- this returns a `PromptValue`, which you can convert to a string or Message object, depending on whether you want to use the formatted value as input to an llm or chat model.\n",
|
|
||||||
"\n",
|
|
||||||
"For convenience, there is a `from_template` method exposed on the template. If you were to use this template, this is what it would look like:"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 4,
|
|
||||||
"id": "180c5cc8",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"template = (\n",
|
|
||||||
" \"You are a helpful assistant that translates {input_language} to {output_language}.\"\n",
|
|
||||||
")\n",
|
|
||||||
"system_message_prompt = SystemMessagePromptTemplate.from_template(template)\n",
|
|
||||||
"human_template = \"{text}\"\n",
|
|
||||||
"human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 5,
|
|
||||||
"id": "fbb043e6",
|
|
||||||
"metadata": {
|
|
||||||
"tags": []
|
|
||||||
},
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"AIMessage(content=\"J'adore la programmation.\", additional_kwargs={}, example=False)"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 5,
|
"execution_count": 5,
|
||||||
@@ -139,18 +79,168 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"chat_prompt = ChatPromptTemplate.from_messages(\n",
|
"messages = [\n",
|
||||||
" [system_message_prompt, human_message_prompt]\n",
|
" (\"system\", \"You are a helpful assistant that translates English to French.\"),\n",
|
||||||
|
" (\"human\", \"Translate this sentence from English to French. I love programming.\"),\n",
|
||||||
|
"]\n",
|
||||||
|
"llm.invoke(messages)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "778f912a-66ea-4a5d-b3de-6c7db4baba26",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Chaining\n",
|
||||||
|
"\n",
|
||||||
|
"We can chain our model with a prompt template like so:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"id": "fbb043e6",
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"AIMessage(content='Ich liebe Programmieren.', response_metadata={'token_usage': {'completion_tokens': 5, 'prompt_tokens': 26, 'total_tokens': 31}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': 'fp_b28b39ffa8', 'finish_reason': 'stop', 'logprobs': None}, id='run-94fa6741-c99b-4513-afce-c3f562631c79-0')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"prompt = ChatPromptTemplate.from_messages(\n",
|
||||||
|
" [\n",
|
||||||
|
" (\n",
|
||||||
|
" \"system\",\n",
|
||||||
|
" \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n",
|
||||||
|
" ),\n",
|
||||||
|
" (\"human\", \"{input}\"),\n",
|
||||||
|
" ]\n",
|
||||||
")\n",
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# get a chat completion from the formatted messages\n",
|
"chain = prompt | llm\n",
|
||||||
"chat.invoke(\n",
|
"chain.invoke(\n",
|
||||||
" chat_prompt.format_prompt(\n",
|
" {\n",
|
||||||
" input_language=\"English\", output_language=\"French\", text=\"I love programming.\"\n",
|
" \"input_language\": \"English\",\n",
|
||||||
" ).to_messages()\n",
|
" \"output_language\": \"German\",\n",
|
||||||
|
" \"input\": \"I love programming.\",\n",
|
||||||
|
" }\n",
|
||||||
")"
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "0b1b52a5-b58d-40c9-bcdd-88eb8fb351e2",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Tool calling\n",
|
||||||
|
"\n",
|
||||||
|
"OpenAI has a [tool calling](https://platform.openai.com/docs/guides/function-calling) (we use \"tool calling\" and \"function calling\" interchangeably here) API that lets you describe tools and their arguments, and have the model return a JSON object with a tool to invoke and the inputs to that tool. tool-calling is extremely useful for building tool-using chains and agents, and for getting structured outputs from models more generally.\n",
|
||||||
|
"\n",
|
||||||
|
"### ChatOpenAI.bind_tools()\n",
|
||||||
|
"\n",
|
||||||
|
"With `ChatAnthropic.bind_tools`, we can easily pass in Pydantic classes, dict schemas, LangChain tools, or even functions as tools to the model. Under the hood these are converted to an Anthropic tool schemas, which looks like:\n",
|
||||||
|
"```\n",
|
||||||
|
"{\n",
|
||||||
|
" \"name\": \"...\",\n",
|
||||||
|
" \"description\": \"...\",\n",
|
||||||
|
" \"parameters\": {...} # JSONSchema\n",
|
||||||
|
"}\n",
|
||||||
|
"```\n",
|
||||||
|
"and passed in every model invocation."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"id": "b7ea7690-ec7a-4337-b392-e87d1f39a6ec",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class GetWeather(BaseModel):\n",
|
||||||
|
" \"\"\"Get the current weather in a given location\"\"\"\n",
|
||||||
|
"\n",
|
||||||
|
" location: str = Field(..., description=\"The city and state, e.g. San Francisco, CA\")\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"llm_with_tools = llm.bind_tools([GetWeather])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"id": "1d1ab955-6a68-42f8-bb5d-86eb1111478a",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_H7fABDuzEau48T10Qn0Lsh0D', 'function': {'arguments': '{\"location\":\"San Francisco\"}', 'name': 'GetWeather'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 15, 'prompt_tokens': 70, 'total_tokens': 85}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': 'fp_b28b39ffa8', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-b469135e-2718-446a-8164-eef37e672ba2-0', tool_calls=[{'name': 'GetWeather', 'args': {'location': 'San Francisco'}, 'id': 'call_H7fABDuzEau48T10Qn0Lsh0D'}])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"ai_msg = llm_with_tools.invoke(\n",
|
||||||
|
" \"what is the weather like in San Francisco\",\n",
|
||||||
|
")\n",
|
||||||
|
"ai_msg"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "768d1ae4-4b1a-48eb-a329-c8d5051067a3",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### AIMessage.tool_calls\n",
|
||||||
|
"Notice that the AIMessage has a `tool_calls` attribute. This contains in a standardized ToolCall format that is model-provider agnostic."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"id": "166cb7ce-831d-4a7c-9721-abc107f11084",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"[{'name': 'GetWeather',\n",
|
||||||
|
" 'args': {'location': 'San Francisco'},\n",
|
||||||
|
" 'id': 'call_H7fABDuzEau48T10Qn0Lsh0D'}]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 11,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"ai_msg.tool_calls"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "e082c9ac-c7c7-4aff-a8ec-8e220262a59c",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"For more on binding tools and tool call outputs, head to the [tool calling](/docs/modules/model_io/chat/function_calling/) docs."
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "57e27714",
|
"id": "57e27714",
|
||||||
@@ -205,7 +295,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.10.5"
|
"version": "3.9.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -1,80 +0,0 @@
|
|||||||
{
|
|
||||||
"cells": [
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 5,
|
|
||||||
"id": "a9667088-04e1-4f67-8221-a0072a2d635f",
|
|
||||||
"metadata": {
|
|
||||||
"execution": {
|
|
||||||
"iopub.execute_input": "2024-03-06T17:04:59.273702Z",
|
|
||||||
"iopub.status.busy": "2024-03-06T17:04:59.272602Z",
|
|
||||||
"iopub.status.idle": "2024-03-06T17:05:00.129177Z",
|
|
||||||
"shell.execute_reply": "2024-03-06T17:05:00.124594Z",
|
|
||||||
"shell.execute_reply.started": "2024-03-06T17:04:59.273646Z"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"AIMessage(content='저는 대형 언어 모델 프로젝트를 구축하고 싶습니다.')"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 5,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
|
||||||
"import os\n",
|
|
||||||
"\n",
|
|
||||||
"os.environ[\"SOLAR_API_KEY\"] = \"SOLAR_API_KEY\"\n",
|
|
||||||
"\n",
|
|
||||||
"from langchain_community.chat_models.solar import SolarChat\n",
|
|
||||||
"from langchain_core.messages import HumanMessage, SystemMessage\n",
|
|
||||||
"\n",
|
|
||||||
"chat = SolarChat(max_tokens=1024)\n",
|
|
||||||
"\n",
|
|
||||||
"messages = [\n",
|
|
||||||
" SystemMessage(\n",
|
|
||||||
" content=\"You are a helpful assistant who translates English to Korean.\"\n",
|
|
||||||
" ),\n",
|
|
||||||
" HumanMessage(\n",
|
|
||||||
" content=\"Translate this sentence from English to Korean. I want to build a project of large language model.\"\n",
|
|
||||||
" ),\n",
|
|
||||||
"]\n",
|
|
||||||
"\n",
|
|
||||||
"chat.invoke(messages)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "8cb792fe-2844-4969-a9e9-f4c0f97b1699",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": []
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"metadata": {
|
|
||||||
"kernelspec": {
|
|
||||||
"display_name": "Python 3 (ipykernel)",
|
|
||||||
"language": "python",
|
|
||||||
"name": "python3"
|
|
||||||
},
|
|
||||||
"language_info": {
|
|
||||||
"codemirror_mode": {
|
|
||||||
"name": "ipython",
|
|
||||||
"version": 3
|
|
||||||
},
|
|
||||||
"file_extension": ".py",
|
|
||||||
"mimetype": "text/x-python",
|
|
||||||
"name": "python",
|
|
||||||
"nbconvert_exporter": "python",
|
|
||||||
"pygments_lexer": "ipython3",
|
|
||||||
"version": "3.9.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"nbformat": 4,
|
|
||||||
"nbformat_minor": 5
|
|
||||||
}
|
|
||||||
@@ -142,11 +142,70 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "markdown",
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"source": [
|
||||||
"source": []
|
"## Tool Calling\n",
|
||||||
|
"ChatTongyi supports tool calling API that lets you describe tools and their arguments, and have the model return a JSON object with a tool to invoke and the inputs to that tool."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'name': 'get_current_weather', 'arguments': '{\"location\": \"San Francisco\"}'}, 'id': '', 'type': 'function'}]}, response_metadata={'model_name': 'qwen-turbo', 'finish_reason': 'tool_calls', 'request_id': 'dae79197-8780-9b7e-8c15-6a83e2a53534', 'token_usage': {'input_tokens': 229, 'output_tokens': 19, 'total_tokens': 248}}, id='run-9e06f837-582b-473b-bb1f-5e99a68ecc10-0', tool_calls=[{'name': 'get_current_weather', 'args': {'location': 'San Francisco'}, 'id': ''}])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"from langchain_community.chat_models.tongyi import ChatTongyi\n",
|
||||||
|
"from langchain_core.messages import HumanMessage, SystemMessage\n",
|
||||||
|
"\n",
|
||||||
|
"tools = [\n",
|
||||||
|
" {\n",
|
||||||
|
" \"type\": \"function\",\n",
|
||||||
|
" \"function\": {\n",
|
||||||
|
" \"name\": \"get_current_time\",\n",
|
||||||
|
" \"description\": \"当你想知道现在的时间时非常有用。\",\n",
|
||||||
|
" \"parameters\": {},\n",
|
||||||
|
" },\n",
|
||||||
|
" },\n",
|
||||||
|
" {\n",
|
||||||
|
" \"type\": \"function\",\n",
|
||||||
|
" \"function\": {\n",
|
||||||
|
" \"name\": \"get_current_weather\",\n",
|
||||||
|
" \"description\": \"当你想查询指定城市的天气时非常有用。\",\n",
|
||||||
|
" \"parameters\": {\n",
|
||||||
|
" \"type\": \"object\",\n",
|
||||||
|
" \"properties\": {\n",
|
||||||
|
" \"location\": {\n",
|
||||||
|
" \"type\": \"string\",\n",
|
||||||
|
" \"description\": \"城市或县区,比如北京市、杭州市、余杭区等。\",\n",
|
||||||
|
" }\n",
|
||||||
|
" },\n",
|
||||||
|
" },\n",
|
||||||
|
" \"required\": [\"location\"],\n",
|
||||||
|
" },\n",
|
||||||
|
" },\n",
|
||||||
|
"]\n",
|
||||||
|
"\n",
|
||||||
|
"messages = [\n",
|
||||||
|
" SystemMessage(content=\"You are a helpful assistant.\"),\n",
|
||||||
|
" HumanMessage(content=\"What is the weather like in San Francisco?\"),\n",
|
||||||
|
"]\n",
|
||||||
|
"chatLLM = ChatTongyi()\n",
|
||||||
|
"llm_kwargs = {\"tools\": tools, \"result_format\": \"message\"}\n",
|
||||||
|
"ai_message = chatLLM.bind(**llm_kwargs).invoke(messages)\n",
|
||||||
|
"ai_message"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|||||||
157
docs/docs/integrations/chat/upstage.ipynb
Normal file
157
docs/docs/integrations/chat/upstage.ipynb
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "raw",
|
||||||
|
"id": "910f5772b6af13c9",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"---\n",
|
||||||
|
"sidebar_label: Upstage\n",
|
||||||
|
"---"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "433f5422ad8e1efa",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# ChatUpstage\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook covers how to get started with Upstage chat models.\n",
|
||||||
|
"\n",
|
||||||
|
"## Installation\n",
|
||||||
|
"\n",
|
||||||
|
"Install `langchain-upstage` package.\n",
|
||||||
|
"\n",
|
||||||
|
"```bash\n",
|
||||||
|
"pip install -U langchain-upstage\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "b3c5c4627fe95eae",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Environment Setup\n",
|
||||||
|
"\n",
|
||||||
|
"Make sure to set the following environment variables:\n",
|
||||||
|
"\n",
|
||||||
|
"- `UPSTAGE_API_KEY`: Your Upstage API key from [Upstage console](https://console.upstage.ai/).\n",
|
||||||
|
"\n",
|
||||||
|
"## Usage"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "20a0067b",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"\n",
|
||||||
|
"os.environ[\"UPSTAGE_API_KEY\"] = \"YOUR_API_KEY\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "8a4d650d76a33494",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false,
|
||||||
|
"is_executing": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain_core.prompts import ChatPromptTemplate\n",
|
||||||
|
"from langchain_upstage import ChatUpstage\n",
|
||||||
|
"\n",
|
||||||
|
"chat = ChatUpstage()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "a1679b5cafaf88b9",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# using chat invoke\n",
|
||||||
|
"chat.invoke(\"Hello, how are you?\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "698a788a63b5c3e5",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# using chat stream\n",
|
||||||
|
"for m in chat.stream(\"Hello, how are you?\"):\n",
|
||||||
|
" print(m)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "36f8a703",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Chaining"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "efa06617e5d4f6b2",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# using chain\n",
|
||||||
|
"prompt = ChatPromptTemplate.from_messages(\n",
|
||||||
|
" [\n",
|
||||||
|
" (\"system\", \"You are a helpful assistant that translates English to French.\"),\n",
|
||||||
|
" (\"human\", \"Translate this sentence from English to French. {english_text}.\"),\n",
|
||||||
|
" ]\n",
|
||||||
|
")\n",
|
||||||
|
"chain = prompt | chat\n",
|
||||||
|
"\n",
|
||||||
|
"chain.invoke({\"english_text\": \"Hello, how are you?\"})"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 2
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython2",
|
||||||
|
"version": "3.9.13"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
||||||
@@ -31,12 +31,12 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.prompts.chat import (\n",
|
"from langchain_core.messages import HumanMessage, SystemMessage\n",
|
||||||
|
"from langchain_core.prompts.chat import (\n",
|
||||||
" ChatPromptTemplate,\n",
|
" ChatPromptTemplate,\n",
|
||||||
" HumanMessagePromptTemplate,\n",
|
" HumanMessagePromptTemplate,\n",
|
||||||
" SystemMessagePromptTemplate,\n",
|
" SystemMessagePromptTemplate,\n",
|
||||||
")\n",
|
")\n",
|
||||||
"from langchain_core.messages import HumanMessage, SystemMessage\n",
|
|
||||||
"from langchain_openai import ChatOpenAI"
|
"from langchain_openai import ChatOpenAI"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -348,7 +348,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"async def ainvoke_with_prompt_template():\n",
|
"async def ainvoke_with_prompt_template():\n",
|
||||||
" from langchain.prompts.chat import (\n",
|
" from langchain_core.prompts.chat import (\n",
|
||||||
" ChatPromptTemplate,\n",
|
" ChatPromptTemplate,\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -17,9 +17,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook shows how to use [ZHIPU AI API](https://open.bigmodel.cn/dev/api) in LangChain with the langchain.chat_models.ChatZhipuAI.\n",
|
"This notebook shows how to use [ZHIPU AI API](https://open.bigmodel.cn/dev/api) in LangChain with the langchain.chat_models.ChatZhipuAI.\n",
|
||||||
"\n",
|
"\n",
|
||||||
">[*ZHIPU AI*](https://open.bigmodel.cn/) is a multi-lingual large language model aligned with human intent, featuring capabilities in Q&A, multi-turn dialogue, and code generation, developed on the foundation of the ChatGLM3. \n",
|
">[*GLM-4*](https://open.bigmodel.cn/) is a multi-lingual large language model aligned with human intent, featuring capabilities in Q&A, multi-turn dialogue, and code generation. The overall performance of the new generation base model GLM-4 has been significantly improved compared to the previous generation, supporting longer contexts; Stronger multimodality; Support faster inference speed, more concurrency, greatly reducing inference costs; Meanwhile, GLM-4 enhances the capabilities of intelligent agents.\n",
|
||||||
"\n",
|
|
||||||
">It's co-developed with Tsinghua University's KEG Laboratory under the ChatGLM3 project, signifying a new era in dialogue pre-training models. The open-source [ChatGLM3](https://github.com/THUDM/ChatGLM3) variant boasts a robust foundation, comprehensive functional support, and widespread availability for both academic and commercial uses. \n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"## Getting started\n",
|
"## Getting started\n",
|
||||||
"### Installation\n",
|
"### Installation\n",
|
||||||
@@ -28,11 +26,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%pip install --quiet httpx[socks]==0.24.1 httpx-sse PyJWT"
|
"#!pip install --upgrade httpx httpx-sse PyJWT"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -45,7 +43,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -63,11 +61,13 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"zhipuai_api_key = \"your_api_key\""
|
"import os\n",
|
||||||
|
"\n",
|
||||||
|
"os.environ[\"ZHIPUAI_API_KEY\"] = \"zhipuai_api_key\""
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -80,12 +80,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"chat = ChatZhipuAI(\n",
|
"chat = ChatZhipuAI(\n",
|
||||||
" api_key=zhipuai_api_key,\n",
|
|
||||||
" model=\"glm-4\",\n",
|
" model=\"glm-4\",\n",
|
||||||
" temperature=0.5,\n",
|
" temperature=0.5,\n",
|
||||||
")"
|
")"
|
||||||
@@ -101,7 +100,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"scrolled": true
|
"scrolled": true
|
||||||
},
|
},
|
||||||
@@ -116,19 +115,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 7,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"\" Formed from bits and bytes,\\nA virtual mind takes flight,\\nConversing, learning fast,\\nEmpathy and wisdom sought.\"\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"response = chat(messages)\n",
|
"response = chat.invoke(messages)\n",
|
||||||
"print(response.content) # Displays the AI-generated poem"
|
"print(response.content) # Displays the AI-generated poem"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -143,7 +134,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 8,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@@ -153,12 +144,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 9,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"streaming_chat = ChatZhipuAI(\n",
|
"streaming_chat = ChatZhipuAI(\n",
|
||||||
" api_key=zhipuai_api_key,\n",
|
|
||||||
" model=\"glm-4\",\n",
|
" model=\"glm-4\",\n",
|
||||||
" temperature=0.5,\n",
|
" temperature=0.5,\n",
|
||||||
" streaming=True,\n",
|
" streaming=True,\n",
|
||||||
@@ -168,30 +158,9 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 10,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
" Formed from data's embrace,\n",
|
|
||||||
"A digital soul to grace,\n",
|
|
||||||
"AI, our trusted guide,\n",
|
|
||||||
"Shaping minds, sides by side."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"AIMessage(content=\" Formed from data's embrace,\\nA digital soul to grace,\\nAI, our trusted guide,\\nShaping minds, sides by side.\")"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 10,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"streaming_chat(messages)"
|
"streaming_chat(messages)"
|
||||||
]
|
]
|
||||||
@@ -206,12 +175,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 11,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"async_chat = ChatZhipuAI(\n",
|
"async_chat = ChatZhipuAI(\n",
|
||||||
" api_key=zhipuai_api_key,\n",
|
|
||||||
" model=\"glm-4\",\n",
|
" model=\"glm-4\",\n",
|
||||||
" temperature=0.5,\n",
|
" temperature=0.5,\n",
|
||||||
")"
|
")"
|
||||||
@@ -219,19 +187,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 12,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"scrolled": true
|
"scrolled": true
|
||||||
},
|
},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"generations=[[ChatGeneration(text=\" Formed from data's embrace,\\nA digital soul to grace,\\nAutomation's tender touch,\\nHarmony of man and machine.\", message=AIMessage(content=\" Formed from data's embrace,\\nA digital soul to grace,\\nAutomation's tender touch,\\nHarmony of man and machine.\"))]] llm_output={} run=[RunInfo(run_id=UUID('25fa687f-3961-4c63-b370-22f7647a4d42'))]\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"response = await async_chat.agenerate([messages])\n",
|
"response = await async_chat.agenerate([messages])\n",
|
||||||
"print(response)"
|
"print(response)"
|
||||||
@@ -239,47 +199,58 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
"source": [
|
||||||
"### Role Play Model\n",
|
"### Using With Functions Call\n",
|
||||||
"Supports character role-playing based on personas, ultra-long multi-turn memory, and personalized dialogues for thousands of unique characters, widely applied in emotional companionship, game intelligent NPCs, virtual avatars for celebrities/stars/movie and TV IPs, digital humans/virtual anchors, text adventure games, and other anthropomorphic dialogue or gaming scenarios."
|
"\n",
|
||||||
]
|
"GLM-4 Model can be used with the function call as well,use the following code to run a simple LangChain json_chat_agent."
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 13,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"meta = {\n",
|
"os.environ[\"TAVILY_API_KEY\"] = \"tavily_api_key\""
|
||||||
" \"user_info\": \"My name is Lu Xingchen, a male, and a renowned director. I am also the collaborative director with Su Mengyuan. I specialize in directing movies with musical themes. Su Mengyuan respects me and regards me as a mentor and good friend.\",\n",
|
],
|
||||||
" \"bot_info\": \"Su Mengyuan, whose real name is Su Yuanxin, is a popular domestic female singer and actress. She rose to fame quickly with her unique voice and exceptional stage presence after participating in a talent show, making her way into the entertainment industry. She is beautiful and charming, but her real allure lies in her talent and diligence. Su Mengyuan is a distinguished graduate of a music academy, skilled in songwriting, and has several popular original songs. Beyond her musical achievements, she is passionate about charity work, actively participating in public welfare activities, and spreading positive energy through her actions. In her work, she is very dedicated and immerses herself fully in her roles during filming, earning praise from industry professionals and love from fans. Despite being in the entertainment industry, she always maintains a low profile and a humble attitude, earning respect from her peers. In expression, Su Mengyuan likes to use 'we' and 'together,' emphasizing team spirit.\",\n",
|
"metadata": {
|
||||||
" \"bot_name\": \"Su Mengyuan\",\n",
|
"collapsed": false
|
||||||
" \"user_name\": \"Lu Xingchen\",\n",
|
},
|
||||||
"}"
|
"execution_count": null
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 14,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"messages = [\n",
|
"from langchain import hub\n",
|
||||||
" AIMessage(\n",
|
"from langchain.agents import AgentExecutor, create_json_chat_agent\n",
|
||||||
" content=\"(Narration: Su Mengyuan stars in a music-themed movie directed by Lu Xingchen. During filming, they have a disagreement over the performance of a particular scene.) Director, about this scene, I think we can try to start from the character's inner emotions to make the performance more authentic.\"\n",
|
"from langchain_community.tools.tavily_search import TavilySearchResults\n",
|
||||||
" ),\n",
|
"\n",
|
||||||
" HumanMessage(\n",
|
"tools = [TavilySearchResults(max_results=1)]\n",
|
||||||
" content=\"I understand your idea, but I believe that if we emphasize the inner emotions too much, it might overshadow the musical elements.\"\n",
|
"prompt = hub.pull(\"hwchase17/react-chat-json\")\n",
|
||||||
" ),\n",
|
"llm = ChatZhipuAI(temperature=0.01, model=\"glm-4\")\n",
|
||||||
" AIMessage(\n",
|
"\n",
|
||||||
" content=\"Hmm, I understand. But the key to this scene is the character's emotional transformation. Could we try to express these emotions through music, so the audience can better feel the character's growth?\"\n",
|
"agent = create_json_chat_agent(llm, tools, prompt)\n",
|
||||||
" ),\n",
|
"agent_executor = AgentExecutor(\n",
|
||||||
" HumanMessage(\n",
|
" agent=agent, tools=tools, verbose=True, handle_parsing_errors=True\n",
|
||||||
" content=\"That sounds good. Let's try to combine the character's emotional transformation with the musical elements and see if we can achieve a better effect.\"\n",
|
")"
|
||||||
" ),\n",
|
],
|
||||||
"]"
|
"metadata": {
|
||||||
]
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"execution_count": null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"agent_executor.invoke({\"input\": \"what is LangChain?\"})"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"execution_count": null
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|||||||
@@ -216,11 +216,11 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from typing import List\n",
|
"from typing import List\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from langchain_community.chat_loaders.base import ChatSession\n",
|
|
||||||
"from langchain_community.chat_loaders.utils import (\n",
|
"from langchain_community.chat_loaders.utils import (\n",
|
||||||
" map_ai_messages,\n",
|
" map_ai_messages,\n",
|
||||||
" merge_chat_runs,\n",
|
" merge_chat_runs,\n",
|
||||||
")\n",
|
")\n",
|
||||||
|
"from langchain_core.chat_sessions import ChatSession\n",
|
||||||
"\n",
|
"\n",
|
||||||
"raw_messages = loader.lazy_load()\n",
|
"raw_messages = loader.lazy_load()\n",
|
||||||
"# Merge consecutive messages from the same sender into a single message\n",
|
"# Merge consecutive messages from the same sender into a single message\n",
|
||||||
|
|||||||
@@ -258,7 +258,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.adapters.openai import convert_messages_for_finetuning"
|
"from langchain_community.adapters.openai import convert_messages_for_finetuning"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -116,11 +116,11 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from typing import List\n",
|
"from typing import List\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from langchain_community.chat_loaders.base import ChatSession\n",
|
|
||||||
"from langchain_community.chat_loaders.utils import (\n",
|
"from langchain_community.chat_loaders.utils import (\n",
|
||||||
" map_ai_messages,\n",
|
" map_ai_messages,\n",
|
||||||
" merge_chat_runs,\n",
|
" merge_chat_runs,\n",
|
||||||
")\n",
|
")\n",
|
||||||
|
"from langchain_core.chat_sessions import ChatSession\n",
|
||||||
"\n",
|
"\n",
|
||||||
"raw_messages = loader.lazy_load()\n",
|
"raw_messages = loader.lazy_load()\n",
|
||||||
"# Merge consecutive messages from the same sender into a single message\n",
|
"# Merge consecutive messages from the same sender into a single message\n",
|
||||||
@@ -173,7 +173,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.adapters.openai import convert_messages_for_finetuning"
|
"from langchain_community.adapters.openai import convert_messages_for_finetuning"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -150,7 +150,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.adapters.openai import convert_messages_for_finetuning\n",
|
"from langchain_community.adapters.openai import convert_messages_for_finetuning\n",
|
||||||
"\n",
|
"\n",
|
||||||
"training_data = convert_messages_for_finetuning(chat_sessions)"
|
"training_data = convert_messages_for_finetuning(chat_sessions)"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -285,7 +285,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.adapters.openai import convert_messages_for_finetuning\n",
|
"from langchain_community.adapters.openai import convert_messages_for_finetuning\n",
|
||||||
"\n",
|
"\n",
|
||||||
"training_data = convert_messages_for_finetuning(chat_sessions)"
|
"training_data = convert_messages_for_finetuning(chat_sessions)"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -87,11 +87,11 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from typing import List\n",
|
"from typing import List\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from langchain_community.chat_loaders.base import ChatSession\n",
|
|
||||||
"from langchain_community.chat_loaders.utils import (\n",
|
"from langchain_community.chat_loaders.utils import (\n",
|
||||||
" map_ai_messages,\n",
|
" map_ai_messages,\n",
|
||||||
" merge_chat_runs,\n",
|
" merge_chat_runs,\n",
|
||||||
")\n",
|
")\n",
|
||||||
|
"from langchain_core.chat_sessions import ChatSession\n",
|
||||||
"\n",
|
"\n",
|
||||||
"raw_messages = loader.lazy_load()\n",
|
"raw_messages = loader.lazy_load()\n",
|
||||||
"# Merge consecutive messages from the same sender into a single message\n",
|
"# Merge consecutive messages from the same sender into a single message\n",
|
||||||
|
|||||||
@@ -136,11 +136,11 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from typing import List\n",
|
"from typing import List\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from langchain_community.chat_loaders.base import ChatSession\n",
|
|
||||||
"from langchain_community.chat_loaders.utils import (\n",
|
"from langchain_community.chat_loaders.utils import (\n",
|
||||||
" map_ai_messages,\n",
|
" map_ai_messages,\n",
|
||||||
" merge_chat_runs,\n",
|
" merge_chat_runs,\n",
|
||||||
")\n",
|
")\n",
|
||||||
|
"from langchain_core.chat_sessions import ChatSession\n",
|
||||||
"\n",
|
"\n",
|
||||||
"raw_messages = loader.lazy_load()\n",
|
"raw_messages = loader.lazy_load()\n",
|
||||||
"# Merge consecutive messages from the same sender into a single message\n",
|
"# Merge consecutive messages from the same sender into a single message\n",
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"import json\n",
|
"import json\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from langchain.adapters.openai import convert_message_to_dict\n",
|
"from langchain_community.adapters.openai import convert_message_to_dict\n",
|
||||||
"from langchain_core.messages import AIMessage"
|
"from langchain_core.messages import AIMessage"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -209,11 +209,11 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from typing import List\n",
|
"from typing import List\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from langchain_community.chat_loaders.base import ChatSession\n",
|
|
||||||
"from langchain_community.chat_loaders.utils import (\n",
|
"from langchain_community.chat_loaders.utils import (\n",
|
||||||
" map_ai_messages,\n",
|
" map_ai_messages,\n",
|
||||||
" merge_chat_runs,\n",
|
" merge_chat_runs,\n",
|
||||||
")\n",
|
")\n",
|
||||||
|
"from langchain_core.chat_sessions import ChatSession\n",
|
||||||
"\n",
|
"\n",
|
||||||
"raw_messages = loader.lazy_load()\n",
|
"raw_messages = loader.lazy_load()\n",
|
||||||
"# Merge consecutive messages from the same sender into a single message\n",
|
"# Merge consecutive messages from the same sender into a single message\n",
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user