एजेंट डेवलपमेंट किट के साथ मल्टीमॉडल इस्तेमाल करना: Gemini 2.5, Firestore, और Cloud Run के साथ Personal खर्च असिस्टेंट का इस्तेमाल करना

1. परिचय

d029d993943b282b.png

क्या आपको कभी अपने सभी निजी खर्चों को मैनेज करने में परेशानी हुई है और आपने ऐसा करने से बचने के लिए आलस किया है? मैं भी! इसलिए, इस कोडलैब में हम Gemini 2.5 की मदद से, खर्च मैनेज करने वाली एक निजी असिस्टेंट बनाएंगे, जो हमारे लिए सभी काम करेगी! अपलोड की गई रसीदें मैनेज करने से लेकर, यह विश्लेषण करने तक कि आपने कॉफ़ी खरीदने के लिए पहले ही ज़्यादा खर्च कर दिया है या नहीं!

इस असिस्टेंट को वेब ब्राउज़र से चैट वेब इंटरफ़ेस के तौर पर ऐक्सेस किया जा सकेगा. इसमें आपके पास, उससे बातचीत करने, कुछ रसीद की इमेज अपलोड करने, और उन्हें सेव करने के लिए असिस्टेंट से कहने का विकल्प होगा. इसके अलावा, फ़ाइल पाने और खर्च का विश्लेषण करने के लिए, कुछ रसीदें खोजी जा सकती हैं. यह सुविधा, Google एजेंट डेवलपमेंट किट फ़्रेमवर्क पर बनाई गई है

ऐप्लिकेशन को दो सेवाओं में बांटा गया है: फ़्रंटएंड और बैकएंड. इससे, आपको तुरंत प्रोटोटाइप बनाने और यह जानने में मदद मिलती है कि यह कैसा लगता है. साथ ही, यह भी समझा जा सकता है कि दोनों को इंटिग्रेट करने के लिए एपीआई कॉन्ट्रैक्ट कैसा दिखता है.

कोडलैब में, आपको सिलसिलेवार तरीके से यह तरीका अपनाना होगा:

  1. अपना Google Cloud प्रोजेक्ट तैयार करें और उस पर सभी ज़रूरी एपीआई चालू करें
  2. Google Cloud Storage पर बकेट और Firestore पर डेटाबेस सेट अप करना
  3. Firestore Indexing बनाना
  4. कोडिंग एनवायरमेंट के लिए Workspace सेटअप करना
  5. ADK एजेंट के सोर्स कोड, टूल, प्रॉम्प्ट वगैरह को व्यवस्थित करना
  6. ADK के लोकल वेब डेवलपमेंट यूज़र इंटरफ़ेस (यूआई) का इस्तेमाल करके एजेंट की जांच करना
  7. Gradio लाइब्रेरी का इस्तेमाल करके, फ़्रंटएंड सेवा - चैट इंटरफ़ेस बनाएं. इससे, कुछ क्वेरी भेजने और रसीद की इमेज अपलोड करने में मदद मिलती है
  8. FastAPI का इस्तेमाल करके, बैकएंड सेवा - एचटीटीपी सर्वर बनाएं. इसमें हमारा ADK एजेंट कोड, SessionService, और Artifact Service मौजूद होती है
  9. एनवायरमेंट वैरिएबल मैनेज करना और Cloud Run पर ऐप्लिकेशन को डिप्लॉय करने के लिए ज़रूरी फ़ाइलों को सेट अप करना
  10. ऐप्लिकेशन को Cloud Run पर डिप्लॉय करना

आर्किटेक्चर की खास जानकारी

6795e9abf2030334.jpeg

ज़रूरी शर्तें

  • Python के साथ आसानी से काम करना
  • एचटीटीपी सेवा का इस्तेमाल करके, बुनियादी फ़ुल-स्टैक आर्किटेक्चर के बारे में जानकारी

आपको क्या सीखने को मिलेगा

  • Gradio की मदद से, फ़्रंटएंड वेब प्रोटोटाइप बनाना
  • FastAPI और Pydantic का इस्तेमाल करके बैकएंड सेवा डेवलप करना
  • ADK एजेंट की कई सुविधाओं का इस्तेमाल करते हुए, उसे बेहतर बनाना
  • टूल का इस्तेमाल
  • सेशन और आर्टफ़ैक्ट मैनेजमेंट
  • Gemini को भेजने से पहले, इनपुट में बदलाव करने के लिए कॉलबैक का इस्तेमाल करना
  • टास्क को बेहतर तरीके से पूरा करने के लिए, प्लान बनाने के लिए BuiltInPlanner का इस्तेमाल करना
  • ADK के लोकल वेब इंटरफ़ेस की मदद से, तुरंत डीबग करना
  • ADK कॉलबैक का इस्तेमाल करके, प्रॉम्प्ट इंजीनियरिंग और Gemini अनुरोध में बदलाव करके, जानकारी को पार्स और वापस पाने के ज़रिए, अलग-अलग तरीकों से इंटरैक्ट करने की सुविधा को ऑप्टिमाइज़ करने की रणनीति
  • वेक्टर डेटाबेस के तौर पर Firestore का इस्तेमाल करके, एजेंटिक रीट्रिवल ऑगमेंटेड जनरेशन
  • Pydantic-settings की मदद से, YAML फ़ाइल में एनवायरमेंट वैरिएबल मैनेज करना
  • Dockerfile का इस्तेमाल करके, Cloud Run पर ऐप्लिकेशन को डिप्लॉय करना और YAML फ़ाइल की मदद से एनवायरमेंट वैरिएबल उपलब्ध कराना

आपको इन चीज़ों की ज़रूरत होगी

  • Chrome वेब ब्राउज़र
  • Gmail खाता
  • बिलिंग की सुविधा वाला Cloud प्रोजेक्ट

यह कोडलैब, सभी लेवल के डेवलपर (शुरुआती डेवलपर भी) के लिए डिज़ाइन किया गया है. इसमें सैंपल ऐप्लिकेशन के लिए Python का इस्तेमाल किया गया है. हालांकि, यहां दिए गए कॉन्सेप्ट को समझने के लिए, Python के बारे में जानकारी होना ज़रूरी नहीं है.

2. शुरू करने से पहले

Cloud Console में चालू प्रोजेक्ट चुनना

इस कोडलैब में यह माना गया है कि आपके पास पहले से ही बिलिंग की सुविधा वाला Google Cloud प्रोजेक्ट है. अगर आपके पास यह सुविधा नहीं है, तो इसे चालू करने के लिए नीचे दिए गए निर्देशों का पालन करें.

  1. Google Cloud Console में, प्रोजेक्ट चुनने वाले पेज पर, Google Cloud प्रोजेक्ट चुनें या बनाएं.
  2. पक्का करें कि आपके Cloud प्रोजेक्ट के लिए बिलिंग की सुविधा चालू हो. किसी प्रोजेक्ट के लिए बिलिंग की सुविधा चालू है या नहीं, यह देखने का तरीका जानें.

9b27622602f6cc4f.png

Firestore डेटाबेस तैयार करना

इसके बाद, हमें Firestore डेटाबेस भी बनाना होगा. नेटिव मोड में Firestore, एक NoSQL दस्तावेज़ डेटाबेस है. इसे अपने-आप स्केल करने, बेहतर परफ़ॉर्मेंस देने, और ऐप्लिकेशन डेवलप करने में आसानी के लिए बनाया गया है. यह वेक्टर डेटाबेस के तौर पर भी काम कर सकता है. इससे हमारी लैब में, रीट्रिवल ऑगमेंटेड जनरेशन तकनीक का इस्तेमाल किया जा सकता है.

  1. खोज बार में "firestore" खोजें और Firestore प्रॉडक्ट पर क्लिक करें

2986f598f448af67.png

  1. इसके बाद, Firestore डेटाबेस बनाएं बटन पर क्लिक करें
  2. डेटाबेस आईडी के नाम के तौर पर (डिफ़ॉल्ट) का इस्तेमाल करें और स्टैंडर्ड वर्शन को चुना हुआ रखें. इस लैब डेमो के लिए, ओपन सुरक्षा नियमों के साथ Firestore नेटिव का इस्तेमाल करें.
  1. आपको यह भी दिखेगा कि इस डेटाबेस में मुफ़्त टीयर का इस्तेमाल किया जा रहा है! इसके बाद, डेटाबेस बनाएं बटन पर क्लिक करें

27a5495b76ed7033.png

इन चरणों को पूरा करने के बाद, आपको उस Firestore डेटाबेस पर रीडायरेक्ट कर दिया जाएगा जिसे आपने अभी-अभी बनाया है

Cloud Shell टर्मिनल में Cloud प्रोजेक्ट सेटअप करना

  1. इसके लिए, आपको Cloud Shell का इस्तेमाल करना होगा. यह Google Cloud में चलने वाला कमांड-लाइन एनवायरमेंट है, जिसमें bq पहले से लोड होता है. Google Cloud कंसोल में सबसे ऊपर, 'Cloud Shell चालू करें' पर क्लिक करें.

1829c3759227c19b.png

  1. Cloud Shell से कनेक्ट होने के बाद, यह जांच करें कि आपकी पुष्टि पहले ही हो चुकी है या नहीं. साथ ही, यह भी देखें कि प्रोजेक्ट आपके प्रोजेक्ट आईडी पर सेट है या नहीं. इसके लिए, यह कमांड इस्तेमाल करें:
