Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

132 smart memory management #142

Closed
wants to merge 204 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
204 commits
Select commit Hold shift + click to select a range
0beb883
disable lintrule
KastanDay Aug 14, 2023
0cb1c6a
disable lintrule
KastanDay Aug 14, 2023
085a5ce
Fix yapf formatting to properly save and commit changes
KastanDay Aug 14, 2023
f7fceab
show course metadata schema
KastanDay Aug 14, 2023
79770ac
Supabase SQL Redesign -- to be faster and better organized (#27)
KastanDay Aug 14, 2023
5600260
Merge branch 'main' of https://github.com/UIUC-Chatbot/ai-ta-backend
jkmin3 Aug 14, 2023
a3e2374
fixed the baseurl so that it is originally off
jkmin3 Aug 14, 2023
6bd9c8b
adding prints for debugging
KastanDay Aug 15, 2023
ae14314
Merge branch 'main' of https://github.com/UIUC-Chatbot/ai-ta-backend
jkmin3 Aug 15, 2023
f706356
adding prints for debugging
KastanDay Aug 15, 2023
46489e8
Merge branch 'main' of https://github.com/UIUC-Chatbot/ai-ta-backend
jkmin3 Aug 15, 2023
31af46a
changed all instances of pagenumber_or_timestamp to pagenumber and ti…
jkmin3 Aug 15, 2023
d5ee953
added text to metadata for split and upload
jkmin3 Aug 15, 2023
da51458
added the new filter system and changed page_content from text
jkmin3 Aug 15, 2023
d51df0b
removed line that showed usecase of function
jkmin3 Aug 15, 2023
f7c36bf
Using the user query embedding in split
jkmin3 Aug 15, 2023
2f311ae
indexed the user_query
jkmin3 Aug 15, 2023
72be1e8
fixed gettopcontexts
jkmin3 Aug 15, 2023
1e1b56a
webscrape no longer scrapes gifs and vector database prints have been…
jkmin3 Aug 15, 2023
7e6d7c9
fixed the getstuffed prompt function
jkmin3 Aug 15, 2023
62b2f13
gettopcontexts aand getstuffed prompt functions are now fully working…
jkmin3 Aug 15, 2023
0f17d4e
fixed issues with the output of number of tokens
jkmin3 Aug 15, 2023
255d040
Fix Supabase SQL migration, esp getTopContexts() (#50)
KastanDay Aug 15, 2023
62c22c1
Merge branch 'main' of https://github.com/UIUC-Chatbot/ai-ta-backend
jkmin3 Aug 15, 2023
101485c
added only safe filetypes in webscrape
jkmin3 Aug 15, 2023
1378333
reverting changes in webscrape
jkmin3 Aug 15, 2023
9d24970
improve delete_entire_course to try each database separately
KastanDay Aug 15, 2023
6c35405
update getAll to include URLs
KastanDay Aug 16, 2023
dbbca0a
enhance parameter names in getAll
KastanDay Aug 16, 2023
996caeb
Merge branch 'main' of https://github.com/UIUC-Chatbot/ai-ta-backend
jkmin3 Aug 17, 2023
603e0f0
webscraping supports all filetypes supported by bulk_ingest
jkmin3 Aug 19, 2023
6ef06fd
Testing .txt and .py ingest functions (#55)
star-nox Aug 21, 2023
a4256da
Merge branch 'main' of https://github.com/UIUC-Chatbot/ai-ta-backend
jkmin3 Aug 22, 2023
4a82e3c
Removed dates for webscraping titles
jkmin3 Aug 22, 2023
9f3bc3d
fixed bug with checking url and filetype
jkmin3 Aug 22, 2023
fb80a04
fixed bug with no variable of filetype when their is the soup
jkmin3 Aug 22, 2023
06b1577
changed response.text to content in web scrape
jkmin3 Aug 23, 2023
e9decaf
reverted changes to text
jkmin3 Aug 23, 2023
bdb9607
Bump flask from 2.3.2 to 2.3.3 (#61)
dependabot[bot] Aug 24, 2023
e70be75
Update protobuf requirement from <=3.20 to <=4.24.1 (#62)
dependabot[bot] Aug 24, 2023
8a21dc0
Bump langchain from 0.0.256 to 0.0.270 (#60)
dependabot[bot] Aug 24, 2023
0f846d5
add libxcrypt1 to aptPkgs
KastanDay Aug 24, 2023
35537ed
fix spelling of libcrypt1
KastanDay Aug 24, 2023
120c858
set pymupdf version to fix build error
KastanDay Aug 24, 2023
f904893
adding git as dependency
KastanDay Aug 24, 2023
92583e6
fixing git dependency
KastanDay Aug 24, 2023
777891e
[SIMULTANIOUS MERGE with Frontend] User query visualizations with Nom…
star-nox Aug 25, 2023
8214f82
make /delete single files INSTANT via background tasks
KastanDay Aug 25, 2023
4dca008
Merge branch 'main' of https://github.com/UIUC-Chatbot/ai-ta-backend
jkmin3 Aug 25, 2023
a898978
Stopped errors in ingest when you start with a pdf url
jkmin3 Aug 25, 2023
2cc2b5b
Fixed delete course, so that it deletes some if not all
jkmin3 Aug 28, 2023
70223fb
Added much better print statements and also removed duplicate urls fr…
jkmin3 Aug 30, 2023
eda0571
Created the stay_on_baseurl to be a boolean paramater. Also I was abl…
jkmin3 Aug 30, 2023
305ee13
forgot to uncomment the split and upload section of webscrabe
jkmin3 Aug 30, 2023
6dcf7ed
push to deploy on railway
jkmin3 Aug 30, 2023
cdceb0e
fixed webscrape nonetype bug
jkmin3 Aug 31, 2023
b41478e
enhance split_and_upload to handle code files that don't have periods
KastanDay Aug 31, 2023
61a296b
reducing workers, increasing threads. will greatly reduce memory foot…
KastanDay Sep 5, 2023
9161d8c
Reduce memory consumption to save cash on Railway (#82)
KastanDay Sep 5, 2023
f8c3bc2
adding defaults for web scraping
KastanDay Sep 5, 2023
805936b
increasing timeout from 30 sec to 30 min, web scrape takes a while so…
KastanDay Sep 5, 2023
e4aea5b
Merge branch 'main' of https://github.com/UIUC-Chatbot/ai-ta-backend
jkmin3 Sep 6, 2023
0af41e6
fixed webscraper
jkmin3 Sep 6, 2023
6ce20e7
HOTFIX for cannot schedule new futures after interpreter shutdown, wh…
KastanDay Sep 6, 2023
0347c7a
Merge branch 'main' of https://github.com/UIUC-Chatbot/ai-ta-backend
jkmin3 Sep 6, 2023
32abf39
quick fix for supabase using try except
jkmin3 Sep 6, 2023
6a1a38f
Fix GitHub ingest: separate files created properly (#76)
star-nox Sep 6, 2023
16381d9
Merge branch 'main' of https://github.com/UIUC-Chatbot/ai-ta-backend
jkmin3 Sep 9, 2023
c6c21e6
Create .env.template
KastanDay Sep 11, 2023
09010b5
📜 Add .env.template to install instructions
KastanDay Sep 11, 2023
7f74f86
Merge branch 'main' of https://github.com/UIUC-Chatbot/ai-ta-backend
jkmin3 Sep 13, 2023
520444a
Improve Nomic Visualizations with full conversations & date filtering…
star-nox Sep 15, 2023
241f819
Refactor ingest file handling for maintainability; it's beautiful! (#73)
jkmin3 Sep 15, 2023
4f6a863
HOTFIX: critical bug with duplicate doucments in Supabase for EVERY p…
KastanDay Sep 15, 2023
26ee9f4
Enhancement to Github ingest, add full URLs, enable file deletion
KastanDay Sep 15, 2023
0a9d074
HOTFIX: required parameter handling on /delete endpoint
KastanDay Sep 16, 2023
8867a51
HOTFIX: url paramater name in /delete
KastanDay Sep 16, 2023
66b757e
remove --max-requests from flask run, hopefully eliminates missing re…
KastanDay Sep 16, 2023
2bd63e3
Merge branch 'main' of https://github.com/UIUC-Chatbot/ai-ta-backend
jkmin3 Sep 16, 2023
d7741d1
HOTFIX: Tiny typo in /webscrape input params
KastanDay Sep 21, 2023
0f432ef
remove libcrypt1 dependency
KastanDay Sep 21, 2023
9eeef83
Fix build issue with apt-get by holding back NIXPACKS version to 1.15.0
KastanDay Sep 22, 2023
a7fb1a8
Merge branch 'main' of https://github.com/UIUC-Chatbot/ai-ta-backend
jkmin3 Sep 25, 2023
b969ee4
ignore all notebooks
KastanDay Sep 27, 2023
6585981
add prints to text ingest
KastanDay Sep 27, 2023
b21aad7
move nomic.login() out of the top level so it's only called when the …
KastanDay Sep 27, 2023
1124b7d
adding more prints to bulk_ingest... I think I see why text files in …
KastanDay Sep 27, 2023
7a7d92b
add prints to /ingest route
KastanDay Sep 27, 2023
1793b94
should fix text ingest, we were using the wrong file extençsion AND m…
KastanDay Sep 27, 2023
5792647
add kwargs to ALL ingest methods to avoid errors
KastanDay Sep 27, 2023
c879b71
fix killer bug that prevented mimetypes from working
KastanDay Sep 28, 2023
08f0ecf
further bugfix to mimetype ingest
KastanDay Sep 28, 2023
4200c5e
I think this solves all mimetype troubles
KastanDay Sep 28, 2023
7eb4ecd
change 'is' to '==' for Callable == 'None'
KastanDay Sep 28, 2023
165dc51
add 'wheel' requirement for faster builds
KastanDay Sep 28, 2023
6c3218d
further refinement of bulk_ingest
KastanDay Sep 28, 2023
7422429
fixed video and audio ingest
KastanDay Sep 28, 2023
86c82c9
Webscrape Refactor for better ingestion (#92)
jkmin3 Oct 2, 2023
9865897
Merge branch 'main' of https://github.com/UIUC-Chatbot/ai-ta-backend
jkmin3 Oct 3, 2023
e0892b3
Bugfix Nomic convo logging "Project Locked" (#98)
star-nox Oct 3, 2023
603c3e3
Adding support for excel files
KastanDay Oct 4, 2023
fc817dd
add CSV loader support
KastanDay Oct 4, 2023
76dc5b9
add dependency for excel ingest
KastanDay Oct 4, 2023
d11898d
add image OCR ingest, add ingest fallback to utf-8 encoding when all …
KastanDay Oct 4, 2023
679033e
Merge branch 'main' of https://github.com/UIUC-Chatbot/ai-ta-backend
jkmin3 Oct 9, 2023
4d0566a
only external sites have a depth of one
jkmin3 Oct 9, 2023
6fbd1bb
update split_and_upload() to work on paragraphs first, then sentences…
KastanDay Oct 10, 2023
97a300d
Update PDF parsing to use utf-8 chars instead of ascii
KastanDay Oct 10, 2023
563e0de
removed content from supabase call, used regex to get rid of # at the…
jkmin3 Oct 11, 2023
b2e1911
disregarded the nav and footer completely, also added new base url re…
jkmin3 Oct 14, 2023
817eb2e
Merge pull request #107 from UIUC-Chatbot/external-site-handling
jkmin3 Oct 14, 2023
89f61aa
Adding Canvas ingest (#33)
star-nox Oct 23, 2023
b0c6d3c
add chunk index
KastanDay Oct 25, 2023
daa0fa9
add chunk index - minor hotfix
KastanDay Oct 25, 2023
936ee47
added chunk index to supabase metadata
star-nox Oct 26, 2023
710cce0
shifted chunk_index after text splitter
star-nox Oct 26, 2023
9f7e03f
Make all necessary adjustments to make our 'metadata' dict, a top-lev…
KastanDay Oct 30, 2023
1c92913
testing missing page content
KastanDay Oct 30, 2023
3eb0607
HOTFIX: silly error in metadata parsing, should be fully fixed
KastanDay Oct 30, 2023
285581f
HOTFIX: update the delete endpoints to new Qdrant schema
KastanDay Oct 30, 2023
3961141
Improved Nomic error handling when nomic map does not yet exist (#118)
star-nox Oct 30, 2023
b495e9d
QDRANT tuning: disable rescore to increase speed
KastanDay Oct 30, 2023
72c6094
Fix Canvas ingest; modified download_files and download_assignments (…
star-nox Oct 30, 2023
a1cc5da
playwright in requirements
minump Nov 6, 2023
5dc61d7
updated gitignore
minump Nov 6, 2023
9ba6e9d
Updated Nomic in requirements.txt
star-nox Nov 6, 2023
aafcccd
Merge branch 'github_agent_webhooks' into 132-smart-memory-management
minump Nov 6, 2023
ca806eb
fix openai version to pre 1.0
KastanDay Nov 7, 2023
17a7779
upgrade python from 3.8 to 3.10
KastanDay Nov 7, 2023
8562de7
trying to fix tesseract // pdfminer requirements for image ingest
KastanDay Nov 7, 2023
be34f01
adding strict versions to all requirements
KastanDay Nov 7, 2023
d4b4e8f
Bump pymupdf from 1.22.5 to 1.23.6 (#136)
dependabot[bot] Nov 7, 2023
170ed79
compatible wheel version
KastanDay Nov 7, 2023
6b94aac
upgrade pip during image startup
KastanDay Nov 7, 2023
c084960
properly upgrade pip
KastanDay Nov 7, 2023
f4b8bd9
Fully lock ALL requirements. Hopefully speed up build times, too
KastanDay Nov 7, 2023
4e80002
Limit unstructured dependencies, image balloned from 700MB to 6GB. Ho…
KastanDay Nov 7, 2023
abf1fc2
Lock version of pip
KastanDay Nov 7, 2023
8a8eac2
Lock (correct) version of pip
KastanDay Nov 7, 2023
cf78800
add libgl1 for cv2 in Docker (for unstructured)
KastanDay Nov 7, 2023
62883e8
adding proper error logging to image ingest
KastanDay Nov 7, 2023
fcfa485
Installing unstructured requirements individually to hopefully redoce…
KastanDay Nov 7, 2023
97bbbd9
Reduce use of unstructured, hopefully the install is much smaller now
KastanDay Nov 7, 2023
2103336
Guarantee Unique S3 Upload paths (#137)
KastanDay Nov 7, 2023
e547a94
Bump typing-extensions from 4.7.1 to 4.8.0 (#90)
dependabot[bot] Nov 7, 2023
27a6680
Bump flask from 2.3.3 to 3.0.0 (#101)
dependabot[bot] Nov 7, 2023
a5b418c
Guard against kwargs failures during webscrape
KastanDay Nov 8, 2023
0d371ba
HOTFIX: kwargs in html and pdf ingest for /webscrape
KastanDay Nov 8, 2023
fd16261
memory management
minump Nov 13, 2023
dc0050f
memory agent
minump Nov 13, 2023
ac45fab
removing unwanted import
minump Nov 13, 2023
ed67886
Merge branch 'github_agent_webhooks' into 132-smart-memory-management
minump Nov 14, 2023
9401d64
resolve merge conflict
minump Nov 14, 2023
3ba257c
Update ml4bio_agent.py
minump Nov 17, 2023
2ef52f2
Merge remote-tracking branch 'origin/github_agent_webhooks' into 132-…
minump Nov 17, 2023
2d4dc73
Merge remote-tracking branch 'origin' into 132-smart-memory-management
minump Nov 17, 2023
094256b
updated gitignore with venv
minump Nov 20, 2023
4ac7d52
memoryCallbacks
minump Nov 20, 2023
7c2ee11
ml4bio agent with memory callbacks
minump Nov 20, 2023
4ec4d73
method to get supabase client and langsmitID
minump Nov 20, 2023
85b6068
docker with nocache
minump Nov 20, 2023
21091c7
updated gitignre
minump Nov 20, 2023
51cd814
Implemented RNA-Seq workflow for PBMC Samples
minump Nov 20, 2023
ce8f4ab
supabase client and langsmith id in utils
minump Nov 21, 2023
80516b4
use above filtered result
minump Nov 21, 2023
3d8a26d
langsmith id from utils
minump Nov 21, 2023
c9c008e
langsmith and supabase frmo utils
minump Nov 21, 2023
8f312cb
langsmith and supabase from utils
minump Nov 21, 2023
52f885a
spacing changes
minump Nov 21, 2023
6f722c3
added custom callback
minump Nov 21, 2023
6350c71
custom callback handler
minump Nov 21, 2023
5faf839
image name for workflow agent
minump Nov 21, 2023
7b2d36f
rename to custom callbacks
minump Nov 21, 2023
4970f54
rename to customcallback
minump Nov 21, 2023
ca7922d
Committing changes for RNA-Seq Workflow
minump Nov 21, 2023
6bf6bbe
added customcallback for tools
minump Nov 22, 2023
e725235
callback for tools
minump Nov 22, 2023
a6d2ebd
callback for vectorStore tool
minump Nov 22, 2023
fd53fc7
comment out onChainStart callback
minump Nov 22, 2023
88e327f
all db calls in utils
minump Nov 27, 2023
84f8071
custom callbacks with db updates
minump Nov 27, 2023
086a218
new memory context fetched from db
minump Nov 27, 2023
58af637
full path imports
minump Nov 28, 2023
b2c13a1
insert in array format for db
minump Nov 28, 2023
5211281
Merge branch 'github_agent_webhooks' into 132-smart-memory-management
minump Nov 28, 2023
4dc2c5c
remove unwanted method
minump Nov 28, 2023
3bbaae1
revert to original main call to webhooks handle event
minump Nov 28, 2023
7c3a4f1
changes in order of imports
minump Nov 28, 2023
dde2582
fix typo
minump Nov 28, 2023
1315b5e
minor change to import and extra space
minump Nov 28, 2023
e9b8ce1
Not used comment
minump Nov 28, 2023
002096c
delete unwanted scripts
minump Nov 28, 2023
43a3dcd
delete payload
minump Nov 28, 2023
f98f141
imports with full paths and fixed typo
minump Nov 28, 2023
d726a8a
Import fixes to get both flask and docker running
rohan-uiuc Nov 28, 2023
ccf3535
update imagename with ourtag
minump Dec 1, 2023
b015858
memory agent created
minump Dec 1, 2023
07c600f
add marvin package
minump Dec 7, 2023
e24d717
agent log parsing using marvin
minump Dec 7, 2023
b7e7654
Merge branch 'github_agent_webhooks' into 132-smart-memory-management
rohan-uiuc Jan 10, 2024
8e2f505
Changes to merge memory management with latest changes in github_agen…
rohan-uiuc Jan 11, 2024
3acf82b
removed marvin and fixed agent finish callback
minump Jan 22, 2024
ad35ba7
added agent finish to memory context
minump Jan 22, 2024
d3e7ebe
check for None response
minump Jan 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Supabase SQL
SUPABASE_URL=
SUPABASE_API_KEY=
SUPABASE_READ_ONLY=
SUPABASE_JWT_SECRET=

MATERIALS_SUPABASE_TABLE=uiuc_chatbot
NEW_NEW_NEWNEW_MATERIALS_SUPABASE_TABLE=documents

# QDRANT
QDRANT_COLLECTION_NAME=uiuc-chatbot
DEV_QDRANT_COLLECTION_NAME=dev
QDRANT_URL=
QDRANT_API_KEY=

REFACTORED_MATERIALS_SUPABASE_TABLE=

# AWS
S3_BUCKET_NAME=uiuc-chatbot
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=

OPENAI_API_KEY=

NOMIC_API_KEY=
LINTRULE_SECRET=

# Github Agent
GITHUB_APP_ID=<OptionalForGithubApps>
GITHUB_APP_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----

-----END RSA PRIVATE KEY-----"

NUMEXPR_MAX_THREADS=2
25 changes: 0 additions & 25 deletions .github/workflows/rules.yml
Original file line number Diff line number Diff line change
@@ -1,25 +0,0 @@
name: Rules Check

on:
pull_request:
branches:
- main

jobs:
rules:
runs-on: ubuntu-latest
environment: lintrule

steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 2 # this part is important!

- name: Install Lint Rules
run: |
curl -fsSL https://www.lintrule.com/install.sh | bash

- name: Run Lint Rules Check
run: |
rules check --secret "${{ secrets.LINTRULE_SECRET }}"
12 changes: 7 additions & 5 deletions .github/workflows/yapf-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ name: Format code
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: pip install yapf
run: pip install yapf
- name: Format code with yapf
run: |
pip install yapf
yapf --in-place --recursive --parallel --style='{based_on_style: google, column_limit: 140, indent_width: 2}' --exclude '*.env' .
run: yapf --in-place --recursive --parallel --style='{based_on_style: google, column_limit: 140, indent_width: 2}' --exclude '*.env' .
- name: Commit changes
uses: EndBug/add-and-commit@v4
with:
author_name: ${{ github.actor }}
author_email: ${{ github.actor }}@users.noreply.github.com
message: "Format code with Yapf & custom options"
message: "Format code"
add: "."
branch: ${{ github.ref }}
branch: ${{ github.ref }}
21 changes: 20 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# don't sync coursera docs
coursera-dl/
*parsed.json
*wandb*
wandb
*.ipynb
*.pem

Expand Down Expand Up @@ -166,3 +166,22 @@ pip-selfcheck.json

# End of https://www.toptal.com/developers/gitignore/api/python
.aider*

# IDEA/Webstorm project files
*.idea/
*.iml

#VSCode metadata
.vscode
.venv*
*.env
*.env.template
venv*

# Mac files
.DS_Store

# npm
node_modules

.rules
8 changes: 0 additions & 8 deletions .rules/no-bugs.md

This file was deleted.

18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,22 @@ Automatic [API Reference](https://uiuc-chatbot.github.io/ai-ta-backend/reference

## 📣 Development

- Install Python requirements `pip install -r requirements.txt`
- Start the server for development (with live reloads) `cd ai_ta_backend` then `flask --app ai_ta_backend.main:app --debug run --port 8000`
1. Rename `.env.template` to `.env` and fill in the required variables
2. Install Python requirements `pip install -r requirements.txt`
3. Start the server for development (with live reloads) `cd ai_ta_backend` then `flask --app ai_ta_backend.main:app --debug run --port 8000`

The docs are auto-built and deployed to [our docs website](https://uiuc-chatbot.github.io/ai-ta-backend/) on every push. Or you can build the docs locally when writing:
- `mkdocs serve`


### Course metadata structure
```
'text': doc.page_content,
'readable_filename': doc.metadata['readable_filename'],
'course_name ': doc.metadata['course_name'],
's3_path': doc.metadata['s3_path'],
'pagenumber': doc.metadata['pagenumber_or_timestamp'], # this is the recent breaking change!!
# OPTIONAL properties
'url': doc.metadata.get('url'), # wouldn't this error out?
'base_url': doc.metadata.get('base_url'),
```
77 changes: 0 additions & 77 deletions ai_ta_backend/agents/MemoryCallbacks.py

This file was deleted.

151 changes: 151 additions & 0 deletions ai_ta_backend/agents/customcallbacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
from typing import Dict, Union, Any, List
import os

from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema import AgentAction, AgentFinish
from langchain.agents import AgentType, initialize_agent, load_tools
from langchain.callbacks import tracing_enabled
from langchain.llms import OpenAI
from langchain import hub
from ai_ta_backend.agents.utils import SupabaseDB, get_langsmith_id
import marvin
from marvin import ai_model
from pydantic import BaseModel, Field

from dotenv import load_dotenv
load_dotenv(override=True, dotenv_path='.env')

marvin.settings.openai.api_key = os.getenv('OPENAI_API_KEY')
@ai_model
class AgentActionParser(BaseModel):
log: str = Field(..., description="clean logs information from input, without new line breaks and markdowns")
action: str = Field(..., description="parse action field from logs")
action_input: str = Field(..., description="parse action input from logs")
action_output: str = Field(..., description="parse action output from logs")
tool: str = Field(..., description="any tool information from input")
tool_input: str = Field(..., description="any tool input")
tool_output: str = Field(..., description="parse tool output")

class CustomCallbackHandler(BaseCallbackHandler):
"""A callback handler that stores the LLM's context and action in memory."""
def __init__(self, run_id=None, image_name=None):
if run_id:
self.langsmith_run_id = run_id
else:
self.langsmith_run_id = get_langsmith_id()

if image_name:
self.image_name = image_name
else:
self.image_name = ''

self.tool_in_progress = {"status": False, "name": ""}
self.db = SupabaseDB(table_name="docker_images", image_name=self.image_name)


def on_tool_start(self, serialized: Dict[str, Any], input_str: str, **kwargs: Any) -> Any:
"""Callback for when a tool starts.
Set tool_in_progress to True and store the tool's name in memory."""
self.tool_in_progress['status'] = True
self.tool_in_progress['name'] = serialized['name']

if self.db.is_exists_image():
data = self.db.fetch_field_from_db("on_tool_start")
if data:
data.append(serialized)
response = self.db.update_field_in_db("on_tool_start", data)
else:
response = self.db.update_field_in_db("on_tool_start", [serialized])
else:
response = self.db.upsert_field_in_db("on_tool_start", [serialized])



def on_tool_end(self, output: str, **kwargs: Any) -> Any:
"""Callback for when a tool ends.
Use this to store the tool's output in a database. Use tool start parameters to identify the tool."""
if self.tool_in_progress['status']:
tool_name = self.tool_in_progress['name']
output = {"name": tool_name, "output": output}
# reset tool_in_progress
self.tool_in_progress['status'] = False
self.tool_in_progress['name'] = ''

if self.db.is_exists_image():
data = self.db.fetch_field_from_db("on_tool_end")
if data is not None or len(data) > 0:
data.append(output)
response = self.db.update_field_in_db("on_tool_end", data)
else:
response = self.db.update_field_in_db("on_tool_end", [output])
else:
response = self.db.upsert_field_in_db("on_tool_end", [output])


def on_tool_error(self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any) -> Any:
"""Run when LLM errors."""
pass

def on_llm_error(self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any) -> Any:
"""Run when LLM errors."""
pass

def on_agent_action(self, action: AgentAction, **kwargs: Any) -> Any:
"""Run when LLM predicts an action. Parse the action and store it in a database."""
action_data = {"action": action.tool, "action_input" : action.tool_input}
# action = action.dict()
# parsed_action = AgentActionParser(action)
# action_data = {"log": parsed_action.log, "action": parsed_action.action, "action_input": parsed_action.action_input,
# "action_output": parsed_action.action_output, "tool": parsed_action.tool,
# "tool_input": parsed_action.tool_input, "tool_output": parsed_action.tool_output}

if self.db.is_exists_image():
data = self.db.fetch_field_from_db("on_agent_action")
if data is not None:
data.append(action_data)
response = self.db.update_field_in_db("on_agent_action", data)
else:
response = self.db.update_field_in_db("on_agent_action", [action_data])
else:
response = self.db.upsert_field_in_db("on_agent_action", [action_data])

def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> Any:
"""Run when LLM finishes. Store the finish in a database."""
finish = finish.dict()
finish_output = finish["return_values"]["output"]

if self.db.is_exists_image():
data = self.db.fetch_field_from_db("on_agent_finish")
if data is not None:
data.append(finish_output)
response = self.db.update_field_in_db("on_agent_finish", data)
else:
response = self.db.update_field_in_db("on_agent_finish", [finish_output])
else:
response = self.db.upsert_field_in_db("on_agent_finish", [finish_output])


# def agent_output():
# TODO
# missing agent output in db


def on_chain_start(self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any) -> Any:
"""Callback for when a chain starts."""
pass



if __name__ == "__main__":
# for testing
handler1 = CustomCallbackHandler()

memory_prompt = hub.pull("kastanday/memory_manager_agent")

llm = OpenAI(temperature=0, streaming=True)
tools = load_tools(["llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION)

# Callbacks for handler1 will be issued by every object involved in the
# Agent execution (llm, llmchain, tool, agent executor)
agent.run("What is 2 raised to the 0.235 power?", callbacks=[handler1])
1 change: 1 addition & 0 deletions ai_ta_backend/agents/github_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,4 @@ def bot_runner_with_retries(self, bot: AgentExecutor, run_instruction, run_id_in

if __name__ == "__main__":
print("No code.")

Loading