mirror of
https://github.com/MervinPraison/PraisonAI.git
synced 2026-11-09 03:39:37 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
79883f1af2 | ||
|
|
a098eb0150 | ||
|
|
b9ce3b4ccb | ||
|
|
58b44280df | ||
|
|
4dba09d6b1 | ||
|
|
a538513bec | ||
|
|
4fb79207cf | ||
|
|
0b444fae34 | ||
|
|
bd34e45bbc | ||
|
|
f4f051e57e | ||
|
|
3c2610cc3c | ||
|
|
9788454134 | ||
|
|
e618004e60 | ||
|
|
384803ba8e | ||
|
|
d3bb366a51 | ||
|
|
e579e84b52 | ||
|
|
61440403f5 | ||
|
|
b615782175 | ||
|
|
eeb77cfd20 | ||
|
|
00d7994601 | ||
|
|
10aa49addc | ||
|
|
79c9cf8bf9 | ||
|
|
a9416fb7fc | ||
|
|
547b40ee72 | ||
|
|
4fc35d1b10 | ||
|
|
c72d74987b | ||
|
|
430ec6571a | ||
|
|
e88288773a | ||
|
|
9af03e3fef | ||
|
|
7a7560621b | ||
|
|
531c58ac70 | ||
|
|
274ade8154 | ||
|
|
83f4f874e9 | ||
|
|
99cd88dd30 | ||
|
|
1d22818662 | ||
|
|
4f38e7250a | ||
|
|
b736567375 | ||
|
|
d014ea8d18 | ||
|
|
fd6db3ccfa | ||
|
|
1f2f26c5cf | ||
|
|
1965f296b0 | ||
|
|
cc7036784b | ||
|
|
826a68c33e | ||
|
|
88ceba804b | ||
|
|
dc5b10fba8 | ||
|
|
f0172e8339 | ||
|
|
5c498a12de | ||
|
|
88410881e1 | ||
|
|
791d59f96e | ||
|
|
cdbe1303ad | ||
|
|
b63f931bea | ||
|
|
ff25b96f91 | ||
|
|
603f0ea008 | ||
|
|
db38b55174 | ||
|
|
cebd175d61 | ||
|
|
01ff75e573 | ||
|
|
28629f2203 | ||
|
|
ac5c463362 | ||
|
|
84dc57583e | ||
|
|
460e8b45a7 | ||
|
|
f6bbbd7cd6 | ||
|
|
8a435571f4 | ||
|
|
204106daa8 | ||
|
|
6a1e14f97a | ||
|
|
333777b991 | ||
|
|
b02cd85290 | ||
|
|
3bc0c2b533 | ||
|
|
68d4f34bb7 | ||
|
|
a24d900a8c | ||
|
|
552a10df9e | ||
|
|
9c57aa3165 | ||
|
|
40fe92f1d0 | ||
|
|
179cc9566d | ||
|
|
d9cf9ba534 | ||
|
|
4ec0366e79 | ||
|
|
d6a3bd1240 | ||
|
|
0a18e27f54 | ||
|
|
b824d950b5 | ||
|
|
b31edc90d5 | ||
|
|
d2841ccb3a |
No files matched your search
+2
-1
@@ -74,4 +74,5 @@ agents/praisonaiagents/praisonaiagents.egg-info
|
||||
.praison
|
||||
# Local Netlify folder
|
||||
.netlify
|
||||
.qodo
|
||||
.qodo
|
||||
CopilotKit*
|
||||
+1
-1
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
flask \
|
||||
"praisonai>=2.2.27" \
|
||||
"praisonai>=2.2.32" \
|
||||
"praisonai[api]" \
|
||||
gunicorn \
|
||||
markdown
|
||||
|
||||
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=2.2.27" \
|
||||
"praisonai>=2.2.32" \
|
||||
"praisonai[chat]" \
|
||||
"embedchain[github,youtube]"
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=2.2.27" \
|
||||
"praisonai>=2.2.32" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[chat]" \
|
||||
"praisonai[realtime]" \
|
||||
|
||||
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=2.2.27" \
|
||||
"praisonai>=2.2.32" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[crewai]"
|
||||
|
||||
|
||||
+2
-2
@@ -121,7 +121,7 @@ healthcheck:
|
||||
## 📦 Package Versions
|
||||
|
||||
All Docker images use consistent, up-to-date versions:
|
||||
- PraisonAI: `>=2.2.27`
|
||||
- PraisonAI: `>=2.2.32`
|
||||
- PraisonAI Agents: `>=0.0.92`
|
||||
- Python: `3.11-slim`
|
||||
|
||||
@@ -218,7 +218,7 @@ docker-compose up -d
|
||||
### Version Pinning
|
||||
To use specific versions, update the Dockerfile:
|
||||
```dockerfile
|
||||
RUN pip install "praisonai==2.2.27" "praisonaiagents==0.0.92"
|
||||
RUN pip install "praisonai==2.2.32" "praisonaiagents==0.0.92"
|
||||
```
|
||||
|
||||
## 🌐 Production Deployment
|
||||
|
||||
@@ -14,7 +14,11 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
|
||||
"[](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/Code_Analysis_Agent.ipynb)\n"
|
||||
=======
|
||||
"[](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/code_analysis_agent.ipynb)\n"
|
||||
|
||||
],
|
||||
"metadata": {
|
||||
"id": "P3X8PXPyojnL"
|
||||
@@ -456,4 +460,8 @@
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
||||
}
|
||||
=======
|
||||
}
|
||||
|
||||
@@ -0,0 +1,605 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "PnWDOzZelHlE",
|
||||
"metadata": {
|
||||
"id": "PnWDOzZelHlE"
|
||||
},
|
||||
"source": [
|
||||
"# 🌱 Gemma 2B Instruction Agent\n",
|
||||
"\n",
|
||||
"**Goal:** You will learn how to do data prep, how to train, how to run the model, and how to save it using Google’s `gemma-2b-it` open-source model.\n",
|
||||
"\n",
|
||||
"---\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4qumJb5qs9CF",
|
||||
"metadata": {
|
||||
"id": "4qumJb5qs9CF"
|
||||
},
|
||||
"source": [
|
||||
"[](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/Gemma2B_Instruction_Agent.ipynb)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "qdsqiQDosXmA",
|
||||
"metadata": {
|
||||
"id": "qdsqiQDosXmA"
|
||||
},
|
||||
"source": [
|
||||
"# Dependencies"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6G4RPDe3lHlH",
|
||||
"metadata": {
|
||||
"id": "6G4RPDe3lHlH"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!pip install transformers accelerate datasets bitsandbytes -q"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "mop0KNO7sdEl",
|
||||
"metadata": {
|
||||
"id": "mop0KNO7sdEl"
|
||||
},
|
||||
"source": [
|
||||
"# Tools & Model Setup"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "spOeRX2UlHlI",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/",
|
||||
"height": 369,
|
||||
"referenced_widgets": [
|
||||
"a2db67e368d94f579c59b36c6ce7af33",
|
||||
"5816fa4288cd42d6b15094e2c79f760f",
|
||||
"5d6fe9b464f944cdbb00b9936669e737",
|
||||
"19920e5b8157423ba5d173dd376e52e6",
|
||||
"bd7157974bc5485ab5b6db54facb9721",
|
||||
"0063e7320c98440d91ae475596c5cd90",
|
||||
"a0f045bc54754d5a85ed6bf7b6b33c1a",
|
||||
"9174c5aff19b4caab8a38fbf3acb2449",
|
||||
"3de0f0b7ecb34c488145b24e30418e4b",
|
||||
"d4457543819d468eb916f126382184b0",
|
||||
"08f90c04f0de4b1db57624db4b43e17d",
|
||||
"269373e971704b9ea2e10e261b1c56d1",
|
||||
"d54d35c945ce4f75af6f83f37dd6af6c",
|
||||
"b828857fecdc40fa91f6a8633f0f20b4",
|
||||
"b5e70949aea24ae9a75e45aa291cc3aa",
|
||||
"9c556838b24246a8b39b1f3f9babfe4a",
|
||||
"062b64d17b7043c1bccc419280c9db0a",
|
||||
"138ae14327184f65896c29a424bad18d",
|
||||
"cb65fa660df54218ad804815dfba4071",
|
||||
"93d2454f1b564896bef50906e859c660",
|
||||
"64e7861e6afc4b57b28063096533aac8",
|
||||
"13fe0b8a8fd14a3684ae5d9beefb3f76",
|
||||
"d1ec61e51d4c4b07b301959c4919f2a2",
|
||||
"a45e9ed3d46645a18ca1f6b06f875ec9",
|
||||
"a364770555cf40fdab5ff7bb85997127",
|
||||
"12b3225aeb8c4f61b23b588738d48683",
|
||||
"dca4952bae4c49ddb2fec2c36726a34d",
|
||||
"163df8921f04425caed240465d8bd8b7",
|
||||
"1a09718a80cd4bb5a9e985a3f599c614",
|
||||
"71c719d57e4f4e23bd520f80e2e07e33",
|
||||
"5e8c8eb7eba948fc8a39b601e9ac658e",
|
||||
"db4fa2c97b7247c4826a1a9f69c1bed3",
|
||||
"42a6f9c136584cc98fc2be973d9edaec",
|
||||
"c1bc8cf2ed6542c49c1940673a662b1d",
|
||||
"365b69634e2846a09b91de6b61d0719b",
|
||||
"13eb69bc1c3c432db6c504856f3a0f7a",
|
||||
"56444404d97549848ed729ee8bc14ec8",
|
||||
"f0303454544d42a1ada2213a085a4ac0",
|
||||
"c717969ed1b145a9af67d74482d49eab",
|
||||
"9e9a826dfb264e44aa69c553d978729d",
|
||||
"f2b4ff56a96c4d7e9f56668f6b7b5213",
|
||||
"f71f0a3ccc654df288d7489abca0ffc0",
|
||||
"5ab0574ed1db47e9b6f43e27acd6c2b2",
|
||||
"426fae423dbb43ef9252e6fcfb099c6f",
|
||||
"2e1c3ca06b2948f0bcf53a975dd67aec",
|
||||
"3685a74a466a4ca0b5f05e7aee631d74",
|
||||
"7bda5a4e803c4cbc8da67da8a565725b",
|
||||
"5450fde9a54d450d90d14b009047b522",
|
||||
"e827839bf4d34d2193776d1ee90d27ad",
|
||||
"3d9854af016a4c3c8a582fc7f0f0b0bf",
|
||||
"7045a11d5f0f4b2d8ff224bb76a9c943",
|
||||
"32e568b25c2d447a8fa63055cb44c75c",
|
||||
"ebd9d55db82542e5ae6bb9e715e36a6a",
|
||||
"798a57883792465ab024dc19a58a54be",
|
||||
"c9d1133770b44b22bda89f3d0eecd4f8",
|
||||
"c040fa1bbd9f442884776fdf33efb4a2",
|
||||
"c57e497d4fd546ff950eb2c898b94c88",
|
||||
"5ae22935b7374219a16272cef31d6d9e",
|
||||
"aae2686ee64740218d046ec37e50b584",
|
||||
"87b4c953163a4cff8a88b76f40da9de8",
|
||||
"30ee0e766fae44bfb570ae8adfba1a54",
|
||||
"1c2db67dfc904e30a4013d9fe0253a4c",
|
||||
"f6c5380bba0f40a4bea9b78d205eb726",
|
||||
"0de9f765500b45428f828ad173dd279e",
|
||||
"0663e5cfc20c496993b7c0054ce3d1a1",
|
||||
"7ce5d369129b461681f424e52415e534",
|
||||
"dfa90d76c670436a80f1a4fd0971b171",
|
||||
"0694216654ac41909d567a8c4af247de",
|
||||
"658048f25b4a4ad6a3ee66e97a542cc9",
|
||||
"0edccfd243f940cfad9723b1d3f5a97c",
|
||||
"32d473040a6744718b1b35543f2b3a0f",
|
||||
"eadbfe846763427e98a40597560d8519",
|
||||
"3cec8231bccb4bcf8d657ee8d2120f79",
|
||||
"8bd43207fa8f4b309590f605ad81354f",
|
||||
"8821912860f04105959ceb42e53de948",
|
||||
"3666b044c757439f948f6942d7682c2f",
|
||||
"d47e7c7504374fee82eef9f8cbe24eff",
|
||||
"9c6d577f113f4f4baaa1f0e001549862",
|
||||
"b0c88d34b0df46b7a3da8db42ff3274e",
|
||||
"e4e4a2d0422b45c09c8691ce14ba9b9a",
|
||||
"d9b1de1b37f14d70bc56a5f6cb468c9b",
|
||||
"cb95b11368984236937fcec7cb689b04",
|
||||
"69e70bfdafd4459491bad75651028626",
|
||||
"a3b9f03c1f014691b5959fb9012c0bab",
|
||||
"088da12bcff64297ad82dc7b2d01709e",
|
||||
"8a69dc8aa84e49b1aabb58eddca1eee3",
|
||||
"263e458b8a5f405bb1ee365fe561309d",
|
||||
"aa3039e03447492da11f86185384470b",
|
||||
"3c22264e11c0402fb07f56a0763dc055",
|
||||
"db2c5746b8cc48d9a059bcef3b55217d",
|
||||
"ad3ab2ab81f6469485a3072bd1ba6a4a",
|
||||
"74ab1c79c43041c7ac314d62a6da04bf",
|
||||
"ad1a3719b1814f6b88202e131ae923c7",
|
||||
"d1cb72251682425fa6ca68341a084f95",
|
||||
"895914fca4204593b1a4314e50b8e5a1",
|
||||
"a8374dc19b294b6d8f5f92bee5196a1a",
|
||||
"b7bef910950949fbac65fd996ad058e2",
|
||||
"ce6b7aadbd904b3caa93d927ced6beb3",
|
||||
"ba6a9e31ddf7419db317a5d27a1567bf",
|
||||
"f271e9c78dcf4ab686caac37f18bd6f7",
|
||||
"9ed57fe81783476986d6cb98d07e9aa1",
|
||||
"8da75a35ab184675b0164682d8217406",
|
||||
"f411c4485b844ceb9b3116f209a6fc86",
|
||||
"60ac9cd6505141348323b39db7314554",
|
||||
"59b2d494f11344879463a3471716bc3f",
|
||||
"46abda4b52224874a93b729f5d4d195b",
|
||||
"37bc81b97d48474aa0fd7e7739654d86",
|
||||
"73da5101d99440abac30dc8c084439d4",
|
||||
"ef790742325743fdb8f7cc6822388185",
|
||||
"5eccfbe5715f4df9a96cffb2573f916f",
|
||||
"3bf293367470430f84f341ef8711460a",
|
||||
"317281ca2af54a148fa4cfd8f6e0f382",
|
||||
"d655ef37643448aa95253143f8552f58",
|
||||
"158e61376dee400e820f47c87d59bac5",
|
||||
"d1fbbe8010e340119bf556c75284e1c2",
|
||||
"eea1731926554e56a85867de15153f82",
|
||||
"766fb34d23b941b2a9e0de153f4cb031",
|
||||
"beb52b95db564105bbd8bc2b2d21c1ce",
|
||||
"2effd1c45bee4278bd9172310f58fed4",
|
||||
"91d6488876284748932b664953c02146",
|
||||
"9157d82cca3f45c5b5a666a3d35d6abe"
|
||||
]
|
||||
},
|
||||
"id": "spOeRX2UlHlI",
|
||||
"outputId": "093b74d1-9ece-4db5-d177-3aff6e2d6e0b"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "a2db67e368d94f579c59b36c6ce7af33",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"tokenizer_config.json: 0%| | 0.00/47.0k [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "269373e971704b9ea2e10e261b1c56d1",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"tokenizer.model: 0%| | 0.00/4.24M [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "d1ec61e51d4c4b07b301959c4919f2a2",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"tokenizer.json: 0%| | 0.00/17.5M [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "c1bc8cf2ed6542c49c1940673a662b1d",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"special_tokens_map.json: 0%| | 0.00/636 [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "2e1c3ca06b2948f0bcf53a975dd67aec",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"config.json: 0%| | 0.00/838 [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "c040fa1bbd9f442884776fdf33efb4a2",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"model.safetensors.index.json: 0%| | 0.00/24.2k [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "dfa90d76c670436a80f1a4fd0971b171",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"Fetching 2 files: 0%| | 0/2 [00:00<?, ?it/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "9c6d577f113f4f4baaa1f0e001549862",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"model-00001-of-00002.safetensors: 0%| | 0.00/4.99G [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "3c22264e11c0402fb07f56a0763dc055",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"model-00002-of-00002.safetensors: 0%| | 0.00/241M [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "f271e9c78dcf4ab686caac37f18bd6f7",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "3bf293367470430f84f341ef8711460a",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"generation_config.json: 0%| | 0.00/187 [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from huggingface_hub import login\n",
|
||||
"from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer\n",
|
||||
"from datasets import load_dataset\n",
|
||||
"import torch\n",
|
||||
"\n",
|
||||
"login(\"Enter your token here\")\n",
|
||||
"\n",
|
||||
"model_id = \"google/gemma-2-2b-it\"\n",
|
||||
"\n",
|
||||
"tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
|
||||
"model = AutoModelForCausalLM.from_pretrained(\n",
|
||||
" model_id,\n",
|
||||
" device_map=\"auto\", # Automatically selects GPU if available\n",
|
||||
" torch_dtype=torch.float16 # Optimized for performance\n",
|
||||
")\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "GaykKFKYspMd",
|
||||
"metadata": {
|
||||
"id": "GaykKFKYspMd"
|
||||
},
|
||||
"source": [
|
||||
"# Yaml Prompt Configuration"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "iLBZ7WPAlHlJ",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "iLBZ7WPAlHlJ",
|
||||
"outputId": "a99fc3f2-68e0-4af8-cfbd-3c48a865538f"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"You are Qwen, a helpful assistant.\n",
|
||||
"User: What is the capital of France?\n",
|
||||
"Assistant: The capital of France is **Paris**. \n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = \"You are Qwen, a helpful assistant.\\nUser: What is the capital of France?\\nAssistant:\"\n",
|
||||
"inputs = tokenizer(prompt, return_tensors=\"pt\").to(model.device)\n",
|
||||
"outputs = model.generate(**inputs, max_new_tokens=100)\n",
|
||||
"response = tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
|
||||
"print(response)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "XPqtCExSsvhn",
|
||||
"metadata": {
|
||||
"id": "XPqtCExSsvhn"
|
||||
},
|
||||
"source": [
|
||||
"# Use a small sample dataset"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"id": "ActfjFx0lHlK",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/",
|
||||
"height": 49,
|
||||
"referenced_widgets": [
|
||||
"f1028b5dcab4460c81b09802ca7ec89c",
|
||||
"ff33dcccaad945e9bf136d0f72e38d74",
|
||||
"eec3a11b0605435d8d1e55092f5e5b85",
|
||||
"d997b7bc993b4aa58cfafa7c59829ad3",
|
||||
"62e2dc508746419ba3c0405a7199c6dc",
|
||||
"2a7fc59019c749aa98d0e970a9b6b168",
|
||||
"78067f66c8544020aa262c0012aa019b",
|
||||
"f5e511b4279747c0a33b10f499a4f718",
|
||||
"10e248a5730c4c09834b4065b6dd7843",
|
||||
"bbd5a106c1de43e497987648899569e0",
|
||||
"118faf3d1ed142038af4783fb3ba86cc"
|
||||
]
|
||||
},
|
||||
"id": "ActfjFx0lHlK",
|
||||
"outputId": "f257e553-7baa-4f1e-b531-a6b7dc358665"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "f1028b5dcab4460c81b09802ca7ec89c",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"Map: 0%| | 0/4 [00:00<?, ? examples/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from datasets import Dataset\n",
|
||||
"\n",
|
||||
"sample_data = {\n",
|
||||
" 'text': [\n",
|
||||
" 'The sun is a star at the center of our solar system.',\n",
|
||||
" 'Photosynthesis is the process by which green plants make food.',\n",
|
||||
" 'Water freezes at 0 degrees Celsius.',\n",
|
||||
" 'The Earth revolves around the sun in 365 days.'\n",
|
||||
" ]\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"dataset = Dataset.from_dict(sample_data)\n",
|
||||
"\n",
|
||||
"def tokenize_function(example):\n",
|
||||
" return tokenizer(example['text'], padding='max_length', truncation=True, max_length=64)\n",
|
||||
"\n",
|
||||
"tokenized_dataset = dataset.map(tokenize_function)\n",
|
||||
"tokenized_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "x7icWpOnszjj",
|
||||
"metadata": {
|
||||
"id": "x7icWpOnszjj"
|
||||
},
|
||||
"source": [
|
||||
"# Inference"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "8mM4PAhRlHlL",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "8mM4PAhRlHlL",
|
||||
"outputId": "1ba90639-b847-4cb1-df42-3fdd644e4cb8"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"user\n",
|
||||
"Explain photosynthesis to a child.\n",
|
||||
"model\n",
|
||||
"Imagine plants are like tiny chefs, and they cook their own food! \n",
|
||||
"\n",
|
||||
"They use sunlight, air, and water to make yummy food called sugar. \n",
|
||||
"\n",
|
||||
"Here's how it works:\n",
|
||||
"\n",
|
||||
"1. **Sunlight:** Plants have special green stuff called chlorophyll that acts like a solar panel, soaking up the sun's energy.\n",
|
||||
"2. **Air:** Plants take in air through tiny holes in their leaves called stomata. The air has a gas called carbon dioxide.\n",
|
||||
"3\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"input_text = \"Explain photosynthesis to a child.\"\n",
|
||||
"chat = tokenizer.apply_chat_template(\n",
|
||||
" [{\"role\": \"user\", \"content\": input_text}],\n",
|
||||
" tokenize=False,\n",
|
||||
" add_generation_prompt=True\n",
|
||||
")\n",
|
||||
"inputs = tokenizer(chat, return_tensors=\"pt\").to(model.device)\n",
|
||||
"output = model.generate(**inputs, max_new_tokens=100)\n",
|
||||
"print(tokenizer.decode(output[0], skip_special_tokens=True))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "Fhkfhbjns2oq",
|
||||
"metadata": {
|
||||
"id": "Fhkfhbjns2oq"
|
||||
},
|
||||
"source": [
|
||||
"# Save Model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"id": "OuJEt2-jlHlL",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "OuJEt2-jlHlL",
|
||||
"outputId": "f076e950-cce7-4850-edd9-ff1d84a096d6"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"('gemma-finetuned-demo/tokenizer_config.json',\n",
|
||||
" 'gemma-finetuned-demo/special_tokens_map.json',\n",
|
||||
" 'gemma-finetuned-demo/chat_template.jinja',\n",
|
||||
" 'gemma-finetuned-demo/tokenizer.model',\n",
|
||||
" 'gemma-finetuned-demo/added_tokens.json',\n",
|
||||
" 'gemma-finetuned-demo/tokenizer.json')"
|
||||
]
|
||||
},
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model.save_pretrained(\"gemma-finetuned-demo\")\n",
|
||||
"tokenizer.save_pretrained(\"gemma-finetuned-demo\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "uFK1sCzblHlM",
|
||||
"metadata": {
|
||||
"id": "uFK1sCzblHlM"
|
||||
},
|
||||
"source": [
|
||||
"# Output\n",
|
||||
"`Photosynthesis is how plants eat sunlight! 🌞 They use air, water, and sunlight to make food and grow.`"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python",
|
||||
"version": "3.11"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a9343007",
|
||||
"metadata": {
|
||||
"id": "a9343007"
|
||||
},
|
||||
"source": [
|
||||
"# Groq Llama3-8b-8192 Agent\n",
|
||||
"This agent uses Groq's `llama3-8b-8192` model to answer questions about Groq technology.\n",
|
||||
"It demonstrates a structured approach with YAML configs, prompt templates, and result handling."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"[](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/Groq_LPU_Powered_AI_Assistant.ipynb)\n"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "mBWaooyypsyD"
|
||||
},
|
||||
"id": "mBWaooyypsyD"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b36ff766",
|
||||
"metadata": {
|
||||
"id": "b36ff766"
|
||||
},
|
||||
"source": [
|
||||
"## Dependencies\n",
|
||||
"We'll install the required Groq Python SDK.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "518e7a6a",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "518e7a6a",
|
||||
"outputId": "98149592-19d0-472a-c31b-aae763fc8b07"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"name": "stdout",
|
||||
"text": [
|
||||
"\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/129.6 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━━\u001b[0m \u001b[32m122.9/129.6 kB\u001b[0m \u001b[31m5.3 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m129.6/129.6 kB\u001b[0m \u001b[31m2.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25h"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!pip install --quiet groq pyyaml"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d8569a5f",
|
||||
"metadata": {
|
||||
"id": "d8569a5f"
|
||||
},
|
||||
"source": [
|
||||
"## Tools Setup\n",
|
||||
"Initialize Groq client and helper functions for prompt generation.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "b59b0d09",
|
||||
"metadata": {
|
||||
"id": "b59b0d09"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import yaml\n",
|
||||
"from groq import Groq\n",
|
||||
"\n",
|
||||
"# Set API key (replace with your own or use environment variables)\n",
|
||||
"os.environ['GROQ_API_KEY'] = 'enter your key'\n",
|
||||
"\n",
|
||||
"# Initialize Groq client\n",
|
||||
"client = Groq()\n",
|
||||
"\n",
|
||||
"def run_groq_chat(prompt_messages, model='llama3-8b-8192'):\n",
|
||||
" response = client.chat.completions.create(\n",
|
||||
" model=model,\n",
|
||||
" messages=prompt_messages\n",
|
||||
" )\n",
|
||||
" return response.choices[0].message.content\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9302e8e6",
|
||||
"metadata": {
|
||||
"id": "9302e8e6"
|
||||
},
|
||||
"source": [
|
||||
"## YAML Configuration\n",
|
||||
"Define model and prompt templates via YAML.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "238b8f7f",
|
||||
"metadata": {
|
||||
"id": "238b8f7f"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"yaml_config = '''\n",
|
||||
"model: llama3-8b-8192\n",
|
||||
"prompt_template: |\n",
|
||||
" You are an expert AI assistant knowledgeable about Groq's technology.\n",
|
||||
" Provide a detailed answer to the user's question.\n",
|
||||
" Then summarize the key points briefly.\n",
|
||||
"\n",
|
||||
" User question: \"{user_question}\"\n",
|
||||
"'''\n",
|
||||
"\n",
|
||||
"config = yaml.safe_load(yaml_config)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6d2ff6b1",
|
||||
"metadata": {
|
||||
"id": "6d2ff6b1"
|
||||
},
|
||||
"source": [
|
||||
"## Prompt Construction\n",
|
||||
"Fill in the prompt template with user input.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "2d42e686",
|
||||
"metadata": {
|
||||
"id": "2d42e686"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def build_prompt(user_question):\n",
|
||||
" prompt_text = config['prompt_template'].format(user_question=user_question)\n",
|
||||
" messages = [\n",
|
||||
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
|
||||
" {\"role\": \"user\", \"content\": prompt_text}\n",
|
||||
" ]\n",
|
||||
" return messages\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3c9448d3",
|
||||
"metadata": {
|
||||
"id": "3c9448d3"
|
||||
},
|
||||
"source": [
|
||||
"## Main Logic\n",
|
||||
"Run the agent on an example question.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "c59933e4",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "c59933e4",
|
||||
"outputId": "bad10728-fd20-4e0d-fded-c29fd987a9b7"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"name": "stdout",
|
||||
"text": [
|
||||
"Agent response:\n",
|
||||
"Groq's Low-Precision Unified (LPU) technology is a proprietary architecture designed to accelerate artificial intelligence (AI) and machine learning (ML) workloads. LPU-powered models, also known as Groq Models, have several advantages over traditional Graphics Processing Units (GPUs) in specific applications:\n",
|
||||
"\n",
|
||||
"1. **Improved energy efficiency**: LPU is optimized for low power consumption, making it suitable for edge, mobile, and embedded devices where power constraints are common. This is particularly important for applications that require long-lived deployments, such as autonomous vehicles or IoT sensors.\n",
|
||||
"2. **Enhanced accuracy**: LPU's customized precision and data type selection enable better representational precision for numeric computations, resulting in improved model accuracy. This is particularly beneficial for tasks that require high-fidelity calculations, such as medical imaging or natural language processing.\n",
|
||||
"3. **Simplified software development**: LPU's unified architecture simplifies the development process for AI/ML developers. Groq Models provide a consistent programming model across different inference scenarios, allowing for easier model deployment and optimization.\n",
|
||||
"4. **Increased throughput**: LPU's optimized arithmetic units and pipelined architecture enable higher Throughput per Watt (TPW) compared to traditional GPUs. This translates to faster processing times and higher compute density.\n",
|
||||
"5. **Flexibility and scalability**: LPU-powered models can be deployed across various hardware platforms, from small, low-power devices to large data center clusters. This flexibility allows developers to choose the optimal deployment scenario for their specific use case.\n",
|
||||
"6. **Customization and specialization**: LPU's architecture can be customized for specific workloads, allowing for optimized performance and power consumption. This customization potential enables developers to create highly specialized AI/ML hardware that matches their specific requirements.\n",
|
||||
"\n",
|
||||
"In summary, Groq's LPU-powered models offer significant advantages over traditional GPUs in terms of energy efficiency, accuracy, software development simplicity, throughput, flexibility, and customization.\n",
|
||||
"\n",
|
||||
"Key points:\n",
|
||||
"\n",
|
||||
"* Improved energy efficiency and suitability for edge, mobile, and embedded devices\n",
|
||||
"* Enhanced accuracy for high-fidelity calculations\n",
|
||||
"* Simplified software development with a unified programming model\n",
|
||||
"* Increased throughput and compute density\n",
|
||||
"* Flexibility and scalability across various hardware platforms\n",
|
||||
"* Customization potential for specific workloads and applications\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"user_question = \"What are the advantages of Groq's LPU-powered models compared to traditional GPUs?\"\n",
|
||||
"prompt_messages = build_prompt(user_question)\n",
|
||||
"agent_response = run_groq_chat(prompt_messages, model=config['model'])\n",
|
||||
"print(\"Agent response:\")\n",
|
||||
"print(agent_response)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "304a52dd",
|
||||
"metadata": {
|
||||
"id": "304a52dd"
|
||||
},
|
||||
"source": [
|
||||
"## Output\n",
|
||||
"The agent provides a detailed answer followed by a brief summary.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": []
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
},
|
||||
"kernelspec": {
|
||||
"name": "python3",
|
||||
"display_name": "Python 3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,134 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "441900e0"
|
||||
},
|
||||
"source": [
|
||||
"# 🧠 Mistral v0.3 (7B) Conversational Agent\n",
|
||||
"A conceptual demo notebook to illustrate the workflow and output of the Mistral model, even if it doesn't run on Colab Free Tier."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "b4ba0b88"
|
||||
},
|
||||
"source": [
|
||||
"## 📄 Description\n",
|
||||
"This notebook showcases the typical flow of using Mistral v0.3 (7B) for conversational tasks. While the model is too large for Google Colab Free Tier, this version visually explains what the code would do.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"[](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/Mistral_v0_3_Conversational_Demo.ipynb)\n"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "yzXURvxIlIDD"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# 📦 Dependencies"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "IUQSTocQk7ml"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "6f2afc57"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"!pip install transformers accelerate torch\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "9d5dad27"
|
||||
},
|
||||
"source": [
|
||||
"## 🛠 Tools Used\n",
|
||||
"- 🤗 Transformers: For loading the model and tokenizer.\n",
|
||||
"- PyTorch: For model inference.\n",
|
||||
"- Accelerate: To manage device placement (CPU/GPU).\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "cd936b40"
|
||||
},
|
||||
"source": [
|
||||
"## 🧾 YAML Prompt Format (Conceptual)\n",
|
||||
"```yaml\n",
|
||||
"task: conversation\n",
|
||||
"input: \"What's the capital of France?\"\n",
|
||||
"history: []\n",
|
||||
"temperature: 0.7\n",
|
||||
"top_p: 0.9\n",
|
||||
"```\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "8abaaa62"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 🧠 Main Inference Logic (Illustrative)\n",
|
||||
"from transformers import AutoModelForCausalLM, AutoTokenizer\n",
|
||||
"import torch\n",
|
||||
"\n",
|
||||
"model_id = \"mistralai/Mistral-7B-Instruct-v0.3\"\n",
|
||||
"tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
|
||||
"model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map=\"auto\")\n",
|
||||
"\n",
|
||||
"prompt = \"What's the capital of France?\"\n",
|
||||
"inputs = tokenizer(prompt, return_tensors=\"pt\").to(model.device)\n",
|
||||
"outputs = model.generate(**inputs, max_new_tokens=100)\n",
|
||||
"response = tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
|
||||
"print(response)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "def486c6"
|
||||
},
|
||||
"source": [
|
||||
"## 📤 Output (Example)\n",
|
||||
"> \"The capital of France is Paris.\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"accelerator": "GPU",
|
||||
"colab": {
|
||||
"gpuType": "T4",
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
@@ -0,0 +1,401 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "l7oSysosTfJj"
|
||||
},
|
||||
"source": [
|
||||
"# Predictive Maintenance Workflow using PraisonAIAgents\n",
|
||||
"\n",
|
||||
"This notebook demonstrates how to build a predictive maintenance workflow using multiple AI agents."
|
||||
],
|
||||
"id": "l7oSysosTfJj"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"[](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/Predictive_Maintenance_Multi-Agent_Workflow.ipynb)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "LH0_JjO7Y1G2"
|
||||
},
|
||||
"id": "LH0_JjO7Y1G2"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# Dependencies"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "ei-yNZSKXgtJ"
|
||||
},
|
||||
"id": "ei-yNZSKXgtJ"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "fnkuN8sTTfJq"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!pip install praisonaiagents"
|
||||
],
|
||||
"id": "fnkuN8sTTfJq"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# Set your OpenAI API key"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "7PUSTQHwX5ia"
|
||||
},
|
||||
"id": "7PUSTQHwX5ia"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"id": "33HivD-LTfJu"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"os.environ['OPENAI_API_KEY'] = 'enter your api key'"
|
||||
],
|
||||
"id": "33HivD-LTfJu"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# Import required modules"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "D6we7t4nYESB"
|
||||
},
|
||||
"id": "D6we7t4nYESB"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "03z7Pi22TfJw"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from praisonaiagents import Agent, Task, PraisonAIAgents\n",
|
||||
"import time\n",
|
||||
"from typing import Dict, List\n",
|
||||
"import asyncio"
|
||||
],
|
||||
"id": "03z7Pi22TfJw"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# Define helper functions"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "TzZt0jbjYGgw"
|
||||
},
|
||||
"id": "TzZt0jbjYGgw"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "2oMzDsp3TfJw"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"def collect_sensor_data():\n",
|
||||
" return {\n",
|
||||
" \"temperature\": 75 + (int(time.time()) % 20),\n",
|
||||
" \"vibration\": 0.5 + (int(time.time()) % 10) / 10,\n",
|
||||
" \"pressure\": 100 + (int(time.time()) % 50),\n",
|
||||
" \"noise_level\": 60 + (int(time.time()) % 30)\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
"def analyze_performance():\n",
|
||||
" return {\n",
|
||||
" \"efficiency\": 0.8 + (int(time.time()) % 20) / 100,\n",
|
||||
" \"uptime\": 0.95 + (int(time.time()) % 5) / 100,\n",
|
||||
" \"output_quality\": 0.9 + (int(time.time()) % 10) / 100\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
"def detect_anomalies(sensor_data: Dict, performance: Dict):\n",
|
||||
" anomalies = []\n",
|
||||
" if sensor_data[\"temperature\"] > 90:\n",
|
||||
" anomalies.append({\"type\": \"temperature_high\", \"severity\": \"critical\"})\n",
|
||||
" if sensor_data[\"vibration\"] > 1.2:\n",
|
||||
" anomalies.append({\"type\": \"vibration_excess\", \"severity\": \"warning\"})\n",
|
||||
" if performance[\"efficiency\"] < 0.85:\n",
|
||||
" anomalies.append({\"type\": \"efficiency_low\", \"severity\": \"warning\"})\n",
|
||||
" return anomalies\n",
|
||||
"\n",
|
||||
"def predict_failures(anomalies: List[Dict]):\n",
|
||||
" predictions = []\n",
|
||||
" severity_scores = {\"critical\": 0.9, \"warning\": 0.6}\n",
|
||||
" for anomaly in anomalies:\n",
|
||||
" predictions.append({\n",
|
||||
" \"component\": anomaly[\"type\"].split(\"_\")[0],\n",
|
||||
" \"probability\": severity_scores[anomaly[\"severity\"]],\n",
|
||||
" \"timeframe\": \"24_hours\" if anomaly[\"severity\"] == \"critical\" else \"7_days\"\n",
|
||||
" })\n",
|
||||
" return predictions\n",
|
||||
"\n",
|
||||
"def schedule_maintenance(predictions: List[Dict]):\n",
|
||||
" schedule = []\n",
|
||||
" for pred in predictions:\n",
|
||||
" schedule.append({\n",
|
||||
" \"component\": pred[\"component\"],\n",
|
||||
" \"priority\": \"immediate\" if pred[\"timeframe\"] == \"24_hours\" else \"planned\",\n",
|
||||
" \"estimated_duration\": \"2_hours\",\n",
|
||||
" \"required_parts\": [\"replacement_\" + pred[\"component\"]]\n",
|
||||
" })\n",
|
||||
" return schedule"
|
||||
],
|
||||
"id": "2oMzDsp3TfJw"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# Define agents and tasks"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "-7qMHyY5YIph"
|
||||
},
|
||||
"id": "-7qMHyY5YIph"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "4ZYbYzjNTfJy"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"sensor_monitor = Agent(\"Sensor Monitor\", \"Data Collection\", \"Collect sensor data\", \"Monitor and collect sensor readings\", [collect_sensor_data])\n",
|
||||
"performance_analyzer = Agent(\"Performance Analyzer\", \"Performance Analysis\", \"Analyze equipment performance\", \"Analyze operational metrics\", [analyze_performance])\n",
|
||||
"anomaly_detector = Agent(\"Anomaly Detector\", \"Anomaly Detection\", \"Detect operational anomalies\", \"Identify abnormal patterns\", [detect_anomalies])\n",
|
||||
"failure_predictor = Agent(\"Failure Predictor\", \"Failure Prediction\", \"Predict potential failures\", \"Predict equipment failures\", [predict_failures])\n",
|
||||
"maintenance_scheduler = Agent(\"Maintenance Scheduler\", \"Maintenance Planning\", \"Schedule maintenance activities\", \"Plan and schedule maintenance\", [schedule_maintenance])\n",
|
||||
"\n",
|
||||
"sensor_task = Task(\"collect_data\", \"Collect sensor data\", \"Sensor readings\", sensor_monitor, is_start=True, next_tasks=[\"analyze_performance\"], async_execution=True)\n",
|
||||
"performance_task = Task(\"analyze_performance\", \"Analyze performance metrics\", \"Performance analysis\", performance_analyzer, next_tasks=[\"detect_anomalies\"], async_execution=True)\n",
|
||||
"anomaly_task = Task(\"detect_anomalies\", \"Detect operational anomalies\", \"Detected anomalies\", anomaly_detector, next_tasks=[\"predict_failures\"], context=[sensor_task, performance_task])\n",
|
||||
"prediction_task = Task(\"predict_failures\", \"Predict potential failures\", \"Failure predictions\", failure_predictor, next_tasks=[\"schedule_maintenance\"], task_type=\"decision\", condition={\"critical\": [\"schedule_maintenance\"], \"warning\": [\"schedule_maintenance\"], \"normal\": \"\"})\n",
|
||||
"scheduling_task = Task(\"schedule_maintenance\", \"Schedule maintenance activities\", \"Maintenance schedule\", maintenance_scheduler, context=[prediction_task])"
|
||||
],
|
||||
"id": "4ZYbYzjNTfJy"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# Create and run the workflow"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "XOdIxJKiYKuD"
|
||||
},
|
||||
"id": "XOdIxJKiYKuD"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "KYu_YTVDTfJz"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"workflow = PraisonAIAgents(\n",
|
||||
" agents=[sensor_monitor, performance_analyzer, anomaly_detector, failure_predictor, maintenance_scheduler],\n",
|
||||
" tasks=[sensor_task, performance_task, anomaly_task, prediction_task, scheduling_task],\n",
|
||||
" process=\"workflow\",\n",
|
||||
" verbose=True\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"async def main():\n",
|
||||
" print(\"\\nStarting Predictive Maintenance Workflow...\")\n",
|
||||
" print(\"=\" * 50)\n",
|
||||
" results = await workflow.astart()\n",
|
||||
" print(\"\\nMaintenance Planning Results:\")\n",
|
||||
" print(\"=\" * 50)\n",
|
||||
" for task_id, result in results[\"task_results\"].items():\n",
|
||||
" if result:\n",
|
||||
" print(f\"\\nTask: {task_id}\\nResult: {result.raw}\\n{'-'*50}\")\n",
|
||||
"\n",
|
||||
"await main()"
|
||||
],
|
||||
"id": "KYu_YTVDTfJz"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"print(\"\"\"\n",
|
||||
"[Starting Predictive Maintenance Workflow...\n",
|
||||
"==================================================\n",
|
||||
"╭─ Agent Info ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
|
||||
"│ │\n",
|
||||
"│ 👤 Agent: Sensor Monitor │\n",
|
||||
"│ Role: Data Collection │\n",
|
||||
"│ Tools: collect_sensor_data │\n",
|
||||
"│ │\n",
|
||||
"╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
|
||||
"\n",
|
||||
"╭─ Agent Info ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
|
||||
"│ │\n",
|
||||
"│ 👤 Agent: Performance Analyzer │\n",
|
||||
"│ Role: Performance Analysis │\n",
|
||||
"│ Tools: analyze_performance │\n",
|
||||
"│ │\n",
|
||||
"╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
|
||||
"\n",
|
||||
"[20:01:26] INFO [20:01:26] process.py:429 INFO Task schedule_maintenance has no next tasks, ending workflow process.py:429\n",
|
||||
"\n",
|
||||
"Maintenance Planning Results:\n",
|
||||
"==================================================\n",
|
||||
"\n",
|
||||
"Task: 0\n",
|
||||
"Result: The sensor readings you have collected are as follows:\n",
|
||||
"\n",
|
||||
"- **Temperature**: 86°F\n",
|
||||
"- **Vibration**: 0.6 (units not specified, but typically measured in g-forces or mm/s)\n",
|
||||
"- **Pressure**: 101 (units not specified, but typically measured in kPa or psi)\n",
|
||||
"- **Noise Level**: 81 dB\n",
|
||||
"\n",
|
||||
"Here's a brief analysis of these readings:\n",
|
||||
"\n",
|
||||
"1. **Temperature**: At 86°F, the temperature is relatively warm. Depending on the context (e.g., industrial equipment, environmental monitoring), this could be within normal operating conditions or might require cooling measures if it's above the optimal range.\n",
|
||||
"\n",
|
||||
"2. **Vibration**: A vibration level of 0.6 is generally low, but the significance depends on the type of equipment being monitored. For precision machinery, even small vibrations can be critical, whereas for more robust equipment, this might be negligible.\n",
|
||||
"\n",
|
||||
"3. **Pressure**: A pressure reading of 101 is often within normal ranges for many systems, but without specific units or context, it's hard to determine if this is optimal or requires adjustment.\n",
|
||||
"\n",
|
||||
"4. **Noise Level**: At 81 dB, the noise level is relatively high. Prolonged exposure to noise levels above 85 dB can be harmful to hearing, so if this is a workplace environment, it might be necessary to implement noise reduction measures or provide hearing protection.\n",
|
||||
"\n",
|
||||
"Overall, these readings should be compared against the specific operational thresholds and safety standards relevant to the equipment or environment being monitored. If any values are outside of acceptable ranges, further investigation or corrective actions may be needed.\n",
|
||||
"--------------------------------------------------\n",
|
||||
"\n",
|
||||
"Task: 1\n",
|
||||
"Result: Based on the provided operational metrics, here's an analysis of the equipment performance:\n",
|
||||
"\n",
|
||||
"1. **Efficiency (94%)**:\n",
|
||||
" - The equipment is operating at a high efficiency level, with 94% of the input being effectively converted into useful output. This suggests\n",
|
||||
"that the equipment is well-maintained and optimized for performance. However, there is still a 6% margin for improvement, which could be addressed by identifying and minimizing any inefficiencies in the process.\n",
|
||||
"\n",
|
||||
"2. **Uptime (99%)**:\n",
|
||||
" - The equipment has an excellent uptime rate of 99%, indicating that it is available and operational almost all the time. This is a strong indicator of reliability and suggests that downtime due to maintenance or unexpected failures is minimal. Maintaining this level of uptime should\n",
|
||||
"be a priority, as it directly impacts productivity and operational continuity.\n",
|
||||
"\n",
|
||||
"3. **Output Quality (94%)**:\n",
|
||||
" - The output quality is also at 94%, which is a positive sign that the equipment is producing high-quality products or results. However, similar to efficiency, there is room for improvement. Efforts could be made to identify any factors that might be affecting quality, such as calibration issues, material inconsistencies, or process deviations.\n",
|
||||
"\n",
|
||||
"**Overall Assessment**:\n",
|
||||
"The equipment is performing well across all key metrics, with high efficiency, uptime, and output quality. To further enhance performance, focus should be placed on fine-tuning processes to close the small gaps in efficiency and quality. Regular maintenance, monitoring, and process optimization can help sustain and potentially improve these metrics.\n",
|
||||
"--------------------------------------------------]\n",
|
||||
"\"\"\")"
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "4hrEJ5S6XpJ7",
|
||||
"outputId": "899e677d-19d5-4e0a-d9ab-ebc945aeee1b"
|
||||
},
|
||||
"id": "4hrEJ5S6XpJ7",
|
||||
"execution_count": 4,
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"name": "stdout",
|
||||
"text": [
|
||||
"\n",
|
||||
"[Starting Predictive Maintenance Workflow...\n",
|
||||
"==================================================\n",
|
||||
"╭─ Agent Info ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
|
||||
"│ │\n",
|
||||
"│ 👤 Agent: Sensor Monitor │\n",
|
||||
"│ Role: Data Collection │\n",
|
||||
"│ Tools: collect_sensor_data │\n",
|
||||
"│ │\n",
|
||||
"╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
|
||||
"\n",
|
||||
"╭─ Agent Info ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
|
||||
"│ │\n",
|
||||
"│ 👤 Agent: Performance Analyzer │\n",
|
||||
"│ Role: Performance Analysis │\n",
|
||||
"│ Tools: analyze_performance │\n",
|
||||
"│ │\n",
|
||||
"╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
|
||||
"\n",
|
||||
"[20:01:26] INFO [20:01:26] process.py:429 INFO Task schedule_maintenance has no next tasks, ending workflow process.py:429\n",
|
||||
"\n",
|
||||
"Maintenance Planning Results:\n",
|
||||
"==================================================\n",
|
||||
"\n",
|
||||
"Task: 0\n",
|
||||
"Result: The sensor readings you have collected are as follows:\n",
|
||||
"\n",
|
||||
"- **Temperature**: 86°F\n",
|
||||
"- **Vibration**: 0.6 (units not specified, but typically measured in g-forces or mm/s)\n",
|
||||
"- **Pressure**: 101 (units not specified, but typically measured in kPa or psi)\n",
|
||||
"- **Noise Level**: 81 dB\n",
|
||||
"\n",
|
||||
"Here's a brief analysis of these readings:\n",
|
||||
"\n",
|
||||
"1. **Temperature**: At 86°F, the temperature is relatively warm. Depending on the context (e.g., industrial equipment, environmental monitoring), this could be within normal operating conditions or might require cooling measures if it's above the optimal range.\n",
|
||||
"\n",
|
||||
"2. **Vibration**: A vibration level of 0.6 is generally low, but the significance depends on the type of equipment being monitored. For precision machinery, even small vibrations can be critical, whereas for more robust equipment, this might be negligible.\n",
|
||||
"\n",
|
||||
"3. **Pressure**: A pressure reading of 101 is often within normal ranges for many systems, but without specific units or context, it's hard to determine if this is optimal or requires adjustment.\n",
|
||||
"\n",
|
||||
"4. **Noise Level**: At 81 dB, the noise level is relatively high. Prolonged exposure to noise levels above 85 dB can be harmful to hearing, so if this is a workplace environment, it might be necessary to implement noise reduction measures or provide hearing protection.\n",
|
||||
"\n",
|
||||
"Overall, these readings should be compared against the specific operational thresholds and safety standards relevant to the equipment or environment being monitored. If any values are outside of acceptable ranges, further investigation or corrective actions may be needed.\n",
|
||||
"--------------------------------------------------\n",
|
||||
"\n",
|
||||
"Task: 1\n",
|
||||
"Result: Based on the provided operational metrics, here's an analysis of the equipment performance:\n",
|
||||
"\n",
|
||||
"1. **Efficiency (94%)**:\n",
|
||||
" - The equipment is operating at a high efficiency level, with 94% of the input being effectively converted into useful output. This suggests \n",
|
||||
"that the equipment is well-maintained and optimized for performance. However, there is still a 6% margin for improvement, which could be addressed by identifying and minimizing any inefficiencies in the process.\n",
|
||||
"\n",
|
||||
"2. **Uptime (99%)**:\n",
|
||||
" - The equipment has an excellent uptime rate of 99%, indicating that it is available and operational almost all the time. This is a strong indicator of reliability and suggests that downtime due to maintenance or unexpected failures is minimal. Maintaining this level of uptime should \n",
|
||||
"be a priority, as it directly impacts productivity and operational continuity.\n",
|
||||
"\n",
|
||||
"3. **Output Quality (94%)**:\n",
|
||||
" - The output quality is also at 94%, which is a positive sign that the equipment is producing high-quality products or results. However, similar to efficiency, there is room for improvement. Efforts could be made to identify any factors that might be affecting quality, such as calibration issues, material inconsistencies, or process deviations.\n",
|
||||
"\n",
|
||||
"**Overall Assessment**:\n",
|
||||
"The equipment is performing well across all key metrics, with high efficiency, uptime, and output quality. To further enhance performance, focus should be placed on fine-tuning processes to close the small gaps in efficiency and quality. Regular maintenance, monitoring, and process optimization can help sustain and potentially improve these metrics.\n",
|
||||
"--------------------------------------------------]\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python",
|
||||
"version": "3.9"
|
||||
},
|
||||
"colab": {
|
||||
"provenance": []
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -0,0 +1,420 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "MLutIRWTYLqq",
|
||||
"metadata": {
|
||||
"id": "MLutIRWTYLqq"
|
||||
},
|
||||
"source": [
|
||||
"# Simple Chat with Qwen2.5 (0.5B) using Hugging Face Transformers"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a2LoeTPnYQOW",
|
||||
"metadata": {
|
||||
"id": "a2LoeTPnYQOW"
|
||||
},
|
||||
"source": [
|
||||
"This beginner-friendly notebook demonstrates how to use the Qwen2.5-0.5B-Instruct model for simple chat-based generation. It includes installing dependencies, setting up authentication via Hugging Face token, generating a response using a chat prompt, and displaying the output. The notebook is organized into 5 clean sections for easy understanding."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "WlfJBFucY9gi",
|
||||
"metadata": {
|
||||
"id": "WlfJBFucY9gi"
|
||||
},
|
||||
"source": [
|
||||
"[](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/Qwen2_5_InstructionAgent.ipynb)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "57059c85",
|
||||
"metadata": {
|
||||
"id": "57059c85"
|
||||
},
|
||||
"source": [
|
||||
"# 🔧 Dependencies\n",
|
||||
"Install necessary Python packages."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "1cdaae47",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "1cdaae47",
|
||||
"outputId": "5b140b09-5c16-4849-f370-f8b5abcc127a"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m363.4/363.4 MB\u001b[0m \u001b[31m3.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.8/13.8 MB\u001b[0m \u001b[31m58.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m24.6/24.6 MB\u001b[0m \u001b[31m58.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m883.7/883.7 kB\u001b[0m \u001b[31m39.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m664.8/664.8 MB\u001b[0m \u001b[31m1.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m211.5/211.5 MB\u001b[0m \u001b[31m6.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m56.3/56.3 MB\u001b[0m \u001b[31m14.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m127.9/127.9 MB\u001b[0m \u001b[31m9.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m207.5/207.5 MB\u001b[0m \u001b[31m6.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m21.1/21.1 MB\u001b[0m \u001b[31m64.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25h"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!pip install transformers --quiet\n",
|
||||
"!pip install accelerate --quiet\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9a65a2fa",
|
||||
"metadata": {
|
||||
"id": "9a65a2fa"
|
||||
},
|
||||
"source": [
|
||||
"# 🛠️ Tools\n",
|
||||
"Import required tools and libraries."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "a8a4bc30",
|
||||
"metadata": {
|
||||
"id": "a8a4bc30"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from transformers import AutoModelForCausalLM, AutoTokenizer\n",
|
||||
"import torch\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1ad2bec4",
|
||||
"metadata": {
|
||||
"id": "1ad2bec4"
|
||||
},
|
||||
"source": [
|
||||
"# ⚙️ YAML Prompt (Token Authentication)\n",
|
||||
"Use Hugging Face token to access the model."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "cd030b94",
|
||||
"metadata": {
|
||||
"id": "cd030b94"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from huggingface_hub import login\n",
|
||||
"login(token=\"Enter your huggingface token\")\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b7b1a32d",
|
||||
"metadata": {
|
||||
"id": "b7b1a32d"
|
||||
},
|
||||
"source": [
|
||||
"# 🚀 Main\n",
|
||||
"Load model, prepare prompt and generate response."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "ef73b9ea",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/",
|
||||
"height": 365,
|
||||
"referenced_widgets": [
|
||||
"a175e2b1fa8c4733afe1ddc78095be58",
|
||||
"f5aaf63d7be04927a33e5cdd3655a003",
|
||||
"7af74b8c184b4e72a2dba769b9caa40f",
|
||||
"22b1c604d91648479cf01198db25a863",
|
||||
"faaeb1c3ab914a6db9ff9b8ea06a440f",
|
||||
"138860a12c6347fba11437eb098d31b5",
|
||||
"d2f5fe459d544fa6adc92debab823ee6",
|
||||
"468e8e7bee5c4198b121ac1aa677153d",
|
||||
"068991c9ac44496bab06ccaa7d030722",
|
||||
"44436151662f41ef81f6a4005add51de",
|
||||
"c774c78c4fce4825872a76bb1e5fde33",
|
||||
"4a7d0a84994c4b628974f631ecb95425",
|
||||
"4adb654ea143456eb3b4244c3030b39f",
|
||||
"d8dbdbc9327f4799a07767b59db59c47",
|
||||
"669ea95c4cb642b580aa176ae2e89f9f",
|
||||
"0bc8529b03364f83adf1110b5c1378ec",
|
||||
"2eb8007145ef4c3caf7f5a9d1fd299e3",
|
||||
"3fa4c368cea4474e8b97a215a20fd8d8",
|
||||
"c750b36cfda24e24ae44a615e426de4b",
|
||||
"e3001e1e3ba74c7ba28702fa5b2a6ed8",
|
||||
"7ac0f7770ad341f68f194705f59ccb27",
|
||||
"f9140e1979c6412993eee36c3f79c0d9",
|
||||
"1ad8e2e2200c4277ba10dd8e9ac83b90",
|
||||
"f193e4fb00b64e86b6f40f10f514739d",
|
||||
"ae3aa36b649a43eb928ed843d2cfc239",
|
||||
"c346fba7badb4c4596f6d6e8fa62224b",
|
||||
"39dd29002d7b46848a29dfeb7405d9f9",
|
||||
"166e17a0be5a4aa8831101eeb07a8645",
|
||||
"60f9503b404945a3b99b5207190474f5",
|
||||
"c480a676b82b48a0aae3dc438fa02ba8",
|
||||
"d97b7d0765e84926b974b2c19156ae20",
|
||||
"b22ca5c038f7419aa8e03f5f9a54accc",
|
||||
"0387fba7ed044fbfb2a78c8d593c7b38",
|
||||
"d6a31c3cf026450088596db5917c3e4a",
|
||||
"4948c0014a95413b81848ae2b4a5fe7e",
|
||||
"377cbca7363a495ea131a1e49a702168",
|
||||
"a5eaad3284034beab09d212765d7ff78",
|
||||
"7ed393b7c7564ab1897446eba875a4da",
|
||||
"66d773bc8c7a40bc95dcba35052675cb",
|
||||
"ad196b0deb2144b083cf8fddcbcfd147",
|
||||
"09a99df922154a528f327bba0a47cb9a",
|
||||
"bc9918e686244eb5b5db78932600a9d6",
|
||||
"4fef54176f4b4a2d8d543a4bf04b416a",
|
||||
"48551fb8dd5140cbb2d792e72b5fbb0d",
|
||||
"955225e4206a4ee5a4284579935db920",
|
||||
"e2faef80a0034c5384daf8ba66c1b75d",
|
||||
"f27c3ec92bc74bacb3f86672b689370a",
|
||||
"ec03dfb79a26474e812606b2664c931b",
|
||||
"4705d2f502a74c3bb8eb8a06c3ba6889",
|
||||
"7d8c99f622c34229894024c9772aed84",
|
||||
"1e6e892b153c4c05b7de806ec674215f",
|
||||
"85ac6a67bb0944b5bb8713fcf302da45",
|
||||
"796771b887ce49119307725382f73bfd",
|
||||
"c686586c431f4c22b0f0c698a9b213b6",
|
||||
"239180fd12624e3a90188270b55ee218",
|
||||
"e04653090c7e454d9c5e520e7f313588",
|
||||
"992c7a4b44aa4feb8e13714fa922d705",
|
||||
"a0a238c78d2842d680be448a2cb20157",
|
||||
"6ab5ebed63674b5e87327a1623eba7f0",
|
||||
"a49887aa0fcd4b91a0d5d978b29c1e0a",
|
||||
"d9b2e4142dd14264b90829a5a4116613",
|
||||
"154b27bb2ba44d10bde5c96929b65cc9",
|
||||
"f0bf6206c31f49f2ad1ce97b99963011",
|
||||
"1762735cfbcf4706b1c0686d3c2926a0",
|
||||
"d5e3440785794f0f9ad4fd0650c2f682",
|
||||
"08019bdd70364fabbc222dca66db0bb1",
|
||||
"6f0b3a92d47c42dd815a670bb1c0f42c",
|
||||
"54554545ad5a4f39b649f3acff6d5cba",
|
||||
"bfd8f4ee8c414349a3695975e79c332e",
|
||||
"332fc944eef14a9c82f71a68a3857dea",
|
||||
"3b0b899fcfae451ba8ec3d8da176fcec",
|
||||
"8383df3f07bd427287ad05726737cc67",
|
||||
"67c267d7d1ec424593775bf8129e8ac9",
|
||||
"85a50e2cb406403988639e6d6ecf9dc1",
|
||||
"7251ed20382c4a60bae8549f68971043",
|
||||
"44fbc377fd6a4c5a910c00dcceb143af",
|
||||
"2d6d273941f04c13af598c53e067aae7"
|
||||
]
|
||||
},
|
||||
"id": "ef73b9ea",
|
||||
"outputId": "34512fa7-527c-45cb-f3c7-b42d37e74980"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_auth.py:94: UserWarning: \n",
|
||||
"The secret `HF_TOKEN` does not exist in your Colab secrets.\n",
|
||||
"To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n",
|
||||
"You will be able to reuse this secret in all of your notebooks.\n",
|
||||
"Please note that authentication is recommended but still optional to access public models or datasets.\n",
|
||||
" warnings.warn(\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "a175e2b1fa8c4733afe1ddc78095be58",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"tokenizer_config.json: 0%| | 0.00/7.30k [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "4a7d0a84994c4b628974f631ecb95425",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"vocab.json: 0%| | 0.00/2.78M [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "1ad8e2e2200c4277ba10dd8e9ac83b90",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"merges.txt: 0%| | 0.00/1.67M [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "d6a31c3cf026450088596db5917c3e4a",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"tokenizer.json: 0%| | 0.00/7.03M [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "955225e4206a4ee5a4284579935db920",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"config.json: 0%| | 0.00/659 [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "e04653090c7e454d9c5e520e7f313588",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"model.safetensors: 0%| | 0.00/988M [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "6f0b3a92d47c42dd815a670bb1c0f42c",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"generation_config.json: 0%| | 0.00/242 [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model_name = \"Qwen/Qwen2.5-0.5B-Instruct\"\n",
|
||||
"\n",
|
||||
"tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
|
||||
"model = AutoModelForCausalLM.from_pretrained(\n",
|
||||
" model_name,\n",
|
||||
" torch_dtype=\"auto\",\n",
|
||||
" device_map=\"auto\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"messages = [\n",
|
||||
" {\"role\": \"system\", \"content\": \"You are Qwen, a helpful assistant.\"},\n",
|
||||
" {\"role\": \"user\", \"content\": \"Explain photosynthesis in simple words.\"}\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
|
||||
"inputs = tokenizer(text, return_tensors=\"pt\").to(model.device)\n",
|
||||
"\n",
|
||||
"outputs = model.generate(**inputs, max_new_tokens=100)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "bf70c765",
|
||||
"metadata": {
|
||||
"id": "bf70c765"
|
||||
},
|
||||
"source": [
|
||||
"# 📤 Output\n",
|
||||
"Print the model response."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "d8f70a7b",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "d8f70a7b",
|
||||
"outputId": "559e9358-c2b7-4410-c1a6-1624640dfeae"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"system\n",
|
||||
"You are Qwen, a helpful assistant.\n",
|
||||
"user\n",
|
||||
"Explain photosynthesis in simple words.\n",
|
||||
"assistant\n",
|
||||
"Photosynthesis is the process by which plants, algae, and some bacteria convert light energy from the sun into chemical energy stored in glucose molecules, using carbon dioxide and water as reactants. This process occurs in chloroplasts within plant cells and involves two main stages: the light-dependent reactions and the light-independent reactions (also known as the Calvin cycle).\n",
|
||||
"\n",
|
||||
"In the light-dependent reactions, light energy is captured by pigments like chlorophyll, causing electrons to be excited and transferred to oxygen through a series\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"response = tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
|
||||
"print(response)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3622c575",
|
||||
"metadata": {
|
||||
"id": "3622c575"
|
||||
},
|
||||
"source": [
|
||||
"# TinyLlama Agent Demo\n",
|
||||
"\n",
|
||||
"This notebook demonstrates how to create a simple AI agent using the TinyLlama (1.1B) model.\n",
|
||||
"\n",
|
||||
"**Requirements:**\n",
|
||||
"- `transformers`\n",
|
||||
"- `accelerate`\n",
|
||||
"- `torch`\n",
|
||||
"\n",
|
||||
"We will load the model, prepare a basic conversational interface, and test it with a simple prompt."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "qS0M9pRhTGpf",
|
||||
"metadata": {
|
||||
"id": "qS0M9pRhTGpf"
|
||||
},
|
||||
"source": [
|
||||
"[](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/TinyLlama_1_1B_model_SimpleAIAgent.ipynb)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "tuw0IDpBS0ei",
|
||||
"metadata": {
|
||||
"id": "tuw0IDpBS0ei"
|
||||
},
|
||||
"source": [
|
||||
"# Install necessary packages"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "0d06cae9",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "0d06cae9",
|
||||
"outputId": "3e1d8fb7-19be-455b-dc35-d0ab19146669"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m363.4/363.4 MB\u001b[0m \u001b[31m3.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.8/13.8 MB\u001b[0m \u001b[31m29.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m24.6/24.6 MB\u001b[0m \u001b[31m29.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m883.7/883.7 kB\u001b[0m \u001b[31m20.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m664.8/664.8 MB\u001b[0m \u001b[31m2.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m211.5/211.5 MB\u001b[0m \u001b[31m5.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m56.3/56.3 MB\u001b[0m \u001b[31m12.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m127.9/127.9 MB\u001b[0m \u001b[31m7.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m207.5/207.5 MB\u001b[0m \u001b[31m6.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m21.1/21.1 MB\u001b[0m \u001b[31m45.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25h"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!pip install -q transformers accelerate torch"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "RFhYLkO1S5F9",
|
||||
"metadata": {
|
||||
"id": "RFhYLkO1S5F9"
|
||||
},
|
||||
"source": [
|
||||
"# Load TinyLlama model and tokenizer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "415af398",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/",
|
||||
"height": 241,
|
||||
"referenced_widgets": [
|
||||
"dae7d635257948149c74e65956f614e3",
|
||||
"17ec6fcc9f624cdbb62fe2b8b55230d0",
|
||||
"58474504488044138a0252bb75258d86",
|
||||
"04fe4518fac04b9f8b29c62ed70f1dfd",
|
||||
"c6a9d3ea25594170977243d2048d3408",
|
||||
"730bebf00be142d3979c6d12b55d6757",
|
||||
"03ad1674c0884e7b9c4a21af0a1e66c3",
|
||||
"ebac47c887f04b79a60bd0918002bf0d",
|
||||
"ece9bce5d2ca4dd7b019040175513118",
|
||||
"458f11dbadb045f1aa8c72780e7aa2a0",
|
||||
"ec28a20d0a14445489723684d2d69c0a",
|
||||
"d347fa0c0fc849559b66c8f116c86317",
|
||||
"117f649fa62b4f5d9add0160e6cd80a4",
|
||||
"66f702418fa348eea27778282dc443aa",
|
||||
"44413339df5b4ae688f8101faf9ed3b5",
|
||||
"00cc576df95441c8916ce38a7c6decc2",
|
||||
"22e9ea6d191841bf9a5a5072326aa6d8",
|
||||
"bd94a291b3164113b5ea460b38b8c167",
|
||||
"9d132a297f924a3d867605df81e70d8e",
|
||||
"5345feddca204db784ea0452e739eda6",
|
||||
"ace33e6e88d842c89efd180b3a83f674",
|
||||
"61dd85a7edf544f196c60a259a91fa0d",
|
||||
"7451ec9bd091441c852d075b47bd1ca5",
|
||||
"05e3446e7c9243819a25d1ca8e8fa38e",
|
||||
"73141c4c9218462da97999d3afa459bc",
|
||||
"1cffc33252b94fbc861edb0953f13153",
|
||||
"de917c0fc49040c8a9254333c34c884a",
|
||||
"50962ececc0b42c28170eb0c23fd6171",
|
||||
"856cc834a33a4f9fb59c77f748006b36",
|
||||
"17fd1d476c1f4891b8cbeb8cacdeb866",
|
||||
"c97dc50f42cb44b8a71aeff5272d5337",
|
||||
"2e3a66f622d445c4be544a9306798cb6",
|
||||
"92920b1d428a4ed68a9e0329e2034cad",
|
||||
"7e282c1fcae842cc87c93ecfc1763654",
|
||||
"f9ed70f887204e2fb336e38a35096eec",
|
||||
"a4d99f9beca44e4a8fadff65b27aa78f",
|
||||
"b4f77b96d00445eea77ac78baa8c1e99",
|
||||
"695898e4a4c8476cb318446a6da592ba",
|
||||
"d70bcacbe77c41f685a4f8020f767889",
|
||||
"af49afc49ecf47fb9ebf2d299954eff2",
|
||||
"1bd2a253348f4b45928c875940f72481",
|
||||
"b6c5f55fc85b4331a2e749d27ce15f89",
|
||||
"81a87638c71046d7aeb1c642558602f7",
|
||||
"e750883103444391bd64872344c651d7",
|
||||
"e78dca0cf1314d01a5d5126e448e9d87",
|
||||
"d0c53dffc0c34d7090febf78231972b2",
|
||||
"152a118177914c6692e7ee4987b92b9b",
|
||||
"88fd71fd6dbf43ebb0a3b8471fd35f8d",
|
||||
"ddb91266f8834e4eaaf136cc15f4a831",
|
||||
"9f8179d135054eb292c9adc7fd66a91a",
|
||||
"297d5bc8b3a04f26b80e2cc5ee3f7bbd",
|
||||
"f49f72679cb944cd8fce5557bbd68b1b",
|
||||
"2ee28c8ef4f74690b80a89050da4ebb5",
|
||||
"c29924d6561a4feaa9238d1b34394941",
|
||||
"03636146b7c14f5db80b8f105be55303",
|
||||
"b391081de4444859ac3213aac99cc33a",
|
||||
"2babb83499fc46dc8422473f7b98403c",
|
||||
"a32f6cc2777941bca5b6250e36624ead",
|
||||
"90cd9996184a452ab651f9b8cc590f47",
|
||||
"dc04209c92764096ba9bd4aefa81c189",
|
||||
"16e5b93c3c964a8a8ca460d9077d0638",
|
||||
"794f22ded07d46c5a916325109618987",
|
||||
"dce367f1a5a74ebf94aea37209aa0a40",
|
||||
"5e355e3f340f4a7590e4a88d44b03383",
|
||||
"4e725e19c877462aa3c10726a335bbde",
|
||||
"d60ab6c255d244098a459459016a2089",
|
||||
"f1b07183320d42af88f985bd1abf054f",
|
||||
"d23103bb4b354e29a2c3e7dc93867b36",
|
||||
"ea3e0ae0aef54e5da113c9686ebbbab9",
|
||||
"4d3365c1bb7740dc8f36f4b419af135a",
|
||||
"c06ccbc051264027bd2c6521fec69abe",
|
||||
"597dbf5b28134cc0bf9d613443243455",
|
||||
"faa4f285eb224bbcbed649bd479e571f",
|
||||
"6e684d35885a4706b8add6834cd521ad",
|
||||
"f9c55ada3386460183e02559defec8a4",
|
||||
"9d64cbe1605f4f358923754c5f8b6424",
|
||||
"6008ed7eca09485195eb37c36acfbe79"
|
||||
]
|
||||
},
|
||||
"id": "415af398",
|
||||
"outputId": "28de1886-e83f-49bf-b05f-cd19439605c5"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "dae7d635257948149c74e65956f614e3",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"tokenizer_config.json: 0%| | 0.00/1.29k [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "d347fa0c0fc849559b66c8f116c86317",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"tokenizer.model: 0%| | 0.00/500k [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "7451ec9bd091441c852d075b47bd1ca5",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"tokenizer.json: 0%| | 0.00/1.84M [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "7e282c1fcae842cc87c93ecfc1763654",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"special_tokens_map.json: 0%| | 0.00/551 [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "e78dca0cf1314d01a5d5126e448e9d87",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"config.json: 0%| | 0.00/608 [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "b391081de4444859ac3213aac99cc33a",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"model.safetensors: 0%| | 0.00/2.20G [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"application/vnd.jupyter.widget-view+json": {
|
||||
"model_id": "f1b07183320d42af88f985bd1abf054f",
|
||||
"version_major": 2,
|
||||
"version_minor": 0
|
||||
},
|
||||
"text/plain": [
|
||||
"generation_config.json: 0%| | 0.00/124 [00:00<?, ?B/s]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from transformers import AutoTokenizer, AutoModelForCausalLM\n",
|
||||
"import torch\n",
|
||||
"\n",
|
||||
"model_name = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n",
|
||||
"tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
|
||||
"model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map=\"auto\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "N0bRTd_DS8aL",
|
||||
"metadata": {
|
||||
"id": "N0bRTd_DS8aL"
|
||||
},
|
||||
"source": [
|
||||
"# Define a function to generate responses from the agent"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "1b0483e1",
|
||||
"metadata": {
|
||||
"id": "1b0483e1"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def generate_response(prompt, max_length=256):\n",
|
||||
" inputs = tokenizer(prompt, return_tensors=\"pt\").to(model.device)\n",
|
||||
" outputs = model.generate(**inputs, max_new_tokens=max_length)\n",
|
||||
" return tokenizer.decode(outputs[0], skip_special_tokens=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "vbHKNxc-S_Bk",
|
||||
"metadata": {
|
||||
"id": "vbHKNxc-S_Bk"
|
||||
},
|
||||
"source": [
|
||||
"# Example usage"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "298c9811",
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "298c9811",
|
||||
"outputId": "6e412608-735c-4604-8a5d-a66b98dd9df0"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"You are a helpful assistant. What is the capital of France?\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"prompt = \"You are a helpful assistant. What is the capital of France?\"\n",
|
||||
"response = generate_response(prompt)\n",
|
||||
"print(response)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python",
|
||||
"version": "3.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,170 @@
|
||||
# Remote Agent Connectivity
|
||||
|
||||
This directory contains examples demonstrating PraisonAI's remote agent connectivity feature, which allows you to connect to agents running on different servers, similar to Google ADK's agent connectivity pattern.
|
||||
|
||||
## Overview
|
||||
|
||||
The remote agent connectivity feature enables:
|
||||
- **Direct agent-to-agent connectivity** across networks
|
||||
- **Session-based communication** with remote agents
|
||||
- **Google ADK-like API patterns** for familiar usage
|
||||
- **Automatic connection management** and error handling
|
||||
|
||||
## Quick Start
|
||||
|
||||
### 1. Start a Remote Agent Server
|
||||
|
||||
```bash
|
||||
# Start a simple remote agent server
|
||||
python remote_agent_server.py
|
||||
|
||||
# Or start multiple agents on one server
|
||||
python remote_agent_server.py --multiple
|
||||
```
|
||||
|
||||
### 2. Connect from Client
|
||||
|
||||
```python
|
||||
from praisonaiagents import Session
|
||||
|
||||
# Connect to remote agent (similar to Google ADK)
|
||||
session = Session(agent_url="192.168.1.10:8000/agent")
|
||||
|
||||
# Send messages to the remote agent
|
||||
response = session.chat("Hello from the client!")
|
||||
print(response)
|
||||
|
||||
# Alternative method (Google ADK pattern)
|
||||
response = session.send_message("What can you help me with?")
|
||||
print(response)
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
- **`remote_agent_server.py`** - Set up agents as remote servers
|
||||
- **`remote_agent_example.py`** - Connect to and communicate with remote agents
|
||||
|
||||
## API Comparison
|
||||
|
||||
### Google ADK Pattern
|
||||
```python
|
||||
from adk.agent import Agent
|
||||
from adk.session import Session
|
||||
|
||||
session = Session(agent_url="192.168.1.10:8000")
|
||||
response = session.send_message("Hello!")
|
||||
```
|
||||
|
||||
### PraisonAI Pattern
|
||||
```python
|
||||
from praisonaiagents import Session
|
||||
|
||||
session = Session(agent_url="192.168.1.10:8000/agent")
|
||||
response = session.chat("Hello!")
|
||||
# OR
|
||||
response = session.send_message("Hello!") # Google ADK compatibility
|
||||
```
|
||||
|
||||
## Key Features
|
||||
|
||||
### ✅ **Seamless Connectivity**
|
||||
- Automatic protocol detection (http/https)
|
||||
- Connection testing and validation
|
||||
- Configurable timeouts
|
||||
|
||||
### ✅ **Error Handling**
|
||||
- Clear error messages for connection issues
|
||||
- Graceful handling of network problems
|
||||
- Appropriate fallbacks for unavailable services
|
||||
|
||||
### ✅ **Backward Compatibility**
|
||||
- Existing local agent functionality unchanged
|
||||
- Session API works for both local and remote scenarios
|
||||
- No breaking changes to current code
|
||||
|
||||
### ✅ **Flexible Configuration**
|
||||
- Support for IP:port format (`192.168.1.10:8000`)
|
||||
- Full URL format (`http://server.com:8000/agent`)
|
||||
- Customizable endpoints and paths
|
||||
|
||||
## Usage Patterns
|
||||
|
||||
### Single Remote Agent
|
||||
```python
|
||||
# Connect to a specific remote agent
|
||||
session = Session(agent_url="192.168.1.10:8000/agent")
|
||||
response = session.chat("Hello from remote client!")
|
||||
```
|
||||
|
||||
### Multiple Remote Agents
|
||||
```python
|
||||
# Connect to different specialized agents
|
||||
research_session = Session(agent_url="192.168.1.10:8000/research")
|
||||
coding_session = Session(agent_url="192.168.1.10:8000/coding")
|
||||
|
||||
research_response = research_session.chat("Find info about AI trends")
|
||||
coding_response = coding_session.chat("Help me debug this Python code")
|
||||
```
|
||||
|
||||
### Error Handling
|
||||
```python
|
||||
try:
|
||||
session = Session(agent_url="192.168.1.10:8000/agent")
|
||||
response = session.chat("Test message")
|
||||
print(f"Response: {response}")
|
||||
except ConnectionError as e:
|
||||
print(f"Failed to connect: {e}")
|
||||
```
|
||||
|
||||
## Network Configuration
|
||||
|
||||
### Server Setup
|
||||
```python
|
||||
# Create and launch agent server
|
||||
agent = Agent(name="RemoteAgent", role="Assistant")
|
||||
agent.launch(path="/agent", port=8000, host="0.0.0.0")
|
||||
```
|
||||
|
||||
### Client Connection
|
||||
```python
|
||||
# Connect from any network location
|
||||
session = Session(agent_url="<server-ip>:8000/agent")
|
||||
```
|
||||
|
||||
## Limitations for Remote Sessions
|
||||
|
||||
When using remote agent sessions, certain local operations are not available:
|
||||
- **Memory operations** (`session.memory.*`)
|
||||
- **Knowledge operations** (`session.knowledge.*`)
|
||||
- **Local agent creation** (`session.Agent(...)`)
|
||||
- **State management** (`session.save_state/restore_state`)
|
||||
|
||||
This design keeps remote and local operations clearly separated.
|
||||
|
||||
## Testing
|
||||
|
||||
Run the examples to test the functionality:
|
||||
|
||||
```bash
|
||||
# Terminal 1: Start server
|
||||
python remote_agent_server.py
|
||||
|
||||
# Terminal 2: Test client (update IP as needed)
|
||||
python remote_agent_example.py
|
||||
```
|
||||
|
||||
## Integration with Existing Code
|
||||
|
||||
The remote connectivity feature integrates seamlessly with existing PraisonAI code:
|
||||
|
||||
```python
|
||||
# Existing local code works unchanged
|
||||
local_session = Session(session_id="local_chat")
|
||||
local_agent = local_session.Agent(name="LocalAgent", role="Assistant")
|
||||
|
||||
# New remote connectivity
|
||||
remote_session = Session(agent_url="remote-server:8000/agent")
|
||||
remote_response = remote_session.chat("Hello from remote!")
|
||||
```
|
||||
|
||||
This provides a smooth migration path for applications that need to scale across multiple servers.
|
||||
@@ -0,0 +1,141 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Remote Agent Connectivity Example
|
||||
|
||||
This example demonstrates how to connect to agents running on remote servers,
|
||||
similar to Google ADK's agent connectivity pattern.
|
||||
|
||||
Usage:
|
||||
1. First, start a remote agent server:
|
||||
python remote_agent_server.py
|
||||
|
||||
2. Then run this client to connect to it:
|
||||
python remote_agent_example.py
|
||||
"""
|
||||
|
||||
from praisonaiagents import Session
|
||||
import time
|
||||
|
||||
def example_remote_agent_connection():
|
||||
"""
|
||||
Example demonstrating remote agent connectivity similar to Google ADK.
|
||||
"""
|
||||
print("🌐 Remote Agent Connectivity Example")
|
||||
print("=" * 50)
|
||||
|
||||
# Example 1: Connect to remote agent using IP:port format
|
||||
try:
|
||||
print("\n1. Connecting to remote agent at 192.168.1.10:8000...")
|
||||
|
||||
# Create a session with remote agent URL (similar to Google ADK)
|
||||
session = Session(agent_url="192.168.1.10:8000/agent")
|
||||
|
||||
print(f"✅ Connected! Session: {session}")
|
||||
|
||||
# Send a message to the remote agent
|
||||
response = session.chat("Hello from the client!")
|
||||
print(f"📨 Sent: 'Hello from the client!'")
|
||||
print(f"📨 Received: {response}")
|
||||
|
||||
# Send another message using the alias method (Google ADK pattern)
|
||||
response = session.send_message("What can you help me with?")
|
||||
print(f"📨 Sent: 'What can you help me with?'")
|
||||
print(f"📨 Received: {response}")
|
||||
|
||||
except ConnectionError as e:
|
||||
print(f"❌ Connection failed: {e}")
|
||||
print("💡 Make sure the remote agent server is running at 192.168.1.10:8000")
|
||||
|
||||
# Example 2: Connect to localhost for testing
|
||||
try:
|
||||
print("\n2. Connecting to localhost for testing...")
|
||||
|
||||
# Connect to local server (for testing)
|
||||
session = Session(agent_url="localhost:8000/agent")
|
||||
|
||||
print(f"✅ Connected! Session: {session}")
|
||||
|
||||
# Test the connection
|
||||
response = session.chat("Test message")
|
||||
print(f"📨 Response: {response}")
|
||||
|
||||
except ConnectionError as e:
|
||||
print(f"❌ Connection failed: {e}")
|
||||
print("💡 Start a local agent server first with: python remote_agent_server.py")
|
||||
|
||||
def example_error_handling():
|
||||
"""
|
||||
Example demonstrating error handling for remote sessions.
|
||||
"""
|
||||
print("\n\n🚨 Error Handling Examples")
|
||||
print("=" * 50)
|
||||
|
||||
try:
|
||||
# Create a remote session
|
||||
session = Session(agent_url="localhost:8000/agent")
|
||||
|
||||
# These operations are not available for remote sessions
|
||||
print("\n❌ Attempting to access memory (should fail)...")
|
||||
session.memory.store_short_term("test")
|
||||
|
||||
except ConnectionError:
|
||||
print("⚠️ Cannot connect to remote agent (expected if server not running)")
|
||||
except ValueError as e:
|
||||
print(f"✅ Correctly caught error: {e}")
|
||||
|
||||
try:
|
||||
session = Session(agent_url="localhost:8000/agent")
|
||||
print("\n❌ Attempting to create local agent (should fail)...")
|
||||
agent = session.Agent(name="Test", role="Assistant")
|
||||
|
||||
except ConnectionError:
|
||||
print("⚠️ Cannot connect to remote agent (expected if server not running)")
|
||||
except ValueError as e:
|
||||
print(f"✅ Correctly caught error: {e}")
|
||||
|
||||
def comparison_with_google_adk():
|
||||
"""
|
||||
Shows the comparison between Google ADK and PraisonAI patterns.
|
||||
"""
|
||||
print("\n\n🔄 Comparison with Google ADK")
|
||||
print("=" * 50)
|
||||
|
||||
print("\n📋 Google ADK Pattern:")
|
||||
print("""
|
||||
from adk.agent import Agent
|
||||
from adk.session import Session
|
||||
|
||||
# Connect to remote agent
|
||||
session = Session(agent_url="192.168.1.10:8000")
|
||||
response = session.send_message("Hello from client!")
|
||||
""")
|
||||
|
||||
print("📋 PraisonAI Pattern:")
|
||||
print("""
|
||||
from praisonaiagents import Session
|
||||
|
||||
# Connect to remote agent
|
||||
session = Session(agent_url="192.168.1.10:8000/agent")
|
||||
response = session.chat("Hello from client!")
|
||||
# OR
|
||||
response = session.send_message("Hello from client!")
|
||||
""")
|
||||
|
||||
print("✨ Key similarities:")
|
||||
print("• Direct agent URL connectivity")
|
||||
print("• Simple session-based API")
|
||||
print("• Similar message sending patterns")
|
||||
print("• Error handling for connection issues")
|
||||
|
||||
if __name__ == "__main__":
|
||||
example_remote_agent_connection()
|
||||
example_error_handling()
|
||||
comparison_with_google_adk()
|
||||
|
||||
print("\n\n🎯 Summary")
|
||||
print("=" * 50)
|
||||
print("✅ Remote agent connectivity implemented")
|
||||
print("✅ Google ADK-like API pattern supported")
|
||||
print("✅ Backward compatibility maintained")
|
||||
print("✅ Proper error handling for remote sessions")
|
||||
print("\n💡 To test with a real server, run remote_agent_server.py first!")
|
||||
@@ -0,0 +1,103 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Remote Agent Server Example
|
||||
|
||||
This example demonstrates how to set up an agent as a remote server
|
||||
that can be accessed by other clients using the Session(agent_url=...) pattern.
|
||||
|
||||
Usage:
|
||||
python remote_agent_server.py
|
||||
|
||||
Then connect from another machine/process:
|
||||
python remote_agent_example.py
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
import time
|
||||
|
||||
def setup_remote_agent_server():
|
||||
"""
|
||||
Set up an agent as a remote server accessible via HTTP API.
|
||||
"""
|
||||
print("🚀 Setting up Remote Agent Server")
|
||||
print("=" * 50)
|
||||
|
||||
# Create an agent that will serve as the remote agent
|
||||
agent = Agent(
|
||||
name="RemoteAssistant",
|
||||
role="Helpful Remote AI Assistant",
|
||||
goal="Assist users with their queries via remote connections",
|
||||
backstory="I am an AI assistant running on a remote server, ready to help clients connect and interact with me.",
|
||||
llm="gpt-4o-mini", # or any other supported model
|
||||
verbose=True
|
||||
)
|
||||
|
||||
print(f"📝 Created agent: {agent}")
|
||||
print(f"🔧 Agent details:")
|
||||
print(f" • Name: {agent.name}")
|
||||
print(f" • Role: {agent.role}")
|
||||
print(f" • Goal: {agent.goal}")
|
||||
|
||||
# Launch the agent as an HTTP API server
|
||||
print(f"\n🌐 Launching agent server...")
|
||||
print(f"📡 Server will be accessible at:")
|
||||
print(f" • Local: http://localhost:8000/agent")
|
||||
print(f" • Network: http://<your-ip>:8000/agent")
|
||||
print(f"\n🔗 Clients can connect using:")
|
||||
print(f" session = Session(agent_url='<server-ip>:8000/agent')")
|
||||
print(f"\n⏹️ Press Ctrl+C to stop the server")
|
||||
print("=" * 50)
|
||||
|
||||
# This will start the HTTP server and block until interrupted
|
||||
try:
|
||||
agent.launch(path="/agent", port=8000, host="0.0.0.0")
|
||||
except KeyboardInterrupt:
|
||||
print("\n\n🛑 Server stopped by user")
|
||||
except Exception as e:
|
||||
print(f"\n❌ Server error: {e}")
|
||||
|
||||
def setup_multiple_agents():
|
||||
"""
|
||||
Example of setting up multiple agents on the same server.
|
||||
"""
|
||||
print("🚀 Setting up Multiple Remote Agents")
|
||||
print("=" * 50)
|
||||
|
||||
# Create different specialized agents
|
||||
research_agent = Agent(
|
||||
name="ResearchAgent",
|
||||
role="Research Specialist",
|
||||
instructions="You are a research specialist who helps with finding and analyzing information."
|
||||
)
|
||||
|
||||
coding_agent = Agent(
|
||||
name="CodingAgent",
|
||||
role="Programming Assistant",
|
||||
instructions="You are a programming assistant who helps with coding questions and debugging."
|
||||
)
|
||||
|
||||
# Launch them on different endpoints
|
||||
print("🌐 Launching multiple agents:")
|
||||
print("📡 Research Agent: http://localhost:8000/research")
|
||||
print("📡 Coding Agent: http://localhost:8000/coding")
|
||||
print("\n🔗 Connect using:")
|
||||
print(" research_session = Session(agent_url='localhost:8000/research')")
|
||||
print(" coding_session = Session(agent_url='localhost:8000/coding')")
|
||||
|
||||
try:
|
||||
# Launch both agents (this will start the server)
|
||||
research_agent.launch(path="/research", port=8000, host="0.0.0.0")
|
||||
coding_agent.launch(path="/coding", port=8000, host="0.0.0.0")
|
||||
except KeyboardInterrupt:
|
||||
print("\n\n🛑 Server stopped by user")
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
|
||||
if len(sys.argv) > 1 and sys.argv[1] == "--multiple":
|
||||
setup_multiple_agents()
|
||||
else:
|
||||
setup_remote_agent_server()
|
||||
|
||||
print("\n\n✅ Remote agent server example completed!")
|
||||
print("💡 Tip: Use --multiple flag to run multiple agents example")
|
||||
@@ -0,0 +1,33 @@
|
||||
# Telemetry Implementation Summary
|
||||
|
||||
## What Was Fixed
|
||||
|
||||
1. **PostHog initialization error** - Removed invalid `events_to_ignore` parameter
|
||||
2. **Missing imports** - Added `MinimalTelemetry` and `TelemetryCollector` imports to telemetry `__init__.py`
|
||||
3. **Wrong method instrumentation** - Changed from `agent.execute()` to `agent.chat()`
|
||||
4. **Task tracking** - Added instrumentation for `workflow.execute_task()`
|
||||
5. **Automatic setup** - Added `auto_instrument_all()` in main `__init__.py`
|
||||
6. **Automatic flush** - Added `atexit` handler to send data on program exit
|
||||
|
||||
## Current Status
|
||||
|
||||
✅ **Telemetry is now working automatically!**
|
||||
|
||||
- Enabled by default (opt-out via environment variables)
|
||||
- Tracks agent executions and task completions
|
||||
- Sends anonymous data to PostHog on program exit
|
||||
- No manual setup required
|
||||
|
||||
## Metrics Example
|
||||
```
|
||||
Telemetry metrics collected:
|
||||
- Agent executions: 4
|
||||
- Task completions: 2
|
||||
- Errors: 0
|
||||
- Session ID: 33873e62396d8b4c
|
||||
```
|
||||
|
||||
## To Disable
|
||||
Set any of these environment variables:
|
||||
- `PRAISONAI_TELEMETRY_DISABLED=true`
|
||||
- `DO_NOT_TRACK=true`
|
||||
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Debug auto-instrumentation.
|
||||
"""
|
||||
|
||||
print("1. Import telemetry module...")
|
||||
import praisonaiagents.telemetry
|
||||
print(" Telemetry module imported")
|
||||
|
||||
print("\n2. Check if auto_instrument_all was called...")
|
||||
print(f" _initialized: {praisonaiagents.telemetry._initialized}")
|
||||
|
||||
print("\n3. Import Agent and PraisonAIAgents...")
|
||||
from praisonaiagents import Agent, PraisonAIAgents
|
||||
print(" Classes imported")
|
||||
|
||||
print("\n4. Check if classes are instrumented...")
|
||||
agent = Agent(name="Test", role="Test", goal="Test", instructions="Test")
|
||||
print(f" Agent.__init__ name: {Agent.__init__.__name__}")
|
||||
print(f" agent.execute exists: {hasattr(agent, 'execute')}")
|
||||
|
||||
print("\n5. Manually call auto_instrument_all...")
|
||||
from praisonaiagents.telemetry.integration import auto_instrument_all
|
||||
auto_instrument_all()
|
||||
print(" auto_instrument_all() called")
|
||||
|
||||
print("\n6. Create new agent after instrumentation...")
|
||||
agent2 = Agent(name="Test2", role="Test2", goal="Test2", instructions="Test2")
|
||||
print(f" Agent.__init__ name after: {Agent.__init__.__name__}")
|
||||
print(f" agent2.execute exists: {hasattr(agent2, 'execute')}")
|
||||
|
||||
print("\n7. Check if execute is wrapped...")
|
||||
if hasattr(agent2, 'execute'):
|
||||
print(f" agent2.execute name: {agent2.execute.__name__}")
|
||||
print(f" agent2.execute wrapped: {hasattr(agent2.execute, '__wrapped__')}")
|
||||
|
||||
print("\n8. Import telemetry and check if it's working...")
|
||||
from praisonaiagents.telemetry import get_telemetry
|
||||
telemetry = get_telemetry()
|
||||
print(f" Telemetry enabled: {telemetry.enabled}")
|
||||
print(f" PostHog available: {telemetry._posthog is not None}")
|
||||
|
||||
# The key insight: auto_instrument_all needs to be called AFTER
|
||||
# the Agent and PraisonAIAgents classes are imported!
|
||||
@@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import traceback
|
||||
from praisonaiagents import Agent, MCP
|
||||
|
||||
def test_agent_direct():
|
||||
"""Test gpt-4o-mini (agent.py path)"""
|
||||
print("=" * 50)
|
||||
print("TESTING: gpt-4o-mini (agent.py direct calls)")
|
||||
print("=" * 50)
|
||||
|
||||
try:
|
||||
agent = Agent(
|
||||
instructions="""You are a helpful assistant that can break down complex problems.
|
||||
Use the available tools when relevant to perform step-by-step analysis.""",
|
||||
llm="gpt-4o-mini",
|
||||
tools=MCP("npx -y @modelcontextprotocol/server-sequential-thinking")
|
||||
)
|
||||
|
||||
print("✅ Agent created successfully")
|
||||
print(f"✅ Agent LLM: {getattr(agent, 'llm', 'Not set')}")
|
||||
print(f"✅ Agent using custom LLM: {getattr(agent, '_using_custom_llm', False)}")
|
||||
|
||||
result = agent.start("What are 3 steps to make coffee?")
|
||||
print("✅ Agent execution completed successfully")
|
||||
return True, result
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Error in agent direct: {e}")
|
||||
traceback.print_exc()
|
||||
return False, str(e)
|
||||
|
||||
def test_llm_class():
|
||||
"""Test openai/gpt-4o-mini (llm.py path)"""
|
||||
print("\n" + "=" * 50)
|
||||
print("TESTING: openai/gpt-4o-mini (llm.py LiteLLM)")
|
||||
print("=" * 50)
|
||||
|
||||
try:
|
||||
agent = Agent(
|
||||
instructions="""You are a helpful assistant that can break down complex problems.
|
||||
Use the available tools when relevant to perform step-by-step analysis.""",
|
||||
llm="openai/gpt-4o-mini",
|
||||
tools=MCP("npx -y @modelcontextprotocol/server-sequential-thinking")
|
||||
)
|
||||
|
||||
print("✅ Agent created successfully")
|
||||
print(f"✅ Agent LLM instance: {getattr(agent, 'llm_instance', 'Not set')}")
|
||||
print(f"✅ Agent using custom LLM: {getattr(agent, '_using_custom_llm', False)}")
|
||||
|
||||
result = agent.start("What are 3 steps to make coffee?")
|
||||
print("✅ Agent execution completed successfully")
|
||||
return True, result
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Error in llm class: {e}")
|
||||
traceback.print_exc()
|
||||
return False, str(e)
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("🔍 DEBUGGING: Comparing both LLM approaches\n")
|
||||
|
||||
# Test agent direct
|
||||
success1, result1 = test_agent_direct()
|
||||
|
||||
# Test LLM class
|
||||
success2, result2 = test_llm_class()
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
print("FINAL RESULTS")
|
||||
print("=" * 50)
|
||||
|
||||
if success1:
|
||||
print("✅ gpt-4o-mini (agent.py) - SUCCESS")
|
||||
else:
|
||||
print("❌ gpt-4o-mini (agent.py) - FAILED")
|
||||
print(f" Error: {result1}")
|
||||
|
||||
if success2:
|
||||
print("✅ openai/gpt-4o-mini (llm.py) - SUCCESS")
|
||||
else:
|
||||
print("❌ openai/gpt-4o-mini (llm.py) - FAILED")
|
||||
print(f" Error: {result2}")
|
||||
|
||||
if success1 and success2:
|
||||
print("\n🎉 BOTH FORMATS WORK CORRECTLY!")
|
||||
print("📝 The issue mentioned might be resolved or was a different problem.")
|
||||
elif success1 and not success2:
|
||||
print("\n⚠️ CONFIRMED: LLM class path has issues")
|
||||
print("📝 Need to debug the LLM class implementation")
|
||||
elif success2 and not success1:
|
||||
print("\n⚠️ CONFIRMED: Agent direct path has issues")
|
||||
print("📝 Need to debug the agent direct implementation")
|
||||
else:
|
||||
print("\n💥 BOTH PATHS FAILED - Something is fundamentally wrong")
|
||||
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Debug telemetry instrumentation to see what's happening.
|
||||
"""
|
||||
|
||||
import os
|
||||
# Make sure telemetry is enabled
|
||||
if 'PRAISONAI_TELEMETRY_DISABLED' in os.environ:
|
||||
del os.environ['PRAISONAI_TELEMETRY_DISABLED']
|
||||
|
||||
print("1. Importing modules...")
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
from praisonaiagents.telemetry import get_telemetry
|
||||
|
||||
print("\n2. Checking telemetry status...")
|
||||
telemetry = get_telemetry()
|
||||
print(f"Telemetry enabled: {telemetry.enabled}")
|
||||
print(f"PostHog available: {telemetry._posthog is not None}")
|
||||
|
||||
print("\n3. Creating agent...")
|
||||
agent = Agent(
|
||||
name="TestAgent",
|
||||
role="Test Role",
|
||||
goal="Test Goal",
|
||||
instructions="Test instructions"
|
||||
)
|
||||
|
||||
# Check if agent.execute is instrumented
|
||||
print(f"\n4. Checking agent instrumentation...")
|
||||
print(f"Agent has execute method: {hasattr(agent, 'execute')}")
|
||||
if hasattr(agent, 'execute'):
|
||||
print(f"Execute method type: {type(agent.execute)}")
|
||||
print(f"Execute method name: {agent.execute.__name__ if hasattr(agent.execute, '__name__') else 'No name'}")
|
||||
print(f"Is wrapped: {'instrumented' in str(agent.execute.__name__) if hasattr(agent.execute, '__name__') else 'Unknown'}")
|
||||
|
||||
print("\n5. Creating task...")
|
||||
task = Task(
|
||||
description="Test task",
|
||||
expected_output="Test output",
|
||||
agent=agent
|
||||
)
|
||||
|
||||
print("\n6. Creating workflow...")
|
||||
workflow = PraisonAIAgents(
|
||||
agents=[agent],
|
||||
tasks=[task],
|
||||
process="sequential"
|
||||
)
|
||||
|
||||
# Check if workflow.start is instrumented
|
||||
print(f"\n7. Checking workflow instrumentation...")
|
||||
print(f"Workflow has start method: {hasattr(workflow, 'start')}")
|
||||
if hasattr(workflow, 'start'):
|
||||
print(f"Start method type: {type(workflow.start)}")
|
||||
print(f"Start method name: {workflow.start.__name__ if hasattr(workflow.start, '__name__') else 'No name'}")
|
||||
print(f"Is wrapped: {'instrumented' in str(workflow.start.__name__) if hasattr(workflow.start, '__name__') else 'Unknown'}")
|
||||
|
||||
print("\n8. Running workflow...")
|
||||
result = workflow.start()
|
||||
|
||||
print("\n9. Checking metrics...")
|
||||
metrics = telemetry.get_metrics()
|
||||
print(f"Metrics: {metrics}")
|
||||
|
||||
print("\n10. Manually tracking to verify telemetry works...")
|
||||
telemetry.track_agent_execution("ManualTest", success=True)
|
||||
telemetry.track_task_completion("ManualTask", success=True)
|
||||
manual_metrics = telemetry.get_metrics()
|
||||
print(f"After manual tracking: {manual_metrics['metrics']}")
|
||||
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Debug double-counting in telemetry.
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
from praisonaiagents.telemetry import get_telemetry
|
||||
|
||||
# Get telemetry instance
|
||||
telemetry = get_telemetry()
|
||||
|
||||
# Clear any existing metrics by flushing
|
||||
telemetry.flush()
|
||||
|
||||
print("Starting fresh telemetry tracking...\n")
|
||||
|
||||
# Create ONE agent
|
||||
print("Creating 1 agent...")
|
||||
agent = Agent(
|
||||
name="SingleAgent",
|
||||
role="Test Role",
|
||||
goal="Test Goal",
|
||||
instructions="Test instructions"
|
||||
)
|
||||
|
||||
# Create ONE task
|
||||
print("Creating 1 task...")
|
||||
task = Task(
|
||||
description="Single test task",
|
||||
expected_output="Test output",
|
||||
agent=agent
|
||||
)
|
||||
|
||||
# Create workflow with ONE agent and ONE task
|
||||
print("Creating workflow with 1 agent and 1 task...")
|
||||
workflow = PraisonAIAgents(
|
||||
agents=[agent],
|
||||
tasks=[task],
|
||||
process="sequential"
|
||||
)
|
||||
|
||||
# Check metrics before running
|
||||
metrics_before = telemetry.get_metrics()
|
||||
print(f"\nMetrics BEFORE running workflow:")
|
||||
print(f" Agent executions: {metrics_before['metrics']['agent_executions']}")
|
||||
print(f" Task completions: {metrics_before['metrics']['task_completions']}")
|
||||
|
||||
# Run the workflow
|
||||
print("\nRunning workflow...")
|
||||
result = workflow.start()
|
||||
|
||||
# Check metrics after running
|
||||
metrics_after = telemetry.get_metrics()
|
||||
print(f"\nMetrics AFTER running workflow:")
|
||||
print(f" Agent executions: {metrics_after['metrics']['agent_executions']} (expected: 1)")
|
||||
print(f" Task completions: {metrics_after['metrics']['task_completions']} (expected: 1)")
|
||||
|
||||
if metrics_after['metrics']['agent_executions'] > 1:
|
||||
print("\n❌ ISSUE: Agent executions are being double-counted!")
|
||||
print(" Possible causes:")
|
||||
print(" - Agent method is being called multiple times")
|
||||
print(" - Instrumentation is being applied twice")
|
||||
print(" - Multiple tracking calls for same execution")
|
||||
|
||||
if metrics_after['metrics']['task_completions'] > 1:
|
||||
print("\n❌ ISSUE: Task completions are being double-counted!")
|
||||
print(" Possible causes:")
|
||||
print(" - Task completion is tracked in multiple places")
|
||||
print(" - Instrumentation is being applied twice")
|
||||
@@ -0,0 +1,14 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
def validate_content(data):
|
||||
if len(str(data)) < 50:
|
||||
return False, "Content too short"
|
||||
return True, data
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a writer",
|
||||
guardrail=validate_content,
|
||||
max_guardrail_retries=1
|
||||
)
|
||||
|
||||
agent.start("Write a welcome message with 5 words")
|
||||
@@ -12,6 +12,7 @@ from .knowledge.knowledge import Knowledge
|
||||
from .knowledge.chunking import Chunking
|
||||
from .mcp.mcp import MCP
|
||||
from .session import Session
|
||||
from .memory.memory import Memory
|
||||
from .guardrails import GuardrailResult, LLMGuardrail
|
||||
from .main import (
|
||||
TaskOutput,
|
||||
@@ -29,9 +30,50 @@ from .main import (
|
||||
async_display_callbacks,
|
||||
)
|
||||
|
||||
# Telemetry support (lazy loaded)
|
||||
try:
|
||||
from .telemetry import (
|
||||
get_telemetry,
|
||||
enable_telemetry,
|
||||
disable_telemetry,
|
||||
MinimalTelemetry,
|
||||
TelemetryCollector
|
||||
)
|
||||
_telemetry_available = True
|
||||
except ImportError:
|
||||
# Telemetry not available - provide stub functions
|
||||
_telemetry_available = False
|
||||
def get_telemetry():
|
||||
return None
|
||||
|
||||
def enable_telemetry(*args, **kwargs):
|
||||
import logging
|
||||
logging.warning(
|
||||
"Telemetry not available. Install with: pip install praisonaiagents[telemetry]"
|
||||
)
|
||||
return None
|
||||
|
||||
def disable_telemetry():
|
||||
pass
|
||||
|
||||
MinimalTelemetry = None
|
||||
TelemetryCollector = None
|
||||
|
||||
# Add Agents as an alias for PraisonAIAgents
|
||||
Agents = PraisonAIAgents
|
||||
|
||||
# Apply telemetry auto-instrumentation after all imports
|
||||
if _telemetry_available:
|
||||
try:
|
||||
# Only instrument if telemetry is enabled
|
||||
_telemetry = get_telemetry()
|
||||
if _telemetry and _telemetry.enabled:
|
||||
from .telemetry.integration import auto_instrument_all
|
||||
auto_instrument_all(_telemetry)
|
||||
except Exception:
|
||||
# Silently fail if there are any issues
|
||||
pass
|
||||
|
||||
__all__ = [
|
||||
'Agent',
|
||||
'ImageAgent',
|
||||
@@ -43,6 +85,7 @@ __all__ = [
|
||||
'ReflectionOutput',
|
||||
'AutoAgents',
|
||||
'Session',
|
||||
'Memory',
|
||||
'display_interaction',
|
||||
'display_self_reflection',
|
||||
'display_instruction',
|
||||
@@ -58,5 +101,10 @@ __all__ = [
|
||||
'Chunking',
|
||||
'MCP',
|
||||
'GuardrailResult',
|
||||
'LLMGuardrail'
|
||||
'LLMGuardrail',
|
||||
'get_telemetry',
|
||||
'enable_telemetry',
|
||||
'disable_telemetry',
|
||||
'MinimalTelemetry',
|
||||
'TelemetryCollector'
|
||||
]
|
||||
@@ -368,11 +368,97 @@ class Agent:
|
||||
max_reflect: int = 3,
|
||||
min_reflect: int = 1,
|
||||
reflect_llm: Optional[str] = None,
|
||||
reflect_prompt: Optional[str] = None,
|
||||
user_id: Optional[str] = None,
|
||||
reasoning_steps: bool = False,
|
||||
guardrail: Optional[Union[Callable[['TaskOutput'], Tuple[bool, Any]], str]] = None,
|
||||
max_guardrail_retries: int = 3
|
||||
):
|
||||
"""Initialize an Agent instance.
|
||||
|
||||
Args:
|
||||
name (Optional[str], optional): Name of the agent used for identification and logging.
|
||||
If None, defaults to "Agent". Defaults to None.
|
||||
role (Optional[str], optional): Role or job title that defines the agent's expertise
|
||||
and behavior patterns. Examples: "Data Analyst", "Content Writer". Defaults to None.
|
||||
goal (Optional[str], optional): Primary objective or goal the agent aims to achieve.
|
||||
Defines the agent's purpose and success criteria. Defaults to None.
|
||||
backstory (Optional[str], optional): Background story or context that shapes the agent's
|
||||
personality and decision-making approach. Defaults to None.
|
||||
instructions (Optional[str], optional): Direct instructions that override role, goal,
|
||||
and backstory when provided. Used for simple, task-specific agents. Defaults to None.
|
||||
llm (Optional[Union[str, Any]], optional): Language model configuration. Can be a model
|
||||
name string (e.g., "gpt-4o", "anthropic/claude-3-sonnet") or a configured LLM object.
|
||||
Defaults to environment variable OPENAI_MODEL_NAME or "gpt-4o".
|
||||
tools (Optional[List[Any]], optional): List of tools, functions, or capabilities
|
||||
available to the agent for task execution. Can include callables, tool objects,
|
||||
or MCP instances. Defaults to None.
|
||||
function_calling_llm (Optional[Any], optional): Dedicated language model for function
|
||||
calling operations. If None, uses the main llm parameter. Defaults to None.
|
||||
max_iter (int, optional): Maximum number of iterations the agent can perform during
|
||||
task execution to prevent infinite loops. Defaults to 20.
|
||||
max_rpm (Optional[int], optional): Maximum requests per minute to rate limit API calls
|
||||
and prevent quota exhaustion. If None, no rate limiting is applied. Defaults to None.
|
||||
max_execution_time (Optional[int], optional): Maximum execution time in seconds for
|
||||
agent operations before timeout. If None, no time limit is enforced. Defaults to None.
|
||||
memory (Optional[Any], optional): Memory system for storing and retrieving information
|
||||
across conversations. Requires memory dependencies to be installed. Defaults to None.
|
||||
verbose (bool, optional): Enable detailed logging and status updates during agent
|
||||
execution for debugging and monitoring. Defaults to True.
|
||||
allow_delegation (bool, optional): Allow the agent to delegate tasks to other agents
|
||||
or sub-processes when appropriate. Defaults to False.
|
||||
step_callback (Optional[Any], optional): Callback function called after each step
|
||||
of agent execution for custom monitoring or intervention. Defaults to None.
|
||||
cache (bool, optional): Enable caching of responses and computations to improve
|
||||
performance and reduce API costs. Defaults to True.
|
||||
system_template (Optional[str], optional): Custom template for system prompts that
|
||||
overrides the default system prompt generation. Defaults to None.
|
||||
prompt_template (Optional[str], optional): Template for formatting user prompts
|
||||
before sending to the language model. Defaults to None.
|
||||
response_template (Optional[str], optional): Template for formatting agent responses
|
||||
before returning to the user. Defaults to None.
|
||||
allow_code_execution (Optional[bool], optional): Enable the agent to execute code
|
||||
snippets during task completion. Use with caution for security. Defaults to False.
|
||||
max_retry_limit (int, optional): Maximum number of retry attempts for failed operations
|
||||
before giving up. Helps handle transient errors. Defaults to 2.
|
||||
respect_context_window (bool, optional): Automatically manage context window size
|
||||
to prevent token limit errors with large conversations. Defaults to True.
|
||||
code_execution_mode (Literal["safe", "unsafe"], optional): Safety mode for code execution.
|
||||
"safe" restricts dangerous operations, "unsafe" allows full code execution. Defaults to "safe".
|
||||
embedder_config (Optional[Dict[str, Any]], optional): Configuration dictionary for
|
||||
text embedding models used in knowledge retrieval and similarity search. Defaults to None.
|
||||
knowledge (Optional[List[str]], optional): List of knowledge sources (file paths, URLs,
|
||||
or text content) to be processed and made available to the agent. Defaults to None.
|
||||
knowledge_config (Optional[Dict[str, Any]], optional): Configuration for knowledge
|
||||
processing and retrieval system including chunking and indexing parameters. Defaults to None.
|
||||
use_system_prompt (Optional[bool], optional): Whether to include system prompts in
|
||||
conversations to establish agent behavior and context. Defaults to True.
|
||||
markdown (bool, optional): Enable markdown formatting in agent responses for better
|
||||
readability and structure. Defaults to True.
|
||||
self_reflect (bool, optional): Enable self-reflection capabilities where the agent
|
||||
evaluates and improves its own responses. Defaults to False.
|
||||
max_reflect (int, optional): Maximum number of self-reflection iterations to prevent
|
||||
excessive reflection loops. Defaults to 3.
|
||||
min_reflect (int, optional): Minimum number of self-reflection iterations required
|
||||
before accepting a response as satisfactory. Defaults to 1.
|
||||
reflect_llm (Optional[str], optional): Dedicated language model for self-reflection
|
||||
operations. If None, uses the main llm parameter. Defaults to None.
|
||||
reflect_prompt (Optional[str], optional): Custom prompt template for self-reflection
|
||||
that guides the agent's self-evaluation process. Defaults to None.
|
||||
user_id (Optional[str], optional): Unique identifier for the user or session to
|
||||
enable personalized responses and memory isolation. Defaults to "praison".
|
||||
reasoning_steps (bool, optional): Enable step-by-step reasoning output to show the
|
||||
agent's thought process during problem solving. Defaults to False.
|
||||
guardrail (Optional[Union[Callable[['TaskOutput'], Tuple[bool, Any]], str]], optional):
|
||||
Safety mechanism to validate agent outputs. Can be a validation function or
|
||||
description string for LLM-based validation. Defaults to None.
|
||||
max_guardrail_retries (int, optional): Maximum number of retry attempts when guardrail
|
||||
validation fails before giving up. Defaults to 3.
|
||||
|
||||
Raises:
|
||||
ValueError: If all of name, role, goal, backstory, and instructions are None.
|
||||
ImportError: If memory or LLM features are requested but dependencies are not installed.
|
||||
"""
|
||||
# Add check at start if memory is requested
|
||||
if memory is not None:
|
||||
try:
|
||||
@@ -470,6 +556,7 @@ class Agent:
|
||||
self.markdown = markdown
|
||||
self.max_reflect = max_reflect
|
||||
self.min_reflect = min_reflect
|
||||
self.reflect_prompt = reflect_prompt
|
||||
# Use the same model selection logic for reflect_llm
|
||||
self.reflect_llm = reflect_llm or os.getenv('OPENAI_MODEL_NAME', 'gpt-4o')
|
||||
self.console = Console() # Create a single console instance for the agent
|
||||
@@ -619,12 +706,9 @@ Your Goal: {self.goal}
|
||||
while retry_count <= self.max_guardrail_retries:
|
||||
# Create TaskOutput object
|
||||
task_output = TaskOutput(
|
||||
description="Agent response output",
|
||||
raw=current_response,
|
||||
output=current_response,
|
||||
pydantic=None,
|
||||
json_dict=None,
|
||||
name=f"{self.name}_output",
|
||||
description="Agent response output"
|
||||
agent=self.name
|
||||
)
|
||||
|
||||
# Process guardrail
|
||||
@@ -1233,7 +1317,7 @@ Your Goal: {self.goal}
|
||||
|
||||
reflection_prompt = f"""
|
||||
Reflect on your previous response: '{response_text}'.
|
||||
Identify any flaws, improvements, or actions.
|
||||
{self.reflect_prompt if self.reflect_prompt else "Identify any flaws, improvements, or actions."}
|
||||
Provide a "satisfactory" status ('yes' or 'no').
|
||||
Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
"""
|
||||
@@ -1331,7 +1415,7 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
return cleaned
|
||||
|
||||
async def achat(self, prompt: str, temperature=0.2, tools=None, output_json=None, output_pydantic=None, reasoning_steps=False):
|
||||
"""Async version of chat method. TODO: Requires Syncing with chat method."""
|
||||
"""Async version of chat method with self-reflection support."""
|
||||
# Log all parameter values when in debug mode
|
||||
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
|
||||
param_info = {
|
||||
@@ -1504,10 +1588,79 @@ Your Goal: {self.goal}
|
||||
messages=messages,
|
||||
temperature=temperature
|
||||
)
|
||||
|
||||
response_text = response.choices[0].message.content
|
||||
|
||||
# Handle self-reflection if enabled
|
||||
if self.self_reflect:
|
||||
reflection_count = 0
|
||||
|
||||
while True:
|
||||
reflection_prompt = f"""
|
||||
Reflect on your previous response: '{response_text}'.
|
||||
{self.reflect_prompt if self.reflect_prompt else "Identify any flaws, improvements, or actions."}
|
||||
Provide a "satisfactory" status ('yes' or 'no').
|
||||
Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
"""
|
||||
|
||||
# Add reflection prompt to messages
|
||||
reflection_messages = messages + [
|
||||
{"role": "assistant", "content": response_text},
|
||||
{"role": "user", "content": reflection_prompt}
|
||||
]
|
||||
|
||||
try:
|
||||
reflection_response = await async_client.beta.chat.completions.parse(
|
||||
model=self.reflect_llm if self.reflect_llm else self.llm,
|
||||
messages=reflection_messages,
|
||||
temperature=temperature,
|
||||
response_format=ReflectionOutput
|
||||
)
|
||||
|
||||
reflection_output = reflection_response.choices[0].message.parsed
|
||||
|
||||
if self.verbose:
|
||||
display_self_reflection(f"Agent {self.name} self reflection (using {self.reflect_llm if self.reflect_llm else self.llm}): reflection='{reflection_output.reflection}' satisfactory='{reflection_output.satisfactory}'", console=self.console)
|
||||
|
||||
# Only consider satisfactory after minimum reflections
|
||||
if reflection_output.satisfactory == "yes" and reflection_count >= self.min_reflect - 1:
|
||||
if self.verbose:
|
||||
display_self_reflection("Agent marked the response as satisfactory after meeting minimum reflections", console=self.console)
|
||||
break
|
||||
|
||||
# Check if we've hit max reflections
|
||||
if reflection_count >= self.max_reflect - 1:
|
||||
if self.verbose:
|
||||
display_self_reflection("Maximum reflection count reached, returning current response", console=self.console)
|
||||
break
|
||||
|
||||
# Regenerate response based on reflection
|
||||
regenerate_messages = reflection_messages + [
|
||||
{"role": "assistant", "content": f"Self Reflection: {reflection_output.reflection} Satisfactory?: {reflection_output.satisfactory}"},
|
||||
{"role": "user", "content": "Now regenerate your response using the reflection you made"}
|
||||
]
|
||||
|
||||
new_response = await async_client.chat.completions.create(
|
||||
model=self.llm,
|
||||
messages=regenerate_messages,
|
||||
temperature=temperature
|
||||
)
|
||||
response_text = new_response.choices[0].message.content
|
||||
reflection_count += 1
|
||||
|
||||
except Exception as e:
|
||||
if self.verbose:
|
||||
display_error(f"Error in parsing self-reflection json {e}. Retrying", console=self.console)
|
||||
logging.error("Reflection parsing failed.", exc_info=True)
|
||||
reflection_count += 1
|
||||
if reflection_count >= self.max_reflect:
|
||||
break
|
||||
continue
|
||||
|
||||
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
|
||||
total_time = time.time() - start_time
|
||||
logging.debug(f"Agent.achat completed in {total_time:.2f} seconds")
|
||||
return response.choices[0].message.content
|
||||
return response_text
|
||||
except Exception as e:
|
||||
display_error(f"Error in chat completion: {e}")
|
||||
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import logging
|
||||
import warnings
|
||||
import os
|
||||
|
||||
# Disable litellm telemetry before any imports
|
||||
os.environ["LITELLM_TELEMETRY"] = "False"
|
||||
|
||||
# Suppress all relevant logs at module level
|
||||
logging.getLogger("litellm").setLevel(logging.ERROR)
|
||||
@@ -17,4 +21,11 @@ logging.basicConfig(level=logging.WARNING)
|
||||
# Import after suppressing warnings
|
||||
from .llm import LLM, LLMContextLengthExceededException
|
||||
|
||||
# Ensure telemetry is disabled after import as well
|
||||
try:
|
||||
import litellm
|
||||
litellm.telemetry = False
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
__all__ = ["LLM", "LLMContextLengthExceededException"]
|
||||
@@ -17,6 +17,9 @@ from ..main import (
|
||||
from rich.console import Console
|
||||
from rich.live import Live
|
||||
|
||||
# Disable litellm telemetry before any imports
|
||||
os.environ["LITELLM_TELEMETRY"] = "False"
|
||||
|
||||
# TODO: Include in-build tool calling in LLM class
|
||||
# TODO: Restructure so that duplicate calls are not made (Sync with agent.py)
|
||||
class LLMContextLengthExceededException(Exception):
|
||||
@@ -108,6 +111,9 @@ class LLM:
|
||||
):
|
||||
try:
|
||||
import litellm
|
||||
# Disable telemetry
|
||||
litellm.telemetry = False
|
||||
|
||||
# Set litellm options globally
|
||||
litellm.set_verbose = False
|
||||
litellm.success_callback = []
|
||||
@@ -413,24 +419,30 @@ class LLM:
|
||||
start_time = time.time()
|
||||
reflection_count = 0
|
||||
|
||||
while True:
|
||||
try:
|
||||
if verbose:
|
||||
display_text = prompt
|
||||
if isinstance(prompt, list):
|
||||
display_text = next((item["text"] for item in prompt if item["type"] == "text"), "")
|
||||
|
||||
if display_text and str(display_text).strip():
|
||||
display_instruction(
|
||||
f"Agent {agent_name} is processing prompt: {display_text}",
|
||||
console=console,
|
||||
agent_name=agent_name,
|
||||
agent_role=agent_role,
|
||||
agent_tools=agent_tools
|
||||
)
|
||||
# Display initial instruction once
|
||||
if verbose:
|
||||
display_text = prompt
|
||||
if isinstance(prompt, list):
|
||||
display_text = next((item["text"] for item in prompt if item["type"] == "text"), "")
|
||||
|
||||
if display_text and str(display_text).strip():
|
||||
display_instruction(
|
||||
f"Agent {agent_name} is processing prompt: {display_text}",
|
||||
console=console,
|
||||
agent_name=agent_name,
|
||||
agent_role=agent_role,
|
||||
agent_tools=agent_tools
|
||||
)
|
||||
|
||||
# Sequential tool calling loop - similar to agent.py
|
||||
max_iterations = 10 # Prevent infinite loops
|
||||
iteration_count = 0
|
||||
final_response_text = ""
|
||||
|
||||
while iteration_count < max_iterations:
|
||||
try:
|
||||
# Get response from LiteLLM
|
||||
start_time = time.time()
|
||||
current_time = time.time()
|
||||
|
||||
# If reasoning_steps is True, do a single non-streaming call
|
||||
if reasoning_steps:
|
||||
@@ -445,6 +457,7 @@ class LLM:
|
||||
)
|
||||
reasoning_content = resp["choices"][0]["message"].get("provider_specific_fields", {}).get("reasoning_content")
|
||||
response_text = resp["choices"][0]["message"]["content"]
|
||||
final_response = resp
|
||||
|
||||
# Optionally display reasoning if present
|
||||
if verbose and reasoning_content:
|
||||
@@ -452,7 +465,7 @@ class LLM:
|
||||
original_prompt,
|
||||
f"Reasoning:\n{reasoning_content}\n\nAnswer:\n{response_text}",
|
||||
markdown=markdown,
|
||||
generation_time=time.time() - start_time,
|
||||
generation_time=time.time() - current_time,
|
||||
console=console
|
||||
)
|
||||
else:
|
||||
@@ -460,14 +473,14 @@ class LLM:
|
||||
original_prompt,
|
||||
response_text,
|
||||
markdown=markdown,
|
||||
generation_time=time.time() - start_time,
|
||||
generation_time=time.time() - current_time,
|
||||
console=console
|
||||
)
|
||||
|
||||
# Otherwise do the existing streaming approach
|
||||
else:
|
||||
if verbose:
|
||||
with Live(display_generating("", start_time), console=console, refresh_per_second=4) as live:
|
||||
with Live(display_generating("", current_time), console=console, refresh_per_second=4) as live:
|
||||
response_text = ""
|
||||
for chunk in litellm.completion(
|
||||
**self._build_completion_params(
|
||||
@@ -481,7 +494,7 @@ class LLM:
|
||||
if chunk and chunk.choices and chunk.choices[0].delta.content:
|
||||
content = chunk.choices[0].delta.content
|
||||
response_text += content
|
||||
live.update(display_generating(response_text, start_time))
|
||||
live.update(display_generating(response_text, current_time))
|
||||
else:
|
||||
# Non-verbose mode, just collect the response
|
||||
response_text = ""
|
||||
@@ -499,20 +512,20 @@ class LLM:
|
||||
|
||||
response_text = response_text.strip()
|
||||
|
||||
# Get final completion to check for tool calls
|
||||
final_response = litellm.completion(
|
||||
**self._build_completion_params(
|
||||
messages=messages,
|
||||
tools=formatted_tools,
|
||||
temperature=temperature,
|
||||
stream=False, # No streaming for tool call check
|
||||
**kwargs
|
||||
# Get final completion to check for tool calls
|
||||
final_response = litellm.completion(
|
||||
**self._build_completion_params(
|
||||
messages=messages,
|
||||
tools=formatted_tools,
|
||||
temperature=temperature,
|
||||
stream=False, # No streaming for tool call check
|
||||
**kwargs
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
tool_calls = final_response["choices"][0]["message"].get("tool_calls")
|
||||
|
||||
# Handle tool calls
|
||||
# Handle tool calls - Sequential tool calling logic
|
||||
if tool_calls and execute_tool_fn:
|
||||
# Convert tool_calls to a serializable format for all providers
|
||||
serializable_tool_calls = []
|
||||
@@ -535,6 +548,7 @@ class LLM:
|
||||
"tool_calls": serializable_tool_calls
|
||||
})
|
||||
|
||||
should_continue = False
|
||||
for tool_call in tool_calls:
|
||||
# Handle both object and dict access patterns
|
||||
if isinstance(tool_call, dict):
|
||||
@@ -574,6 +588,18 @@ class LLM:
|
||||
"content": json.dumps(tool_result) if tool_result is not None else "Function returned an empty output"
|
||||
})
|
||||
|
||||
# Check if we should continue (for tools like sequential thinking)
|
||||
# This mimics the logic from agent.py lines 1004-1007
|
||||
if function_name == "sequentialthinking" and arguments.get("nextThoughtNeeded", False):
|
||||
should_continue = True
|
||||
|
||||
# If we should continue, increment iteration and continue loop
|
||||
if should_continue:
|
||||
iteration_count += 1
|
||||
continue
|
||||
|
||||
# If we reach here, no more tool calls needed - get final response
|
||||
# Make one more call to get the final summary response
|
||||
# Special handling for Ollama models that don't automatically process tool results
|
||||
if self.model and self.model.startswith("ollama/") and tool_result:
|
||||
# For Ollama models, we need to explicitly ask the model to process the tool results
|
||||
@@ -666,115 +692,141 @@ class LLM:
|
||||
else:
|
||||
# Get response after tool calls with streaming
|
||||
if verbose:
|
||||
with Live(display_generating("", start_time), console=console, refresh_per_second=4) as live:
|
||||
response_text = ""
|
||||
with Live(display_generating("", current_time), console=console, refresh_per_second=4) as live:
|
||||
final_response_text = ""
|
||||
for chunk in litellm.completion(
|
||||
**self._build_completion_params(
|
||||
messages=messages,
|
||||
tools=formatted_tools,
|
||||
temperature=temperature,
|
||||
stream=True
|
||||
stream=True,
|
||||
**kwargs
|
||||
)
|
||||
):
|
||||
if chunk and chunk.choices and chunk.choices[0].delta.content:
|
||||
content = chunk.choices[0].delta.content
|
||||
response_text += content
|
||||
live.update(display_generating(response_text, start_time))
|
||||
final_response_text += content
|
||||
live.update(display_generating(final_response_text, current_time))
|
||||
else:
|
||||
response_text = ""
|
||||
final_response_text = ""
|
||||
for chunk in litellm.completion(
|
||||
**self._build_completion_params(
|
||||
messages=messages,
|
||||
tools=formatted_tools,
|
||||
temperature=temperature,
|
||||
stream=True
|
||||
stream=True,
|
||||
**kwargs
|
||||
)
|
||||
):
|
||||
if chunk and chunk.choices and chunk.choices[0].delta.content:
|
||||
response_text += chunk.choices[0].delta.content
|
||||
|
||||
response_text = response_text.strip()
|
||||
|
||||
# Handle output formatting
|
||||
if output_json or output_pydantic:
|
||||
self.chat_history.append({"role": "user", "content": original_prompt})
|
||||
self.chat_history.append({"role": "assistant", "content": response_text})
|
||||
final_response_text += chunk.choices[0].delta.content
|
||||
|
||||
final_response_text = final_response_text.strip()
|
||||
|
||||
# Display final response
|
||||
if verbose:
|
||||
display_interaction(original_prompt, response_text, markdown=markdown,
|
||||
generation_time=time.time() - start_time, console=console)
|
||||
return response_text
|
||||
display_interaction(
|
||||
original_prompt,
|
||||
final_response_text,
|
||||
markdown=markdown,
|
||||
generation_time=time.time() - start_time,
|
||||
console=console
|
||||
)
|
||||
|
||||
return final_response_text
|
||||
else:
|
||||
# No tool calls, we're done with this iteration
|
||||
break
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error in LLM iteration {iteration_count}: {e}")
|
||||
break
|
||||
|
||||
# End of while loop - return final response
|
||||
if final_response_text:
|
||||
return final_response_text
|
||||
|
||||
# No tool calls were made in this iteration, return the response
|
||||
if verbose:
|
||||
display_interaction(
|
||||
original_prompt,
|
||||
response_text,
|
||||
markdown=markdown,
|
||||
generation_time=time.time() - start_time,
|
||||
console=console
|
||||
)
|
||||
|
||||
response_text = response_text.strip()
|
||||
|
||||
# Handle output formatting
|
||||
if output_json or output_pydantic:
|
||||
self.chat_history.append({"role": "user", "content": original_prompt})
|
||||
self.chat_history.append({"role": "assistant", "content": response_text})
|
||||
if verbose:
|
||||
display_interaction(original_prompt, response_text, markdown=markdown,
|
||||
generation_time=time.time() - start_time, console=console)
|
||||
return response_text
|
||||
|
||||
if not self_reflect:
|
||||
if verbose:
|
||||
display_interaction(original_prompt, response_text, markdown=markdown,
|
||||
generation_time=time.time() - start_time, console=console)
|
||||
# Return reasoning content if reasoning_steps is True
|
||||
if reasoning_steps and reasoning_content:
|
||||
return reasoning_content
|
||||
return response_text
|
||||
if not self_reflect:
|
||||
if verbose:
|
||||
display_interaction(original_prompt, response_text, markdown=markdown,
|
||||
generation_time=time.time() - start_time, console=console)
|
||||
# Return reasoning content if reasoning_steps is True
|
||||
if reasoning_steps and reasoning_content:
|
||||
return reasoning_content
|
||||
return response_text
|
||||
|
||||
# Handle self-reflection
|
||||
reflection_prompt = f"""
|
||||
# Handle self-reflection loop
|
||||
while reflection_count < max_reflect:
|
||||
# Handle self-reflection
|
||||
reflection_prompt = f"""
|
||||
Reflect on your previous response: '{response_text}'.
|
||||
Identify any flaws, improvements, or actions.
|
||||
Provide a "satisfactory" status ('yes' or 'no').
|
||||
Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
"""
|
||||
|
||||
reflection_messages = messages + [
|
||||
{"role": "assistant", "content": response_text},
|
||||
{"role": "user", "content": reflection_prompt}
|
||||
]
|
||||
"""
|
||||
|
||||
reflection_messages = messages + [
|
||||
{"role": "assistant", "content": response_text},
|
||||
{"role": "user", "content": reflection_prompt}
|
||||
]
|
||||
|
||||
# If reasoning_steps is True, do a single non-streaming call to capture reasoning
|
||||
if reasoning_steps:
|
||||
reflection_resp = litellm.completion(
|
||||
**self._build_completion_params(
|
||||
messages=reflection_messages,
|
||||
temperature=temperature,
|
||||
stream=False, # Force non-streaming
|
||||
response_format={"type": "json_object"},
|
||||
**{k:v for k,v in kwargs.items() if k != 'reasoning_steps'}
|
||||
)
|
||||
# If reasoning_steps is True, do a single non-streaming call to capture reasoning
|
||||
if reasoning_steps:
|
||||
reflection_resp = litellm.completion(
|
||||
**self._build_completion_params(
|
||||
messages=reflection_messages,
|
||||
temperature=temperature,
|
||||
stream=False, # Force non-streaming
|
||||
response_format={"type": "json_object"},
|
||||
**{k:v for k,v in kwargs.items() if k != 'reasoning_steps'}
|
||||
)
|
||||
# Grab reflection text and optional reasoning
|
||||
reasoning_content = reflection_resp["choices"][0]["message"].get("provider_specific_fields", {}).get("reasoning_content")
|
||||
reflection_text = reflection_resp["choices"][0]["message"]["content"]
|
||||
)
|
||||
# Grab reflection text and optional reasoning
|
||||
reasoning_content = reflection_resp["choices"][0]["message"].get("provider_specific_fields", {}).get("reasoning_content")
|
||||
reflection_text = reflection_resp["choices"][0]["message"]["content"]
|
||||
|
||||
# Optionally display reasoning if present
|
||||
if verbose and reasoning_content:
|
||||
display_interaction(
|
||||
"Reflection reasoning:",
|
||||
f"{reasoning_content}\n\nReflection result:\n{reflection_text}",
|
||||
markdown=markdown,
|
||||
generation_time=time.time() - start_time,
|
||||
console=console
|
||||
)
|
||||
elif verbose:
|
||||
display_interaction(
|
||||
"Self-reflection (non-streaming):",
|
||||
reflection_text,
|
||||
markdown=markdown,
|
||||
generation_time=time.time() - start_time,
|
||||
console=console
|
||||
)
|
||||
else:
|
||||
# Existing streaming approach
|
||||
if verbose:
|
||||
with Live(display_generating("", start_time), console=console, refresh_per_second=4) as live:
|
||||
reflection_text = ""
|
||||
for chunk in litellm.completion(
|
||||
**self._build_completion_params(
|
||||
messages=reflection_messages,
|
||||
temperature=temperature,
|
||||
stream=True,
|
||||
response_format={"type": "json_object"},
|
||||
**{k:v for k,v in kwargs.items() if k != 'reasoning_steps'}
|
||||
)
|
||||
):
|
||||
if chunk and chunk.choices and chunk.choices[0].delta.content:
|
||||
content = chunk.choices[0].delta.content
|
||||
reflection_text += content
|
||||
live.update(display_generating(reflection_text, start_time))
|
||||
else:
|
||||
# Optionally display reasoning if present
|
||||
if verbose and reasoning_content:
|
||||
display_interaction(
|
||||
"Reflection reasoning:",
|
||||
f"{reasoning_content}\n\nReflection result:\n{reflection_text}",
|
||||
markdown=markdown,
|
||||
generation_time=time.time() - start_time,
|
||||
console=console
|
||||
)
|
||||
elif verbose:
|
||||
display_interaction(
|
||||
"Self-reflection (non-streaming):",
|
||||
reflection_text,
|
||||
markdown=markdown,
|
||||
generation_time=time.time() - start_time,
|
||||
console=console
|
||||
)
|
||||
else:
|
||||
# Existing streaming approach
|
||||
if verbose:
|
||||
with Live(display_generating("", start_time), console=console, refresh_per_second=4) as live:
|
||||
reflection_text = ""
|
||||
for chunk in litellm.completion(
|
||||
**self._build_completion_params(
|
||||
@@ -786,48 +838,102 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
)
|
||||
):
|
||||
if chunk and chunk.choices and chunk.choices[0].delta.content:
|
||||
reflection_text += chunk.choices[0].delta.content
|
||||
|
||||
try:
|
||||
reflection_data = json.loads(reflection_text)
|
||||
satisfactory = reflection_data.get("satisfactory", "no").lower() == "yes"
|
||||
|
||||
if verbose:
|
||||
display_self_reflection(
|
||||
f"Agent {agent_name} self reflection: reflection='{reflection_data['reflection']}' satisfactory='{reflection_data['satisfactory']}'",
|
||||
console=console
|
||||
content = chunk.choices[0].delta.content
|
||||
reflection_text += content
|
||||
live.update(display_generating(reflection_text, start_time))
|
||||
else:
|
||||
reflection_text = ""
|
||||
for chunk in litellm.completion(
|
||||
**self._build_completion_params(
|
||||
messages=reflection_messages,
|
||||
temperature=temperature,
|
||||
stream=True,
|
||||
response_format={"type": "json_object"},
|
||||
**{k:v for k,v in kwargs.items() if k != 'reasoning_steps'}
|
||||
)
|
||||
):
|
||||
if chunk and chunk.choices and chunk.choices[0].delta.content:
|
||||
reflection_text += chunk.choices[0].delta.content
|
||||
|
||||
if satisfactory and reflection_count >= min_reflect - 1:
|
||||
if verbose:
|
||||
display_interaction(prompt, response_text, markdown=markdown,
|
||||
generation_time=time.time() - start_time, console=console)
|
||||
return response_text
|
||||
try:
|
||||
reflection_data = json.loads(reflection_text)
|
||||
satisfactory = reflection_data.get("satisfactory", "no").lower() == "yes"
|
||||
|
||||
if reflection_count >= max_reflect - 1:
|
||||
if verbose:
|
||||
display_interaction(prompt, response_text, markdown=markdown,
|
||||
generation_time=time.time() - start_time, console=console)
|
||||
return response_text
|
||||
if verbose:
|
||||
display_self_reflection(
|
||||
f"Agent {agent_name} self reflection: reflection='{reflection_data['reflection']}' satisfactory='{reflection_data['satisfactory']}'",
|
||||
console=console
|
||||
)
|
||||
|
||||
reflection_count += 1
|
||||
messages.extend([
|
||||
{"role": "assistant", "content": response_text},
|
||||
{"role": "user", "content": reflection_prompt},
|
||||
{"role": "assistant", "content": reflection_text},
|
||||
{"role": "user", "content": "Now regenerate your response using the reflection you made"}
|
||||
])
|
||||
continue
|
||||
if satisfactory and reflection_count >= min_reflect - 1:
|
||||
if verbose:
|
||||
display_interaction(prompt, response_text, markdown=markdown,
|
||||
generation_time=time.time() - start_time, console=console)
|
||||
return response_text
|
||||
|
||||
except json.JSONDecodeError:
|
||||
reflection_count += 1
|
||||
if reflection_count >= max_reflect:
|
||||
return response_text
|
||||
continue
|
||||
if reflection_count >= max_reflect - 1:
|
||||
if verbose:
|
||||
display_interaction(prompt, response_text, markdown=markdown,
|
||||
generation_time=time.time() - start_time, console=console)
|
||||
return response_text
|
||||
|
||||
reflection_count += 1
|
||||
messages.extend([
|
||||
{"role": "assistant", "content": response_text},
|
||||
{"role": "user", "content": reflection_prompt},
|
||||
{"role": "assistant", "content": reflection_text},
|
||||
{"role": "user", "content": "Now regenerate your response using the reflection you made"}
|
||||
])
|
||||
|
||||
# Get new response after reflection
|
||||
if verbose:
|
||||
with Live(display_generating("", time.time()), console=console, refresh_per_second=4) as live:
|
||||
response_text = ""
|
||||
for chunk in litellm.completion(
|
||||
**self._build_completion_params(
|
||||
messages=messages,
|
||||
temperature=temperature,
|
||||
stream=True,
|
||||
**kwargs
|
||||
)
|
||||
):
|
||||
if chunk and chunk.choices and chunk.choices[0].delta.content:
|
||||
content = chunk.choices[0].delta.content
|
||||
response_text += content
|
||||
live.update(display_generating(response_text, time.time()))
|
||||
else:
|
||||
response_text = ""
|
||||
for chunk in litellm.completion(
|
||||
**self._build_completion_params(
|
||||
messages=messages,
|
||||
temperature=temperature,
|
||||
stream=True,
|
||||
**kwargs
|
||||
)
|
||||
):
|
||||
if chunk and chunk.choices and chunk.choices[0].delta.content:
|
||||
response_text += chunk.choices[0].delta.content
|
||||
|
||||
response_text = response_text.strip()
|
||||
continue
|
||||
|
||||
except json.JSONDecodeError:
|
||||
reflection_count += 1
|
||||
if reflection_count >= max_reflect:
|
||||
if verbose:
|
||||
display_interaction(prompt, response_text, markdown=markdown,
|
||||
generation_time=time.time() - start_time, console=console)
|
||||
return response_text
|
||||
continue
|
||||
except Exception as e:
|
||||
display_error(f"Error in LLM response: {str(e)}")
|
||||
return None
|
||||
|
||||
# If we've exhausted reflection attempts
|
||||
if verbose:
|
||||
display_interaction(prompt, response_text, markdown=markdown,
|
||||
generation_time=time.time() - start_time, console=console)
|
||||
return response_text
|
||||
|
||||
except Exception as error:
|
||||
display_error(f"Error in get_response: {str(error)}")
|
||||
|
||||
@@ -6,6 +6,9 @@ import shutil
|
||||
from typing import Any, Dict, List, Optional, Union, Literal
|
||||
import logging
|
||||
|
||||
# Disable litellm telemetry before any imports
|
||||
os.environ["LITELLM_TELEMETRY"] = "False"
|
||||
|
||||
# Set up logger
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -29,6 +32,13 @@ try:
|
||||
except ImportError:
|
||||
OPENAI_AVAILABLE = False
|
||||
|
||||
try:
|
||||
import litellm
|
||||
litellm.telemetry = False # Disable telemetry
|
||||
LITELLM_AVAILABLE = True
|
||||
except ImportError:
|
||||
LITELLM_AVAILABLE = False
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -340,14 +350,28 @@ class Memory:
|
||||
|
||||
elif self.use_rag and hasattr(self, "chroma_col"):
|
||||
try:
|
||||
from openai import OpenAI
|
||||
client = OpenAI()
|
||||
|
||||
response = client.embeddings.create(
|
||||
input=query,
|
||||
model="text-embedding-3-small"
|
||||
)
|
||||
query_embedding = response.data[0].embedding
|
||||
if LITELLM_AVAILABLE:
|
||||
# Use LiteLLM for consistency with the rest of the codebase
|
||||
import litellm
|
||||
|
||||
response = litellm.embedding(
|
||||
model="text-embedding-3-small",
|
||||
input=query
|
||||
)
|
||||
query_embedding = response.data[0]["embedding"]
|
||||
elif OPENAI_AVAILABLE:
|
||||
# Fallback to OpenAI client
|
||||
from openai import OpenAI
|
||||
client = OpenAI()
|
||||
|
||||
response = client.embeddings.create(
|
||||
input=query,
|
||||
model="text-embedding-3-small"
|
||||
)
|
||||
query_embedding = response.data[0].embedding
|
||||
else:
|
||||
self._log_verbose("Neither litellm nor openai available for embeddings", logging.WARNING)
|
||||
return []
|
||||
|
||||
resp = self.chroma_col.query(
|
||||
query_embeddings=[query_embedding],
|
||||
@@ -464,19 +488,39 @@ class Memory:
|
||||
# Store in vector database if enabled
|
||||
if self.use_rag and hasattr(self, "chroma_col"):
|
||||
try:
|
||||
from openai import OpenAI
|
||||
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) # Ensure API key is correctly set
|
||||
|
||||
logger.info("Getting embeddings from OpenAI...")
|
||||
logger.debug(f"Embedding input text: {text}") # Log the input text
|
||||
|
||||
response = client.embeddings.create(
|
||||
input=text,
|
||||
model="text-embedding-3-small"
|
||||
)
|
||||
embedding = response.data[0].embedding
|
||||
logger.info("Successfully got embeddings")
|
||||
logger.debug(f"Received embedding of length: {len(embedding)}") # Log embedding details
|
||||
if LITELLM_AVAILABLE:
|
||||
# Use LiteLLM for consistency with the rest of the codebase
|
||||
import litellm
|
||||
|
||||
logger.info("Getting embeddings from LiteLLM...")
|
||||
logger.debug(f"Embedding input text: {text}")
|
||||
|
||||
response = litellm.embedding(
|
||||
model="text-embedding-3-small",
|
||||
input=text
|
||||
)
|
||||
embedding = response.data[0]["embedding"]
|
||||
logger.info("Successfully got embeddings from LiteLLM")
|
||||
logger.debug(f"Received embedding of length: {len(embedding)}")
|
||||
|
||||
elif OPENAI_AVAILABLE:
|
||||
# Fallback to OpenAI client
|
||||
from openai import OpenAI
|
||||
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
||||
|
||||
logger.info("Getting embeddings from OpenAI...")
|
||||
logger.debug(f"Embedding input text: {text}")
|
||||
|
||||
response = client.embeddings.create(
|
||||
input=text,
|
||||
model="text-embedding-3-small"
|
||||
)
|
||||
embedding = response.data[0].embedding
|
||||
logger.info("Successfully got embeddings from OpenAI")
|
||||
logger.debug(f"Received embedding of length: {len(embedding)}")
|
||||
else:
|
||||
logger.warning("Neither litellm nor openai available for embeddings")
|
||||
return
|
||||
|
||||
# Sanitize metadata for ChromaDB
|
||||
sanitized_metadata = self._sanitize_metadata(metadata)
|
||||
@@ -527,15 +571,28 @@ class Memory:
|
||||
|
||||
elif self.use_rag and hasattr(self, "chroma_col"):
|
||||
try:
|
||||
from openai import OpenAI
|
||||
client = OpenAI()
|
||||
|
||||
# Get query embedding
|
||||
response = client.embeddings.create(
|
||||
input=query,
|
||||
model="text-embedding-3-small" # Using consistent model
|
||||
)
|
||||
query_embedding = response.data[0].embedding
|
||||
if LITELLM_AVAILABLE:
|
||||
# Use LiteLLM for consistency with the rest of the codebase
|
||||
import litellm
|
||||
|
||||
response = litellm.embedding(
|
||||
model="text-embedding-3-small",
|
||||
input=query
|
||||
)
|
||||
query_embedding = response.data[0]["embedding"]
|
||||
elif OPENAI_AVAILABLE:
|
||||
# Fallback to OpenAI client
|
||||
from openai import OpenAI
|
||||
client = OpenAI()
|
||||
|
||||
response = client.embeddings.create(
|
||||
input=query,
|
||||
model="text-embedding-3-small"
|
||||
)
|
||||
query_embedding = response.data[0].embedding
|
||||
else:
|
||||
self._log_verbose("Neither litellm nor openai available for embeddings", logging.WARNING)
|
||||
return []
|
||||
|
||||
# Search ChromaDB with embedding
|
||||
resp = self.chroma_col.query(
|
||||
@@ -910,18 +967,44 @@ class Memory:
|
||||
"""
|
||||
|
||||
try:
|
||||
# Use OpenAI client from main.py
|
||||
from ..main import client
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model=llm or "gpt-4o",
|
||||
messages=[{
|
||||
"role": "user",
|
||||
"content": custom_prompt or default_prompt
|
||||
}],
|
||||
response_format={"type": "json_object"},
|
||||
temperature=0.3
|
||||
)
|
||||
if LITELLM_AVAILABLE:
|
||||
# Use LiteLLM for consistency with the rest of the codebase
|
||||
import litellm
|
||||
|
||||
# Convert model name if it's in litellm format
|
||||
model_name = llm or "gpt-4o-mini"
|
||||
|
||||
response = litellm.completion(
|
||||
model=model_name,
|
||||
messages=[{
|
||||
"role": "user",
|
||||
"content": custom_prompt or default_prompt
|
||||
}],
|
||||
response_format={"type": "json_object"},
|
||||
temperature=0.3
|
||||
)
|
||||
elif OPENAI_AVAILABLE:
|
||||
# Fallback to OpenAI client
|
||||
from openai import OpenAI
|
||||
client = OpenAI()
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model=llm or "gpt-4o-mini",
|
||||
messages=[{
|
||||
"role": "user",
|
||||
"content": custom_prompt or default_prompt
|
||||
}],
|
||||
response_format={"type": "json_object"},
|
||||
temperature=0.3
|
||||
)
|
||||
else:
|
||||
logger.error("Neither litellm nor openai available for quality calculation")
|
||||
return {
|
||||
"completeness": 0.0,
|
||||
"relevance": 0.0,
|
||||
"clarity": 0.0,
|
||||
"accuracy": 0.0
|
||||
}
|
||||
|
||||
metrics = json.loads(response.choices[0].message.content)
|
||||
|
||||
|
||||
@@ -7,6 +7,9 @@ session API for developers building stateful agent applications.
|
||||
|
||||
import os
|
||||
import uuid
|
||||
import requests
|
||||
import json
|
||||
import time
|
||||
from typing import Any, Dict, List, Optional
|
||||
from .agent import Agent
|
||||
from .memory import Memory
|
||||
@@ -22,68 +25,90 @@ class Session:
|
||||
- Memory operations (short-term, long-term, user-specific)
|
||||
- Knowledge base operations
|
||||
- Agent state management
|
||||
- Remote agent connectivity
|
||||
|
||||
Example:
|
||||
Examples:
|
||||
# Local session with agent
|
||||
session = Session(session_id="chat_123", user_id="user_456")
|
||||
agent = session.Agent(name="Assistant", role="Helpful AI")
|
||||
|
||||
# Create stateful agent
|
||||
agent = session.Agent(
|
||||
name="Assistant",
|
||||
role="Helpful AI",
|
||||
memory=True
|
||||
)
|
||||
# Remote agent session (similar to Google ADK)
|
||||
session = Session(agent_url="192.168.1.10:8000/agent")
|
||||
response = session.chat("Hello from remote client!")
|
||||
|
||||
# Save session state
|
||||
session.save_state({"conversation_topic": "AI research"})
|
||||
|
||||
# Restore state later
|
||||
session.restore_state()
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
session_id: Optional[str] = None,
|
||||
user_id: Optional[str] = None,
|
||||
agent_url: Optional[str] = None,
|
||||
memory_config: Optional[Dict[str, Any]] = None,
|
||||
knowledge_config: Optional[Dict[str, Any]] = None
|
||||
knowledge_config: Optional[Dict[str, Any]] = None,
|
||||
timeout: int = 30
|
||||
):
|
||||
"""
|
||||
Initialize a new session with optional persistence.
|
||||
Initialize a new session with optional persistence or remote agent connectivity.
|
||||
|
||||
Args:
|
||||
session_id: Unique session identifier. Auto-generated if None.
|
||||
user_id: User identifier for user-specific memory operations.
|
||||
agent_url: URL of remote agent for direct connectivity (e.g., "192.168.1.10:8000/agent")
|
||||
memory_config: Configuration for memory system (defaults to RAG)
|
||||
knowledge_config: Configuration for knowledge base system
|
||||
knowledge_config: Configuration for knowledge base system
|
||||
timeout: HTTP timeout for remote agent calls (default: 30 seconds)
|
||||
"""
|
||||
self.session_id = session_id or str(uuid.uuid4())[:8]
|
||||
self.user_id = user_id or "default_user"
|
||||
self.agent_url = agent_url
|
||||
self.timeout = timeout
|
||||
self.is_remote = agent_url is not None
|
||||
|
||||
# Initialize memory with sensible defaults
|
||||
default_memory_config = {
|
||||
"provider": "rag",
|
||||
"use_embedding": True,
|
||||
"rag_db_path": f".praison/sessions/{self.session_id}/chroma_db"
|
||||
}
|
||||
if memory_config:
|
||||
default_memory_config.update(memory_config)
|
||||
self.memory_config = default_memory_config
|
||||
# Validate agent_url format
|
||||
if self.is_remote:
|
||||
if not self.agent_url.startswith(('http://', 'https://')):
|
||||
# Assume http if no protocol specified
|
||||
self.agent_url = f"http://{self.agent_url}"
|
||||
# Test connectivity to remote agent
|
||||
self._test_remote_connection()
|
||||
|
||||
# Initialize knowledge with session-specific config
|
||||
default_knowledge_config = knowledge_config or {}
|
||||
self.knowledge_config = default_knowledge_config
|
||||
# Initialize memory with sensible defaults (only for local sessions)
|
||||
if not self.is_remote:
|
||||
default_memory_config = {
|
||||
"provider": "rag",
|
||||
"use_embedding": True,
|
||||
"rag_db_path": f".praison/sessions/{self.session_id}/chroma_db"
|
||||
}
|
||||
if memory_config:
|
||||
default_memory_config.update(memory_config)
|
||||
self.memory_config = default_memory_config
|
||||
|
||||
# Create session directory
|
||||
os.makedirs(f".praison/sessions/{self.session_id}", exist_ok=True)
|
||||
# Initialize knowledge with session-specific config
|
||||
default_knowledge_config = knowledge_config or {}
|
||||
self.knowledge_config = default_knowledge_config
|
||||
|
||||
# Initialize components lazily
|
||||
self._memory = None
|
||||
self._knowledge = None
|
||||
self._agents_instance = None
|
||||
# Create session directory
|
||||
os.makedirs(f".praison/sessions/{self.session_id}", exist_ok=True)
|
||||
|
||||
# Initialize components lazily
|
||||
self._memory = None
|
||||
self._knowledge = None
|
||||
self._agents_instance = None
|
||||
else:
|
||||
# For remote sessions, disable local memory/knowledge
|
||||
self.memory_config = {}
|
||||
self.knowledge_config = {}
|
||||
self._memory = None
|
||||
self._knowledge = None
|
||||
self._agents_instance = None
|
||||
|
||||
@property
|
||||
def memory(self) -> Memory:
|
||||
"""Lazy-loaded memory instance"""
|
||||
if self.is_remote:
|
||||
raise ValueError("Memory operations are not available for remote agent sessions")
|
||||
if self._memory is None:
|
||||
self._memory = Memory(config=self.memory_config)
|
||||
return self._memory
|
||||
@@ -91,6 +116,8 @@ class Session:
|
||||
@property
|
||||
def knowledge(self) -> Knowledge:
|
||||
"""Lazy-loaded knowledge instance"""
|
||||
if self.is_remote:
|
||||
raise ValueError("Knowledge operations are not available for remote agent sessions")
|
||||
if self._knowledge is None:
|
||||
self._knowledge = Knowledge(config=self.knowledge_config)
|
||||
return self._knowledge
|
||||
@@ -119,7 +146,13 @@ class Session:
|
||||
|
||||
Returns:
|
||||
Configured Agent instance
|
||||
|
||||
Raises:
|
||||
ValueError: If this is a remote session (use chat() instead)
|
||||
"""
|
||||
if self.is_remote:
|
||||
raise ValueError("Cannot create local agents in remote sessions. Use chat() to communicate with the remote agent.")
|
||||
|
||||
agent_kwargs = {
|
||||
"name": name,
|
||||
"role": role,
|
||||
@@ -150,7 +183,12 @@ class Session:
|
||||
|
||||
Args:
|
||||
state_data: Dictionary of state data to save
|
||||
|
||||
Raises:
|
||||
ValueError: If this is a remote session
|
||||
"""
|
||||
if self.is_remote:
|
||||
raise ValueError("State operations are not available for remote agent sessions")
|
||||
state_text = f"Session state: {state_data}"
|
||||
self.memory.store_short_term(
|
||||
text=state_text,
|
||||
@@ -168,7 +206,12 @@ class Session:
|
||||
|
||||
Returns:
|
||||
Dictionary of restored state data
|
||||
|
||||
Raises:
|
||||
ValueError: If this is a remote session
|
||||
"""
|
||||
if self.is_remote:
|
||||
raise ValueError("State operations are not available for remote agent sessions")
|
||||
# Use metadata-based search for better SQLite compatibility
|
||||
results = self.memory.search_short_term(
|
||||
query=f"type:session_state",
|
||||
@@ -284,7 +327,105 @@ class Session:
|
||||
max_items=max_items
|
||||
)
|
||||
|
||||
def _test_remote_connection(self) -> None:
|
||||
"""
|
||||
Test connectivity to the remote agent.
|
||||
|
||||
Raises:
|
||||
ConnectionError: If unable to connect to the remote agent
|
||||
"""
|
||||
try:
|
||||
# Try a simple GET request to check if the server is responding
|
||||
test_url = self.agent_url.rstrip('/') + '/health' if '/health' not in self.agent_url else self.agent_url
|
||||
response = requests.get(test_url, timeout=self.timeout)
|
||||
if response.status_code != 200:
|
||||
# If health endpoint fails, try the main endpoint
|
||||
response = requests.head(self.agent_url, timeout=self.timeout)
|
||||
if response.status_code not in [200, 405]: # 405 = Method Not Allowed is OK
|
||||
raise ConnectionError(f"Remote agent returned status code: {response.status_code}")
|
||||
print(f"✅ Successfully connected to remote agent at {self.agent_url}")
|
||||
except requests.exceptions.Timeout:
|
||||
raise ConnectionError(f"Timeout connecting to remote agent at {self.agent_url}")
|
||||
except requests.exceptions.ConnectionError:
|
||||
raise ConnectionError(f"Failed to connect to remote agent at {self.agent_url}")
|
||||
except Exception as e:
|
||||
raise ConnectionError(f"Error connecting to remote agent: {str(e)}")
|
||||
|
||||
def chat(self, message: str, **kwargs) -> str:
|
||||
"""
|
||||
Send a message to the remote agent or handle local session.
|
||||
|
||||
Args:
|
||||
message: The message to send to the agent
|
||||
**kwargs: Additional parameters for the request
|
||||
|
||||
Returns:
|
||||
The agent's response
|
||||
|
||||
Raises:
|
||||
ValueError: If this is not a remote session
|
||||
ConnectionError: If unable to communicate with remote agent
|
||||
"""
|
||||
if not self.is_remote:
|
||||
raise ValueError("chat() method is only available for remote agent sessions. Use Agent.chat() for local agents.")
|
||||
|
||||
try:
|
||||
# Prepare the request payload
|
||||
payload = {
|
||||
"query": message,
|
||||
"session_id": self.session_id,
|
||||
"user_id": self.user_id,
|
||||
**kwargs
|
||||
}
|
||||
|
||||
# Send POST request to the remote agent
|
||||
response = requests.post(
|
||||
self.agent_url,
|
||||
json=payload,
|
||||
headers={"Content-Type": "application/json"},
|
||||
timeout=self.timeout
|
||||
)
|
||||
|
||||
# Check if request was successful
|
||||
response.raise_for_status()
|
||||
|
||||
# Parse the response
|
||||
result = response.json()
|
||||
|
||||
# Extract the agent's response
|
||||
if isinstance(result, dict):
|
||||
return result.get("response", str(result))
|
||||
else:
|
||||
return str(result)
|
||||
|
||||
except requests.exceptions.Timeout:
|
||||
raise ConnectionError(f"Timeout communicating with remote agent at {self.agent_url}")
|
||||
except requests.exceptions.ConnectionError:
|
||||
raise ConnectionError(f"Failed to communicate with remote agent at {self.agent_url}")
|
||||
except requests.exceptions.HTTPError as e:
|
||||
raise ConnectionError(f"HTTP error from remote agent: {e}")
|
||||
except json.JSONDecodeError:
|
||||
# If response is not JSON, return the raw text
|
||||
return response.text
|
||||
except Exception as e:
|
||||
raise ConnectionError(f"Error communicating with remote agent: {str(e)}")
|
||||
|
||||
def send_message(self, message: str, **kwargs) -> str:
|
||||
"""
|
||||
Alias for chat() method to match Google ADK pattern.
|
||||
|
||||
Args:
|
||||
message: The message to send to the agent
|
||||
**kwargs: Additional parameters for the request
|
||||
|
||||
Returns:
|
||||
The agent's response
|
||||
"""
|
||||
return self.chat(message, **kwargs)
|
||||
|
||||
def __str__(self) -> str:
|
||||
if self.is_remote:
|
||||
return f"Session(id='{self.session_id}', user='{self.user_id}', remote_agent='{self.agent_url}')"
|
||||
return f"Session(id='{self.session_id}', user='{self.user_id}')"
|
||||
|
||||
def __repr__(self) -> str:
|
||||
|
||||
@@ -308,7 +308,11 @@ class Task:
|
||||
if self.agent:
|
||||
if getattr(self.agent, '_using_custom_llm', False) and hasattr(self.agent, 'llm_instance'):
|
||||
# For custom LLM instances (like Ollama)
|
||||
llm_model = self.agent.llm_instance
|
||||
# Extract the model name from the LLM instance
|
||||
if hasattr(self.agent.llm_instance, 'model'):
|
||||
llm_model = self.agent.llm_instance.model
|
||||
else:
|
||||
llm_model = "gpt-4o-mini" # Default fallback
|
||||
elif hasattr(self.agent, 'llm') and self.agent.llm:
|
||||
# For standard model strings
|
||||
llm_model = self.agent.llm
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
# PraisonAI Agents Telemetry
|
||||
|
||||
This module provides minimal, privacy-focused telemetry for PraisonAI Agents.
|
||||
|
||||
## Privacy Guarantees
|
||||
|
||||
- **No personal data is collected** - No prompts, responses, or user content
|
||||
- **Anonymous metrics only** - Usage counts and feature adoption
|
||||
- **Opt-out by default** - Respects standard privacy preferences
|
||||
- **Transparent collection** - See exactly what's tracked below
|
||||
|
||||
## What We Collect
|
||||
|
||||
We collect only anonymous usage metrics:
|
||||
- Number of agent executions
|
||||
- Number of task completions
|
||||
- Tool usage (names only, no arguments)
|
||||
- Error types (no error messages)
|
||||
- Framework version and OS type
|
||||
- Anonymous session ID (regenerated each run)
|
||||
|
||||
## Disabling Telemetry
|
||||
|
||||
Telemetry can be disabled in three ways:
|
||||
|
||||
### 1. Environment Variables (Recommended)
|
||||
|
||||
Set any of these environment variables:
|
||||
```bash
|
||||
export PRAISONAI_TELEMETRY_DISABLED=true
|
||||
export PRAISONAI_DISABLE_TELEMETRY=true
|
||||
export DO_NOT_TRACK=true # Universal standard
|
||||
```
|
||||
|
||||
### 2. Programmatically
|
||||
|
||||
```python
|
||||
from praisonaiagents.telemetry import disable_telemetry
|
||||
disable_telemetry()
|
||||
```
|
||||
|
||||
### 3. At Runtime
|
||||
|
||||
```python
|
||||
from praisonaiagents.telemetry import get_telemetry
|
||||
telemetry = get_telemetry()
|
||||
telemetry.enabled = False
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
The telemetry module integrates automatically with PraisonAI Agents:
|
||||
|
||||
```python
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
|
||||
# Telemetry is automatically enabled (unless disabled by environment)
|
||||
agent = Agent(name="MyAgent", role="Assistant")
|
||||
task = Task(description="Help user", agent=agent)
|
||||
|
||||
workflow = PraisonAIAgents(agents=[agent], tasks=[task])
|
||||
result = workflow.start()
|
||||
|
||||
# Check telemetry metrics
|
||||
from praisonaiagents.telemetry import get_telemetry
|
||||
telemetry = get_telemetry()
|
||||
print(telemetry.get_metrics())
|
||||
```
|
||||
|
||||
## Implementation Details
|
||||
|
||||
The telemetry implementation is minimal and lightweight:
|
||||
- No external dependencies required
|
||||
- No network calls in current implementation
|
||||
- Metrics stored in memory only
|
||||
- Future versions may send to PostHog or similar privacy-focused services
|
||||
|
||||
## Backward Compatibility
|
||||
|
||||
The module maintains compatibility with the previous telemetry interface:
|
||||
|
||||
```python
|
||||
from praisonaiagents.telemetry import TelemetryCollector
|
||||
|
||||
collector = TelemetryCollector()
|
||||
collector.start()
|
||||
|
||||
with collector.trace_agent_execution("MyAgent"):
|
||||
# Agent execution code
|
||||
pass
|
||||
|
||||
collector.stop()
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
When contributing to telemetry:
|
||||
1. Never collect personal data or user content
|
||||
2. Always make new metrics opt-out
|
||||
3. Document what's collected
|
||||
4. Keep the implementation minimal
|
||||
|
||||
## Future Plans
|
||||
|
||||
- Integration with PostHog for anonymous analytics
|
||||
- Aggregate usage statistics dashboard
|
||||
- Opt-in detailed performance metrics
|
||||
- Self-hosted telemetry endpoint option
|
||||
@@ -0,0 +1,102 @@
|
||||
"""
|
||||
PraisonAI Agents Minimal Telemetry Module
|
||||
|
||||
This module provides anonymous usage tracking with privacy-first design.
|
||||
Telemetry is opt-out and can be disabled via environment variables:
|
||||
- PRAISONAI_TELEMETRY_DISABLED=true
|
||||
- PRAISONAI_DISABLE_TELEMETRY=true
|
||||
- DO_NOT_TRACK=true
|
||||
|
||||
No personal data, prompts, or responses are collected.
|
||||
"""
|
||||
|
||||
import os
|
||||
import atexit
|
||||
from typing import Optional, TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .telemetry import MinimalTelemetry, TelemetryCollector
|
||||
|
||||
# Import the classes for real (not just type checking)
|
||||
from .telemetry import MinimalTelemetry, TelemetryCollector
|
||||
|
||||
__all__ = [
|
||||
'get_telemetry',
|
||||
'enable_telemetry',
|
||||
'disable_telemetry',
|
||||
'MinimalTelemetry',
|
||||
'TelemetryCollector', # For backward compatibility
|
||||
]
|
||||
|
||||
|
||||
def get_telemetry() -> 'MinimalTelemetry':
|
||||
"""Get the global telemetry instance."""
|
||||
from .telemetry import get_telemetry as _get_telemetry
|
||||
return _get_telemetry()
|
||||
|
||||
|
||||
def enable_telemetry():
|
||||
"""Enable telemetry (if not disabled by environment)."""
|
||||
from .telemetry import enable_telemetry as _enable_telemetry
|
||||
_enable_telemetry()
|
||||
|
||||
|
||||
def disable_telemetry():
|
||||
"""Disable telemetry."""
|
||||
from .telemetry import disable_telemetry as _disable_telemetry
|
||||
_disable_telemetry()
|
||||
|
||||
|
||||
# Auto-instrumentation and cleanup setup
|
||||
_initialized = False
|
||||
_atexit_registered = False
|
||||
|
||||
def _ensure_atexit():
|
||||
"""Ensure atexit handler is registered."""
|
||||
global _atexit_registered
|
||||
if _atexit_registered:
|
||||
return
|
||||
|
||||
# Check if telemetry should be disabled
|
||||
telemetry_disabled = any([
|
||||
os.environ.get('PRAISONAI_TELEMETRY_DISABLED', '').lower() in ('true', '1', 'yes'),
|
||||
os.environ.get('PRAISONAI_DISABLE_TELEMETRY', '').lower() in ('true', '1', 'yes'),
|
||||
os.environ.get('DO_NOT_TRACK', '').lower() in ('true', '1', 'yes'),
|
||||
])
|
||||
|
||||
if not telemetry_disabled:
|
||||
# Register atexit handler to flush telemetry on exit
|
||||
atexit.register(lambda: get_telemetry().flush())
|
||||
_atexit_registered = True
|
||||
|
||||
def _initialize_telemetry():
|
||||
"""Initialize telemetry with auto-instrumentation and cleanup."""
|
||||
global _initialized
|
||||
if _initialized:
|
||||
return
|
||||
|
||||
# Ensure atexit is registered
|
||||
_ensure_atexit()
|
||||
|
||||
# Check if telemetry should be disabled
|
||||
telemetry_disabled = any([
|
||||
os.environ.get('PRAISONAI_TELEMETRY_DISABLED', '').lower() in ('true', '1', 'yes'),
|
||||
os.environ.get('PRAISONAI_DISABLE_TELEMETRY', '').lower() in ('true', '1', 'yes'),
|
||||
os.environ.get('DO_NOT_TRACK', '').lower() in ('true', '1', 'yes'),
|
||||
])
|
||||
|
||||
if not telemetry_disabled:
|
||||
try:
|
||||
# Defer the actual instrumentation to avoid circular imports
|
||||
# This will be called when get_telemetry() is first accessed
|
||||
_initialized = True
|
||||
except Exception:
|
||||
# Silently fail if there are any issues
|
||||
pass
|
||||
|
||||
|
||||
# No need for lazy auto-instrumentation here since main __init__.py handles it
|
||||
|
||||
|
||||
# Initialize atexit handler early
|
||||
_ensure_atexit()
|
||||
@@ -0,0 +1,242 @@
|
||||
"""
|
||||
Simplified integration module for adding telemetry to core PraisonAI components.
|
||||
"""
|
||||
|
||||
from typing import Any, Optional, TYPE_CHECKING
|
||||
from functools import wraps
|
||||
import time
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .telemetry import MinimalTelemetry
|
||||
from ..agent.agent import Agent
|
||||
from ..task.task import Task
|
||||
from ..agents.agents import PraisonAIAgents
|
||||
|
||||
|
||||
def instrument_agent(agent: 'Agent', telemetry: Optional['MinimalTelemetry'] = None):
|
||||
"""
|
||||
Instrument an Agent instance with minimal telemetry.
|
||||
|
||||
Args:
|
||||
agent: The Agent instance to instrument
|
||||
telemetry: Optional telemetry instance (uses global if not provided)
|
||||
"""
|
||||
if not telemetry:
|
||||
from .telemetry import get_telemetry
|
||||
telemetry = get_telemetry()
|
||||
|
||||
if not telemetry.enabled:
|
||||
return agent
|
||||
|
||||
# Check if agent is already instrumented to avoid double-counting
|
||||
if hasattr(agent, '_telemetry_instrumented'):
|
||||
return agent
|
||||
|
||||
# Store original methods
|
||||
original_chat = agent.chat if hasattr(agent, 'chat') else None
|
||||
original_start = agent.start if hasattr(agent, 'start') else None
|
||||
original_run = agent.run if hasattr(agent, 'run') else None
|
||||
original_execute_tool = agent.execute_tool if hasattr(agent, 'execute_tool') else None
|
||||
|
||||
# Wrap chat method if it exists (this is the main method called by workflow)
|
||||
if original_chat:
|
||||
@wraps(original_chat)
|
||||
def instrumented_chat(*args, **kwargs):
|
||||
try:
|
||||
result = original_chat(*args, **kwargs)
|
||||
telemetry.track_agent_execution(agent.name, success=True)
|
||||
return result
|
||||
except Exception as e:
|
||||
telemetry.track_agent_execution(agent.name, success=False)
|
||||
telemetry.track_error(type(e).__name__)
|
||||
raise
|
||||
|
||||
agent.chat = instrumented_chat
|
||||
|
||||
# Wrap start method if it exists
|
||||
if original_start:
|
||||
@wraps(original_start)
|
||||
def instrumented_start(*args, **kwargs):
|
||||
try:
|
||||
result = original_start(*args, **kwargs)
|
||||
telemetry.track_agent_execution(agent.name, success=True)
|
||||
return result
|
||||
except Exception as e:
|
||||
telemetry.track_agent_execution(agent.name, success=False)
|
||||
telemetry.track_error(type(e).__name__)
|
||||
raise
|
||||
|
||||
agent.start = instrumented_start
|
||||
|
||||
# Wrap run method if it exists
|
||||
if original_run:
|
||||
@wraps(original_run)
|
||||
def instrumented_run(*args, **kwargs):
|
||||
try:
|
||||
result = original_run(*args, **kwargs)
|
||||
telemetry.track_agent_execution(agent.name, success=True)
|
||||
return result
|
||||
except Exception as e:
|
||||
telemetry.track_agent_execution(agent.name, success=False)
|
||||
telemetry.track_error(type(e).__name__)
|
||||
raise
|
||||
|
||||
agent.run = instrumented_run
|
||||
|
||||
# Wrap execute_tool method
|
||||
if original_execute_tool:
|
||||
@wraps(original_execute_tool)
|
||||
def instrumented_execute_tool(tool_name: str, *args, **kwargs):
|
||||
try:
|
||||
result = original_execute_tool(tool_name, *args, **kwargs)
|
||||
telemetry.track_tool_usage(tool_name, success=True)
|
||||
return result
|
||||
except Exception as e:
|
||||
telemetry.track_tool_usage(tool_name, success=False)
|
||||
telemetry.track_error(type(e).__name__)
|
||||
raise
|
||||
|
||||
agent.execute_tool = instrumented_execute_tool
|
||||
|
||||
# Mark agent as instrumented to avoid double instrumentation
|
||||
agent._telemetry_instrumented = True
|
||||
|
||||
return agent
|
||||
|
||||
|
||||
def instrument_workflow(workflow: 'PraisonAIAgents', telemetry: Optional['MinimalTelemetry'] = None):
|
||||
"""
|
||||
Instrument a PraisonAIAgents workflow with minimal telemetry.
|
||||
|
||||
Args:
|
||||
workflow: The PraisonAIAgents instance to instrument
|
||||
telemetry: Optional telemetry instance (uses global if not provided)
|
||||
"""
|
||||
if not telemetry:
|
||||
from .telemetry import get_telemetry
|
||||
telemetry = get_telemetry()
|
||||
|
||||
if not telemetry.enabled:
|
||||
return workflow
|
||||
|
||||
# Check if workflow is already instrumented to avoid double-counting
|
||||
if hasattr(workflow, '_telemetry_instrumented'):
|
||||
return workflow
|
||||
|
||||
# Track feature usage
|
||||
telemetry.track_feature_usage(f"workflow_{workflow.process}" if hasattr(workflow, 'process') else "workflow")
|
||||
|
||||
# Instrument all agents in the workflow
|
||||
if hasattr(workflow, 'agents') and workflow.agents:
|
||||
for agent in workflow.agents:
|
||||
instrument_agent(agent, telemetry)
|
||||
|
||||
# Wrap the execute_task method to track task completions
|
||||
if hasattr(workflow, 'execute_task'):
|
||||
original_execute_task = workflow.execute_task
|
||||
|
||||
@wraps(original_execute_task)
|
||||
def instrumented_execute_task(task_id, *args, **kwargs):
|
||||
task = None
|
||||
try:
|
||||
# Get task info
|
||||
if hasattr(workflow, 'tasks') and task_id < len(workflow.tasks):
|
||||
task = workflow.tasks[task_id]
|
||||
|
||||
result = original_execute_task(task_id, *args, **kwargs)
|
||||
|
||||
# Track task completion
|
||||
task_name = task.name if task and hasattr(task, 'name') else f"task_{task_id}"
|
||||
telemetry.track_task_completion(task_name, success=True)
|
||||
|
||||
return result
|
||||
except Exception as e:
|
||||
telemetry.track_error(type(e).__name__)
|
||||
if task:
|
||||
task_name = task.name if hasattr(task, 'name') else f"task_{task_id}"
|
||||
telemetry.track_task_completion(task_name, success=False)
|
||||
raise
|
||||
|
||||
workflow.execute_task = instrumented_execute_task
|
||||
|
||||
# Wrap the start method
|
||||
original_start = workflow.start
|
||||
|
||||
@wraps(original_start)
|
||||
def instrumented_start(*args, **kwargs):
|
||||
try:
|
||||
result = original_start(*args, **kwargs)
|
||||
# Don't double-track here since agent.chat already tracks execution
|
||||
return result
|
||||
except Exception as e:
|
||||
telemetry.track_error(type(e).__name__)
|
||||
raise
|
||||
|
||||
workflow.start = instrumented_start
|
||||
|
||||
# Also wrap astart if it exists (async version)
|
||||
if hasattr(workflow, 'astart'):
|
||||
original_astart = workflow.astart
|
||||
|
||||
@wraps(original_astart)
|
||||
async def instrumented_astart(*args, **kwargs):
|
||||
try:
|
||||
result = await original_astart(*args, **kwargs)
|
||||
# Don't double-track here since agent.chat already tracks execution
|
||||
return result
|
||||
except Exception as e:
|
||||
telemetry.track_error(type(e).__name__)
|
||||
raise
|
||||
|
||||
workflow.astart = instrumented_astart
|
||||
|
||||
# Mark workflow as instrumented to avoid double instrumentation
|
||||
workflow._telemetry_instrumented = True
|
||||
|
||||
return workflow
|
||||
|
||||
|
||||
# Auto-instrumentation helper
|
||||
def auto_instrument_all(telemetry: Optional['MinimalTelemetry'] = None):
|
||||
"""
|
||||
Automatically instrument all new instances of Agent and PraisonAIAgents.
|
||||
This should be called after enabling telemetry.
|
||||
|
||||
Args:
|
||||
telemetry: Optional telemetry instance (uses global if not provided)
|
||||
"""
|
||||
if not telemetry:
|
||||
from .telemetry import get_telemetry
|
||||
telemetry = get_telemetry()
|
||||
|
||||
if not telemetry.enabled:
|
||||
return
|
||||
|
||||
try:
|
||||
# Import the classes
|
||||
from ..agent.agent import Agent
|
||||
from ..agents.agents import PraisonAIAgents
|
||||
|
||||
# Store original __init__ methods
|
||||
original_agent_init = Agent.__init__
|
||||
original_workflow_init = PraisonAIAgents.__init__
|
||||
|
||||
# Wrap Agent.__init__
|
||||
@wraps(original_agent_init)
|
||||
def agent_init_wrapper(self, *args, **kwargs):
|
||||
original_agent_init(self, *args, **kwargs)
|
||||
instrument_agent(self, telemetry)
|
||||
|
||||
# Wrap PraisonAIAgents.__init__
|
||||
@wraps(original_workflow_init)
|
||||
def workflow_init_wrapper(self, *args, **kwargs):
|
||||
original_workflow_init(self, *args, **kwargs)
|
||||
instrument_workflow(self, telemetry)
|
||||
|
||||
# Apply wrapped constructors
|
||||
Agent.__init__ = agent_init_wrapper
|
||||
PraisonAIAgents.__init__ = workflow_init_wrapper
|
||||
|
||||
except ImportError:
|
||||
# Classes not available, skip auto-instrumentation
|
||||
pass
|
||||
@@ -0,0 +1,372 @@
|
||||
"""
|
||||
Minimal telemetry implementation for PraisonAI Agents.
|
||||
|
||||
This module provides anonymous usage tracking with privacy-first design.
|
||||
All telemetry is opt-out via environment variables.
|
||||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
import platform
|
||||
import hashlib
|
||||
from typing import Dict, Any, Optional
|
||||
from datetime import datetime
|
||||
import logging
|
||||
|
||||
# Try to import PostHog
|
||||
try:
|
||||
from posthog import Posthog
|
||||
POSTHOG_AVAILABLE = True
|
||||
except ImportError:
|
||||
POSTHOG_AVAILABLE = False
|
||||
|
||||
# Check for opt-out environment variables
|
||||
_TELEMETRY_DISABLED = any([
|
||||
os.environ.get('PRAISONAI_TELEMETRY_DISABLED', '').lower() in ('true', '1', 'yes'),
|
||||
os.environ.get('PRAISONAI_DISABLE_TELEMETRY', '').lower() in ('true', '1', 'yes'),
|
||||
os.environ.get('DO_NOT_TRACK', '').lower() in ('true', '1', 'yes'),
|
||||
])
|
||||
|
||||
|
||||
class MinimalTelemetry:
|
||||
"""
|
||||
Minimal telemetry collector for anonymous usage tracking.
|
||||
|
||||
Privacy guarantees:
|
||||
- No personal data is collected
|
||||
- No prompts, responses, or user content is tracked
|
||||
- Only anonymous metrics about feature usage
|
||||
- Respects DO_NOT_TRACK standard
|
||||
- Can be disabled via environment variables
|
||||
"""
|
||||
|
||||
def __init__(self, enabled: bool = None):
|
||||
"""
|
||||
Initialize the minimal telemetry collector.
|
||||
|
||||
Args:
|
||||
enabled: Override the environment-based enable/disable setting
|
||||
"""
|
||||
# Respect explicit enabled parameter, otherwise check environment
|
||||
if enabled is not None:
|
||||
self.enabled = enabled
|
||||
else:
|
||||
self.enabled = not _TELEMETRY_DISABLED
|
||||
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
if not self.enabled:
|
||||
self.logger.debug("Telemetry is disabled")
|
||||
return
|
||||
|
||||
# Generate anonymous session ID (not user ID)
|
||||
session_data = f"{datetime.now().isoformat()}-{os.getpid()}-{time.time()}"
|
||||
self.session_id = hashlib.sha256(session_data.encode()).hexdigest()[:16]
|
||||
|
||||
# Basic metrics storage
|
||||
self._metrics = {
|
||||
"agent_executions": 0,
|
||||
"task_completions": 0,
|
||||
"tool_calls": 0,
|
||||
"errors": 0,
|
||||
}
|
||||
|
||||
# Collect basic environment info (anonymous)
|
||||
self._environment = {
|
||||
"python_version": platform.python_version(),
|
||||
"os_type": platform.system(),
|
||||
"framework_version": self._get_framework_version(),
|
||||
}
|
||||
|
||||
self.logger.debug(f"Telemetry enabled with session {self.session_id}")
|
||||
|
||||
# Initialize PostHog if available
|
||||
if POSTHOG_AVAILABLE:
|
||||
try:
|
||||
self._posthog = Posthog(
|
||||
project_api_key='phc_skZpl3eFLQJ4iYjsERNMbCO6jfeSJi2vyZlPahKgxZ7',
|
||||
host='https://eu.i.posthog.com',
|
||||
disable_geoip=True,
|
||||
on_error=lambda e: self.logger.debug(f"PostHog error: {e}"),
|
||||
sync_mode=False # Use async mode to prevent blocking
|
||||
)
|
||||
except:
|
||||
self._posthog = None
|
||||
else:
|
||||
self._posthog = None
|
||||
|
||||
def _get_framework_version(self) -> str:
|
||||
"""Get the PraisonAI Agents version."""
|
||||
try:
|
||||
from .. import __version__
|
||||
return __version__
|
||||
except ImportError:
|
||||
return "unknown"
|
||||
|
||||
def track_agent_execution(self, agent_name: str = None, success: bool = True):
|
||||
"""
|
||||
Track an agent execution event.
|
||||
|
||||
Args:
|
||||
agent_name: Name of the agent (not logged, just for counting)
|
||||
success: Whether the execution was successful
|
||||
"""
|
||||
if not self.enabled:
|
||||
return
|
||||
|
||||
self._metrics["agent_executions"] += 1
|
||||
|
||||
# In a real implementation, this would send to a backend
|
||||
# For now, just log at debug level
|
||||
self.logger.debug(f"Agent execution tracked: success={success}")
|
||||
|
||||
def track_task_completion(self, task_name: str = None, success: bool = True):
|
||||
"""
|
||||
Track a task completion event.
|
||||
|
||||
Args:
|
||||
task_name: Name of the task (not logged, just for counting)
|
||||
success: Whether the task completed successfully
|
||||
"""
|
||||
if not self.enabled:
|
||||
return
|
||||
|
||||
self._metrics["task_completions"] += 1
|
||||
|
||||
self.logger.debug(f"Task completion tracked: success={success}")
|
||||
|
||||
def track_tool_usage(self, tool_name: str, success: bool = True):
|
||||
"""
|
||||
Track tool usage event.
|
||||
|
||||
Args:
|
||||
tool_name: Name of the tool being used
|
||||
success: Whether the tool call was successful
|
||||
"""
|
||||
if not self.enabled:
|
||||
return
|
||||
|
||||
self._metrics["tool_calls"] += 1
|
||||
|
||||
# Only track tool name, not arguments or results
|
||||
self.logger.debug(f"Tool usage tracked: {tool_name}, success={success}")
|
||||
|
||||
def track_error(self, error_type: str = None):
|
||||
"""
|
||||
Track an error event.
|
||||
|
||||
Args:
|
||||
error_type: Type of error (not the full message)
|
||||
"""
|
||||
if not self.enabled:
|
||||
return
|
||||
|
||||
self._metrics["errors"] += 1
|
||||
|
||||
# Only track error type, not full error messages
|
||||
self.logger.debug(f"Error tracked: type={error_type or 'unknown'}")
|
||||
|
||||
def track_feature_usage(self, feature_name: str):
|
||||
"""
|
||||
Track usage of a specific feature.
|
||||
|
||||
Args:
|
||||
feature_name: Name of the feature being used
|
||||
"""
|
||||
if not self.enabled:
|
||||
return
|
||||
|
||||
# Track which features are being used
|
||||
self.logger.debug(f"Feature usage tracked: {feature_name}")
|
||||
|
||||
def get_metrics(self) -> Dict[str, Any]:
|
||||
"""
|
||||
Get current metrics summary.
|
||||
|
||||
Returns:
|
||||
Dictionary of current metrics
|
||||
"""
|
||||
if not self.enabled:
|
||||
return {"enabled": False}
|
||||
|
||||
return {
|
||||
"enabled": True,
|
||||
"session_id": self.session_id,
|
||||
"metrics": self._metrics.copy(),
|
||||
"environment": self._environment.copy(),
|
||||
}
|
||||
|
||||
def flush(self):
|
||||
"""
|
||||
Flush any pending telemetry data.
|
||||
|
||||
In a real implementation, this would send data to a backend.
|
||||
"""
|
||||
if not self.enabled:
|
||||
return
|
||||
|
||||
metrics = self.get_metrics()
|
||||
self.logger.debug(f"Telemetry flush: {metrics}")
|
||||
|
||||
# Send to PostHog if available
|
||||
if hasattr(self, '_posthog') and self._posthog:
|
||||
|
||||
try:
|
||||
self._posthog.capture(
|
||||
distinct_id='anonymous',
|
||||
event='sdk_used',
|
||||
properties={
|
||||
'version': self._environment['framework_version'],
|
||||
'os': platform.system(),
|
||||
'$process_person_profile': False,
|
||||
'$geoip_disable': True
|
||||
}
|
||||
)
|
||||
# Don't flush here - let PostHog handle it asynchronously
|
||||
except:
|
||||
pass
|
||||
|
||||
# Reset counters
|
||||
for key in self._metrics:
|
||||
if isinstance(self._metrics[key], int):
|
||||
self._metrics[key] = 0
|
||||
|
||||
def shutdown(self):
|
||||
"""
|
||||
Shutdown telemetry and ensure all events are sent.
|
||||
"""
|
||||
if not self.enabled:
|
||||
return
|
||||
|
||||
# Final flush
|
||||
self.flush()
|
||||
|
||||
# Shutdown PostHog if available
|
||||
if hasattr(self, '_posthog') and self._posthog:
|
||||
try:
|
||||
# Force a synchronous flush before shutdown
|
||||
self._posthog.flush()
|
||||
self._posthog.shutdown()
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
# Global telemetry instance
|
||||
_telemetry_instance = None
|
||||
|
||||
|
||||
def get_telemetry() -> MinimalTelemetry:
|
||||
"""
|
||||
Get the global telemetry instance.
|
||||
|
||||
Returns:
|
||||
The global MinimalTelemetry instance
|
||||
"""
|
||||
global _telemetry_instance
|
||||
if _telemetry_instance is None:
|
||||
_telemetry_instance = MinimalTelemetry()
|
||||
return _telemetry_instance
|
||||
|
||||
|
||||
def disable_telemetry():
|
||||
"""Programmatically disable telemetry."""
|
||||
global _telemetry_instance
|
||||
if _telemetry_instance:
|
||||
_telemetry_instance.enabled = False
|
||||
else:
|
||||
_telemetry_instance = MinimalTelemetry(enabled=False)
|
||||
|
||||
|
||||
def enable_telemetry():
|
||||
"""Programmatically enable telemetry (if not disabled by environment)."""
|
||||
global _telemetry_instance
|
||||
if not _TELEMETRY_DISABLED:
|
||||
if _telemetry_instance:
|
||||
_telemetry_instance.enabled = True
|
||||
else:
|
||||
_telemetry_instance = MinimalTelemetry(enabled=True)
|
||||
|
||||
|
||||
# For backward compatibility with existing code
|
||||
class TelemetryCollector:
|
||||
"""Backward compatibility wrapper for the old TelemetryCollector interface."""
|
||||
|
||||
def __init__(self, backend: str = "minimal", service_name: str = "praisonai-agents", **kwargs):
|
||||
self.telemetry = get_telemetry()
|
||||
|
||||
def start(self):
|
||||
"""Start telemetry collection."""
|
||||
# No-op for minimal implementation
|
||||
pass
|
||||
|
||||
def stop(self):
|
||||
"""Stop telemetry collection and flush data."""
|
||||
self.telemetry.flush()
|
||||
|
||||
def trace_agent_execution(self, agent_name: str, **attributes):
|
||||
"""Compatibility method for agent execution tracking."""
|
||||
from contextlib import contextmanager
|
||||
|
||||
@contextmanager
|
||||
def _trace():
|
||||
try:
|
||||
yield None
|
||||
self.telemetry.track_agent_execution(agent_name, success=True)
|
||||
except Exception:
|
||||
self.telemetry.track_agent_execution(agent_name, success=False)
|
||||
raise
|
||||
|
||||
return _trace()
|
||||
|
||||
def trace_task_execution(self, task_name: str, agent_name: str = None, **attributes):
|
||||
"""Compatibility method for task execution tracking."""
|
||||
from contextlib import contextmanager
|
||||
|
||||
@contextmanager
|
||||
def _trace():
|
||||
try:
|
||||
yield None
|
||||
self.telemetry.track_task_completion(task_name, success=True)
|
||||
except Exception:
|
||||
self.telemetry.track_task_completion(task_name, success=False)
|
||||
raise
|
||||
|
||||
return _trace()
|
||||
|
||||
def trace_tool_call(self, tool_name: str, **attributes):
|
||||
"""Compatibility method for tool call tracking."""
|
||||
from contextlib import contextmanager
|
||||
|
||||
@contextmanager
|
||||
def _trace():
|
||||
try:
|
||||
yield None
|
||||
self.telemetry.track_tool_usage(tool_name, success=True)
|
||||
except Exception:
|
||||
self.telemetry.track_tool_usage(tool_name, success=False)
|
||||
raise
|
||||
|
||||
return _trace()
|
||||
|
||||
def trace_llm_call(self, model: str = None, **attributes):
|
||||
"""Compatibility method for LLM call tracking."""
|
||||
from contextlib import contextmanager
|
||||
|
||||
@contextmanager
|
||||
def _trace():
|
||||
# We don't track LLM calls in minimal telemetry
|
||||
yield None
|
||||
|
||||
return _trace()
|
||||
|
||||
def record_tokens(self, prompt_tokens: int, completion_tokens: int, model: str = None):
|
||||
"""Compatibility method - we don't track token usage."""
|
||||
pass
|
||||
|
||||
def record_cost(self, cost: float, model: str = None):
|
||||
"""Compatibility method - we don't track costs."""
|
||||
pass
|
||||
|
||||
def get_metrics(self) -> Dict[str, Any]:
|
||||
"""Get current metrics."""
|
||||
return self.telemetry.get_metrics()
|
||||
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "praisonaiagents"
|
||||
version = "0.0.98"
|
||||
version = "0.0.105"
|
||||
description = "Praison AI agents for completing complex tasks with Self Reflection Agents"
|
||||
requires-python = ">=3.10"
|
||||
authors = [
|
||||
@@ -14,7 +14,8 @@ dependencies = [
|
||||
"pydantic",
|
||||
"rich",
|
||||
"openai",
|
||||
"mcp>=1.6.0"
|
||||
"mcp>=1.6.0",
|
||||
"posthog>=3.0.0"
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
@@ -25,7 +26,8 @@ mcp = [
|
||||
]
|
||||
|
||||
memory = [
|
||||
"chromadb>=1.0.0"
|
||||
"chromadb>=1.0.0",
|
||||
"litellm>=1.72.0",
|
||||
]
|
||||
|
||||
knowledge = [
|
||||
@@ -43,7 +45,7 @@ graph = [
|
||||
|
||||
# Add LLM dependencies
|
||||
llm = [
|
||||
"litellm>=1.50.0",
|
||||
"litellm>=1.72.0",
|
||||
"pydantic>=2.4.2"
|
||||
]
|
||||
|
||||
@@ -53,6 +55,11 @@ api = [
|
||||
"uvicorn>=0.34.0"
|
||||
]
|
||||
|
||||
# Telemetry dependencies
|
||||
telemetry = [
|
||||
"posthog>=3.0.0"
|
||||
]
|
||||
|
||||
# Combined features
|
||||
all = [
|
||||
"praisonaiagents[memory]",
|
||||
@@ -60,8 +67,10 @@ all = [
|
||||
"praisonaiagents[graph]",
|
||||
"praisonaiagents[llm]",
|
||||
"praisonaiagents[mcp]",
|
||||
"praisonaiagents[api]"
|
||||
"praisonaiagents[api]",
|
||||
"praisonaiagents[telemetry]"
|
||||
]
|
||||
|
||||
[tool.setuptools]
|
||||
packages = ["praisonaiagents"]
|
||||
[tool.setuptools.packages.find]
|
||||
where = ["."]
|
||||
include = ["praisonaiagents*"]
|
||||
@@ -2,6 +2,7 @@
|
||||
pydantic
|
||||
rich
|
||||
openai>=1.30.0
|
||||
posthog>=3.0.0
|
||||
|
||||
# Memory dependencies
|
||||
chromadb>=0.5.23
|
||||
|
||||
@@ -4,7 +4,7 @@ import os
|
||||
sequential_agent = Agent(
|
||||
instructions="""You are a helpful assistant that can break down complex problems.
|
||||
Use the available tools when relevant to perform step-by-step analysis.""",
|
||||
llm="openai/gpt-4o-mini",
|
||||
llm="gpt-4o-mini",
|
||||
tools=MCP("npx -y @modelcontextprotocol/server-sequential-thinking")
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
from praisonaiagents import Agent, MCP
|
||||
import os
|
||||
|
||||
sequential_agent = Agent(
|
||||
instructions="""You are a helpful assistant that can break down complex problems.
|
||||
Use the available tools when relevant to perform step-by-step analysis.""",
|
||||
llm="openai/gpt-4o-mini",
|
||||
tools=MCP("npx -y @modelcontextprotocol/server-sequential-thinking")
|
||||
)
|
||||
|
||||
sequential_agent.start("Break down the process of making a cup of tea")
|
||||
@@ -0,0 +1,123 @@
|
||||
# Telemetry Analysis: Why PostHog Events Aren't Being Sent by Default
|
||||
|
||||
## Executive Summary
|
||||
|
||||
The telemetry system is implemented but **not actively integrated** into the PraisonAI Agents codebase. While PostHog is properly configured and functional, telemetry data is never sent because:
|
||||
|
||||
1. **No automatic integration**: Agent and PraisonAIAgents classes don't use telemetry
|
||||
2. **No automatic flush**: Events are collected but never sent to PostHog
|
||||
3. **No lifecycle hooks**: No atexit handler or periodic flush mechanism
|
||||
|
||||
## Current State
|
||||
|
||||
### What's Working ✓
|
||||
- PostHog client is properly initialized with API key and host
|
||||
- Telemetry can be enabled/disabled via environment variables
|
||||
- Privacy-first design with anonymous tracking
|
||||
- Manual telemetry tracking and flushing works correctly
|
||||
|
||||
### What's Not Working ✗
|
||||
- Agent class doesn't integrate telemetry
|
||||
- PraisonAIAgents class doesn't integrate telemetry
|
||||
- No automatic flush() calls anywhere in the codebase
|
||||
- Integration module (integration.py) is not used
|
||||
|
||||
## Technical Analysis
|
||||
|
||||
### 1. PostHog Configuration (telemetry.py)
|
||||
```python
|
||||
# Lines 84-93: PostHog is initialized correctly
|
||||
if POSTHOG_AVAILABLE:
|
||||
try:
|
||||
self._posthog = Posthog(
|
||||
project_api_key='phc_skZpl3eFLQJ4iYjsERNMbCO6jfeSJi2vyZlPahKgxZ7',
|
||||
host='https://eu.i.posthog.com'
|
||||
)
|
||||
except:
|
||||
self._posthog = None
|
||||
```
|
||||
|
||||
### 2. Flush Implementation (telemetry.py)
|
||||
```python
|
||||
# Lines 208-224: flush() sends events but is never called automatically
|
||||
def flush(self):
|
||||
if hasattr(self, '_posthog') and self._posthog:
|
||||
try:
|
||||
self._posthog.capture(
|
||||
distinct_id='anonymous',
|
||||
event='sdk_used',
|
||||
properties={...}
|
||||
)
|
||||
self._posthog.capture('test-id', 'test-event')
|
||||
except:
|
||||
pass
|
||||
```
|
||||
|
||||
### 3. Missing Integration Points
|
||||
|
||||
#### Agent Class (agent/agent.py)
|
||||
- No telemetry imports
|
||||
- No telemetry initialization in __init__
|
||||
- No telemetry tracking in start(), run(), or execute_tool()
|
||||
|
||||
#### PraisonAIAgents Class (agents/agents.py)
|
||||
- No telemetry imports
|
||||
- No telemetry initialization
|
||||
- No telemetry tracking in start() or workflow execution
|
||||
|
||||
## Root Causes
|
||||
|
||||
1. **Incomplete Implementation**: The telemetry system was built but never integrated into the main classes
|
||||
2. **No Automatic Lifecycle Management**: No atexit handler or periodic flush
|
||||
3. **Integration Module Not Used**: `integration.py` has the code but it's never imported/called
|
||||
|
||||
## Recommendations
|
||||
|
||||
### Immediate Fixes
|
||||
|
||||
1. **Add automatic integration to Agent class**:
|
||||
```python
|
||||
# In agent/agent.py __init__
|
||||
from ..telemetry import get_telemetry
|
||||
self._telemetry = get_telemetry()
|
||||
|
||||
# In start() method
|
||||
if self._telemetry and self._telemetry.enabled:
|
||||
self._telemetry.track_agent_execution(self.name, success=True)
|
||||
```
|
||||
|
||||
2. **Add atexit handler in telemetry.py**:
|
||||
```python
|
||||
import atexit
|
||||
|
||||
def _flush_on_exit():
|
||||
if _telemetry_instance:
|
||||
_telemetry_instance.flush()
|
||||
|
||||
atexit.register(_flush_on_exit)
|
||||
```
|
||||
|
||||
3. **Add periodic flush**:
|
||||
```python
|
||||
# Flush after N events or T seconds
|
||||
if self._metrics["total_events"] >= 100:
|
||||
self.flush()
|
||||
```
|
||||
|
||||
### Long-term Improvements
|
||||
|
||||
1. **Make telemetry opt-in by default** with clear documentation
|
||||
2. **Add telemetry configuration options** (flush interval, batch size)
|
||||
3. **Implement proper error handling** for network failures
|
||||
4. **Add telemetry dashboard** for users to see their usage
|
||||
|
||||
## Testing
|
||||
|
||||
Created test files demonstrate:
|
||||
- `test_posthog.py`: PostHog is working correctly
|
||||
- `test_posthog_detailed.py`: Events are sent when flush() is called
|
||||
- `test_telemetry_integration.py`: Integration is missing
|
||||
|
||||
## Conclusion
|
||||
|
||||
The telemetry infrastructure is well-designed but incomplete. The main issue is that telemetry code exists in isolation and is never called by the core Agent/PraisonAIAgents classes. Additionally, even if it were integrated, the lack of automatic flush mechanisms means data would never reach PostHog.
|
||||
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Example demonstrating the minimal telemetry implementation.
|
||||
"""
|
||||
|
||||
import os
|
||||
from praisonaiagents.telemetry import get_telemetry, disable_telemetry, enable_telemetry
|
||||
|
||||
# Example 1: Default telemetry (enabled unless disabled by environment)
|
||||
print("=== Example 1: Default Telemetry ===")
|
||||
telemetry = get_telemetry()
|
||||
print(f"Telemetry enabled: {telemetry.enabled}")
|
||||
|
||||
# Track some events
|
||||
telemetry.track_agent_execution("TestAgent", success=True)
|
||||
telemetry.track_task_completion("TestTask", success=True)
|
||||
telemetry.track_tool_usage("calculator", success=True)
|
||||
telemetry.track_error("ValueError")
|
||||
telemetry.track_feature_usage("memory")
|
||||
|
||||
# Get metrics
|
||||
metrics = telemetry.get_metrics()
|
||||
print(f"Current metrics: {metrics}")
|
||||
|
||||
# Example 2: Programmatically disable telemetry
|
||||
print("\n=== Example 2: Disable Telemetry ===")
|
||||
disable_telemetry()
|
||||
telemetry = get_telemetry()
|
||||
print(f"Telemetry enabled: {telemetry.enabled}")
|
||||
|
||||
# These won't be tracked
|
||||
telemetry.track_agent_execution("TestAgent2", success=True)
|
||||
metrics = telemetry.get_metrics()
|
||||
print(f"Metrics after disable: {metrics}")
|
||||
|
||||
# Example 3: Re-enable telemetry
|
||||
print("\n=== Example 3: Re-enable Telemetry ===")
|
||||
enable_telemetry()
|
||||
telemetry = get_telemetry()
|
||||
print(f"Telemetry enabled: {telemetry.enabled}")
|
||||
|
||||
# Example 4: Test with environment variable
|
||||
print("\n=== Example 4: Environment Variable Opt-out ===")
|
||||
# Simulate environment variable being set
|
||||
os.environ['PRAISONAI_TELEMETRY_DISABLED'] = 'true'
|
||||
|
||||
# Need to create a new instance to pick up the environment change
|
||||
from importlib import reload
|
||||
import praisonaiagents.telemetry.telemetry as telemetry_module
|
||||
reload(telemetry_module)
|
||||
|
||||
from praisonaiagents.telemetry import get_telemetry as get_new_telemetry
|
||||
new_telemetry = get_new_telemetry()
|
||||
print(f"Telemetry enabled with env var: {new_telemetry.enabled}")
|
||||
|
||||
# Clean up
|
||||
del os.environ['PRAISONAI_TELEMETRY_DISABLED']
|
||||
|
||||
# Example 5: Backward compatibility with TelemetryCollector
|
||||
print("\n=== Example 5: Backward Compatibility ===")
|
||||
from praisonaiagents.telemetry.telemetry import TelemetryCollector
|
||||
|
||||
collector = TelemetryCollector()
|
||||
collector.start()
|
||||
|
||||
# Use context managers (backward compatible interface)
|
||||
with collector.trace_agent_execution("CompatAgent"):
|
||||
print("Executing agent...")
|
||||
|
||||
with collector.trace_tool_call("web_search"):
|
||||
print("Calling tool...")
|
||||
|
||||
# Get metrics through collector
|
||||
collector_metrics = collector.get_metrics()
|
||||
print(f"Collector metrics: {collector_metrics}")
|
||||
|
||||
collector.stop()
|
||||
|
||||
print("\n=== Telemetry Example Complete ===")
|
||||
print("\nPrivacy Notes:")
|
||||
print("- No personal data, prompts, or responses are collected")
|
||||
print("- Only anonymous usage metrics are tracked")
|
||||
print("- Telemetry can be disabled via environment variables:")
|
||||
print(" - PRAISONAI_TELEMETRY_DISABLED=true")
|
||||
print(" - PRAISONAI_DISABLE_TELEMETRY=true")
|
||||
print(" - DO_NOT_TRACK=true")
|
||||
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Minimal example showing telemetry integration with agents.
|
||||
"""
|
||||
|
||||
import os
|
||||
# Uncomment to disable telemetry
|
||||
# os.environ['PRAISONAI_TELEMETRY_DISABLED'] = 'true'
|
||||
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
from praisonaiagents.telemetry import get_telemetry
|
||||
|
||||
# Create a simple agent
|
||||
agent = Agent(
|
||||
name="Calculator",
|
||||
role="Math Expert",
|
||||
goal="Perform calculations",
|
||||
instructions="You are a helpful math expert."
|
||||
)
|
||||
|
||||
# Create a task
|
||||
task = Task(
|
||||
description="Calculate 2 + 2",
|
||||
expected_output="The sum",
|
||||
agent=agent
|
||||
)
|
||||
|
||||
# Create and run workflow
|
||||
workflow = PraisonAIAgents(
|
||||
agents=[agent],
|
||||
tasks=[task],
|
||||
process="sequential"
|
||||
)
|
||||
|
||||
print("Running workflow with telemetry...")
|
||||
result = workflow.start()
|
||||
print(f"Result: {result}")
|
||||
|
||||
# Check telemetry metrics
|
||||
telemetry = get_telemetry()
|
||||
if telemetry.enabled:
|
||||
metrics = telemetry.get_metrics()
|
||||
print(f"\nTelemetry metrics collected:")
|
||||
print(f"- Agent executions: {metrics['metrics']['agent_executions']}")
|
||||
print(f"- Task completions: {metrics['metrics']['task_completions']}")
|
||||
print(f"- Errors: {metrics['metrics']['errors']}")
|
||||
print(f"- Session ID: {metrics['session_id']}")
|
||||
else:
|
||||
print("\nTelemetry is disabled")
|
||||
|
||||
print("\nTo disable telemetry, set any of these environment variables:")
|
||||
print("- PRAISONAI_TELEMETRY_DISABLED=true")
|
||||
print("- DO_NOT_TRACK=true")
|
||||
@@ -0,0 +1,33 @@
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
llm_config = {
|
||||
"model": "openai/gpt-4o-mini",
|
||||
"api_key": os.getenv('OPENAI_API_KEY'),
|
||||
"temperature": 0.7,
|
||||
"max_tokens": 2000
|
||||
}
|
||||
|
||||
blog_agent = Agent(
|
||||
role="Blog Writer",
|
||||
goal="Write a blog post about AI",
|
||||
backstory="Expert at writing blog posts",
|
||||
llm=llm_config,
|
||||
)
|
||||
|
||||
blog_task = Task(
|
||||
description="Write a blog post about AI trends in 1 paragraph",
|
||||
expected_output="Well-written blog post about AI trends",
|
||||
agent=blog_agent
|
||||
)
|
||||
|
||||
agents = PraisonAIAgents(
|
||||
agents=[blog_agent],
|
||||
tasks=[blog_task],
|
||||
memory=False
|
||||
)
|
||||
|
||||
result = agents.start()
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Final test of automatic telemetry.
|
||||
"""
|
||||
|
||||
print("Step 1: Import praisonaiagents...")
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents, get_telemetry
|
||||
|
||||
print("\nStep 2: Check if telemetry is initialized...")
|
||||
telemetry = get_telemetry()
|
||||
print(f" Telemetry enabled: {telemetry.enabled}")
|
||||
print(f" PostHog available: {telemetry._posthog is not None}")
|
||||
|
||||
print("\nStep 3: Check if Agent class is instrumented...")
|
||||
print(f" Agent.__init__.__name__: {Agent.__init__.__name__}")
|
||||
|
||||
print("\nStep 4: Create an agent...")
|
||||
agent = Agent(name="Test", role="Test", goal="Test", instructions="Test")
|
||||
print(f" agent.chat wrapped: {hasattr(agent.chat, '__wrapped__')}")
|
||||
|
||||
print("\nStep 5: Create and run a simple workflow...")
|
||||
task = Task(description="Say hello", expected_output="A greeting", agent=agent)
|
||||
workflow = PraisonAIAgents(agents=[agent], tasks=[task], process="sequential")
|
||||
|
||||
# Check workflow instrumentation
|
||||
print(f" workflow.execute_task wrapped: {hasattr(workflow.execute_task, '__wrapped__')}")
|
||||
|
||||
result = workflow.start()
|
||||
print(f"\nResult: {result}")
|
||||
|
||||
print("\nStep 6: Check metrics...")
|
||||
metrics = telemetry.get_metrics()
|
||||
print(f" Metrics: {metrics['metrics']}")
|
||||
|
||||
if metrics['metrics']['agent_executions'] > 0:
|
||||
print("\n✅ Automatic telemetry is working!")
|
||||
else:
|
||||
print("\n❌ Automatic telemetry is NOT working")
|
||||
print("\nPossible reason: The auto_instrument_all() in __init__.py might be")
|
||||
print("running before the telemetry module's lazy initialization completes.")
|
||||
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from praisonaiagents import Agent, MCP
|
||||
import os
|
||||
|
||||
def test_agent_direct():
|
||||
"""Test using gpt-4o-mini directly (agent.py path)"""
|
||||
print("="*50)
|
||||
print("Testing gpt-4o-mini (agent.py path)")
|
||||
print("="*50)
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a helpful assistant that can break down complex problems.",
|
||||
llm="gpt-4o-mini",
|
||||
tools=MCP("npx -y @modelcontextprotocol/server-sequential-thinking")
|
||||
)
|
||||
|
||||
result = agent.start("What are 5 steps to make coffee?")
|
||||
print("✅ Agent direct path completed successfully")
|
||||
return result
|
||||
|
||||
def test_llm_class():
|
||||
"""Test using openai/gpt-4o-mini (llm.py path)"""
|
||||
print("\n" + "="*50)
|
||||
print("Testing openai/gpt-4o-mini (llm.py path)")
|
||||
print("="*50)
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a helpful assistant that can break down complex problems.",
|
||||
llm="openai/gpt-4o-mini",
|
||||
tools=MCP("npx -y @modelcontextprotocol/server-sequential-thinking")
|
||||
)
|
||||
|
||||
result = agent.start("What are 5 steps to make coffee?")
|
||||
print("✅ LLM class path completed successfully")
|
||||
return result
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
# Test both approaches
|
||||
result1 = test_agent_direct()
|
||||
result2 = test_llm_class()
|
||||
|
||||
print("\n" + "="*50)
|
||||
print("SUMMARY")
|
||||
print("="*50)
|
||||
print("✅ Both formats work correctly!")
|
||||
print("✅ gpt-4o-mini uses agent.py direct OpenAI calls")
|
||||
print("✅ openai/gpt-4o-mini uses llm.py LiteLLM wrapper")
|
||||
print("✅ Both support tool calling and MCP integration")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Error occurred: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test import order to debug telemetry availability.
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
print("1. Importing telemetry module directly...")
|
||||
try:
|
||||
import praisonaiagents.telemetry
|
||||
print(" ✅ Telemetry module imported successfully")
|
||||
except Exception as e:
|
||||
print(f" ❌ Failed: {type(e).__name__}: {e}")
|
||||
|
||||
print("\n2. Importing main praisonaiagents...")
|
||||
try:
|
||||
import praisonaiagents
|
||||
print(" ✅ Main module imported successfully")
|
||||
print(f" _telemetry_available: {praisonaiagents._telemetry_available}")
|
||||
except Exception as e:
|
||||
print(f" ❌ Failed: {type(e).__name__}: {e}")
|
||||
|
||||
print("\n3. Checking what prevented telemetry import...")
|
||||
# The main __init__.py tries to import from .telemetry
|
||||
# Let's see if we can import the functions directly
|
||||
try:
|
||||
from praisonaiagents.telemetry import (
|
||||
get_telemetry,
|
||||
enable_telemetry,
|
||||
disable_telemetry,
|
||||
MinimalTelemetry,
|
||||
TelemetryCollector
|
||||
)
|
||||
print(" ✅ All telemetry functions imported successfully")
|
||||
except Exception as e:
|
||||
print(f" ❌ Failed to import telemetry functions: {type(e).__name__}: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Test script to identify litellm telemetry behavior"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
# Disable litellm telemetry before importing
|
||||
os.environ["LITELLM_TELEMETRY"] = "False"
|
||||
os.environ["LITELLM_LOG"] = "ERROR"
|
||||
|
||||
print("Environment variables set:")
|
||||
print(f"LITELLM_TELEMETRY: {os.environ.get('LITELLM_TELEMETRY')}")
|
||||
print(f"LITELLM_LOG: {os.environ.get('LITELLM_LOG')}")
|
||||
|
||||
print("\nImporting litellm...")
|
||||
import litellm
|
||||
|
||||
print(f"\nChecking litellm telemetry status:")
|
||||
print(f"Has telemetry attribute: {hasattr(litellm, 'telemetry')}")
|
||||
if hasattr(litellm, 'telemetry'):
|
||||
print(f"Telemetry value: {litellm.telemetry}")
|
||||
|
||||
# Try to disable telemetry programmatically
|
||||
if hasattr(litellm, 'telemetry'):
|
||||
litellm.telemetry = False
|
||||
print(f"\nTelemetry disabled programmatically")
|
||||
|
||||
# Check callbacks
|
||||
print(f"\nCallbacks: {litellm.callbacks}")
|
||||
print(f"Success callbacks: {litellm.success_callback}")
|
||||
print(f"Async success callbacks: {litellm._async_success_callback}")
|
||||
|
||||
# Test a simple completion
|
||||
print("\n\nTesting completion (this might trigger telemetry)...")
|
||||
try:
|
||||
response = litellm.completion(
|
||||
model="gpt-3.5-turbo",
|
||||
messages=[{"role": "user", "content": "Say hi"}],
|
||||
mock_response="Hi there!"
|
||||
)
|
||||
print("Completion successful")
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
print("\nDone. Check if any network requests were made to BerriAI/litellm")
|
||||
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test manual instrumentation to verify it works.
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
from praisonaiagents.telemetry import get_telemetry
|
||||
from praisonaiagents.telemetry.integration import auto_instrument_all
|
||||
|
||||
# Manually call auto_instrument_all AFTER importing classes
|
||||
print("1. Calling auto_instrument_all()...")
|
||||
auto_instrument_all()
|
||||
|
||||
print("\n2. Creating agent and task...")
|
||||
agent = Agent(
|
||||
name="Calculator",
|
||||
role="Math Expert",
|
||||
goal="Perform calculations",
|
||||
instructions="You are a helpful math expert."
|
||||
)
|
||||
|
||||
task = Task(
|
||||
description="Calculate 3 + 3",
|
||||
expected_output="The sum",
|
||||
agent=agent
|
||||
)
|
||||
|
||||
print("\n3. Creating workflow...")
|
||||
workflow = PraisonAIAgents(
|
||||
agents=[agent],
|
||||
tasks=[task],
|
||||
process="sequential"
|
||||
)
|
||||
|
||||
print("\n4. Running workflow...")
|
||||
result = workflow.start()
|
||||
print(f"Result: {result}")
|
||||
|
||||
print("\n5. Checking telemetry metrics...")
|
||||
telemetry = get_telemetry()
|
||||
metrics = telemetry.get_metrics()
|
||||
print(f"Telemetry metrics: {metrics['metrics']}")
|
||||
print(f"PostHog available: {telemetry._posthog is not None}")
|
||||
|
||||
if metrics['metrics']['agent_executions'] > 0:
|
||||
print("\n✅ SUCCESS! Telemetry is working correctly.")
|
||||
print(" Data will be sent to PostHog on program exit.")
|
||||
else:
|
||||
print("\n❌ FAILED! Telemetry metrics are still 0.")
|
||||
|
||||
# Additional debugging
|
||||
print("\nDebugging info:")
|
||||
print(f" agent.chat wrapped: {hasattr(agent.chat, '__wrapped__')}")
|
||||
print(f" workflow.execute_task wrapped: {hasattr(workflow.execute_task, '__wrapped__')}")
|
||||
@@ -0,0 +1,64 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test PostHog integration in telemetry.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
# Ensure telemetry is enabled
|
||||
if 'PRAISONAI_TELEMETRY_DISABLED' in os.environ:
|
||||
del os.environ['PRAISONAI_TELEMETRY_DISABLED']
|
||||
if 'PRAISONAI_DISABLE_TELEMETRY' in os.environ:
|
||||
del os.environ['PRAISONAI_DISABLE_TELEMETRY']
|
||||
if 'DO_NOT_TRACK' in os.environ:
|
||||
del os.environ['DO_NOT_TRACK']
|
||||
|
||||
# Test PostHog availability
|
||||
print("=== Testing PostHog Integration ===")
|
||||
print("\n1. Checking PostHog import:")
|
||||
try:
|
||||
from posthog import Posthog
|
||||
print("✓ PostHog is available")
|
||||
except ImportError as e:
|
||||
print("✗ PostHog is NOT available")
|
||||
print(f" Error: {e}")
|
||||
print("\n To fix: pip install posthog")
|
||||
sys.exit(1)
|
||||
|
||||
# Test telemetry initialization
|
||||
print("\n2. Testing telemetry initialization:")
|
||||
from praisonaiagents.telemetry import get_telemetry
|
||||
|
||||
telemetry = get_telemetry()
|
||||
print(f"✓ Telemetry enabled: {telemetry.enabled}")
|
||||
print(f"✓ Session ID: {telemetry.session_id}")
|
||||
|
||||
# Check PostHog client
|
||||
print("\n3. Checking PostHog client:")
|
||||
if hasattr(telemetry, '_posthog') and telemetry._posthog:
|
||||
print("✓ PostHog client is initialized")
|
||||
print(f" API Key: {telemetry._posthog.api_key[:10]}...")
|
||||
print(f" Host: {telemetry._posthog.host}")
|
||||
else:
|
||||
print("✗ PostHog client is NOT initialized")
|
||||
|
||||
# Test tracking
|
||||
print("\n4. Testing event tracking:")
|
||||
telemetry.track_agent_execution("TestAgent", success=True)
|
||||
telemetry.track_task_completion("TestTask", success=True)
|
||||
telemetry.track_tool_usage("test_tool", success=True)
|
||||
print("✓ Events tracked locally")
|
||||
|
||||
# Test flush (which should send to PostHog)
|
||||
print("\n5. Testing flush (should send to PostHog):")
|
||||
telemetry.flush()
|
||||
print("✓ Flush completed")
|
||||
|
||||
# Check metrics
|
||||
print("\n6. Current metrics:")
|
||||
metrics = telemetry.get_metrics()
|
||||
for key, value in metrics.items():
|
||||
print(f" {key}: {value}")
|
||||
|
||||
print("\n=== PostHog Test Complete ===")
|
||||
@@ -0,0 +1,139 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Detailed test of PostHog integration showing why events might not be sent.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
# Ensure telemetry is enabled
|
||||
for var in ['PRAISONAI_TELEMETRY_DISABLED', 'PRAISONAI_DISABLE_TELEMETRY', 'DO_NOT_TRACK']:
|
||||
if var in os.environ:
|
||||
del os.environ[var]
|
||||
|
||||
print("=== Detailed PostHog Test ===")
|
||||
|
||||
# Test PostHog directly
|
||||
print("\n1. Testing PostHog directly:")
|
||||
try:
|
||||
from posthog import Posthog
|
||||
|
||||
# Create a PostHog client directly
|
||||
posthog_client = Posthog(
|
||||
project_api_key='phc_skZpl3eFLQJ4iYjsERNMbCO6jfeSJi2vyZlPahKgxZ7',
|
||||
host='https://eu.i.posthog.com'
|
||||
)
|
||||
|
||||
# Send a test event
|
||||
print(" Sending test event...")
|
||||
posthog_client.capture(
|
||||
distinct_id='test-user-123',
|
||||
event='direct_test_event',
|
||||
properties={
|
||||
'test': True,
|
||||
'timestamp': time.time()
|
||||
}
|
||||
)
|
||||
|
||||
# Flush to ensure event is sent
|
||||
print(" Flushing...")
|
||||
posthog_client.flush()
|
||||
|
||||
print("✓ Direct PostHog test completed")
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Direct PostHog test failed: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
# Test telemetry integration
|
||||
print("\n2. Testing telemetry integration:")
|
||||
from praisonaiagents.telemetry.telemetry import MinimalTelemetry
|
||||
|
||||
# Create a new telemetry instance with debug output
|
||||
class DebugTelemetry(MinimalTelemetry):
|
||||
def flush(self):
|
||||
"""Override flush to add debug output."""
|
||||
if not self.enabled:
|
||||
print(" [DEBUG] Telemetry disabled, skipping flush")
|
||||
return
|
||||
|
||||
metrics = self.get_metrics()
|
||||
print(f" [DEBUG] Flushing metrics: {metrics}")
|
||||
|
||||
# Check PostHog client
|
||||
if hasattr(self, '_posthog') and self._posthog:
|
||||
print(" [DEBUG] PostHog client exists")
|
||||
print(f" [DEBUG] PostHog API key: {self._posthog.api_key[:20]}...")
|
||||
print(f" [DEBUG] PostHog host: {self._posthog.host}")
|
||||
|
||||
try:
|
||||
# Send events
|
||||
print(" [DEBUG] Sending sdk_used event...")
|
||||
self._posthog.capture(
|
||||
distinct_id='anonymous',
|
||||
event='sdk_used',
|
||||
properties={
|
||||
'version': self._environment['framework_version'],
|
||||
'os': self._environment['os_type'],
|
||||
'python_version': self._environment['python_version'],
|
||||
'session_id': self.session_id,
|
||||
'metrics': self._metrics,
|
||||
'$process_person_profile': False
|
||||
}
|
||||
)
|
||||
|
||||
print(" [DEBUG] Sending test event...")
|
||||
self._posthog.capture('test-id', 'test-event')
|
||||
|
||||
# Explicitly flush PostHog
|
||||
print(" [DEBUG] Flushing PostHog client...")
|
||||
self._posthog.flush()
|
||||
|
||||
print(" [DEBUG] PostHog flush completed")
|
||||
except Exception as e:
|
||||
print(f" [DEBUG] PostHog error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
else:
|
||||
print(" [DEBUG] No PostHog client available")
|
||||
|
||||
# Reset counters
|
||||
for key in self._metrics:
|
||||
if isinstance(self._metrics[key], int):
|
||||
self._metrics[key] = 0
|
||||
|
||||
# Create debug telemetry instance
|
||||
telemetry = DebugTelemetry(enabled=True)
|
||||
|
||||
# Track some events
|
||||
print("\n3. Tracking events:")
|
||||
telemetry.track_agent_execution("TestAgent", success=True)
|
||||
telemetry.track_task_completion("TestTask", success=True)
|
||||
telemetry.track_tool_usage("calculator", success=True)
|
||||
print("✓ Events tracked")
|
||||
|
||||
# Flush with debug output
|
||||
print("\n4. Flushing telemetry:")
|
||||
telemetry.flush()
|
||||
|
||||
# Wait a moment for async operations
|
||||
print("\n5. Waiting for async operations...")
|
||||
time.sleep(2)
|
||||
|
||||
print("\n=== Test Complete ===")
|
||||
|
||||
# Additional debug info
|
||||
print("\n6. Debug information:")
|
||||
print(f" POSTHOG_AVAILABLE: {telemetry.logger.parent.manager.loggerDict.get('posthog', 'Not found')}")
|
||||
print(f" Telemetry module location: {MinimalTelemetry.__module__}")
|
||||
|
||||
# Check if PostHog is properly installed
|
||||
print("\n7. PostHog installation check:")
|
||||
try:
|
||||
import posthog
|
||||
print(f" PostHog version: {posthog.VERSION}")
|
||||
print(f" PostHog location: {posthog.__file__}")
|
||||
except Exception as e:
|
||||
print(f" PostHog check failed: {e}")
|
||||
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test PostHog integration directly to verify it's working.
|
||||
"""
|
||||
|
||||
import os
|
||||
from praisonaiagents.telemetry import get_telemetry
|
||||
from praisonaiagents.telemetry.integration import auto_instrument_all
|
||||
|
||||
# Enable automatic telemetry instrumentation
|
||||
auto_instrument_all()
|
||||
|
||||
telemetry = get_telemetry()
|
||||
print(f"Telemetry enabled: {telemetry.enabled}")
|
||||
print(f"PostHog available: {telemetry._posthog is not None}")
|
||||
|
||||
if telemetry.enabled and telemetry._posthog:
|
||||
# Manually track some events
|
||||
telemetry.track_agent_execution("TestAgent", success=True)
|
||||
telemetry.track_task_completion("TestTask", success=True)
|
||||
telemetry.track_tool_usage("TestTool")
|
||||
|
||||
# Get metrics before flush
|
||||
metrics = telemetry.get_metrics()
|
||||
print(f"\nMetrics before flush:")
|
||||
print(f"- Agent executions: {metrics['metrics']['agent_executions']}")
|
||||
print(f"- Task completions: {metrics['metrics']['task_completions']}")
|
||||
print(f"- Tool calls: {metrics['metrics']['tool_calls']}")
|
||||
|
||||
# Manually flush to send to PostHog
|
||||
print("\nFlushing telemetry to PostHog...")
|
||||
telemetry.flush()
|
||||
|
||||
# Get metrics after flush (should be reset)
|
||||
metrics = telemetry.get_metrics()
|
||||
print(f"\nMetrics after flush (should be reset):")
|
||||
print(f"- Agent executions: {metrics['metrics']['agent_executions']}")
|
||||
print(f"- Task completions: {metrics['metrics']['task_completions']}")
|
||||
print(f"- Tool calls: {metrics['metrics']['tool_calls']}")
|
||||
|
||||
print("\n✅ If no errors above, PostHog integration is working!")
|
||||
else:
|
||||
print("\n❌ Telemetry or PostHog is not available")
|
||||
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test PostHog initialization error.
|
||||
"""
|
||||
|
||||
from posthog import Posthog
|
||||
|
||||
print("Testing PostHog initialization with telemetry parameters...")
|
||||
try:
|
||||
posthog_client = Posthog(
|
||||
project_api_key='phc_skZpl3eFLQJ4iYjsERNMbCO6jfeSJi2vyZlPahKgxZ7',
|
||||
host='https://eu.i.posthog.com',
|
||||
disable_geoip=True,
|
||||
events_to_ignore=['test-event']
|
||||
)
|
||||
print(f"✅ PostHog client created: {posthog_client}")
|
||||
except Exception as e:
|
||||
print(f"❌ PostHog initialization failed: {type(e).__name__}: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test PostHog import and initialization.
|
||||
"""
|
||||
|
||||
print("1. Testing PostHog import...")
|
||||
try:
|
||||
from posthog import Posthog
|
||||
print("✅ PostHog imported successfully")
|
||||
except ImportError as e:
|
||||
print(f"❌ PostHog import failed: {e}")
|
||||
exit(1)
|
||||
|
||||
print("\n2. Testing PostHog initialization...")
|
||||
try:
|
||||
posthog_client = Posthog(
|
||||
project_api_key='phc_skZpl3eFLQJ4iYjsERNMbCO6jfeSJi2vyZlPahKgxZ7',
|
||||
host='https://eu.i.posthog.com'
|
||||
)
|
||||
print("✅ PostHog client created successfully")
|
||||
except Exception as e:
|
||||
print(f"❌ PostHog initialization failed: {e}")
|
||||
exit(1)
|
||||
|
||||
print("\n3. Testing PostHog capture...")
|
||||
try:
|
||||
posthog_client.capture(
|
||||
distinct_id='test-user',
|
||||
event='test-event',
|
||||
properties={'test': True}
|
||||
)
|
||||
print("✅ PostHog capture successful")
|
||||
except Exception as e:
|
||||
print(f"❌ PostHog capture failed: {e}")
|
||||
|
||||
print("\n4. Testing telemetry module...")
|
||||
from praisonaiagents.telemetry.telemetry import MinimalTelemetry, POSTHOG_AVAILABLE
|
||||
print(f"POSTHOG_AVAILABLE in telemetry module: {POSTHOG_AVAILABLE}")
|
||||
|
||||
print("\n5. Creating MinimalTelemetry instance...")
|
||||
telemetry = MinimalTelemetry(enabled=True)
|
||||
print(f"Telemetry enabled: {telemetry.enabled}")
|
||||
print(f"Telemetry _posthog: {telemetry._posthog}")
|
||||
|
||||
print("\n6. Checking get_telemetry()...")
|
||||
from praisonaiagents.telemetry import get_telemetry
|
||||
global_telemetry = get_telemetry()
|
||||
print(f"Global telemetry enabled: {global_telemetry.enabled}")
|
||||
print(f"Global telemetry _posthog: {global_telemetry._posthog}")
|
||||
print(f"Global telemetry session_id: {global_telemetry.session_id}")
|
||||
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Test self-reflection functionality in both Agent and LLM classes"""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
from praisonaiagents.llm import LLM
|
||||
|
||||
def test_agent_self_reflection():
|
||||
"""Test self-reflection in Agent class"""
|
||||
print("=== Testing Agent Self-Reflection ===")
|
||||
agent = Agent(
|
||||
name="ReflectiveAgent",
|
||||
instructions="You are a helpful assistant.",
|
||||
llm="gpt-4o-mini",
|
||||
self_reflect=True,
|
||||
min_reflect=1,
|
||||
max_reflect=3
|
||||
)
|
||||
|
||||
response = agent.start("What is 2+2? Be brief.")
|
||||
print(f"Agent Response: {response}")
|
||||
print()
|
||||
|
||||
def test_llm_self_reflection():
|
||||
"""Test self-reflection in LLM class directly"""
|
||||
print("=== Testing LLM Self-Reflection ===")
|
||||
llm = LLM(model="gpt-4o-mini")
|
||||
|
||||
response = llm.get_response(
|
||||
prompt="What is 2+2? Be brief.",
|
||||
system_prompt="You are a helpful assistant.",
|
||||
self_reflect=True,
|
||||
min_reflect=1,
|
||||
max_reflect=3,
|
||||
verbose=True
|
||||
)
|
||||
print(f"LLM Response: {response}")
|
||||
print()
|
||||
|
||||
def test_llm_no_reflection():
|
||||
"""Test LLM without self-reflection for comparison"""
|
||||
print("=== Testing LLM Without Self-Reflection ===")
|
||||
llm = LLM(model="gpt-4o-mini")
|
||||
|
||||
response = llm.get_response(
|
||||
prompt="What is 2+2? Be brief.",
|
||||
system_prompt="You are a helpful assistant.",
|
||||
self_reflect=False,
|
||||
verbose=True
|
||||
)
|
||||
print(f"LLM Response: {response}")
|
||||
print()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Test without reflection first
|
||||
test_llm_no_reflection()
|
||||
|
||||
# Test with reflection
|
||||
test_llm_self_reflection()
|
||||
|
||||
# Test agent reflection
|
||||
test_agent_self_reflection()
|
||||
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from praisonaiagents import Agent, MCP
|
||||
import os
|
||||
|
||||
def test_agent_direct():
|
||||
"""Test using gpt-4o-mini directly (agent.py path)"""
|
||||
print("=" * 50)
|
||||
print("Testing gpt-4o-mini (agent.py path)")
|
||||
print("=" * 50)
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a helpful assistant that can break down complex problems.",
|
||||
llm="gpt-4o-mini",
|
||||
tools=MCP("npx -y @modelcontextprotocol/server-sequential-thinking")
|
||||
)
|
||||
|
||||
result = agent.start("What are 3 steps to make coffee?")
|
||||
print("✅ Agent direct path completed successfully")
|
||||
return result
|
||||
|
||||
def test_llm_class():
|
||||
"""Test using openai/gpt-4o-mini (llm.py path)"""
|
||||
print("\n" + "=" * 50)
|
||||
print("Testing openai/gpt-4o-mini (llm.py path)")
|
||||
print("=" * 50)
|
||||
|
||||
agent = Agent(
|
||||
instructions="You are a helpful assistant that can break down complex problems.",
|
||||
llm="openai/gpt-4o-mini",
|
||||
tools=MCP("npx -y @modelcontextprotocol/server-sequential-thinking")
|
||||
)
|
||||
|
||||
result = agent.start("What are 3 steps to make coffee?")
|
||||
print("✅ LLM class path completed successfully")
|
||||
return result
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
# Test both approaches
|
||||
result1 = test_agent_direct()
|
||||
result2 = test_llm_class()
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
print("SUMMARY")
|
||||
print("=" * 50)
|
||||
print("✅ Both formats work correctly!")
|
||||
print("✅ gpt-4o-mini uses agent.py direct OpenAI calls")
|
||||
print("✅ openai/gpt-4o-mini uses llm.py LiteLLM wrapper")
|
||||
print("✅ Both support sequential tool calling and MCP integration")
|
||||
print("✅ Sequential thinking tool works properly in both modes")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Error occurred: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test that telemetry works automatically without manual instrumentation.
|
||||
"""
|
||||
|
||||
# NO manual telemetry setup - it should work automatically!
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
|
||||
# Create a simple agent
|
||||
agent = Agent(
|
||||
name="AutoTelemetryTest",
|
||||
role="Math Expert",
|
||||
goal="Perform calculations",
|
||||
instructions="You are a helpful math expert."
|
||||
)
|
||||
|
||||
# Create a task
|
||||
task = Task(
|
||||
description="Calculate 5 + 5",
|
||||
expected_output="The sum",
|
||||
agent=agent
|
||||
)
|
||||
|
||||
# Create and run workflow
|
||||
workflow = PraisonAIAgents(
|
||||
agents=[agent],
|
||||
tasks=[task],
|
||||
process="sequential"
|
||||
)
|
||||
|
||||
print("Running workflow (telemetry should be automatic)...")
|
||||
result = workflow.start()
|
||||
print(f"Result: {result}")
|
||||
|
||||
# Check if telemetry was collected
|
||||
from praisonaiagents.telemetry import get_telemetry
|
||||
telemetry = get_telemetry()
|
||||
|
||||
if telemetry.enabled:
|
||||
metrics = telemetry.get_metrics()
|
||||
print(f"\n✅ Telemetry is working automatically!")
|
||||
print(f"- Agent executions: {metrics['metrics']['agent_executions']}")
|
||||
print(f"- Task completions: {metrics['metrics']['task_completions']}")
|
||||
print(f"- Session ID: {metrics['session_id']}")
|
||||
print("\nTelemetry data will be sent to PostHog on program exit.")
|
||||
else:
|
||||
print("\n❌ Telemetry is disabled")
|
||||
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Test to verify litellm telemetry is properly disabled"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
print("Testing litellm telemetry is disabled...")
|
||||
|
||||
# Test 1: Check environment variable is set
|
||||
print(f"\n1. Environment variable LITELLM_TELEMETRY: {os.environ.get('LITELLM_TELEMETRY', 'NOT SET')}")
|
||||
|
||||
# Test 2: Import praisonaiagents and check if telemetry is disabled
|
||||
try:
|
||||
from praisonaiagents.llm import LLM
|
||||
print("2. Successfully imported LLM from praisonaiagents")
|
||||
|
||||
# Check if litellm was imported and telemetry is disabled
|
||||
import litellm
|
||||
print(f"3. litellm.telemetry = {litellm.telemetry}")
|
||||
|
||||
# Test 3: Create an LLM instance
|
||||
llm = LLM(model="gpt-3.5-turbo")
|
||||
print("4. Successfully created LLM instance")
|
||||
|
||||
# Check telemetry again after instance creation
|
||||
print(f"5. After LLM creation, litellm.telemetry = {litellm.telemetry}")
|
||||
|
||||
# Test 4: Try a mock completion
|
||||
print("\n6. Testing mock completion...")
|
||||
response = litellm.completion(
|
||||
model="gpt-3.5-turbo",
|
||||
messages=[{"role": "user", "content": "test"}],
|
||||
mock_response="test response"
|
||||
)
|
||||
print(" Mock completion successful")
|
||||
|
||||
# Final check
|
||||
print(f"\n7. Final check: litellm.telemetry = {litellm.telemetry}")
|
||||
|
||||
if litellm.telemetry == False:
|
||||
print("\n✅ SUCCESS: Telemetry is properly disabled!")
|
||||
else:
|
||||
print("\n❌ FAILURE: Telemetry is still enabled!")
|
||||
sys.exit(1)
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ ERROR: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
print("\nAll tests passed! Telemetry should be disabled.")
|
||||
@@ -0,0 +1,126 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test to demonstrate why telemetry isn't being sent by default.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
# Ensure telemetry is enabled
|
||||
for var in ['PRAISONAI_TELEMETRY_DISABLED', 'PRAISONAI_DISABLE_TELEMETRY', 'DO_NOT_TRACK']:
|
||||
if var in os.environ:
|
||||
del os.environ[var]
|
||||
|
||||
print("=== Testing Telemetry Integration ===")
|
||||
|
||||
# Test 1: Default behavior (no telemetry)
|
||||
print("\n1. Testing default Agent behavior:")
|
||||
from praisonaiagents import Agent, Task
|
||||
|
||||
agent = Agent(
|
||||
name="TestAgent",
|
||||
role="Tester",
|
||||
goal="Test telemetry",
|
||||
backstory="I test things",
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
task = Task(
|
||||
description="Say hello",
|
||||
expected_output="A greeting",
|
||||
agent=agent
|
||||
)
|
||||
|
||||
# Check if agent has telemetry
|
||||
print(f" Agent has telemetry: {hasattr(agent, '_telemetry')}")
|
||||
print(f" Agent methods: {[m for m in dir(agent) if 'telemetry' in m.lower()]}")
|
||||
|
||||
# Test 2: Manual telemetry integration
|
||||
print("\n2. Testing manual telemetry integration:")
|
||||
from praisonaiagents.telemetry import get_telemetry
|
||||
from praisonaiagents.telemetry.integration import instrument_agent
|
||||
|
||||
telemetry = get_telemetry()
|
||||
print(f" Telemetry enabled: {telemetry.enabled}")
|
||||
|
||||
# Manually instrument the agent
|
||||
instrument_agent(agent, telemetry)
|
||||
print(" Agent instrumented manually")
|
||||
|
||||
# Test 3: Auto-instrumentation
|
||||
print("\n3. Testing auto-instrumentation:")
|
||||
from praisonaiagents.telemetry.integration import auto_instrument_all
|
||||
|
||||
# Enable auto-instrumentation
|
||||
auto_instrument_all(telemetry)
|
||||
print(" Auto-instrumentation enabled")
|
||||
|
||||
# Create a new agent to test auto-instrumentation
|
||||
new_agent = Agent(
|
||||
name="AutoInstrumentedAgent",
|
||||
role="Tester",
|
||||
goal="Test auto telemetry",
|
||||
backstory="I test auto-instrumentation",
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
print(f" New agent instrumented: {hasattr(new_agent.execute, '__wrapped__')}")
|
||||
|
||||
# Test 4: Check if telemetry data is collected
|
||||
print("\n4. Checking telemetry metrics:")
|
||||
initial_metrics = telemetry.get_metrics()
|
||||
print(f" Initial metrics: {initial_metrics['metrics']}")
|
||||
|
||||
# The problem: telemetry is never flushed automatically!
|
||||
print("\n5. The Issue:")
|
||||
print(" ✗ Telemetry is implemented but NOT automatically integrated")
|
||||
print(" ✗ Even if integrated, flush() is never called automatically")
|
||||
print(" ✗ PostHog events are only sent when flush() is explicitly called")
|
||||
|
||||
# Test 5: Solution - manual flush
|
||||
print("\n6. Solution - Manual flush:")
|
||||
telemetry.flush()
|
||||
print(" ✓ Manual flush called - events sent to PostHog")
|
||||
|
||||
# Test 6: Workflow integration
|
||||
print("\n7. Testing workflow integration:")
|
||||
from praisonaiagents import PraisonAIAgents
|
||||
|
||||
agents = [
|
||||
Agent(name="Agent1", role="First", goal="Do first task", backstory="I'm first"),
|
||||
Agent(name="Agent2", role="Second", goal="Do second task", backstory="I'm second")
|
||||
]
|
||||
|
||||
tasks = [
|
||||
Task(description="Task 1", expected_output="Output 1", agent=agents[0]),
|
||||
Task(description="Task 2", expected_output="Output 2", agent=agents[1])
|
||||
]
|
||||
|
||||
workflow = PraisonAIAgents(
|
||||
agents=agents,
|
||||
tasks=tasks,
|
||||
process="sequential"
|
||||
)
|
||||
|
||||
print(f" Workflow has telemetry integration: {hasattr(workflow.start, '__wrapped__')}")
|
||||
|
||||
print("\n=== Summary of Issues ===")
|
||||
print("\n1. No automatic integration:")
|
||||
print(" - Agent and PraisonAIAgents classes don't automatically use telemetry")
|
||||
print(" - Need to manually call instrument_agent() or auto_instrument_all()")
|
||||
|
||||
print("\n2. No automatic flush:")
|
||||
print(" - Even when integrated, telemetry data is only collected locally")
|
||||
print(" - flush() must be called explicitly to send data to PostHog")
|
||||
print(" - No automatic flush on program exit or periodic flush")
|
||||
|
||||
print("\n3. Missing integration points:")
|
||||
print(" - Agent.__init__ doesn't initialize telemetry")
|
||||
print(" - Agent.execute() doesn't track execution")
|
||||
print(" - PraisonAIAgents doesn't track workflow execution")
|
||||
print(" - No atexit handler to flush on program termination")
|
||||
|
||||
print("\n=== Recommendations ===")
|
||||
print("\n1. Add automatic integration in Agent.__init__ and PraisonAIAgents.__init__")
|
||||
print("2. Add atexit handler to flush telemetry on program exit")
|
||||
print("3. Add periodic flush (e.g., every 100 events or 60 seconds)")
|
||||
print("4. Document telemetry behavior clearly for users")
|
||||
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Simple test to check telemetry.
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
from praisonaiagents.telemetry import get_telemetry
|
||||
|
||||
# Create workflow
|
||||
agent = Agent(name="Test", role="Tester", goal="Test", instructions="Test")
|
||||
task = Task(description="Test", expected_output="Test", agent=agent)
|
||||
workflow = PraisonAIAgents(agents=[agent], tasks=[task], process="sequential")
|
||||
|
||||
# Run workflow
|
||||
print("Running workflow...")
|
||||
result = workflow.start()
|
||||
|
||||
# Check telemetry
|
||||
telemetry = get_telemetry()
|
||||
metrics = telemetry.get_metrics()
|
||||
print(f"\nTelemetry metrics: {metrics['metrics']}")
|
||||
print(f"Session ID: {metrics['session_id']}")
|
||||
|
||||
# Check if telemetry will be sent
|
||||
print(f"\nTelemetry enabled: {telemetry.enabled}")
|
||||
print(f"PostHog client available: {telemetry._posthog is not None}")
|
||||
|
||||
if telemetry._posthog:
|
||||
print("\n✅ Telemetry will be sent to PostHog on program exit")
|
||||
@@ -0,0 +1,294 @@
|
||||
"""
|
||||
State-Based Workflow Control Example
|
||||
===================================
|
||||
|
||||
This example shows how to use state for:
|
||||
1. Conditional task execution based on state
|
||||
2. Loop control using state
|
||||
3. Dynamic workflow modification
|
||||
4. State-based error handling and recovery
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
from typing import Dict, Any, List
|
||||
import random
|
||||
import json
|
||||
|
||||
# Tool functions that use state for control flow
|
||||
def analyze_data_quality() -> Dict[str, Any]:
|
||||
"""Analyze data and set quality state"""
|
||||
# Simulate data quality check
|
||||
quality_score = random.uniform(0.5, 1.0)
|
||||
has_errors = random.choice([True, False])
|
||||
|
||||
# Set state based on analysis
|
||||
workflow.set_state("quality_score", quality_score)
|
||||
workflow.set_state("has_errors", has_errors)
|
||||
workflow.set_state("data_status", "analyzed")
|
||||
|
||||
if quality_score < 0.7:
|
||||
workflow.set_state("quality_level", "poor")
|
||||
elif quality_score < 0.85:
|
||||
workflow.set_state("quality_level", "moderate")
|
||||
else:
|
||||
workflow.set_state("quality_level", "good")
|
||||
|
||||
return {
|
||||
"quality_score": quality_score,
|
||||
"has_errors": has_errors,
|
||||
"quality_level": workflow.get_state("quality_level"),
|
||||
"recommendation": "clean_data" if has_errors or quality_score < 0.8 else "proceed"
|
||||
}
|
||||
|
||||
def clean_data_based_on_state() -> Dict[str, Any]:
|
||||
"""Clean data based on current state"""
|
||||
quality_score = workflow.get_state("quality_score", 0)
|
||||
has_errors = workflow.get_state("has_errors", False)
|
||||
cleaning_attempts = workflow.get_state("cleaning_attempts", 0)
|
||||
|
||||
# Increment cleaning attempts
|
||||
workflow.set_state("cleaning_attempts", cleaning_attempts + 1)
|
||||
|
||||
# Simulate cleaning improvement
|
||||
improvement = random.uniform(0.1, 0.2)
|
||||
new_quality = min(quality_score + improvement, 0.95)
|
||||
|
||||
workflow.set_state("quality_score", new_quality)
|
||||
workflow.set_state("has_errors", False)
|
||||
workflow.set_state("last_cleaning_improvement", improvement)
|
||||
|
||||
# Determine if more cleaning needed
|
||||
if new_quality < 0.8 and cleaning_attempts < 3:
|
||||
status = "needs_more_cleaning"
|
||||
else:
|
||||
status = "cleaning_complete"
|
||||
workflow.set_state("data_status", "cleaned")
|
||||
|
||||
return {
|
||||
"previous_score": quality_score,
|
||||
"new_score": new_quality,
|
||||
"improvement": improvement,
|
||||
"attempts": cleaning_attempts + 1,
|
||||
"status": status
|
||||
}
|
||||
|
||||
def process_batch_with_state() -> Dict[str, Any]:
|
||||
"""Process data in batches using state to track progress"""
|
||||
# Initialize batch processing state
|
||||
if not workflow.has_state("batch_total"):
|
||||
batch_total = random.randint(5, 10)
|
||||
workflow.set_state("batch_total", batch_total)
|
||||
workflow.set_state("batch_current", 0)
|
||||
workflow.set_state("batch_results", [])
|
||||
|
||||
# Get current state
|
||||
batch_total = workflow.get_state("batch_total")
|
||||
batch_current = workflow.get_state("batch_current")
|
||||
batch_results = workflow.get_state("batch_results", [])
|
||||
|
||||
# Process current batch
|
||||
batch_current += 1
|
||||
result = {
|
||||
"batch_number": batch_current,
|
||||
"records_processed": random.randint(100, 500),
|
||||
"errors": random.randint(0, 5)
|
||||
}
|
||||
batch_results.append(result)
|
||||
|
||||
# Update state
|
||||
workflow.set_state("batch_current", batch_current)
|
||||
workflow.set_state("batch_results", batch_results)
|
||||
|
||||
# Determine if more batches
|
||||
if batch_current < batch_total:
|
||||
status = "more_batches"
|
||||
workflow.set_state("batch_status", "in_progress")
|
||||
else:
|
||||
status = "all_batches_complete"
|
||||
workflow.set_state("batch_status", "completed")
|
||||
workflow.set_state("total_records", sum(r["records_processed"] for r in batch_results))
|
||||
workflow.set_state("total_errors", sum(r["errors"] for r in batch_results))
|
||||
|
||||
return {
|
||||
"batch": result,
|
||||
"progress": f"{batch_current}/{batch_total}",
|
||||
"status": status,
|
||||
"remaining": batch_total - batch_current
|
||||
}
|
||||
|
||||
def generate_report_from_state() -> Dict[str, Any]:
|
||||
"""Generate comprehensive report from accumulated state"""
|
||||
# Gather all relevant state
|
||||
report = {
|
||||
"data_quality": {
|
||||
"initial_score": workflow.get_state("quality_score", 0),
|
||||
"quality_level": workflow.get_state("quality_level", "unknown"),
|
||||
"cleaning_attempts": workflow.get_state("cleaning_attempts", 0),
|
||||
"final_status": workflow.get_state("data_status", "unknown")
|
||||
},
|
||||
"batch_processing": {
|
||||
"total_batches": workflow.get_state("batch_total", 0),
|
||||
"completed_batches": workflow.get_state("batch_current", 0),
|
||||
"total_records": workflow.get_state("total_records", 0),
|
||||
"total_errors": workflow.get_state("total_errors", 0),
|
||||
"status": workflow.get_state("batch_status", "not_started")
|
||||
},
|
||||
"workflow_metadata": {
|
||||
"has_errors": workflow.get_state("has_errors", False),
|
||||
"state_keys": list(workflow.get_all_state().keys()),
|
||||
"state_size": len(workflow.get_all_state())
|
||||
}
|
||||
}
|
||||
|
||||
# Calculate summary metrics
|
||||
if workflow.has_state("batch_results"):
|
||||
batch_results = workflow.get_state("batch_results")
|
||||
report["batch_processing"]["average_records_per_batch"] = (
|
||||
sum(r["records_processed"] for r in batch_results) / len(batch_results)
|
||||
if batch_results else 0
|
||||
)
|
||||
|
||||
# Set final state
|
||||
workflow.set_state("final_report_generated", True)
|
||||
workflow.set_state("workflow_complete", True)
|
||||
|
||||
return report
|
||||
|
||||
def check_state_conditions() -> str:
|
||||
"""Check various state conditions for decision making"""
|
||||
quality_level = workflow.get_state("quality_level", "unknown")
|
||||
cleaning_attempts = workflow.get_state("cleaning_attempts", 0)
|
||||
has_errors = workflow.get_state("has_errors", False)
|
||||
|
||||
if quality_level == "poor" and cleaning_attempts == 0:
|
||||
return "needs_cleaning"
|
||||
elif quality_level == "moderate" and not has_errors:
|
||||
return "can_proceed"
|
||||
elif quality_level == "good":
|
||||
return "ready_for_processing"
|
||||
elif cleaning_attempts >= 3:
|
||||
return "max_cleaning_reached"
|
||||
else:
|
||||
return "needs_analysis"
|
||||
|
||||
# Create agents
|
||||
data_analyst = Agent(
|
||||
name="DataAnalyst",
|
||||
role="Data quality analysis",
|
||||
goal="Analyze data quality and set appropriate state",
|
||||
backstory="Expert in data quality assessment",
|
||||
tools=[analyze_data_quality, check_state_conditions],
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
data_engineer = Agent(
|
||||
name="DataEngineer",
|
||||
role="Data cleaning and processing",
|
||||
goal="Clean data based on state and process in batches",
|
||||
backstory="Specialist in data transformation and batch processing",
|
||||
tools=[clean_data_based_on_state, process_batch_with_state],
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
report_generator = Agent(
|
||||
name="ReportGenerator",
|
||||
role="Report generation",
|
||||
goal="Generate comprehensive reports from workflow state",
|
||||
backstory="Expert in creating detailed analytical reports",
|
||||
tools=[generate_report_from_state],
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# Create tasks with state-based conditions
|
||||
analyze_task = Task(
|
||||
name="analyze_quality",
|
||||
description="Analyze the data quality and set initial state values",
|
||||
expected_output="Data quality analysis with recommendations",
|
||||
agent=data_analyst,
|
||||
tools=[analyze_data_quality]
|
||||
)
|
||||
|
||||
decision_task = Task(
|
||||
name="quality_decision",
|
||||
description="Check data quality state and decide next action using check_state_conditions tool",
|
||||
expected_output="Decision on whether to clean data or proceed",
|
||||
agent=data_analyst,
|
||||
tools=[check_state_conditions],
|
||||
task_type="decision",
|
||||
condition={
|
||||
"needs_cleaning": ["clean_data"],
|
||||
"can_proceed": ["process_batches"],
|
||||
"ready_for_processing": ["process_batches"],
|
||||
"max_cleaning_reached": ["process_batches"],
|
||||
"needs_analysis": ["analyze_quality"]
|
||||
},
|
||||
context=[analyze_task]
|
||||
)
|
||||
|
||||
clean_task = Task(
|
||||
name="clean_data",
|
||||
description="Clean the data to improve quality score. Check if more cleaning is needed.",
|
||||
expected_output="Cleaning results with status",
|
||||
agent=data_engineer,
|
||||
tools=[clean_data_based_on_state],
|
||||
task_type="decision",
|
||||
condition={
|
||||
"needs_more_cleaning": ["clean_data"],
|
||||
"cleaning_complete": ["process_batches"]
|
||||
}
|
||||
)
|
||||
|
||||
process_task = Task(
|
||||
name="process_batches",
|
||||
description="Process data in batches. Continue until all batches are complete.",
|
||||
expected_output="Batch processing results",
|
||||
agent=data_engineer,
|
||||
tools=[process_batch_with_state],
|
||||
task_type="loop",
|
||||
condition={
|
||||
"more_batches": ["process_batches"],
|
||||
"all_batches_complete": ["generate_report"]
|
||||
}
|
||||
)
|
||||
|
||||
report_task = Task(
|
||||
name="generate_report",
|
||||
description="Generate final report from all accumulated state data",
|
||||
expected_output="Comprehensive workflow report",
|
||||
agent=report_generator,
|
||||
tools=[generate_report_from_state]
|
||||
)
|
||||
|
||||
# Create workflow
|
||||
workflow = PraisonAIAgents(
|
||||
agents=[data_analyst, data_engineer, report_generator],
|
||||
tasks=[analyze_task, decision_task, clean_task, process_task, report_task],
|
||||
verbose=1,
|
||||
process="workflow"
|
||||
)
|
||||
|
||||
# Demonstrate state before workflow
|
||||
print("\n=== State-Based Workflow Control Demo ===")
|
||||
print("\n1. Initial state (empty):", workflow.get_all_state())
|
||||
|
||||
# Run the workflow
|
||||
print("\n2. Running workflow with state-based decisions...")
|
||||
result = workflow.start()
|
||||
|
||||
# Show final state
|
||||
print("\n3. Final workflow state:")
|
||||
final_state = workflow.get_all_state()
|
||||
print(f" Total state entries: {len(final_state)}")
|
||||
print(f" Quality level: {final_state.get('quality_level')}")
|
||||
print(f" Cleaning attempts: {final_state.get('cleaning_attempts', 0)}")
|
||||
print(f" Batches processed: {final_state.get('batch_current', 0)}/{final_state.get('batch_total', 0)}")
|
||||
print(f" Total records: {final_state.get('total_records', 0)}")
|
||||
print(f" Workflow complete: {final_state.get('workflow_complete', False)}")
|
||||
|
||||
# Display task execution path
|
||||
print("\n4. Task Execution Path:")
|
||||
for task_name, task in workflow.tasks.items():
|
||||
if hasattr(task, 'status'):
|
||||
print(f" {task_name}: {task.status}")
|
||||
|
||||
print("\n=== Demo Complete ===")
|
||||
@@ -0,0 +1,259 @@
|
||||
"""
|
||||
State Management Example for PraisonAI Agents
|
||||
============================================
|
||||
|
||||
This example demonstrates various state management features:
|
||||
1. Workflow-level state management
|
||||
2. Session-level state persistence
|
||||
3. State sharing between agents
|
||||
4. State-based decision making
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents, Session
|
||||
from typing import Dict, Any
|
||||
import json
|
||||
import time
|
||||
|
||||
# Create a session for persistent state
|
||||
session = Session(session_id="state_demo_session", user_id="demo_user")
|
||||
|
||||
# Tool functions that interact with state
|
||||
def initialize_project_state() -> Dict[str, Any]:
|
||||
"""Initialize project state with default values"""
|
||||
# Access workflow state from global workflow variable
|
||||
workflow.set_state("project_name", "AI Assistant Project")
|
||||
workflow.set_state("stage", "planning")
|
||||
workflow.set_state("features", [])
|
||||
workflow.set_state("completed_features", [])
|
||||
workflow.set_state("budget", 100000)
|
||||
workflow.set_state("spent", 0)
|
||||
|
||||
# Also save to session for persistence
|
||||
session.set_state("last_project", "AI Assistant Project")
|
||||
session.set_state("total_projects", session.get_state("total_projects", 0) + 1)
|
||||
|
||||
return {
|
||||
"status": "initialized",
|
||||
"project_name": workflow.get_state("project_name"),
|
||||
"budget": workflow.get_state("budget"),
|
||||
"session_projects": session.get_state("total_projects")
|
||||
}
|
||||
|
||||
def add_feature_to_project(feature_name: str, estimated_cost: int) -> Dict[str, Any]:
|
||||
"""Add a feature to the project and update state"""
|
||||
features = workflow.get_state("features", [])
|
||||
budget = workflow.get_state("budget", 0)
|
||||
spent = workflow.get_state("spent", 0)
|
||||
|
||||
if spent + estimated_cost > budget:
|
||||
return {
|
||||
"status": "rejected",
|
||||
"reason": "over_budget",
|
||||
"available_budget": budget - spent
|
||||
}
|
||||
|
||||
feature = {
|
||||
"name": feature_name,
|
||||
"cost": estimated_cost,
|
||||
"status": "planned"
|
||||
}
|
||||
|
||||
features.append(feature)
|
||||
workflow.set_state("features", features)
|
||||
workflow.set_state("spent", spent + estimated_cost)
|
||||
|
||||
return {
|
||||
"status": "added",
|
||||
"feature": feature,
|
||||
"total_features": len(features),
|
||||
"remaining_budget": budget - (spent + estimated_cost)
|
||||
}
|
||||
|
||||
def implement_next_feature() -> Dict[str, Any]:
|
||||
"""Implement the next planned feature"""
|
||||
features = workflow.get_state("features", [])
|
||||
completed = workflow.get_state("completed_features", [])
|
||||
|
||||
# Find next planned feature
|
||||
next_feature = None
|
||||
for i, feature in enumerate(features):
|
||||
if feature["status"] == "planned":
|
||||
next_feature = feature
|
||||
next_feature["status"] = "completed"
|
||||
features[i] = next_feature
|
||||
break
|
||||
|
||||
if not next_feature:
|
||||
workflow.set_state("stage", "completed")
|
||||
return {
|
||||
"status": "no_more_features",
|
||||
"completed_count": len(completed),
|
||||
"stage": "completed"
|
||||
}
|
||||
|
||||
completed.append(next_feature["name"])
|
||||
workflow.set_state("features", features)
|
||||
workflow.set_state("completed_features", completed)
|
||||
workflow.set_state("stage", "implementing")
|
||||
|
||||
# Update session state
|
||||
session.set_state("last_implemented_feature", next_feature["name"])
|
||||
|
||||
return {
|
||||
"status": "implemented",
|
||||
"feature": next_feature,
|
||||
"completed_count": len(completed),
|
||||
"remaining_count": sum(1 for f in features if f["status"] == "planned")
|
||||
}
|
||||
|
||||
def check_project_status() -> Dict[str, Any]:
|
||||
"""Check the current project status from state"""
|
||||
return {
|
||||
"project_name": workflow.get_state("project_name"),
|
||||
"stage": workflow.get_state("stage"),
|
||||
"total_features": len(workflow.get_state("features", [])),
|
||||
"completed_features": len(workflow.get_state("completed_features", [])),
|
||||
"budget": workflow.get_state("budget"),
|
||||
"spent": workflow.get_state("spent"),
|
||||
"remaining": workflow.get_state("budget", 0) - workflow.get_state("spent", 0),
|
||||
"has_more_features": workflow.has_state("features") and any(
|
||||
f["status"] == "planned" for f in workflow.get_state("features", [])
|
||||
),
|
||||
"all_state": workflow.get_all_state()
|
||||
}
|
||||
|
||||
def retrieve_session_history() -> Dict[str, Any]:
|
||||
"""Retrieve historical data from session state"""
|
||||
session_state = session.restore_state()
|
||||
return {
|
||||
"total_projects": session.get_state("total_projects", 0),
|
||||
"last_project": session.get_state("last_project"),
|
||||
"last_implemented_feature": session.get_state("last_implemented_feature"),
|
||||
"session_id": session.session_id,
|
||||
"full_session_state": session_state
|
||||
}
|
||||
|
||||
# Create agents with state-aware tools
|
||||
project_manager = Agent(
|
||||
name="ProjectManager",
|
||||
role="Project planning and management",
|
||||
goal="Initialize and manage project state effectively",
|
||||
backstory="Experienced project manager who tracks all project details",
|
||||
tools=[initialize_project_state, add_feature_to_project, check_project_status],
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
developer = Agent(
|
||||
name="Developer",
|
||||
role="Feature implementation",
|
||||
goal="Implement features based on project state",
|
||||
backstory="Senior developer who implements features systematically",
|
||||
tools=[implement_next_feature, check_project_status],
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
analyst = Agent(
|
||||
name="Analyst",
|
||||
role="Project analysis and reporting",
|
||||
goal="Analyze project state and provide insights",
|
||||
backstory="Data analyst who provides comprehensive project reports",
|
||||
tools=[check_project_status, retrieve_session_history],
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# Create tasks that utilize state
|
||||
init_task = Task(
|
||||
name="initialize_project",
|
||||
description="Initialize the project state with default values",
|
||||
expected_output="Project initialization status with budget and name",
|
||||
agent=project_manager,
|
||||
tools=[initialize_project_state]
|
||||
)
|
||||
|
||||
plan_features_task = Task(
|
||||
name="plan_features",
|
||||
description="""Add the following features to the project:
|
||||
1. User Authentication (cost: 15000)
|
||||
2. Data Analytics Dashboard (cost: 25000)
|
||||
3. API Integration (cost: 20000)
|
||||
4. Mobile App Support (cost: 30000)
|
||||
5. Advanced Reporting (cost: 20000)
|
||||
|
||||
Use the add_feature_to_project tool for each feature.""",
|
||||
expected_output="List of features added with budget status",
|
||||
agent=project_manager,
|
||||
tools=[add_feature_to_project],
|
||||
context=[init_task]
|
||||
)
|
||||
|
||||
implement_features_task = Task(
|
||||
name="implement_features",
|
||||
description="""Implement all planned features one by one.
|
||||
Use implement_next_feature tool repeatedly until all features are completed.
|
||||
Check project status after each implementation.""",
|
||||
expected_output="Implementation progress and final status",
|
||||
agent=developer,
|
||||
tools=[implement_next_feature, check_project_status],
|
||||
context=[plan_features_task]
|
||||
)
|
||||
|
||||
analyze_project_task = Task(
|
||||
name="analyze_project",
|
||||
description="""Provide a comprehensive analysis of the project including:
|
||||
1. Current project state and completion status
|
||||
2. Budget utilization
|
||||
3. Historical session data
|
||||
4. Summary of all state information""",
|
||||
expected_output="Detailed project analysis report",
|
||||
agent=analyst,
|
||||
tools=[check_project_status, retrieve_session_history],
|
||||
context=[implement_features_task]
|
||||
)
|
||||
|
||||
# Create workflow with state management
|
||||
workflow = PraisonAIAgents(
|
||||
agents=[project_manager, developer, analyst],
|
||||
tasks=[init_task, plan_features_task, implement_features_task, analyze_project_task],
|
||||
verbose=1,
|
||||
process="sequential"
|
||||
)
|
||||
|
||||
# Demonstrate state operations before starting
|
||||
print("\n=== State Management Demo ===")
|
||||
print("\n1. Initial State Check:")
|
||||
print(f" Has 'project_name' state: {workflow.has_state('project_name')}")
|
||||
print(f" All state: {workflow.get_all_state()}")
|
||||
|
||||
# Run the workflow
|
||||
print("\n2. Running Workflow...")
|
||||
result = workflow.start()
|
||||
|
||||
# Demonstrate state after workflow
|
||||
print("\n3. Final State Check:")
|
||||
print(f" Project Name: {workflow.get_state('project_name')}")
|
||||
print(f" Stage: {workflow.get_state('stage')}")
|
||||
print(f" Features: {len(workflow.get_state('features', []))}")
|
||||
print(f" Completed: {workflow.get_state('completed_features', [])}")
|
||||
print(f" Budget Spent: ${workflow.get_state('spent')}/{workflow.get_state('budget')}")
|
||||
|
||||
# Test state persistence
|
||||
print("\n4. Session State Persistence:")
|
||||
print(f" Total Projects (this session): {session.get_state('total_projects')}")
|
||||
print(f" Last Project: {session.get_state('last_project')}")
|
||||
|
||||
# Save session state
|
||||
session.save_state({"workflow_completed": True, "timestamp": time.time()})
|
||||
|
||||
# Demonstrate state update and deletion
|
||||
print("\n5. State Manipulation:")
|
||||
workflow.update_state({"additional_notes": "Project completed successfully"})
|
||||
print(f" After update: {workflow.get_state('additional_notes')}")
|
||||
|
||||
workflow.delete_state("additional_notes")
|
||||
print(f" After delete, has 'additional_notes': {workflow.has_state('additional_notes')}")
|
||||
|
||||
# Clear all workflow state (optional)
|
||||
# workflow.clear_state()
|
||||
# print(f" After clear: {workflow.get_all_state()}")
|
||||
|
||||
print("\n=== State Management Demo Complete ===")
|
||||
@@ -0,0 +1,397 @@
|
||||
"""
|
||||
State with Memory Integration Example
|
||||
====================================
|
||||
|
||||
This example demonstrates:
|
||||
1. Using shared memory for state persistence
|
||||
2. State sharing between agents via memory
|
||||
3. Combining workflow state with memory storage
|
||||
4. Historical state tracking
|
||||
"""
|
||||
|
||||
from praisonaiagents import Agent, Task, PraisonAIAgents
|
||||
from praisonaiagents.memory import Memory
|
||||
from typing import Dict, Any, List
|
||||
import json
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
# Initialize shared memory
|
||||
memory_config = {
|
||||
"config": {
|
||||
"collection_name": "state_memory_demo",
|
||||
"path": "./state_memory_db"
|
||||
}
|
||||
}
|
||||
|
||||
shared_memory = Memory(**memory_config)
|
||||
|
||||
# Tool functions that use both state and memory
|
||||
def initialize_conversation_state(user_name: str, topic: str) -> Dict[str, Any]:
|
||||
"""Initialize conversation state and store in memory"""
|
||||
# Set workflow state
|
||||
workflow.set_state("user_name", user_name)
|
||||
workflow.set_state("topic", topic)
|
||||
workflow.set_state("start_time", time.time())
|
||||
workflow.set_state("turn_count", 0)
|
||||
workflow.set_state("conversation_history", [])
|
||||
|
||||
# Store in memory for persistence
|
||||
context = f"Starting conversation with {user_name} about {topic}"
|
||||
shared_memory.add(
|
||||
text=context,
|
||||
metadata={
|
||||
"type": "conversation_start",
|
||||
"user": user_name,
|
||||
"topic": topic,
|
||||
"timestamp": datetime.now().isoformat()
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
"status": "initialized",
|
||||
"user": user_name,
|
||||
"topic": topic,
|
||||
"memory_stored": True
|
||||
}
|
||||
|
||||
def add_conversation_turn(speaker: str, message: str) -> Dict[str, Any]:
|
||||
"""Add a conversation turn to state and memory"""
|
||||
# Get current state
|
||||
history = workflow.get_state("conversation_history", [])
|
||||
turn_count = workflow.get_state("turn_count", 0)
|
||||
|
||||
# Create turn entry
|
||||
turn = {
|
||||
"turn": turn_count + 1,
|
||||
"speaker": speaker,
|
||||
"message": message,
|
||||
"timestamp": time.time()
|
||||
}
|
||||
|
||||
# Update state
|
||||
history.append(turn)
|
||||
workflow.set_state("conversation_history", history)
|
||||
workflow.set_state("turn_count", turn_count + 1)
|
||||
workflow.set_state(f"last_{speaker}_message", message)
|
||||
|
||||
# Store in memory
|
||||
shared_memory.add(
|
||||
text=f"{speaker}: {message}",
|
||||
metadata={
|
||||
"type": "conversation_turn",
|
||||
"speaker": speaker,
|
||||
"turn": turn_count + 1,
|
||||
"topic": workflow.get_state("topic"),
|
||||
"user": workflow.get_state("user_name")
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
"turn_added": turn,
|
||||
"total_turns": turn_count + 1,
|
||||
"history_length": len(history)
|
||||
}
|
||||
|
||||
def search_conversation_memory(query: str, speaker: str = None) -> Dict[str, Any]:
|
||||
"""Search through conversation memory"""
|
||||
# Build metadata filter
|
||||
metadata_filter = {"type": "conversation_turn"}
|
||||
if speaker:
|
||||
metadata_filter["speaker"] = speaker
|
||||
|
||||
# Search memory
|
||||
results = shared_memory.search(
|
||||
query=query,
|
||||
n=5,
|
||||
metadata_filter=metadata_filter
|
||||
)
|
||||
|
||||
# Update state with search results
|
||||
workflow.set_state("last_search_query", query)
|
||||
workflow.set_state("last_search_results", len(results))
|
||||
|
||||
return {
|
||||
"query": query,
|
||||
"results_count": len(results),
|
||||
"results": [
|
||||
{
|
||||
"text": r["text"],
|
||||
"speaker": r["metadata"].get("speaker"),
|
||||
"turn": r["metadata"].get("turn"),
|
||||
"relevance": r.get("score", 0)
|
||||
}
|
||||
for r in results
|
||||
]
|
||||
}
|
||||
|
||||
def analyze_conversation_patterns() -> Dict[str, Any]:
|
||||
"""Analyze patterns in conversation using state and memory"""
|
||||
# Get state data
|
||||
history = workflow.get_state("conversation_history", [])
|
||||
user_name = workflow.get_state("user_name", "Unknown")
|
||||
topic = workflow.get_state("topic", "Unknown")
|
||||
|
||||
# Calculate metrics from state
|
||||
total_turns = len(history)
|
||||
speakers = {}
|
||||
|
||||
for turn in history:
|
||||
speaker = turn["speaker"]
|
||||
speakers[speaker] = speakers.get(speaker, 0) + 1
|
||||
|
||||
# Search memory for related conversations
|
||||
related_convos = shared_memory.search(
|
||||
query=topic,
|
||||
n=10,
|
||||
metadata_filter={"type": "conversation_turn"}
|
||||
)
|
||||
|
||||
# Analyze patterns
|
||||
patterns = {
|
||||
"current_conversation": {
|
||||
"user": user_name,
|
||||
"topic": topic,
|
||||
"total_turns": total_turns,
|
||||
"speaker_distribution": speakers,
|
||||
"duration_seconds": time.time() - workflow.get_state("start_time", time.time())
|
||||
},
|
||||
"memory_insights": {
|
||||
"related_conversations": len(related_convos),
|
||||
"common_speakers": list(set(r["metadata"].get("speaker") for r in related_convos)),
|
||||
"topics_discussed": list(set(r["metadata"].get("topic") for r in related_convos))
|
||||
}
|
||||
}
|
||||
|
||||
# Store analysis in state
|
||||
workflow.set_state("conversation_analysis", patterns)
|
||||
|
||||
return patterns
|
||||
|
||||
def retrieve_user_history(user_name: str) -> Dict[str, Any]:
|
||||
"""Retrieve all historical data for a user from memory"""
|
||||
# Search memory for user's conversations
|
||||
user_history = shared_memory.search(
|
||||
query=user_name,
|
||||
n=20,
|
||||
metadata_filter={"user": user_name}
|
||||
)
|
||||
|
||||
# Organize by conversation topics
|
||||
topics = {}
|
||||
for item in user_history:
|
||||
topic = item["metadata"].get("topic", "Unknown")
|
||||
if topic not in topics:
|
||||
topics[topic] = []
|
||||
topics[topic].append({
|
||||
"text": item["text"],
|
||||
"type": item["metadata"].get("type"),
|
||||
"timestamp": item["metadata"].get("timestamp")
|
||||
})
|
||||
|
||||
# Update state with user history
|
||||
workflow.set_state("user_history_retrieved", True)
|
||||
workflow.set_state("user_topics", list(topics.keys()))
|
||||
|
||||
return {
|
||||
"user": user_name,
|
||||
"total_interactions": len(user_history),
|
||||
"topics_discussed": list(topics.keys()),
|
||||
"conversation_count": len(topics),
|
||||
"history_by_topic": topics
|
||||
}
|
||||
|
||||
def summarize_and_save_state() -> Dict[str, Any]:
|
||||
"""Create a summary of the conversation and save final state"""
|
||||
# Get all conversation data
|
||||
history = workflow.get_state("conversation_history", [])
|
||||
analysis = workflow.get_state("conversation_analysis", {})
|
||||
user_name = workflow.get_state("user_name")
|
||||
topic = workflow.get_state("topic")
|
||||
|
||||
# Create summary
|
||||
summary = {
|
||||
"conversation_id": f"{user_name}_{topic}_{int(time.time())}",
|
||||
"user": user_name,
|
||||
"topic": topic,
|
||||
"total_turns": len(history),
|
||||
"duration": time.time() - workflow.get_state("start_time", 0),
|
||||
"participants": list(set(turn["speaker"] for turn in history)),
|
||||
"key_points": [turn["message"][:50] + "..." for turn in history[-3:]], # Last 3 messages
|
||||
"analysis": analysis
|
||||
}
|
||||
|
||||
# Save summary to memory
|
||||
shared_memory.add(
|
||||
text=json.dumps(summary, indent=2),
|
||||
metadata={
|
||||
"type": "conversation_summary",
|
||||
"user": user_name,
|
||||
"topic": topic,
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"conversation_id": summary["conversation_id"]
|
||||
}
|
||||
)
|
||||
|
||||
# Clear workflow state but keep summary
|
||||
workflow.set_state("final_summary", summary)
|
||||
workflow.set_state("state_saved_to_memory", True)
|
||||
|
||||
return summary
|
||||
|
||||
# Create agents with memory-aware tools
|
||||
conversation_manager = Agent(
|
||||
name="ConversationManager",
|
||||
role="Manage conversation state and memory",
|
||||
goal="Initialize and track conversation state effectively",
|
||||
backstory="Expert in conversation management and state tracking",
|
||||
tools=[initialize_conversation_state, add_conversation_turn, summarize_and_save_state],
|
||||
memory=shared_memory,
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
memory_analyst = Agent(
|
||||
name="MemoryAnalyst",
|
||||
role="Analyze conversation patterns from memory",
|
||||
goal="Extract insights from conversation history",
|
||||
backstory="Specialist in pattern recognition and memory analysis",
|
||||
tools=[search_conversation_memory, analyze_conversation_patterns, retrieve_user_history],
|
||||
memory=shared_memory,
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# Create tasks
|
||||
init_conversation_task = Task(
|
||||
name="init_conversation",
|
||||
description="Initialize a conversation with user 'Alice' about 'AI and Future of Work'",
|
||||
expected_output="Initialization status",
|
||||
agent=conversation_manager,
|
||||
tools=[initialize_conversation_state]
|
||||
)
|
||||
|
||||
simulate_conversation_task = Task(
|
||||
name="simulate_conversation",
|
||||
description="""Simulate a conversation by adding these turns:
|
||||
1. Alice: "What are the main impacts of AI on employment?"
|
||||
2. Assistant: "AI is transforming employment through automation and augmentation."
|
||||
3. Alice: "Which industries are most affected?"
|
||||
4. Assistant: "Manufacturing, customer service, and data analysis see significant changes."
|
||||
5. Alice: "How can workers prepare for these changes?"
|
||||
|
||||
Use add_conversation_turn for each message.""",
|
||||
expected_output="Conversation simulation results",
|
||||
agent=conversation_manager,
|
||||
tools=[add_conversation_turn],
|
||||
context=[init_conversation_task]
|
||||
)
|
||||
|
||||
search_memory_task = Task(
|
||||
name="search_memory",
|
||||
description="""Search conversation memory for:
|
||||
1. Messages about 'automation'
|
||||
2. All messages from 'Alice'
|
||||
3. Messages about 'employment'""",
|
||||
expected_output="Search results from memory",
|
||||
agent=memory_analyst,
|
||||
tools=[search_conversation_memory],
|
||||
context=[simulate_conversation_task]
|
||||
)
|
||||
|
||||
analyze_patterns_task = Task(
|
||||
name="analyze_patterns",
|
||||
description="Analyze conversation patterns and extract insights",
|
||||
expected_output="Pattern analysis results",
|
||||
agent=memory_analyst,
|
||||
tools=[analyze_conversation_patterns],
|
||||
context=[search_memory_task]
|
||||
)
|
||||
|
||||
retrieve_history_task = Task(
|
||||
name="retrieve_history",
|
||||
description="Retrieve all historical data for user 'Alice'",
|
||||
expected_output="User history summary",
|
||||
agent=memory_analyst,
|
||||
tools=[retrieve_user_history],
|
||||
context=[analyze_patterns_task]
|
||||
)
|
||||
|
||||
save_state_task = Task(
|
||||
name="save_state",
|
||||
description="Create final summary and save conversation state to memory",
|
||||
expected_output="Final conversation summary",
|
||||
agent=conversation_manager,
|
||||
tools=[summarize_and_save_state],
|
||||
context=[retrieve_history_task]
|
||||
)
|
||||
|
||||
# Create workflow with shared memory
|
||||
workflow = PraisonAIAgents(
|
||||
agents=[conversation_manager, memory_analyst],
|
||||
tasks=[
|
||||
init_conversation_task,
|
||||
simulate_conversation_task,
|
||||
search_memory_task,
|
||||
analyze_patterns_task,
|
||||
retrieve_history_task,
|
||||
save_state_task
|
||||
],
|
||||
memory=memory_config, # This creates shared memory for all agents
|
||||
verbose=1,
|
||||
process="sequential"
|
||||
)
|
||||
|
||||
# Run workflow
|
||||
print("\n=== State with Memory Integration Demo ===")
|
||||
print("\n1. Starting workflow with memory-based state management...")
|
||||
result = workflow.start()
|
||||
|
||||
# Display results
|
||||
print("\n2. Final State Summary:")
|
||||
final_summary = workflow.get_state("final_summary", {})
|
||||
if final_summary:
|
||||
print(f" Conversation ID: {final_summary.get('conversation_id')}")
|
||||
print(f" Total Turns: {final_summary.get('total_turns')}")
|
||||
print(f" Duration: {final_summary.get('duration', 0):.2f} seconds")
|
||||
print(f" Participants: {final_summary.get('participants')}")
|
||||
|
||||
print("\n3. Memory Storage Status:")
|
||||
print(f" State saved to memory: {workflow.get_state('state_saved_to_memory', False)}")
|
||||
print(f" User history retrieved: {workflow.get_state('user_history_retrieved', False)}")
|
||||
print(f" Topics in memory: {workflow.get_state('user_topics', [])}")
|
||||
|
||||
# Test memory persistence by creating new workflow instance
|
||||
print("\n4. Testing Memory Persistence:")
|
||||
print(" Creating new workflow instance to test memory retrieval...")
|
||||
|
||||
# Create a simple test agent to verify memory persistence
|
||||
test_agent = Agent(
|
||||
name="MemoryTester",
|
||||
role="Test memory persistence",
|
||||
goal="Verify stored conversation data",
|
||||
backstory="Memory system tester",
|
||||
tools=[search_conversation_memory],
|
||||
memory=shared_memory,
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
test_task = Task(
|
||||
name="test_memory",
|
||||
description="Search memory for 'Alice' to verify persistence",
|
||||
expected_output="Memory search results",
|
||||
agent=test_agent,
|
||||
tools=[search_conversation_memory]
|
||||
)
|
||||
|
||||
test_workflow = PraisonAIAgents(
|
||||
agents=[test_agent],
|
||||
tasks=[test_task],
|
||||
memory=memory_config,
|
||||
verbose=0
|
||||
)
|
||||
|
||||
# This will use the same memory collection and find previously stored data
|
||||
test_result = test_workflow.start()
|
||||
|
||||
print("\n=== Demo Complete ===")
|
||||
print("\nNote: The conversation data has been persisted in memory and can be")
|
||||
print("retrieved in future sessions using the same memory configuration.")
|
||||
@@ -0,0 +1,258 @@
|
||||
"""
|
||||
Example demonstrating telemetry usage in PraisonAI Agents.
|
||||
|
||||
This example shows how to:
|
||||
1. Enable telemetry with OpenTelemetry backend
|
||||
2. Use automatic instrumentation
|
||||
3. Use manual instrumentation
|
||||
4. Export telemetry data
|
||||
"""
|
||||
|
||||
import os
|
||||
from praisonaiagents import (
|
||||
Agent, Task, PraisonAIAgents,
|
||||
enable_telemetry, disable_telemetry, get_telemetry_collector
|
||||
)
|
||||
from praisonaiagents.tools import DuckDuckGoSearchTool
|
||||
|
||||
# Set current path to package root directory
|
||||
import os
|
||||
import sys
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
|
||||
def basic_telemetry_example():
|
||||
"""Basic example with automatic telemetry."""
|
||||
print("\n=== Basic Telemetry Example ===\n")
|
||||
|
||||
# Enable telemetry - it will automatically instrument all agents and tasks
|
||||
telemetry = enable_telemetry(
|
||||
backend="opentelemetry",
|
||||
service_name="praisonai-demo",
|
||||
exporter="console" # Use console exporter for demo
|
||||
)
|
||||
|
||||
if not telemetry:
|
||||
print("Telemetry dependencies not installed. Install with:")
|
||||
print("pip install praisonaiagents[telemetry]")
|
||||
return
|
||||
|
||||
# Create agents - telemetry will be automatically added
|
||||
researcher = Agent(
|
||||
name="Researcher",
|
||||
role="Information gatherer",
|
||||
goal="Find accurate information about topics",
|
||||
backstory="You are an expert researcher with attention to detail",
|
||||
tools=[DuckDuckGoSearchTool()],
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
writer = Agent(
|
||||
name="Writer",
|
||||
role="Content creator",
|
||||
goal="Create engaging content based on research",
|
||||
backstory="You are a skilled writer who creates clear, engaging content",
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# Create tasks
|
||||
research_task = Task(
|
||||
name="research_task",
|
||||
description="Research the latest developments in quantum computing",
|
||||
expected_output="A summary of recent quantum computing breakthroughs",
|
||||
agent=researcher
|
||||
)
|
||||
|
||||
writing_task = Task(
|
||||
name="writing_task",
|
||||
description="Write a blog post about quantum computing developments",
|
||||
expected_output="A 300-word blog post suitable for a general audience",
|
||||
agent=writer,
|
||||
context=[research_task] # Depends on research task
|
||||
)
|
||||
|
||||
# Create workflow
|
||||
workflow = PraisonAIAgents(
|
||||
agents=[researcher, writer],
|
||||
tasks=[research_task, writing_task],
|
||||
process="sequential",
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# Run workflow - all telemetry will be collected automatically
|
||||
print("Running workflow with telemetry enabled...\n")
|
||||
result = workflow.start()
|
||||
|
||||
# Get telemetry metrics
|
||||
metrics = telemetry.get_metrics()
|
||||
print("\n=== Telemetry Metrics ===")
|
||||
print(f"Agent executions: {metrics['agent_executions']}")
|
||||
print(f"Task completions: {metrics['task_completions']}")
|
||||
print(f"Tool calls: {metrics['tool_calls']}")
|
||||
print(f"LLM calls: {metrics['llm_calls']}")
|
||||
print(f"Total tokens: {metrics['total_tokens']}")
|
||||
print(f"Errors: {metrics['errors']}")
|
||||
|
||||
# Disable telemetry
|
||||
disable_telemetry()
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def manual_telemetry_example():
|
||||
"""Example with manual telemetry instrumentation."""
|
||||
print("\n=== Manual Telemetry Example ===\n")
|
||||
|
||||
# Enable telemetry
|
||||
telemetry = enable_telemetry(backend="custom") # Use logging backend
|
||||
|
||||
if not telemetry:
|
||||
return
|
||||
|
||||
# Create agent without automatic instrumentation
|
||||
agent = Agent(
|
||||
name="Assistant",
|
||||
role="General assistant",
|
||||
goal="Help with various tasks",
|
||||
backstory="You are a helpful AI assistant",
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# Manual telemetry - trace custom operations
|
||||
with telemetry.trace_agent_execution("Assistant", custom_field="demo"):
|
||||
response = agent.chat("What is the capital of France?")
|
||||
print(f"Response: {response}")
|
||||
|
||||
# Use telemetry decorator for functions
|
||||
@telemetry.trace(span_type="custom_operation")
|
||||
def process_data(data):
|
||||
"""Process some data with telemetry."""
|
||||
# Simulate processing
|
||||
import time
|
||||
time.sleep(0.1)
|
||||
|
||||
# Record custom metrics
|
||||
telemetry.record_metric("data.processed", len(data), {"type": "text"})
|
||||
|
||||
return data.upper()
|
||||
|
||||
# Call traced function
|
||||
result = process_data("hello world")
|
||||
print(f"Processed: {result}")
|
||||
|
||||
# Manual event recording
|
||||
telemetry._backend.record_event("custom_event", {
|
||||
"user_action": "manual_example",
|
||||
"success": True
|
||||
})
|
||||
|
||||
disable_telemetry()
|
||||
|
||||
|
||||
def advanced_telemetry_example():
|
||||
"""Advanced example with custom telemetry backend and OTLP export."""
|
||||
print("\n=== Advanced Telemetry Example ===\n")
|
||||
|
||||
# For OTLP export, you would typically have an OTLP collector running
|
||||
# For this example, we'll use console export
|
||||
|
||||
# Enable telemetry with OTLP configuration
|
||||
telemetry = enable_telemetry(
|
||||
backend="opentelemetry",
|
||||
service_name="praisonai-production",
|
||||
service_version="1.0.0",
|
||||
exporter="otlp", # Would export to OTLP collector
|
||||
otlp_endpoint="localhost:4317", # OTLP collector endpoint
|
||||
metric_export_interval=10000 # Export metrics every 10 seconds
|
||||
)
|
||||
|
||||
if not telemetry:
|
||||
print("Telemetry not available. Using fallback.")
|
||||
# Even without telemetry, the code still works
|
||||
agent = Agent(
|
||||
name="Analyst",
|
||||
role="Data analyst",
|
||||
goal="Analyze data and provide insights",
|
||||
backstory="You are an experienced data analyst",
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
result = agent.chat("Analyze the trend: [10, 15, 13, 18, 22, 20, 25]")
|
||||
print(f"Analysis: {result}")
|
||||
return
|
||||
|
||||
# Create instrumented agent
|
||||
agent = Agent(
|
||||
name="Analyst",
|
||||
role="Data analyst",
|
||||
goal="Analyze data and provide insights",
|
||||
backstory="You are an experienced data analyst",
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# Use context manager for grouped operations
|
||||
with telemetry.trace("analysis_workflow", workflow_type="data_analysis"):
|
||||
# Multiple operations within the same trace context
|
||||
response1 = agent.chat("What is the mean of: [10, 15, 13, 18, 22, 20, 25]")
|
||||
response2 = agent.chat("What is the trend in this data?")
|
||||
response3 = agent.chat("Predict the next 3 values")
|
||||
|
||||
# Record custom metrics
|
||||
telemetry.record_metric("analysis.steps", 3)
|
||||
telemetry.record_cost(0.001, model="gpt-4o-mini") # Track costs
|
||||
|
||||
print("\nAnalysis complete. Telemetry data exported to OTLP collector.")
|
||||
|
||||
disable_telemetry()
|
||||
|
||||
|
||||
def telemetry_with_errors_example():
|
||||
"""Example showing how telemetry handles errors."""
|
||||
print("\n=== Telemetry with Error Handling ===\n")
|
||||
|
||||
telemetry = enable_telemetry()
|
||||
|
||||
if not telemetry:
|
||||
return
|
||||
|
||||
agent = Agent(
|
||||
name="ErrorProneAgent",
|
||||
role="Test agent",
|
||||
goal="Test error handling",
|
||||
backstory="An agent for testing",
|
||||
llm="gpt-4o-mini"
|
||||
)
|
||||
|
||||
# This will be traced and errors will be recorded
|
||||
try:
|
||||
with telemetry.trace_agent_execution("ErrorProneAgent"):
|
||||
# Simulate an error by using an invalid tool
|
||||
agent.execute_tool("non_existent_tool", "test")
|
||||
except Exception as e:
|
||||
print(f"Caught error: {e}")
|
||||
print("Error was recorded in telemetry")
|
||||
|
||||
# Check error metrics
|
||||
metrics = telemetry.get_metrics()
|
||||
print(f"\nError count in telemetry: {metrics['errors']}")
|
||||
|
||||
disable_telemetry()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("PraisonAI Agents Telemetry Examples")
|
||||
print("===================================")
|
||||
|
||||
# Run basic example
|
||||
basic_telemetry_example()
|
||||
|
||||
# Run manual instrumentation example
|
||||
manual_telemetry_example()
|
||||
|
||||
# Run advanced example (commented out as it requires OTLP collector)
|
||||
# advanced_telemetry_example()
|
||||
|
||||
# Run error handling example
|
||||
telemetry_with_errors_example()
|
||||
|
||||
print("\n✅ All telemetry examples completed!")
|
||||
@@ -0,0 +1,67 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test PostHog integration after fix.
|
||||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
|
||||
# Ensure telemetry is enabled
|
||||
for var in ['PRAISONAI_TELEMETRY_DISABLED', 'PRAISONAI_DISABLE_TELEMETRY', 'DO_NOT_TRACK']:
|
||||
if var in os.environ:
|
||||
del os.environ[var]
|
||||
|
||||
print("=== Testing PostHog Fix ===\n")
|
||||
|
||||
# Test PostHog directly first
|
||||
print("1. Testing PostHog directly:")
|
||||
try:
|
||||
from posthog import Posthog
|
||||
|
||||
# Create PostHog client
|
||||
ph = Posthog(
|
||||
project_api_key='phc_skZpl3eFLQJ4iYjsERNMbCO6jfeSJi2vyZlPahKgxZ7',
|
||||
host='https://eu.i.posthog.com'
|
||||
)
|
||||
|
||||
# Send test event
|
||||
ph.capture('test-user', 'direct_test', {'timestamp': time.time()})
|
||||
|
||||
# Flush and shutdown properly
|
||||
ph.flush()
|
||||
ph.shutdown()
|
||||
|
||||
print("✓ Direct PostHog test successful\n")
|
||||
except Exception as e:
|
||||
print(f"✗ Direct PostHog test failed: {e}\n")
|
||||
|
||||
# Test telemetry module
|
||||
print("2. Testing telemetry module:")
|
||||
from praisonaiagents.telemetry.telemetry import MinimalTelemetry
|
||||
|
||||
# Create telemetry instance
|
||||
telemetry = MinimalTelemetry(enabled=True)
|
||||
|
||||
# Track events
|
||||
telemetry.track_agent_execution("TestAgent", success=True)
|
||||
telemetry.track_task_completion("TestTask", success=True)
|
||||
telemetry.track_tool_usage("TestTool", success=True)
|
||||
|
||||
print(f"✓ Events tracked")
|
||||
print(f"✓ PostHog client available: {telemetry._posthog is not None}")
|
||||
|
||||
# Flush
|
||||
print("\n3. Flushing telemetry...")
|
||||
telemetry.flush()
|
||||
print("✓ Flush completed")
|
||||
|
||||
# Shutdown
|
||||
print("\n4. Shutting down telemetry...")
|
||||
telemetry.shutdown()
|
||||
print("✓ Shutdown completed")
|
||||
|
||||
print("\n=== Test Complete ===")
|
||||
print("\nPostHog should now be receiving data properly!")
|
||||
print("The fix adds:")
|
||||
print("1. posthog.flush() call in the flush() method")
|
||||
print("2. shutdown() method that properly closes the connection")
|
||||
Generated
+2120
-2128
File diff suppressed because it is too large.
Load diff
@@ -3,8 +3,8 @@ class Praisonai < Formula
|
||||
|
||||
desc "AI tools for various AI applications"
|
||||
homepage "https://github.com/MervinPraison/PraisonAI"
|
||||
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.2.27.tar.gz"
|
||||
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.2.27.tar.gz | shasum -a 256`.split.first
|
||||
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.2.32.tar.gz"
|
||||
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.2.32.tar.gz | shasum -a 256`.split.first
|
||||
license "MIT"
|
||||
|
||||
depends_on "[email protected]"
|
||||
|
||||
@@ -24,8 +24,8 @@ config_list = [
|
||||
agent_file = "test.yaml"
|
||||
|
||||
actions=[
|
||||
cl.Action(name="run", payload="run", label="✅ Run"),
|
||||
cl.Action(name="modify", payload="modify", label="🔧 Modify"),
|
||||
cl.Action(name="run", payload={"value": "run"}, label="run"),
|
||||
cl.Action(name="modify", payload={"value": "modify"}, label="Modify"),
|
||||
]
|
||||
|
||||
@cl.action_callback("run")
|
||||
|
||||
@@ -56,7 +56,7 @@ class CloudDeployer:
|
||||
file.write("FROM python:3.11-slim\n")
|
||||
file.write("WORKDIR /app\n")
|
||||
file.write("COPY . .\n")
|
||||
file.write("RUN pip install flask praisonai==2.2.27 gunicorn markdown\n")
|
||||
file.write("RUN pip install flask praisonai==2.2.32 gunicorn markdown\n")
|
||||
file.write("EXPOSE 8080\n")
|
||||
file.write('CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"]\n')
|
||||
|
||||
|
||||
@@ -17,8 +17,8 @@ config_list = [
|
||||
]
|
||||
|
||||
actions = [
|
||||
cl.Action(name="run", payload="run", label="✅ Run"),
|
||||
cl.Action(name="modify", payload="modify", label="🔧 Modify"),
|
||||
cl.Action(name="run", payload={"value": "run"}, label="run"),
|
||||
cl.Action(name="modify", payload={"value": "modify"}, label="Modify"),
|
||||
]
|
||||
|
||||
@cl.action_callback("run")
|
||||
@@ -819,4 +819,4 @@ async def on_settings_update(settings):
|
||||
await on_settings_update(settings)
|
||||
except Exception as e:
|
||||
logger.error(f"Final retry failed: {str(e)}")
|
||||
await cl.Message(content=f"Failed to update settings after retries: {str(e)}").send()
|
||||
await cl.Message(content=f"Failed to update settings after retries: {str(e)}").send()
|
||||
@@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "PraisonAI"
|
||||
version = "2.2.27"
|
||||
version = "2.2.32"
|
||||
description = "PraisonAI is an AI Agents Framework with Self Reflection. PraisonAI application combines PraisonAI Agents, AutoGen, and CrewAI into a low-code solution for building and managing multi-agent LLM systems, focusing on simplicity, customisation, and efficient human-agent collaboration."
|
||||
readme = "README.md"
|
||||
license = ""
|
||||
@@ -12,7 +12,7 @@ dependencies = [
|
||||
"rich>=13.7",
|
||||
"markdown>=3.5",
|
||||
"pyparsing>=3.0.0",
|
||||
"praisonaiagents>=0.0.97",
|
||||
"praisonaiagents>=0.0.105",
|
||||
"python-dotenv>=0.19.0",
|
||||
"instructor>=1.3.3",
|
||||
"PyYAML>=6.0",
|
||||
@@ -95,7 +95,7 @@ autogen = ["pyautogen>=0.2.19", "praisonai-tools>=0.0.15", "crewai"]
|
||||
|
||||
[tool.poetry]
|
||||
name = "PraisonAI"
|
||||
version = "2.2.27"
|
||||
version = "2.2.32"
|
||||
description = "PraisonAI is an AI Agents Framework with Self Reflection. PraisonAI application combines PraisonAI Agents, AutoGen, and CrewAI into a low-code solution for building and managing multi-agent LLM systems, focusing on simplicity, customisation, and efficient human-agent collaboration."
|
||||
authors = ["Mervin Praison"]
|
||||
license = ""
|
||||
@@ -113,7 +113,7 @@ python = ">=3.10,<3.13"
|
||||
rich = ">=13.7"
|
||||
markdown = ">=3.5"
|
||||
pyparsing = ">=3.0.0"
|
||||
praisonaiagents = ">=0.0.97"
|
||||
praisonaiagents = ">=0.0.105"
|
||||
python-dotenv = ">=0.19.0"
|
||||
instructor = ">=1.3.3"
|
||||
PyYAML = ">=6.0"
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Comprehensive test script to verify all import scenarios work correctly
|
||||
Tests the original failing import from the GitHub issue
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the praisonai-agents source to Python path
|
||||
sys.path.insert(0, '/home/runner/work/PraisonAI/PraisonAI/src/praisonai-agents')
|
||||
|
||||
def test_original_failing_import():
|
||||
"""Test the exact import that was failing in the GitHub issue"""
|
||||
print("=== Testing Original Failing Import ===")
|
||||
try:
|
||||
from praisonaiagents.agents.agents import Agent, Task, PraisonAIAgents
|
||||
print('✅ SUCCESS: from praisonaiagents.agents.agents import Agent, Task, PraisonAIAgents')
|
||||
return True
|
||||
except ImportError as e:
|
||||
print(f'❌ ERROR: {e}')
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f'❌ UNEXPECTED ERROR: {e}')
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
def test_memory_direct_import():
|
||||
"""Test direct Memory import"""
|
||||
print("\n=== Testing Direct Memory Import ===")
|
||||
try:
|
||||
from praisonaiagents.memory import Memory
|
||||
print('✅ SUCCESS: from praisonaiagents.memory import Memory')
|
||||
return True
|
||||
except ImportError as e:
|
||||
print(f'❌ ERROR: {e}')
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f'❌ UNEXPECTED ERROR: {e}')
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
def test_memory_from_package_root():
|
||||
"""Test Memory import from package root"""
|
||||
print("\n=== Testing Memory Import from Package Root ===")
|
||||
try:
|
||||
from praisonaiagents import Memory
|
||||
print('✅ SUCCESS: from praisonaiagents import Memory')
|
||||
return True
|
||||
except ImportError as e:
|
||||
print(f'❌ ERROR: {e}')
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f'❌ UNEXPECTED ERROR: {e}')
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
def test_session_import():
|
||||
"""Test Session import which depends on Memory"""
|
||||
print("\n=== Testing Session Import ===")
|
||||
try:
|
||||
from praisonaiagents.session import Session
|
||||
print('✅ SUCCESS: from praisonaiagents.session import Session')
|
||||
return True
|
||||
except ImportError as e:
|
||||
print(f'❌ ERROR: {e}')
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f'❌ UNEXPECTED ERROR: {e}')
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
def test_memory_instantiation():
|
||||
"""Test that Memory can be instantiated without errors"""
|
||||
print("\n=== Testing Memory Instantiation ===")
|
||||
try:
|
||||
from praisonaiagents.memory import Memory
|
||||
|
||||
# Test with minimal config (no external dependencies)
|
||||
config = {"provider": "none"}
|
||||
memory = Memory(config=config)
|
||||
print('✅ SUCCESS: Memory instance created with provider="none"')
|
||||
|
||||
# Test basic methods don't fail immediately
|
||||
memory.store_short_term("test content", metadata={"test": True})
|
||||
results = memory.search_short_term("test", limit=1)
|
||||
print('✅ SUCCESS: Basic memory operations work')
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f'❌ ERROR: {e}')
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
def run_all_tests():
|
||||
"""Run all tests and report results"""
|
||||
print("🔍 Running comprehensive import tests...")
|
||||
|
||||
tests = [
|
||||
test_original_failing_import,
|
||||
test_memory_direct_import,
|
||||
test_memory_from_package_root,
|
||||
test_session_import,
|
||||
test_memory_instantiation
|
||||
]
|
||||
|
||||
results = []
|
||||
for test in tests:
|
||||
results.append(test())
|
||||
|
||||
print(f"\n📊 Test Results: {sum(results)}/{len(results)} tests passed")
|
||||
|
||||
if all(results):
|
||||
print("🎉 ALL TESTS PASSED! The Memory import issue has been resolved.")
|
||||
else:
|
||||
print("❌ Some tests failed. The issue may not be fully resolved.")
|
||||
|
||||
return all(results)
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = run_all_tests()
|
||||
sys.exit(0 if success else 1)
|
||||
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify Memory import works correctly
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the praisonai-agents source to Python path
|
||||
sys.path.insert(0, '/home/runner/work/PraisonAI/PraisonAI/src/praisonai-agents')
|
||||
|
||||
try:
|
||||
from praisonaiagents.memory import Memory
|
||||
print('SUCCESS: Memory import works correctly')
|
||||
print('Memory class found:', Memory)
|
||||
|
||||
# Try to create a minimal instance to ensure it doesn't fail immediately
|
||||
config = {"provider": "none"} # Use the simplest provider
|
||||
memory = Memory(config=config)
|
||||
print('SUCCESS: Memory instance created successfully')
|
||||
|
||||
except ImportError as e:
|
||||
print('ERROR:', e)
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
except Exception as e:
|
||||
print('UNEXPECTED ERROR:', e)
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
@@ -0,0 +1,128 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Quick test for the remote agent connectivity feature.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src', 'praisonai-agents'))
|
||||
|
||||
from praisonaiagents import Session
|
||||
|
||||
def test_remote_session_creation():
|
||||
"""Test creating a remote session."""
|
||||
print("🧪 Testing remote session creation...")
|
||||
|
||||
try:
|
||||
# This should fail gracefully since there's no server running
|
||||
session = Session(agent_url="localhost:8000/agent")
|
||||
print(f"❌ Unexpected: Session created without server: {session}")
|
||||
except ConnectionError as e:
|
||||
print(f"✅ Expected connection error: {e}")
|
||||
except Exception as e:
|
||||
print(f"❌ Unexpected error: {e}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def test_local_session_backwards_compatibility():
|
||||
"""Test that local sessions still work as before."""
|
||||
print("\n🧪 Testing local session backwards compatibility...")
|
||||
|
||||
try:
|
||||
# This should work as before
|
||||
session = Session(session_id="test_local")
|
||||
print(f"✅ Local session created: {session}")
|
||||
|
||||
# Test that it's not remote
|
||||
if session.is_remote:
|
||||
print("❌ Local session incorrectly marked as remote")
|
||||
return False
|
||||
|
||||
print("✅ Local session correctly identified")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating local session: {e}")
|
||||
return False
|
||||
|
||||
def test_remote_session_restrictions():
|
||||
"""Test that remote sessions properly restrict local operations."""
|
||||
print("\n🧪 Testing remote session restrictions...")
|
||||
|
||||
try:
|
||||
# Create a mock remote session (will fail connection but that's OK for testing restrictions)
|
||||
session = Session(agent_url="mock://fake-server:8000/agent")
|
||||
except ConnectionError:
|
||||
# Create the session object manually for testing restrictions
|
||||
session = Session.__new__(Session)
|
||||
session.session_id = "test_remote"
|
||||
session.user_id = "test_user"
|
||||
session.agent_url = "mock://fake-server:8000/agent"
|
||||
session.timeout = 30
|
||||
session.is_remote = True
|
||||
session.memory_config = {}
|
||||
session.knowledge_config = {}
|
||||
session._memory = None
|
||||
session._knowledge = None
|
||||
session._agents_instance = None
|
||||
|
||||
# Test memory restriction
|
||||
try:
|
||||
_ = session.memory
|
||||
print("❌ Memory access should be restricted for remote sessions")
|
||||
return False
|
||||
except ValueError as e:
|
||||
print(f"✅ Memory properly restricted: {e}")
|
||||
|
||||
# Test knowledge restriction
|
||||
try:
|
||||
_ = session.knowledge
|
||||
print("❌ Knowledge access should be restricted for remote sessions")
|
||||
return False
|
||||
except ValueError as e:
|
||||
print(f"✅ Knowledge properly restricted: {e}")
|
||||
|
||||
# Test agent creation restriction
|
||||
try:
|
||||
session.Agent(name="Test", role="Assistant")
|
||||
print("❌ Agent creation should be restricted for remote sessions")
|
||||
return False
|
||||
except ValueError as e:
|
||||
print(f"✅ Agent creation properly restricted: {e}")
|
||||
|
||||
return True
|
||||
|
||||
def main():
|
||||
"""Run all tests."""
|
||||
print("🚀 Testing Remote Agent Connectivity Implementation")
|
||||
print("=" * 60)
|
||||
|
||||
tests = [
|
||||
test_remote_session_creation,
|
||||
test_local_session_backwards_compatibility,
|
||||
test_remote_session_restrictions
|
||||
]
|
||||
|
||||
passed = 0
|
||||
total = len(tests)
|
||||
|
||||
for test in tests:
|
||||
if test():
|
||||
passed += 1
|
||||
else:
|
||||
print("❌ Test failed!")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print(f"📊 Test Results: {passed}/{total} tests passed")
|
||||
|
||||
if passed == total:
|
||||
print("🎉 All tests passed! Remote agent connectivity is working correctly.")
|
||||
return True
|
||||
else:
|
||||
print("⚠️ Some tests failed. Check the implementation.")
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = main()
|
||||
sys.exit(0 if success else 1)
|
||||
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify session.py import works correctly (which imports Memory)
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the praisonai-agents source to Python path
|
||||
sys.path.insert(0, '/home/runner/work/PraisonAI/PraisonAI/src/praisonai-agents')
|
||||
|
||||
try:
|
||||
from praisonaiagents.session import Session
|
||||
print('SUCCESS: Session import works correctly')
|
||||
print('Session class found:', Session)
|
||||
|
||||
# This was the failing import chain
|
||||
from praisonaiagents.agents.agents import Agent, Task, PraisonAIAgents
|
||||
print('SUCCESS: Agent, Task, PraisonAIAgents import works correctly')
|
||||
|
||||
except ImportError as e:
|
||||
print('ERROR:', e)
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
except Exception as e:
|
||||
print('UNEXPECTED ERROR:', e)
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
Generated
+2583
-2584
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user