gcloud auth list
  1. Cloud Shell में यह कमांड चलाकर पुष्टि करें कि gcloud कमांड को आपके प्रोजेक्ट के बारे में पता है.
gcloud config list project
  1. अगर आपका प्रोजेक्ट सेट नहीं है, तो इसे सेट करने के लिए इस निर्देश का इस्तेमाल करें:
gcloud config set project <YOUR_PROJECT_ID>

इसके अलावा, कंसोल में भी PROJECT_ID आईडी देखा जा सकता है

4032c45803813f30.jpeg

इस पर क्लिक करने पर, आपको दाईं ओर अपने सभी प्रोजेक्ट और प्रोजेक्ट आईडी दिखेंगे

8dc17eb4271de6b5.jpeg

  1. नीचे दिए गए निर्देश का इस्तेमाल करके, ज़रूरी एपीआई चालू करें. इसमें कुछ मिनट लग सकते हैं. इसलिए, कृपया इंतज़ार करें.
gcloud services enable aiplatform.googleapis.com \
                       firestore.googleapis.com \
                       run.googleapis.com \
                       cloudbuild.googleapis.com \
                       cloudresourcemanager.googleapis.com

निर्देश पूरा होने पर, आपको यहां दिखाए गए मैसेज जैसा मैसेज दिखेगा:

Operation "operations/..." finished successfully.

gcloud कमांड के बजाय, कंसोल में जाकर हर प्रॉडक्ट को खोजें या इस लिंक का इस्तेमाल करें.

अगर कोई एपीआई छूट जाता है, तो उसे लागू करने के दौरान कभी भी चालू किया जा सकता है.

gcloud के निर्देशों और इस्तेमाल के बारे में जानने के लिए, दस्तावेज़ देखें.

Google Cloud Storage बकेट तैयार करना

इसके बाद, अपलोड की गई फ़ाइल को सेव करने के लिए, हमें उसी टर्मिनल से GCS बकेट तैयार करनी होगी. बकेट बनाने के लिए, यह कमांड चलाएं

gsutil mb -l us-central1 gs://personal-expense-assistant-receipts

इससे यह आउटपुट दिखेगा

Creating gs://personal-expense-assistant-receipts/...

इसकी पुष्टि करने के लिए, ब्राउज़र में सबसे ऊपर बाईं ओर मौजूद नेविगेशन मेन्यू में जाएं और क्लाउड स्टोरेज -> बकेट चुनें

d27475d5ce4fcc9d.png

Firestore, मूल रूप से एक NoSQL डेटाबेस है. यह डेटा मॉडल में बेहतर परफ़ॉर्मेंस और सुविधाएं देता है. हालांकि, जटिल क्वेरी के लिए, इसमें कुछ सीमाएं हैं. हम कुछ कंपाउंड मल्टी फ़ील्ड क्वेरी और वेक्टर सर्च का इस्तेमाल करने वाले हैं. इसलिए, हमें पहले कुछ इंडेक्स बनाने होंगे. ज़्यादा जानकारी के लिए, इस दस्तावेज़ को पढ़ें

  1. कंपाउंड क्वेरी के साथ काम करने के लिए इंडेक्स बनाने के लिए, यह कमांड चलाएं
gcloud firestore indexes composite create \
        --collection-group=personal-expense-assistant-receipts \
        --field-config field-path=total_amount,order=ASCENDING \
        --field-config field-path=transaction_time,order=ASCENDING \
        --field-config field-path=__name__,order=ASCENDING \
        --database="(default)"
  1. वेक्टर सर्च की सुविधा के लिए, यह फ़ंक्शन चलाएं
gcloud firestore indexes composite create \
        --collection-group="personal-expense-assistant-receipts" \
        --query-scope=COLLECTION \
        --field-config field-path="embedding",vector-config='{"dimension":"768", "flat": "{}"}' \
        --database="(default)"

बनाया गया इंडेक्स देखने के लिए, Cloud Console में Firestore पर जाएं. इसके बाद, (डिफ़ॉल्ट) डेटाबेस इंस्टेंस पर क्लिक करें और नेविगेशन बार में इंडेक्स चुनें

8b3a4012985ee0b6.png

Cloud Shell Editor पर जाएं और ऐप्लिकेशन की वर्किंग डायरेक्ट्री सेटअप करें

अब हम कोडिंग से जुड़े कुछ काम करने के लिए, अपना कोड एडिटर सेट अप कर सकते हैं. इसके लिए, हम Cloud Shell एडिटर का इस्तेमाल करेंगे

  1. 'एडिटर खोलें' बटन पर क्लिक करें. इससे Cloud Shell एडिटर खुल जाएगा. यहां अपना कोड लिखा जा सकता है b16d56e4979ec951.png
  2. पक्का करें कि Cloud Code प्रोजेक्ट, Cloud Shell एडिटर के सबसे नीचे बाईं ओर (स्टेटस बार) में सेट हो, जैसा कि नीचे दी गई इमेज में हाइलाइट किया गया है. साथ ही, यह उस चालू Google Cloud प्रोजेक्ट पर सेट हो जिसमें आपने बिलिंग की सुविधा चालू की है. अगर कहा जाए, तो अनुमति दें. अगर आपने पहले से ही पिछले निर्देश का पालन किया है, तो हो सकता है कि बटन, साइन इन बटन के बजाय सीधे आपके चालू किए गए प्रोजेक्ट पर ले जाए

f5003b9c38b43262.png

  1. इसके बाद, GitHub से इस कोडलैब के लिए टेंप्लेट की वर्किंग डायरेक्ट्री को क्लोन करें. इसके लिए, नीचे दिया गया कमांड चलाएं. इससे personal-expense-assistant डायरेक्ट्री में वर्किंग डायरेक्ट्री बन जाएगी
git clone https://github.com/alphinside/personal-expense-assistant-adk-codelab-starter.git personal-expense-assistant
  1. इसके बाद, Cloud Shell Editor के सबसे ऊपर मौजूद सेक्शन में जाएं और फ़ाइल->फ़ोल्डर खोलें पर क्लिक करें. इसके बाद, अपनी username डायरेक्ट्री ढूंढें और personal-expense-assistant डायरेक्ट्री ढूंढें. इसके बाद, ठीक है बटन पर क्लिक करें. इससे चुनी गई डायरेक्ट्री, मुख्य वर्किंग डायरेक्ट्री बन जाएगी. इस उदाहरण में, उपयोगकर्ता नाम alvinprayuda है. इसलिए, डायरेक्ट्री का पाथ यहां दिखाया गया है

2c53696f81d805cc.png

a766d380600a988.png

अब, आपका Cloud Shell Editor ऐसा दिखेगा

528df7169f01b016.png

एनवायरमेंट सेटअप करना

Python वर्चुअल एनवायरमेंट तैयार करना

अगला चरण, डेवलपमेंट एनवायरमेंट तैयार करना है. आपका मौजूदा चालू टर्मिनल, personal-expense-assistant वर्किंग डायरेक्ट्री में होना चाहिए. इस कोडलैब में, हम Python 3.12 का इस्तेमाल करेंगे. साथ ही, Python वर्शन और वर्चुअल एनवायरमेंट बनाने और मैनेज करने की प्रोसेस को आसान बनाने के लिए, uv python प्रोजेक्ट मैनेजर का इस्तेमाल करेंगे

  1. अगर आपने अब तक टर्मिनल नहीं खोला है, तो टर्मिनल -> नया टर्मिनल पर क्लिक करके इसे खोलें. इसके अलावा, Ctrl + Shift + C का इस्तेमाल करके भी टर्मिनल खोला जा सकता है. इससे ब्राउज़र के सबसे नीचे एक टर्मिनल विंडो खुलेगी

f8457daf0bed059e.jpeg

  1. uv डाउनलोड करें और यहां दिए गए निर्देश का इस्तेमाल करके, Python 3.12 इंस्टॉल करें
curl -LsSf https://astral.sh/uv/0.6.16/install.sh | sh && \
source $HOME/.local/bin/env && \
uv python install 3.12
  1. अब uv का इस्तेमाल करके वर्चुअल एनवायरमेंट को शुरू करते हैं. इसके लिए , यह कमांड चलाएं
uv sync --frozen

इससे .venv डायरेक्ट्री बन जाएगी और डिपेंडेंसी इंस्टॉल हो जाएंगी. pyproject.toml में एक नज़र डालने से, आपको इस तरह दिखाई गई डिपेंडेंसी के बारे में जानकारी मिलेगी

dependencies = [
    "datasets>=3.5.0",
    "google-adk>=0.2.0",
    "google-cloud-firestore>=2.20.1",
    "gradio>=5.23.1",
    "pydantic>=2.10.6",
    "pydantic-settings[yaml]>=2.8.1",
]
  1. वर्चुअल एनवायरमेंट की जांच करने के लिए, main.py नाम की नई फ़ाइल बनाएं और नीचे दिया गया कोड कॉपी करें
def main():
   print("Hello from personal-expense-assistant-adk!")

if __name__ == "__main__":
   main()
  1. इसके बाद, यह कमांड चलाएं
uv run main.py

आपको नीचे दिखाया गया आउटपुट मिलेगा

Using CPython 3.12
Creating virtual environment at: .venv
Hello from personal-expense-assistant-adk!

इससे पता चलता है कि Python प्रोजेक्ट सही तरीके से सेट अप किया जा रहा है.

कॉन्फ़िगरेशन फ़ाइलें सेटअप करना

अब हमें इस प्रोजेक्ट के लिए कॉन्फ़िगरेशन फ़ाइलें सेट अप करनी होंगी. हम YAML फ़ाइल से कॉन्फ़िगरेशन पढ़ने के लिए, pydantic-settings का इस्तेमाल करते हैं.

नीचे दिए गए कॉन्फ़िगरेशन के साथ, settings.yaml नाम की फ़ाइल बनाएं. फ़ाइल->नई टेक्स्ट फ़ाइल पर क्लिक करें और नीचे दिया गया कोड डालें. इसके बाद, इसे settings.yaml के तौर पर सेव करें

GCLOUD_LOCATION: "us-central1"
GCLOUD_PROJECT_ID: "your_gcloud_project_id"
BACKEND_URL: "http://localhost:8081/chat"
STORAGE_BUCKET_NAME: "personal-expense-assistant-receipts"
DB_COLLECTION_NAME: "personal-expense-assistant-receipts"

इस कोडलैब के लिए, हम GCLOUD_LOCATION, BACKEND_URL, STORAGE_BUCKET_NAME, DB_COLLECTION_NAME, और BACKEND_URL के लिए पहले से कॉन्फ़िगर की गई वैल्यू का इस्तेमाल करेंगे .

अब हम अगले चरण पर जा सकते हैं. पहले एजेंट और फिर सेवाएं बनाना

3. Google ADK और Gemini 2.5 का इस्तेमाल करके एजेंट बनाना

ADK डायरेक्ट्री स्ट्रक्चर के बारे में जानकारी

आइए, सबसे पहले यह जानते हैं कि ADK में क्या-क्या है और एजेंट को कैसे बनाया जाता है. ADK का पूरा दस्तावेज़ इस यूआरएल पर ऐक्सेस किया जा सकता है . ADK, सीएलआई कमांड को लागू करने के दौरान हमें कई सुविधाएं देता है. इनमें से कुछ इस तरह के हैं :

  • एजेंट डायरेक्ट्री का स्ट्रक्चर सेटअप करना
  • सीएलआई इनपुट आउटपुट की मदद से, तुरंत इंटरैक्ट करने की कोशिश करें
  • लोकल डेवलपमेंट यूज़र इंटरफ़ेस (यूआई) वेब इंटरफ़ेस को तुरंत सेटअप करना

अब, सीएलआई कमांड का इस्तेमाल करके एजेंट डायरेक्ट्री स्ट्रक्चर बनाएं. यह कमांड चलाएं

uv run adk create expense_manager_agent \
   --model gemini-2.5-flash-preview-04-17 \
   --project {your-project-id} \
   --region us-central1

इससे एजेंट डायरेक्ट्री का यह स्ट्रक्चर बनेगा

expense_manager_agent/
├── __init__.py
├── .env
├── agent.py

init.py और agent.py की जांच करने पर, आपको यह कोड दिखेगा

# __init__.py

from . import agent
# agent.py

from google.adk.agents import Agent

root_agent = Agent(
    model='gemini-2.5-flash-preview-04-17',
    name='root_agent',
    description='A helpful assistant for user questions.',
    instruction='Answer user questions to the best of your knowledge',
)

Expense Manager एजेंट बनाना

आइए, अपना खर्च मैनेजर एजेंट बनाते हैं! expense_manager_agent/agent.py फ़ाइल खोलें और नीचे दिया गया कोड कॉपी करें. इसमें root_agent शामिल होगा.

# expense_manager_agent/agent.py

from google.adk.agents import Agent
from expense_manager_agent.tools import (
    store_receipt_data,
    search_receipts_by_metadata_filter,
    search_relevant_receipts_by_natural_language_query,
    get_receipt_data_by_image_id,
)
from expense_manager_agent.callbacks import modify_image_data_in_history
import os
from settings import get_settings
from google.adk.planners import BuiltInPlanner
from google.genai import types

SETTINGS = get_settings()
os.environ["GOOGLE_CLOUD_PROJECT"] = SETTINGS.GCLOUD_PROJECT_ID
os.environ["GOOGLE_CLOUD_LOCATION"] = SETTINGS.GCLOUD_LOCATION
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "TRUE"

# Get the code file directory path and read the task prompt file
current_dir = os.path.dirname(os.path.abspath(__file__))
prompt_path = os.path.join(current_dir, "task_prompt.md")
with open(prompt_path, "r") as file:
    task_prompt = file.read()

root_agent = Agent(
    name="expense_manager_agent",
    model="gemini-2.5-flash-preview-04-17",
    description=(
        "Personal expense agent to help user track expenses, analyze receipts, and manage their financial records"
    ),
    instruction=task_prompt,
    tools=[
        store_receipt_data,
        get_receipt_data_by_image_id,
        search_receipts_by_metadata_filter,
        search_relevant_receipts_by_natural_language_query,
    ],
    planner=BuiltInPlanner(
        thinking_config=types.ThinkingConfig(
            thinking_budget=2048,
        )
    ),
    before_model_callback=modify_image_data_in_history,
)

कोड के बारे में जानकारी

इस स्क्रिप्ट में, एजेंट को शुरू करने की प्रोसेस शामिल होती है. इसमें हम ये चीज़ें शुरू करते हैं:

  • इस्तेमाल किए जाने वाले मॉडल को gemini-2.5-flash-preview-04-17 पर सेट करें
  • एजेंट के ब्यौरे और निर्देश को सिस्टम प्रॉम्प्ट के तौर पर सेट अप करें. इसे task_prompt.md से पढ़ा जा रहा है
  • एजेंट की सुविधा के साथ काम करने के लिए ज़रूरी टूल उपलब्ध कराना
  • Gemini 2.5 के फ़्लैश थिंकिंग मॉडल की सुविधाओं का इस्तेमाल करके, आखिरी जवाब जनरेट करने या उसे लागू करने से पहले प्लानिंग की सुविधा चालू करना
  • अनुमान लगाने से पहले भेजे जाने वाले इमेज डेटा की संख्या को सीमित करने के लिए, Gemini को अनुरोध भेजने से पहले कॉलबैक इंटरसेप्ट सेट अप करें

4. एजेंट टूल कॉन्फ़िगर करना

हमारे खर्च मैनेजर एजेंट के पास ये काम करने की सुविधाएं होंगी:

  • रसीद की इमेज से डेटा निकालना और डेटा और फ़ाइल को सेव करना
  • खर्च के डेटा पर सटीक खोज
  • खर्च के डेटा पर कॉन्टेक्स्ट के हिसाब से खोज

इसलिए, हमें इस सुविधा को काम करने के लिए सही टूल चाहिए. expense_manager_agent डायरेक्ट्री में नई फ़ाइल बनाएं और उसका नाम tools.py रखें. इसके बाद, नीचे दिया गया कोड कॉपी करें

# expense_manager_agent/tools.py

import datetime
from typing import Dict, List, Any
from google.cloud import firestore
from google.cloud.firestore_v1.vector import Vector
from google.cloud.firestore_v1 import FieldFilter
from google.cloud.firestore_v1.base_query import And
from google.cloud.firestore_v1.base_vector_query import DistanceMeasure
from settings import get_settings
from google import genai

SETTINGS = get_settings()
DB_CLIENT = firestore.Client(
    project=SETTINGS.GCLOUD_PROJECT_ID
)  # Will use "(default)" database
COLLECTION = DB_CLIENT.collection(SETTINGS.DB_COLLECTION_NAME)
GENAI_CLIENT = genai.Client(
    vertexai=True, location=SETTINGS.GCLOUD_LOCATION, project=SETTINGS.GCLOUD_PROJECT_ID
)
EMBEDDING_DIMENSION = 768
EMBEDDING_FIELD_NAME = "embedding"
INVALID_ITEMS_FORMAT_ERR = """
Invalid items format. Must be a list of dictionaries with 'name', 'price', and 'quantity' keys."""
RECEIPT_DESC_FORMAT = """
Store Name: {store_name}
Transaction Time: {transaction_time}
Total Amount: {total_amount}
Currency: {currency}
Purchased Items:
{purchased_items}
Receipt Image ID: {receipt_id}
"""


def sanitize_image_id(image_id: str) -> str:
    """Sanitize image ID by removing any leading/trailing whitespace."""
    if image_id.startswith("[IMAGE-"):
        image_id = image_id.split("ID ")[1].split("]")[0]

    return image_id.strip()


def store_receipt_data(
    image_id: str,
    store_name: str,
    transaction_time: str,
    total_amount: float,
    purchased_items: List[Dict[str, Any]],
    currency: str = "IDR",
) -> str:
    """
    Store receipt data in the database.

    Args:
        image_id (str): The unique identifier of the image. For example IMAGE-POSITION 0-ID 12345,
            the ID of the image is 12345.
        store_name (str): The name of the store.
        transaction_time (str): The time of purchase, in ISO format ("YYYY-MM-DDTHH:MM:SS.ssssssZ").
        total_amount (float): The total amount spent.
        purchased_items (List[Dict[str, Any]]): A list of items purchased with their prices. Each item must have:
            - name (str): The name of the item.
            - price (float): The price of the item.
            - quantity (int, optional): The quantity of the item. Defaults to 1 if not provided.
        currency (str, optional): The currency of the transaction, can be derived from the store location.
            If unsure, default is "IDR".

    Returns:
        str: A success message with the receipt ID.

    Raises:
        Exception: If the operation failed or input is invalid.
    """
    try:
        # In case of it provide full image placeholder, extract the id string
        image_id = sanitize_image_id(image_id)

        # Check if the receipt already exists
        doc = get_receipt_data_by_image_id(image_id)

        if doc:
            return f"Receipt with ID {image_id} already exists"

        # Validate transaction time
        if not isinstance(transaction_time, str):
            raise ValueError(
                "Invalid transaction time: must be a string in ISO format 'YYYY-MM-DDTHH:MM:SS.ssssssZ'"
            )
        try:
            datetime.datetime.fromisoformat(transaction_time.replace("Z", "+00:00"))
        except ValueError:
            raise ValueError(
                "Invalid transaction time format. Must be in ISO format 'YYYY-MM-DDTHH:MM:SS.ssssssZ'"
            )

        # Validate items format
        if not isinstance(purchased_items, list):
            raise ValueError(INVALID_ITEMS_FORMAT_ERR)

        for _item in purchased_items:
            if (
                not isinstance(_item, dict)
                or "name" not in _item
                or "price" not in _item
            ):
                raise ValueError(INVALID_ITEMS_FORMAT_ERR)

            if "quantity" not in _item:
                _item["quantity"] = 1

        # Create a combined text from all receipt information for better embedding
        result = GENAI_CLIENT.models.embed_content(
            model="text-embedding-004",
            contents=RECEIPT_DESC_FORMAT.format(
                store_name=store_name,
                transaction_time=transaction_time,
                total_amount=total_amount,
                currency=currency,
                purchased_items=purchased_items,
                receipt_id=image_id,
            ),
        )

        embedding = result.embeddings[0].values

        doc = {
            "receipt_id": image_id,
            "store_name": store_name,
            "transaction_time": transaction_time,
            "total_amount": total_amount,
            "currency": currency,
            "purchased_items": purchased_items,
            EMBEDDING_FIELD_NAME: Vector(embedding),
        }

        COLLECTION.add(doc)

        return f"Receipt stored successfully with ID: {image_id}"
    except Exception as e:
        raise Exception(f"Failed to store receipt: {str(e)}")


def search_receipts_by_metadata_filter(
    start_time: str,
    end_time: str,
    min_total_amount: float = -1.0,
    max_total_amount: float = -1.0,
) -> str:
    """
    Filter receipts by metadata within a specific time range and optionally by amount.

    Args:
        start_time (str): The start datetime for the filter (in ISO format, e.g. 'YYYY-MM-DDTHH:MM:SS.ssssssZ').
        end_time (str): The end datetime for the filter (in ISO format, e.g. 'YYYY-MM-DDTHH:MM:SS.ssssssZ').
        min_total_amount (float): The minimum total amount for the filter (inclusive). Defaults to -1.
        max_total_amount (float): The maximum total amount for the filter (inclusive). Defaults to -1.

    Returns:
        str: A string containing the list of receipt data matching all applied filters.

    Raises:
        Exception: If the search failed or input is invalid.
    """
    try:
        # Validate start and end times
        if not isinstance(start_time, str) or not isinstance(end_time, str):
            raise ValueError("start_time and end_time must be strings in ISO format")
        try:
            datetime.datetime.fromisoformat(start_time.replace("Z", "+00:00"))
            datetime.datetime.fromisoformat(end_time.replace("Z", "+00:00"))
        except ValueError:
            raise ValueError("start_time and end_time must be strings in ISO format")

        # Start with the base collection reference
        query = COLLECTION

        # Build the composite query by properly chaining conditions
        # Notes that this demo assume 1 user only,
        # need to refactor the query for multiple user
        filters = [
            FieldFilter("transaction_time", ">=", start_time),
            FieldFilter("transaction_time", "<=", end_time),
        ]

        # Add optional filters
        if min_total_amount != -1:
            filters.append(FieldFilter("total_amount", ">=", min_total_amount))

        if max_total_amount != -1:
            filters.append(FieldFilter("total_amount", "<=", max_total_amount))

        # Apply the filters
        composite_filter = And(filters=filters)
        query = query.where(filter=composite_filter)

        # Execute the query and collect results
        search_result_description = "Search by Metadata Results:\n"
        for doc in query.stream():
            data = doc.to_dict()
            data.pop(
                EMBEDDING_FIELD_NAME, None
            )  # Remove embedding as it's not needed for display

            search_result_description += f"\n{RECEIPT_DESC_FORMAT.format(**data)}"

        return search_result_description
    except Exception as e:
        raise Exception(f"Error filtering receipts: {str(e)}")


def search_relevant_receipts_by_natural_language_query(
    query_text: str, limit: int = 5
) -> str:
    """
    Search for receipts with content most similar to the query using vector search.
    This tool can be use for user query that is difficult to translate into metadata filters.
    Such as store name or item name which sensitive to string matching.
    Use this tool if you cannot utilize the search by metadata filter tool.

    Args:
        query_text (str): The search text (e.g., "coffee", "dinner", "groceries").
        limit (int, optional): Maximum number of results to return (default: 5).

    Returns:
        str: A string containing the list of contextually relevant receipt data.

    Raises:
        Exception: If the search failed or input is invalid.
    """
    try:
        # Generate embedding for the query text
        result = GENAI_CLIENT.models.embed_content(
            model="text-embedding-004", contents=query_text
        )
        query_embedding = result.embeddings[0].values

        # Notes that this demo assume 1 user only,
        # need to refactor the query for multiple user
        vector_query = COLLECTION.find_nearest(
            vector_field=EMBEDDING_FIELD_NAME,
            query_vector=Vector(query_embedding),
            distance_measure=DistanceMeasure.EUCLIDEAN,
            limit=limit,
        )

        # Execute the query and collect results
        search_result_description = "Search by Contextual Relevance Results:\n"
        for doc in vector_query.stream():
            data = doc.to_dict()
            data.pop(
                EMBEDDING_FIELD_NAME, None
            )  # Remove embedding as it's not needed for display
            search_result_description += f"\n{RECEIPT_DESC_FORMAT.format(**data)}"

        return search_result_description
    except Exception as e:
        raise Exception(f"Error searching receipts: {str(e)}")


def get_receipt_data_by_image_id(image_id: str) -> Dict[str, Any]:
    """
    Retrieve receipt data from the database using the image_id.

    Args:
        image_id (str): The unique identifier of the receipt image. For example, if the placeholder is
            [IMAGE-ID 12345], the ID to use is 12345.

    Returns:
        Dict[str, Any]: A dictionary containing the receipt data with the following keys:
            - receipt_id (str): The unique identifier of the receipt image.
            - store_name (str): The name of the store.
            - transaction_time (str): The time of purchase in UTC.
            - total_amount (float): The total amount spent.
            - currency (str): The currency of the transaction.
            - purchased_items (List[Dict[str, Any]]): List of items purchased with their details.
        Returns an empty dictionary if no receipt is found.
    """
    # In case of it provide full image placeholder, extract the id string
    image_id = sanitize_image_id(image_id)

    # Query the receipts collection for documents with matching receipt_id (image_id)
    # Notes that this demo assume 1 user only,
    # need to refactor the query for multiple user
    query = COLLECTION.where(filter=FieldFilter("receipt_id", "==", image_id)).limit(1)
    docs = list(query.stream())

    if not docs:
        return {}

    # Get the first matching document
    doc_data = docs[0].to_dict()
    doc_data.pop(EMBEDDING_FIELD_NAME, None)

    return doc_data

कोड के बारे में जानकारी

टूल के फ़ंक्शन को लागू करने के लिए, हम इन दो मुख्य आइडिया के आधार पर टूल डिज़ाइन करते हैं:

  • इमेज आईडी स्ट्रिंग प्लेसहोल्डर [IMAGE-ID <hash-of-image-1>] का इस्तेमाल करके, रसीद के डेटा को पार्स करना और ओरिजनल फ़ाइल पर मैप करना
  • Firestore डेटाबेस का इस्तेमाल करके डेटा सेव करना और उसे वापस पाना

टूल "store_receipt_data"

6119e1f37f516707.png

यह टूल, ऑप्टिकल कैरेक्टर रिकॉग्निशन टूल है. यह इमेज डेटा से ज़रूरी जानकारी पार्स करेगा. साथ ही, इमेज आईडी स्ट्रिंग को पहचानकर, उन्हें Firestore डेटाबेस में सेव करने के लिए मैप करेगा.

इसके अलावा, यह टूल text-embedding-004 का इस्तेमाल करके, रसीद के कॉन्टेंट को एम्बेड किए गए कॉन्टेंट में बदल देता है, ताकि सभी मेटाडेटा और एम्बेड किए गए कॉन्टेंट को एक साथ सेव और इंडेक्स किया जा सके. क्वेरी या कॉन्टेक्स्ट के हिसाब से खोज की सुविधा चालू करना.

इस टूल को इस्तेमाल करने के बाद, आपको पता चलेगा कि रसीद का डेटा पहले से ही Firestore डेटाबेस में इंडेक्स किया गया है. इसकी जानकारी यहां दी गई है

7b448fcde40fac5a.png

टूल "search_receipts_by_metadata_filter"

9d51a3f12289d184.png

यह टूल, उपयोगकर्ता की क्वेरी को मेटाडेटा क्वेरी फ़िल्टर में बदल देता है. इसकी मदद से, तारीख की सीमा और/या कुल लेन-देन के हिसाब से खोज की जा सकती है. यह मैच होने वाली रसीद का पूरा डेटा दिखाएगा. इस प्रोसेस में, हम एम्बेड करने वाले फ़ील्ड को हटा देंगे, क्योंकि एजेंट को कॉन्टेक्स्ट को समझने के लिए इसकी ज़रूरत नहीं है

टूल "search_relevant_receipts_by_natural_language_query"

b97d3aab9aa53bc9.png

यह हमारा रिट्रीवल ऑगमेंटेड जनरेशन (आरएजी) टूल है. वेक्टर डेटाबेस से काम की रसीदें पाने के लिए, हमारे एजेंट के पास अपनी क्वेरी डिज़ाइन करने की सुविधा होती है. साथ ही, यह भी तय किया जा सकता है कि इस टूल का इस्तेमाल कब किया जाए. एजेंट को यह तय करने की अनुमति देना कि वह इस आरएजी टूल का इस्तेमाल करेगा या नहीं और अपनी क्वेरी डिज़ाइन करेगा, यह एजेंटिक आरएजी के तरीके की परिभाषाओं में से एक है.

हम इसे न सिर्फ़ अपनी क्वेरी बनाने की अनुमति देते हैं, बल्कि यह भी चुनने की अनुमति देते हैं कि उसे कितने काम के दस्तावेज़ चाहिए. सही प्रॉम्प्ट इंजीनियरिंग के साथ, उदाहरण के लिए

# Example prompt

Always filter the result from tool
search_relevant_receipts_by_natural_language_query as the returned 
result may contain irrelevant information

इससे यह टूल एक बेहतर टूल बन जाएगा, जो लगभग किसी भी चीज़ को खोज सकता है. हालांकि, नियरेस्ट नेबर सर्च के सटीक न होने की वजह से, हो सकता है कि यह आपको उम्मीद के मुताबिक सभी नतीजे न दिखाए.

5. कॉलबैक की मदद से बातचीत के संदर्भ में बदलाव करना

Google ADK की मदद से, हम अलग-अलग लेवल पर एजेंट के रनटाइम को "इंटरसेप्ट" कर सकते हैं. इस सुविधा के बारे में ज़्यादा जानने के लिए, इस दस्तावेज़ को पढ़ें . इस लैब में, हम before_model_callback का इस्तेमाल करके, एलएलएम को भेजे जाने से पहले अनुरोध में बदलाव करते हैं. इससे, बातचीत के पुराने इतिहास के संदर्भ में इमेज डेटा हटाया जा सकता है. साथ ही, उपयोगकर्ता के पिछले तीन इंटरैक्शन में सिर्फ़ इमेज डेटा शामिल किया जा सकता है

हालांकि, हम चाहते हैं कि ज़रूरत पड़ने पर एजेंट के पास इमेज डेटा का संदर्भ हो. इसलिए, हमने बातचीत में हर इमेज बाइट डेटा के बाद, स्ट्रिंग इमेज आईडी प्लेसहोल्डर जोड़ने के लिए एक तरीका जोड़ा है. इससे एजेंट को इमेज आईडी को उसकी असली फ़ाइल डेटा से लिंक करने में मदद मिलेगी. इसका इस्तेमाल, इमेज को सेव करने या वापस पाने के दौरान किया जा सकता है. स्ट्रक्चर इस तरह दिखेगा

<image-byte-data-1>
[IMAGE-ID <hash-of-image-1>]
<image-byte-data-2>
[IMAGE-ID <hash-of-image-2>]
And so on..

जब बातचीत के इतिहास में बाइट डेटा पुराना हो जाता है, तब भी टूल के इस्तेमाल की मदद से डेटा ऐक्सेस करने के लिए, स्ट्रिंग आइडेंटिफ़ायर मौजूद रहता है. इमेज का डेटा हटाने के बाद, इतिहास के स्ट्रक्चर का उदाहरण

[IMAGE-ID <hash-of-image-1>]
[IMAGE-ID <hash-of-image-2>]
And so on..

आइए प्रारंभ करें! expense_manager_agent डायरेक्ट्री में नई फ़ाइल बनाएं और उसका नाम callbacks.py रखें. इसके बाद, नीचे दिया गया कोड कॉपी करें

# expense_manager_agent/callbacks.py

import hashlib
from google.genai import types
from google.adk.agents.callback_context import CallbackContext
from google.adk.models.llm_request import LlmRequest


def modify_image_data_in_history(
    callback_context: CallbackContext, llm_request: LlmRequest
) -> None:
    # The following code will modify the request sent to LLM
    # We will only keep image data in the last 3 user messages using a reverse and counter approach

    # Count how many user messages we've processed
    user_message_count = 0

    # Process the reversed list
    for content in reversed(llm_request.contents):
        # Only count for user manual query, not function call
        if (content.role == "user") and (content.parts[0].function_response is None):
            user_message_count += 1
            modified_content_parts = []

            # Check any missing image ID placeholder for any image data
            # Then remove image data from conversation history if more than 3 user messages
            for idx, part in enumerate(content.parts):
                if part.inline_data is None:
                    modified_content_parts.append(part)
                    continue

                if (
                    (idx + 1 >= len(content.parts))
                    or (content.parts[idx + 1].text is None)
                    or (not content.parts[idx + 1].text.startswith("[IMAGE-ID "))
                ):
                    # Generate hash ID for the image and add a placeholder
                    image_data = part.inline_data.data
                    hasher = hashlib.sha256(image_data)
                    image_hash_id = hasher.hexdigest()[:12]
                    placeholder = f"[IMAGE-ID {image_hash_id}]"

                    # Only keep image data in the last 3 user messages
                    if user_message_count <= 3:
                        modified_content_parts.append(part)

                    modified_content_parts.append(types.Part(text=placeholder))

                else:
                    # Only keep image data in the last 3 user messages
                    if user_message_count <= 3:
                        modified_content_parts.append(part)

            # This will modify the contents inside the llm_request
            content.parts = modified_content_parts

6. प्रॉम्प्ट

जटिल इंटरैक्शन और सुविधाओं के साथ एजेंट डिज़ाइन करने के लिए, हमें एजेंट को निर्देश देने के लिए एक अच्छा प्रॉम्प्ट ढूंढना होगा, ताकि वह हमारी पसंद के मुताबिक काम कर सके.

पहले, हमारे पास बातचीत के इतिहास में इमेज डेटा को हैंडल करने का एक तरीका था. साथ ही, हमारे पास ऐसे टूल भी थे जिनका इस्तेमाल करना आसान नहीं था. जैसे, search_relevant_receipts_by_natural_language_query. हम चाहते हैं कि एजेंट, रसीद की सही इमेज खोजकर हमें दिखा सके. इसका मतलब है कि हमें इस पूरी जानकारी को सही तरीके से, प्रॉम्प्ट के सही स्ट्रक्चर में बताना होगा

हम एजेंट से आउटपुट को नीचे दिए गए मार्कडाउन फ़ॉर्मैट में स्ट्रक्चर करने के लिए कहेंगे, ताकि सोचने की प्रोसेस, फ़ाइनल रिस्पॉन्स, और अटैचमेंट ( अगर कोई हो) को पार्स किया जा सके

# THINKING PROCESS

Thinking process here

# FINAL RESPONSE

Response to the user here

Attachments put inside json block

{
    "attachments": [
      "[IMAGE-ID <hash-id-1>]",
      "[IMAGE-ID <hash-id-2>]",
      ...
    ]
}

आइए, इस प्रॉम्प्ट से शुरू करते हैं, ताकि हम खर्च मैनेजर एजेंट के व्यवहार से जुड़ी अपनी शुरुआती उम्मीद को पूरा कर सकें. task_prompt.md फ़ाइल, हमारी मौजूदा वर्किंग डायरेक्ट्री में पहले से मौजूद होनी चाहिए. हालांकि, हमें इसे expense_manager_agent डायरेक्ट्री में ले जाना होगा. इसे किसी दूसरी जगह ले जाने के लिए, यह कमांड चलाएं

mv task_prompt.md expense_manager_agent/task_prompt.md

7. एजेंट की जांच करना

अब सीएलआई के ज़रिए एजेंट से बातचीत करने की कोशिश करते हैं. इसके लिए, यह निर्देश चलाएं

uv run adk run expense_manager_agent

यह इस तरह का आउटपुट दिखाएगा. यहां एजेंट के साथ चैट की जा सकती है. हालांकि, इस इंटरफ़ेस से सिर्फ़ टेक्स्ट भेजा जा सकता है

Log setup complete: /tmp/agents_log/agent.xxxx_xxx.log
To access latest log: tail -F /tmp/agents_log/agent.latest.log
Running agent root_agent, type exit to exit.
user: hello
[root_agent]: Hello there! How can I help you today?
user: 

अब सीएलआई इंटरैक्शन के अलावा, ADK की मदद से डेवलपमेंट यूज़र इंटरफ़ेस (यूआई) भी बनाया जा सकता है. इससे इंटरैक्शन के दौरान, इंटरैक्ट करने और जांच करने में मदद मिलती है. लोकल डेवलपमेंट यूज़र इंटरफ़ेस (यूआई) सर्वर को शुरू करने के लिए, यह कमांड चलाएं

uv run adk web --port 8080

इससे, नीचे दिए गए उदाहरण की तरह आउटपुट जनरेट होगा. इसका मतलब है कि हम वेब इंटरफ़ेस को पहले से ही ऐक्सेस कर सकते हैं

INFO:     Started server process [xxxx]
INFO:     Waiting for application startup.

+-----------------------------------------------------------------------------+
| ADK Web Server started                                                      |
|                                                                             |
| For local testing, access at http://localhost:8080.                         |
+-----------------------------------------------------------------------------+

INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)

अब इसे देखने के लिए, Cloud Shell Editor के सबसे ऊपर मौजूद वेब की झलक बटन पर क्लिक करें. इसके बाद, पोर्ट 8080 पर झलक देखें को चुनें

e7c9f56c2463164.png

आपको नीचे दिया गया वेब पेज दिखेगा. यहां सबसे ऊपर बाईं ओर मौजूद ड्रॉप-डाउन बटन पर, उपलब्ध एजेंट चुने जा सकते हैं. हमारे मामले में, यह expense_manager_agent होना चाहिए. इसके बाद, बॉट से इंटरैक्ट किया जा सकता है. आपको बाईं विंडो में, एजेंट के रनटाइम के दौरान लॉग की जानकारी के बारे में कई जानकारी दिखेगी

b0244afd8da6cc42.png

आइए, कुछ कार्रवाइयां आज़माते हैं! इन दो रसीद के उदाहरणों को अपलोड करें ( सोर्स : Hugging face डेटासेट mousserlane/id_receipt_dataset ) . हर इमेज पर दायां क्लिक करें और इमेज इस तरह सेव करें.. को चुनें ( इससे रसीद की इमेज डाउनलोड हो जाएगी), इसके बाद "क्लिप" आइकॉन पर क्लिक करके, फ़ाइल को बॉट पर अपलोड करें और बताएं कि आपको इन रसीद को सेव करना है

b8ee334373c6e6af.png c83a8c58ac2eff28.png

इसके बाद, कुछ खोजने या फ़ाइल वापस पाने के लिए, इन क्वेरी को आज़माएं

  • "साल 2023 के दौरान हुए खर्च और कुल खर्च की जानकारी दें"
  • "मुझे Indomaret की रसीद की फ़ाइल भेजो"

जब यह कुछ टूल का इस्तेमाल करता है, तो डेवलपमेंट यूज़र इंटरफ़ेस (यूआई) में जाकर देखा जा सकता है कि क्या हो रहा है

bf47d0b35d5a4f28.png

देखें कि एजेंट आपके सवालों का जवाब कैसे देता है. साथ ही, यह भी देखें कि क्या वह task_prompt.py में दिए गए प्रॉम्प्ट में बताए गए सभी नियमों का पालन करता है. बधाई हो! अब आपके पास एक पूरी तरह से काम करने वाला डेवलपमेंट एजेंट है.

अब इसे सही और बेहतर यूज़र इंटरफ़ेस (यूआई) के साथ पूरा करने का समय आ गया है. साथ ही, इमेज फ़ाइल को अपलोड और डाउनलोड करने की सुविधाएं भी जोड़ी जानी हैं.

8. Gradio का इस्तेमाल करके फ़्रंटएंड सेवा बनाना

हम एक चैट वेब इंटरफ़ेस बनाएंगे, जो ऐसा दिखेगा

d029d993943b282b.png

इसमें एक चैट इंटरफ़ेस होता है, जिसमें एक इनपुट फ़ील्ड होता है. इसकी मदद से, उपयोगकर्ता टेक्स्ट भेज सकते हैं और रसीद की इमेज फ़ाइलें अपलोड कर सकते हैं.

हम Gradio का इस्तेमाल करके, फ़्रंटएंड सेवा बनाएंगे.

नई फ़ाइल बनाएं. इसके लिए, फ़ाइल->नई टेक्स्ट फ़ाइल पर क्लिक करें और इसे frontend.py नाम दें. इसके बाद,नीचे दिया गया कोड कॉपी करके सेव करें

import mimetypes
import gradio as gr
import requests
import base64
from typing import List, Dict, Any
from settings import get_settings
from PIL import Image
import io
from schema import ImageData, ChatRequest, ChatResponse


SETTINGS = get_settings()


def encode_image_to_base64_and_get_mime_type(image_path: str) -> ImageData:
    """Encode a file to base64 string and get MIME type.

    Reads an image file and returns the base64-encoded image data and its MIME type.

    Args:
        image_path: Path to the image file to encode.

    Returns:
        ImageData object containing the base64 encoded image data and its MIME type.
    """
    # Read the image file
    with open(image_path, "rb") as file:
        image_content = file.read()

    # Get the mime type
    mime_type = mimetypes.guess_type(image_path)[0]

    # Base64 encode the image
    base64_data = base64.b64encode(image_content).decode("utf-8")

    # Return as ImageData object
    return ImageData(serialized_image=base64_data, mime_type=mime_type)


def decode_base64_to_image(base64_data: str) -> Image.Image:
    """Decode a base64 string to PIL Image.

    Converts a base64-encoded image string back to a PIL Image object
    that can be displayed or processed further.

    Args:
        base64_data: Base64 encoded string of the image.

    Returns:
        PIL Image object of the decoded image.
    """
    # Decode the base64 string and convert to PIL Image
    image_data = base64.b64decode(base64_data)
    image_buffer = io.BytesIO(image_data)
    image = Image.open(image_buffer)

    return image


def get_response_from_llm_backend(
    message: Dict[str, Any],
    history: List[Dict[str, Any]],
) -> List[str | gr.Image]:
    """Send the message and history to the backend and get a response.

    Args:
        message: Dictionary containing the current message with 'text' and optional 'files' keys.
        history: List of previous message dictionaries in the conversation.

    Returns:
        List containing text response and any image attachments from the backend service.
    """
    # Extract files and convert to base64
    image_data = []
    if uploaded_files := message.get("files", []):
        for file_path in uploaded_files:
            image_data.append(encode_image_to_base64_and_get_mime_type(file_path))

    # Prepare the request payload
    payload = ChatRequest(
        text=message["text"],
        files=image_data,
        session_id="default_session",
        user_id="default_user",
    )

    # Send request to backend
    try:
        response = requests.post(SETTINGS.BACKEND_URL, json=payload.model_dump())
        response.raise_for_status()  # Raise exception for HTTP errors

        result = ChatResponse(**response.json())
        if result.error:
            return [f"Error: {result.error}"]

        chat_responses = []

        if result.thinking_process:
            chat_responses.append(
                gr.ChatMessage(
                    role="assistant",
                    content=result.thinking_process,
                    metadata={"title": "🧠 Thinking Process"},
                )
            )

        chat_responses.append(gr.ChatMessage(role="assistant", content=result.response))

        if result.attachments:
            for attachment in result.attachments:
                image_data = attachment.serialized_image
                chat_responses.append(gr.Image(decode_base64_to_image(image_data)))

        return chat_responses
    except requests.exceptions.RequestException as e:
        return [f"Error connecting to backend service: {str(e)}"]


if __name__ == "__main__":
    demo = gr.ChatInterface(
        get_response_from_llm_backend,
        title="Personal Expense Assistant",
        description="This assistant can help you to store receipts data, find receipts, and track your expenses during certain period.",
        type="messages",
        multimodal=True,
        textbox=gr.MultimodalTextbox(file_count="multiple", file_types=["image"]),
    )

    demo.launch(
        server_name="0.0.0.0",
        server_port=8080,
    )

इसके बाद, हम इस कमांड की मदद से फ़्रंटएंड सेवा को चलाने की कोशिश कर सकते हैं. main.py फ़ाइल का नाम बदलकर frontend.py करना न भूलें

uv run frontend.py

आपको अपने Cloud Console में इससे मिलता-जुलता आउटपुट दिखेगा

* Running on local URL:  http://0.0.0.0:8080

To create a public link, set `share=True` in `launch()`.

इसके बाद, स्थानीय यूआरएल लिंक पर ctrl+क्लिक करके, वेब इंटरफ़ेस देखा जा सकता है. इसके अलावा, Cloud Editor के सबसे ऊपर दाईं ओर मौजूद वेब की झलक बटन पर क्लिक करके भी फ़्रंटएंड ऐप्लिकेशन को ऐक्सेस किया जा सकता है. इसके बाद, पोर्ट 8080 पर झलक देखें को चुनें

49cbdfdf77964065.jpeg

आपको वेब इंटरफ़ेस दिखेगा. हालांकि, बैकएंड सेवा अभी सेट अप नहीं होने की वजह से, चैट सबमिट करने पर आपको गड़बड़ी का अनुमानित मैसेज दिखेगा

5caec77d95c35927.png

अब, सेवा को चलने दें और अभी इसे बंद न करें. हम बैकएंड सेवा को किसी दूसरे टर्मिनल टैब में चलाएंगे

कोड के बारे में जानकारी

इस फ़्रंटएंड कोड में, हम उपयोगकर्ता को टेक्स्ट भेजने और एक से ज़्यादा फ़ाइलें अपलोड करने की सुविधा देते हैं. Gradio की मदद से, gr.ChatInterface और gr.MultimodalTextbox के साथ मिलकर, इस तरह की सुविधा बनाई जा सकती है

अब फ़ाइल और टेक्स्ट को बैकएंड पर भेजने से पहले, हमें फ़ाइल का mimetype पता करना होगा, क्योंकि बैकएंड को इसकी ज़रूरत होती है. हमें इमेज फ़ाइल के बाइट को Base64 में एन्कोड करके, mimetype के साथ भेजना होगा.

class ImageData(BaseModel):
    """Model for image data with hash identifier.

    Attributes:
        serialized_image: Optional Base64 encoded string of the image content.
        mime_type: MIME type of the image.
    """

    serialized_image: str
    mime_type: str

फ़ोरग्राउंड - बैकएंड इंटरैक्शन के लिए इस्तेमाल किए गए स्कीमा के बारे में schema.py में बताया गया है. हम स्कीमा में डेटा की पुष्टि करने के लिए, Pydantic BaseModel का इस्तेमाल करते हैं

जवाब मिलने पर, हम पहले से ही यह तय कर लेते हैं कि किस हिस्से में सोच-विचार की प्रक्रिया, फ़ाइनल जवाब, और अटैचमेंट शामिल है. इसलिए, हम यूज़र इंटरफ़ेस (यूआई) कॉम्पोनेंट के साथ हर कॉम्पोनेंट को दिखाने के लिए, Gradio कॉम्पोनेंट का इस्तेमाल कर सकते हैं.

class ChatResponse(BaseModel):
    """Model for a chat response.

    Attributes:
        response: The text response from the model.
        thinking_process: Optional thinking process of the model.
        attachments: List of image data to be displayed to the user.
        error: Optional error message if something went wrong.
    """

    response: str
    thinking_process: str = ""
    attachments: List[ImageData] = []
    error: Optional[str] = None

9. FastAPI का इस्तेमाल करके बैकएंड सेवा बनाना

इसके बाद, हमें बैकएंड बनाना होगा, जो एजेंट के रनटाइम को लागू करने के लिए, अन्य कॉम्पोनेंट के साथ हमारे एजेंट को शुरू कर सके.

नई फ़ाइल बनाएं. इसके लिए, फ़ाइल->नई टेक्स्ट फ़ाइल पर क्लिक करें. इसके बाद,यहां दिया गया कोड कॉपी करके चिपकाएं और इसे backend.py के तौर पर सेव करें

from expense_manager_agent.agent import root_agent as expense_manager_agent
from google.adk.sessions import InMemorySessionService
from google.adk.runners import Runner
from google.adk.events import Event
from fastapi import FastAPI, Body, Depends
from typing import AsyncIterator
from types import SimpleNamespace
import uvicorn
from contextlib import asynccontextmanager
import asyncio
from utils import (
    extract_attachment_ids_and_sanitize_response,
    download_image_from_gcs,
    extract_thinking_process,
    format_user_request_to_adk_content_and_store_artifacts,
)
from schema import ImageData, ChatRequest, ChatResponse
import logger
from google.adk.artifacts import GcsArtifactService
from settings import get_settings

SETTINGS = get_settings()
APP_NAME = "expense_manager_app"


# Application state to hold service contexts
class AppContexts(SimpleNamespace):
    """A class to hold application contexts with attribute access"""

    session_service: InMemorySessionService = None
    artifact_service: GcsArtifactService = None
    expense_manager_agent_runner: Runner = None


# Initialize application state
app_contexts = AppContexts()


@asynccontextmanager
async def lifespan(app: FastAPI):
    # Initialize service contexts during application startup
    app_contexts.session_service = InMemorySessionService()
    app_contexts.artifact_service = GcsArtifactService(
        bucket_name=SETTINGS.STORAGE_BUCKET_NAME
    )
    app_contexts.expense_manager_agent_runner = Runner(
        agent=expense_manager_agent,  # The agent we want to run
        app_name=APP_NAME,  # Associates runs with our app
        session_service=app_contexts.session_service,  # Uses our session manager
        artifact_service=app_contexts.artifact_service,  # Uses our artifact manager
    )

    logger.info("Application started successfully")
    yield
    logger.info("Application shutting down")
    # Perform cleanup during application shutdown if necessary


# Helper function to get application state as a dependency
async def get_app_contexts() -> AppContexts:
    return app_contexts


# Create FastAPI app
app = FastAPI(title="Personal Expense Assistant API", lifespan=lifespan)


@app.post("/chat", response_model=ChatResponse)
async def chat(
    request: ChatRequest = Body(...),
    app_context: AppContexts = Depends(get_app_contexts),
) -> ChatResponse:
    """Process chat request and get response from the agent"""

    # Prepare the user's message in ADK format and store image artifacts
    content = await asyncio.to_thread(
        format_user_request_to_adk_content_and_store_artifacts,
        request=request,
        app_name=APP_NAME,
        artifact_service=app_context.artifact_service,
    )

    final_response_text = "Agent did not produce a final response."  # Default

    # Use the session ID from the request or default if not provided
    session_id = request.session_id
    user_id = request.user_id

    # Create session if it doesn't exist
    if not app_context.session_service.get_session(
        app_name=APP_NAME, user_id=user_id, session_id=session_id
    ):
        app_context.session_service.create_session(
            app_name=APP_NAME, user_id=user_id, session_id=session_id
        )

    try:
        # Process the message with the agent
        # Type annotation: runner.run_async returns an AsyncIterator[Event]
        events_iterator: AsyncIterator[Event] = (
            app_context.expense_manager_agent_runner.run_async(
                user_id=user_id, session_id=session_id, new_message=content
            )
        )
        async for event in events_iterator:  # event has type Event
            # Key Concept: is_final_response() marks the concluding message for the turn
            if event.is_final_response():
                if event.content and event.content.parts:
                    # Extract text from the first part
                    final_response_text = event.content.parts[0].text
                elif event.actions and event.actions.escalate:
                    # Handle potential errors/escalations
                    final_response_text = f"Agent escalated: {event.error_message or 'No specific message.'}"
                break  # Stop processing events once the final response is found

        logger.info(
            "Received final response from agent", raw_final_response=final_response_text
        )

        # Extract and process any attachments and thinking process in the response
        base64_attachments = []
        sanitized_text, attachment_ids = extract_attachment_ids_and_sanitize_response(
            final_response_text
        )
        sanitized_text, thinking_process = extract_thinking_process(sanitized_text)

        # Download images from GCS and replace hash IDs with base64 data
        for image_hash_id in attachment_ids:
            # Download image data and get MIME type
            result = await asyncio.to_thread(
                download_image_from_gcs,
                artifact_service=app_context.artifact_service,
                image_hash=image_hash_id,
                app_name=APP_NAME,
                user_id=user_id,
                session_id=session_id,
            )
            if result:
                base64_data, mime_type = result
                base64_attachments.append(
                    ImageData(serialized_image=base64_data, mime_type=mime_type)
                )

        logger.info(
            "Processed response with attachments",
            sanitized_response=sanitized_text,
            thinking_process=thinking_process,
            attachment_ids=attachment_ids,
        )

        return ChatResponse(
            response=sanitized_text,
            thinking_process=thinking_process,
            attachments=base64_attachments,
        )

    except Exception as e:
        logger.error("Error processing chat request", error_message=str(e))
        return ChatResponse(
            response="", error=f"Error in generating response: {str(e)}"
        )


# Only run the server if this file is executed directly
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8081)

इसके बाद, हम बैकएंड सेवा को चलाने की कोशिश कर सकते हैं. याद रखें कि पिछले चरण में, हमने फ़्रंटएंड सेवा को सही तरीके से चलाया था. अब हमें नया टर्मिनल खोलना होगा और इस बैकएंड सेवा को चलाने की कोशिश करनी होगी

  1. नया टर्मिनल बनाएं. सबसे नीचे मौजूद अपने टर्मिनल पर जाएं और नया टर्मिनल बनाने के लिए, "+" बटन ढूंढें. इसके अलावा, नया टर्मिनल खोलने के लिए, Ctrl + Shift + C दबाएं

3e52a362475553dc.jpeg

  1. इसके बाद, पक्का करें कि आप personal-expense-assistant वर्किंग डायरेक्ट्री में हों. इसके बाद, यह कमांड चलाएं
uv run backend.py
  1. अगर यह प्रोसेस पूरी हो जाती है, तो आपको ऐसा आउटपुट दिखेगा
INFO:     Started server process [xxxxx]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8081 (Press CTRL+C to quit)

कोड के बारे में जानकारी

ADK एजेंट, SessionService, और ArtifactService को शुरू करना

बैकएंड सेवा में एजेंट को चलाने के लिए, हमें एक रनर बनाना होगा, जो SessionService और हमारे एजेंट, दोनों को लेता है. SessionService, बातचीत का इतिहास और स्थिति मैनेज करेगा. इसलिए, Runner के साथ इंटिग्रेट होने पर, यह हमारे एजेंट को मौजूदा बातचीत का कॉन्टेक्स्ट पाने की सुविधा देगा.

हम अपलोड की गई फ़ाइल को मैनेज करने के लिए, ArtifactService का भी इस्तेमाल करते हैं. ADK सेशन और आर्टफ़ैक्ट के बारे में ज़्यादा जानकारी यहां दी गई है

...

@asynccontextmanager
async def lifespan(app: FastAPI):
    # Initialize service contexts during application startup
    app_contexts.session_service = InMemorySessionService()
    app_contexts.artifact_service = GcsArtifactService(
        bucket_name=SETTINGS.STORAGE_BUCKET_NAME
    )
    app_contexts.expense_manager_agent_runner = Runner(
        agent=expense_manager_agent,  # The agent we want to run
        app_name=APP_NAME,  # Associates runs with our app
        session_service=app_contexts.session_service,  # Uses our session manager
        artifact_service=app_contexts.artifact_service,  # Uses our artifact manager
    )

    logger.info("Application started successfully")
    yield
    logger.info("Application shutting down")
    # Perform cleanup during application shutdown if necessary

...

इस डेमो में, हम अपने एजेंट Runner के साथ इंटिग्रेट करने के लिए, InMemorySessionService और GcsArtifactService का इस्तेमाल करते हैं. बातचीत का इतिहास, मेमोरी में सेव होता है. इसलिए, बैकएंड सेवा बंद होने या फिर से शुरू होने पर, यह इतिहास मिट जाएगा. हम इन्हें FastAPI ऐप्लिकेशन लाइफ़साइकल में शुरू करते हैं, ताकि इन्हें /chat रूट में डिपेंडेंसी के तौर पर इंजेक्ट किया जा सके.

GcsArtifactService की मदद से इमेज अपलोड और डाउनलोड करना

अपलोड की गई सभी इमेज को GcsArtifactService, आर्टफ़ैक्ट के तौर पर सेव करेगा. इसकी जांच, utils.py में मौजूद format_user_request_to_adk_content_and_store_artifacts फ़ंक्शन में की जा सकती है

...    

# Prepare the user's message in ADK format and store image artifacts
content = await asyncio.to_thread(
    format_user_request_to_adk_content_and_store_artifacts,
    request=request,
    app_name=APP_NAME,
    artifact_service=app_context.artifact_service,
)

...

एजेंट रनर से प्रोसेस किए जाने वाले सभी अनुरोधों को types.Content टाइप में फ़ॉर्मैट करना होगा. फ़ंक्शन में, हम हर इमेज का डेटा भी प्रोसेस करते हैं और उसका आईडी निकालते हैं, ताकि उसे इमेज आईडी प्लेसहोल्डर से बदला जा सके.

रेगुलर एक्सप्रेशन का इस्तेमाल करके इमेज आईडी निकालने के बाद, अटैचमेंट डाउनलोड करने के लिए भी इसी तरह की प्रोसेस अपनाई जाती है:

...
sanitized_text, attachment_ids = extract_attachment_ids_and_sanitize_response(
    final_response_text
)
sanitized_text, thinking_process = extract_thinking_process(sanitized_text)

# Download images from GCS and replace hash IDs with base64 data
for image_hash_id in attachment_ids:
    # Download image data and get MIME type
    result = await asyncio.to_thread(
        download_image_from_gcs,
        artifact_service=app_context.artifact_service,
        image_hash=image_hash_id,
        app_name=APP_NAME,
        user_id=user_id,
        session_id=session_id,
    )
...

10. इंटिग्रेशन टेस्ट

अब, आपके पास Cloud Console के अलग-अलग टैब में कई सेवाएं होनी चाहिए:

  • पोर्ट 8080 पर चलने वाली फ़ोरग्राउंड सेवा
* Running on local URL:  http://0.0.0.0:8080

To create a public link, set `share=True` in `launch()`.
  • पोर्ट 8081 पर चलने वाली बैकएंड सेवा
INFO:     Started server process [xxxxx]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8081 (Press CTRL+C to quit)

फ़िलहाल, आपको अपनी रसीद की इमेज अपलोड करने और पोर्ट 8080 पर वेब ऐप्लिकेशन से, Assistant के साथ आसानी से चैट करने की सुविधा मिलनी चाहिए.

Cloud Shell Editor के सबसे ऊपर मौजूद, वेब की झलक बटन पर क्लिक करें. इसके बाद, पोर्ट 8080 पर झलक देखें को चुनें

e7c9f56c2463164.png

अब Assistant के साथ कुछ बातचीत करते हैं!

नीचे दी गई रसीदों को डाउनलोड करें. इन रसीद के डेटा की तारीख की सीमा, साल 2023 से 2024 के बीच है. साथ ही, असिस्टेंट से इसे सेव/अपलोड करने के लिए कहें

अलग-अलग चीज़ों के बारे में पूछना

  • "मुझे 2023-2024 के दौरान हर महीने के खर्च की जानकारी दो"
  • "मुझे कॉफी के लेन-देन की रसीद दिखाओ"
  • "मुझे Yakiniku Like की रसीद की फ़ाइल दो"
  • वगैरह

यहां सफल इंटरैक्शन का कुछ स्निपेट दिया गया है

f6ba4537438033b2.png

313a43d32b0901ef.png

11. Cloud Run पर डिप्लॉय करना

अब, हम इस बेहतरीन ऐप्लिकेशन को कहीं से भी ऐक्सेस करना चाहते हैं. ऐसा करने के लिए, हम इस ऐप्लिकेशन को पैकेज कर सकते हैं और उसे Cloud Run पर डिप्लॉय कर सकते हैं. इस डेमो के लिए, इस सेवा को सार्वजनिक सेवा के तौर पर दिखाया जाएगा, जिसे दूसरे लोग ऐक्सेस कर सकते हैं. हालांकि, ध्यान रखें कि इस तरह के ऐप्लिकेशन के लिए यह सबसे सही तरीका नहीं है, क्योंकि यह निजी ऐप्लिकेशन के लिए ज़्यादा सही है

6795e9abf2030334.jpeg

इस कोडलैब में, हम फ़्रंटएंड और बैकएंड, दोनों सेवाओं को एक कंटेनर में डालेंगे. दोनों सेवाओं को मैनेज करने के लिए, हमें supervisord की मदद की ज़रूरत होगी. supervisord.conf फ़ाइल की जांच करके, Dockerfile में यह देखा जा सकता है कि हमने supervisord को एंट्री पॉइंट के तौर पर सेट किया है या नहीं.

अब हमारे पास Cloud Run पर अपने ऐप्लिकेशन डिप्लॉय करने के लिए ज़रूरी सभी फ़ाइलें मौजूद हैं. अब इसे डिप्लॉय करते हैं. Cloud Shell टर्मिनल पर जाएं और पक्का करें कि मौजूदा प्रोजेक्ट, आपके चालू प्रोजेक्ट के तौर पर कॉन्फ़िगर किया गया हो. अगर ऐसा नहीं है, तो प्रोजेक्ट आईडी सेट करने के लिए gcloud configure कमांड का इस्तेमाल करें:

gcloud config set project [PROJECT_ID]

इसके बाद, इसे Cloud Run पर डिप्लॉय करने के लिए, यह कमांड चलाएं.

gcloud run deploy personal-expense-assistant \
                  --source . \
                  --port=8080 \
                  --allow-unauthenticated \
                  --env-vars-file=settings.yaml \
                  --memory 1024Mi \
                  --region us-central1

अगर आपसे Docker रिपॉज़िटरी के लिए आर्टफ़ैक्ट रजिस्ट्री बनाने की पुष्टि करने के लिए कहा जाता है, तो बस हां में जवाब दें. ध्यान दें कि हम यहां बिना पुष्टि किए ऐक्सेस की अनुमति दे रहे हैं, क्योंकि यह एक डेमो ऐप्लिकेशन है. हमारा सुझाव है कि अपने एंटरप्राइज़ और प्रोडक्शन ऐप्लिकेशन के लिए, पुष्टि करने के सही तरीके का इस्तेमाल करें.

डिप्लॉयमेंट पूरा होने के बाद, आपको नीचे दिए गए लिंक जैसा एक लिंक मिलेगा:

https://personal-expense-assistant-*******.us-central1.run.app

अब गुप्त विंडो या अपने मोबाइल डिवाइस से अपने ऐप्लिकेशन का इस्तेमाल करें. यह पहले से लाइव होना चाहिए.

12. चुनौती

अब समय आ गया है कि आप अपनी खोज की स्किल को बेहतर बनाएं. क्या आपके पास कोड में बदलाव करने की ज़रूरी जानकारी है, ताकि बैकएंड में एक से ज़्यादा उपयोगकर्ताओं को शामिल किया जा सके? किन कॉम्पोनेंट को अपडेट करना है?

13. व्यवस्थित करें

इस कोडलैब में इस्तेमाल किए गए संसाधनों के लिए, अपने Google Cloud खाते से शुल्क न लिए जाएं, इसके लिए यह तरीका अपनाएं:

  1. Google Cloud Console में, संसाधन मैनेज करें पेज पर जाएं.
  2. प्रोजेक्ट की सूची में, वह प्रोजेक्ट चुनें जिसे मिटाना है. इसके बाद, मिटाएं पर क्लिक करें.
  3. डायलॉग बॉक्स में, प्रोजेक्ट आईडी टाइप करें. इसके बाद, प्रोजेक्ट मिटाने के लिए बंद करें पर क्लिक करें.
  4. इसके अलावा, आपके पास Console पर Cloud Run पर जाकर, हाल ही में डिप्लॉय की गई सेवा को चुनने और मिटाने का विकल्प भी है.