Pydantic AI ચુકવણી એકીકરણ.
કોઈ Pydantic AI પેકેજ નથી, અને તમને એકની જરૂર નથી. @agent.tool_plain સાથે એક ટાઇપ કરેલ કાર્ય નોંધણી કરો, વાસ્તવિક blockchain0x ક્લાયન્ટને કૉલ કરો, અને તમારો એજન્ટ Base પર USDC ખસેડી શકે છે.
કોઈ Pydantic AI-specific package નથી, અને તમને તેની જરૂર પણ નથી. Pydantic AI સાથે function ને validated tool માં ફેરવે છે, તેથી તમે real Python client ને typed function માં wrap કરો છો અને agent USDC મોકલી શકે છે, invoices settle કરી શકે છે, અને wallets વાંચી શકે છે. તે OpenAI, Anthropic, Google, Mistral, Groq, અને અન્ય તમામ Pydantic AI providers across કામ કરે છે, કારણ કે tool model નહીં, પરંતુ HTTP API call કરે છે. Payments Base પર settle થાય છે.
Pydantic AI ની ટાઇપિંગ જીતે છે. તેને ચુકવણીઓ માટે પણ ઉપયોગ કરો.
Pydantic AI નો સંપૂર્ણ ઉદ્દેશ મજબૂત ટાઇપિંગ છે: એજન્ટ ઇનપુટ્સ ટાઇપ કરેલ છે, આઉટપુટ્સ ટાઇપ કરેલ છે, નિર્ભરતા ટાઇપ કરેલ છે, પુનરાવર્તન ટાઇપ કરેલ છે. ફ્રેમવર્ક બાઉન્ડરી પર ખોટા ડેટાને નકારી દે છે તેના બદલે કે તે LLM કૉલ દ્વારા ફેલાય. તમારું વૉલેટ સાધન ચોક્કસ રીતે તે પર આધાર રાખે છે - તર્કોને ટાઇપ સાથે જાહેર કરો, અને Pydantic AI સહી અને ડોકસ્ટ્રિંગમાંથી માન્ય સ્કીમા બનાવે છે પહેલા મોડેલને તેને કૉલ કરવાની મંજૂરી આપવામાં આવે.
વાસ્તવિક લાભ: જો મોડલ send_usdc ને એક ફ્લોટ સાથે કૉલ કરવાનો પ્રયાસ કરે છે, તો આધાર-એકમના સ્ટ્રિંગની જગ્યાએ, નિષ્ફળતા Pydantic ની માન્યતા સ્તરે થાય છે કોઈપણ HTTP કૉલ કરતા પહેલા અમારું. તમે એક સ્પષ્ટ ValidationError જુઓ છો જે ઉપયોગી સંદેશ સાથે છે, ન કે API માંથી એક 422 સાથે ક્રિપ્ટિક ફીલ્ડ પાથ. તે અહીં ક્લાયન્ટને તમારી જાતે લપેટવા માટેનો સંપૂર્ણ કારણ છે generic એડેપ્ટર માટે પહોંચવા કરતાં - તમે વાયર સુધી ટાઇપિંગ રાખો છો.
Pydantic AI અને મુખ્ય SDK સ્થાપિત કરો. બે કી.
જોડવા માટે કોઈ blockchain0x Pydantic AI પેકેજ નથી. તમે Pydantic AI (Python 3.10+) અને વાસ્તવિક blockchain0x મુખ્ય SDKને ઇન્સ્ટોલ કરો છો, પછી નીચેનું કાર્ય લખો. તે સમગ્ર નિર્ભરતા યાદી છે.
pip install pydantic-ai blockchain0xexport OPENAI_API_KEY=sk-... export BLOCKCHAIN0X_API_KEY=sk_test_... # sk_test_ = Base Sepolia, sk_live_ = Base mainnet
OPENAI_API_KEY (અથવા તમે જે provider વાપરો તેનું સમકક્ષ). BLOCKCHAIN0X_API_KEY તમારા dashboardમાંથી મળતી sk_test_ testnet અથવા sk_live_ mainnet key છે; client તેને environmentમાંથી વાંચે છે. જો તમારો agent પૈસા પણ મેળવે છે, તો webhook handler ને વધારામાં BLOCKCHAIN0X_WEBHOOK_SECRETની જરૂર પડશે.
એક ટાઇપ કરેલ ફંક્શન જે ચૂકવણી કરે છે, ટૂલ તરીકે નોંધાયેલ છે.
નીચે સમગ્ર એકીકરણ છે. send_usdc વાસ્તવિક blockchain0x ક્લાયન્ટને કોલ કરે છે; @agent.tool_plain તેને પ્રકારના સંકેતો અને ડોકસ્ટ્રિંગમાંથી બનાવેલ સ્કીમા સાથે નોંધણી કરે છે. તેને ચલાવો અને એજન્ટ Base પર USDC ખસેડે છે, કોલ પહેલાં દલીલોને માન્ય કરવામાં આવે છે.
from pydantic_ai import Agent from blockchain0x import Client blockchain0x = Client() # reads BLOCKCHAIN0X_API_KEY from the environment agent = Agent( "openai:gpt-4o", system_prompt="You pay vendor invoices in USDC within owner-set limits.", ) # Register a plain function as a tool. No dedicated package needed. @agent.tool_plain def send_usdc(agent_id: str, to: str, amount_wei: str) -> str: """Send a USDC payment from an agent wallet. amount_wei is USDC base units (6 decimals), so "10000" is 0.01 USDC. """ return str( blockchain0x.payments.create(body={"agentId": agent_id, "to": to, "amountWei": amount_wei}) ) result = agent.run_sync( "Pay 0.01 USDC from agent agt_123 to 0xVendor for the dataset." ) print(result.output)
When the agent decides to pay, it calls send_usdc, the SDK submits the transfer, and you get a transaction hash back. amount_wei is base units, so 0.01 USDC is "10000". A sk_test_ key keeps it on Base Sepolia until you switch to sk_live_. Want a typed result object instead of a string? Set the agent's output_type to your own Pydantic model and return it from the tool. Add read and settle functions the same way.
સહી કરેલ વેબહૂક સાથે ઇનબાઉન્ડ ચુકવણીઓની પુષ્ટિ કરો.
જો તમારો એજન્ટ પણ USDC પ્રાપ્ત કરે છે, તો તેને વેબહૂક સાથે પુષ્ટિ કરો, પોલિંગ દ્વારા નહીં. ચકાસણી સહાયક નોડ SDK માં શિપ થાય છે; એક પાયથન સેવા માં તમે દસ્તાવેજિત HMAC સામે હાથથી ચકાસો છો. જો તમે ટાઇપ કરેલા ઇવેન્ટ્સ માંગતા હો, તો તમારા પોતાના Pydantic મોડલને વ્યાખ્યાયિત કરો અને સહીની ચકાસણી પછી કાચા શરીરને model_validate_json કરો - ટાઇપિંગ ઉમેરવા માટે તમારી છે. નીચેનું ફાસ્ટએપીઆઈ ઉદાહરણ.
import hmac, hashlib, os, time from fastapi import FastAPI, Request, HTTPException app = FastAPI() SECRET = os.environ["BLOCKCHAIN0X_WEBHOOK_SECRET"].encode() @app.post("/webhooks/payment") async def receive(request: Request): raw = await request.body() # RAW bytes - do not parse first sig = request.headers.get("X-Blockchain0x-Signature", "") ts = request.headers.get("X-Blockchain0x-Timestamp", "") parts = dict(p.split("=", 1) for p in sig.split(",") if "=" in p) t, v1 = parts.get("t", ts), parts.get("v1", sig) want = hmac.new(SECRET, t.encode() + b"." + raw, hashlib.sha256).hexdigest() if not hmac.compare_digest(want, v1) or abs(time.time() - int(t)) > 300: raise HTTPException(status_code=401) if request.headers.get("X-Blockchain0x-Event-Type") == "payment.received": await trigger_followup() # USDC landed - run the next step return {"ok": True}
અલ્ગોરિધમ t.rawBody પર HMAC-SHA256 છે, એક સ્થિર-સમયની તુલના, અને 300-સેકન્ડનું પુનરાવર્તન વિન્ડો. કાચી બોડીને await request.body() દ્વારા વાંચો, ક્યારેય request.json() ફરીથી-સિરિયલાઇઝ ન કરો, કારણ કે તે સહીના કવર કરેલા બાઇટ્સને બદલે છે. શિપ કરેલ ઇવેન્ટ્સ છે payment.received, payment.sent, wallet.deployed, અને webhook.test; શાખા કરવા માટે X-Blockchain0x-Event-Type હેડર પર નારોઅ.
તમે લપેટી રહેલા ક્લાયન્ટ ખુલ્લા છે. તેને વાંચો.
ક્લોન કરવા માટે કોઈ Pydantic AI starter package નથી - ઉપર આપેલું recipe જ integration છે. blockchain0x SDKs GitHub પર open source છે; આ recipe Python SDK (blockchain0x-python)ને wrap કરે છે, અને સંપૂર્ણ method surface docsમાં છે. function bodies માટે reference તરીકે તેને વાંચો.
github.com/tosh-labs/blockchain0x-pythonસંપૂર્ણ SDK method surface અને scopes the docsમાં documented છે. Base Sepolia સામે sk_test_ keyથી શરૂ કરો, પછી function અપેક્ષા મુજબ કામ કરે ત્યારે sk_live_ પર switch કરો.
પાંચ Pydantic AI-વિશિષ્ટ જાળાઓથી બચવું.
Pydantic AI ની મજબૂત ટાઇપિંગ સીમા પર મોટાભાગના એકીકરણ બગ્સ પકડે છે; આ થોડા છે જે પસાર થાય છે.
કોઈ Pydantic AI પેકેજ નથી - તમે એક ટૂલ નોંધણી કરો છો
Blockchain0x LangChain અને CrewAI માટે એડેપ્ટર્સ મોકલે છે અને MCP સર્વર; ત્યાં કોઈ સમર્પિત Pydantic AI પેકેજ નથી. ઉપરનો નુસખો માર્ગ છે: @agent.tool_plain સાથે શણગારેલ એક સાદી ટાઇપ કરેલ કાર્ય જે વાસ્તવિક blockchain0x ક્લાયન્ટને કોલ કરે છે. Pydantic AI હસ્તાક્ષર અને ડોકસ્ટ્રિંગને વાંચે છે જેથી માન્ય સાધન સ્કીમા બનાવવામાં આવે, જે ચોક્કસપણે તે ટાઇપિંગ જીત છે જે તમે Pydantic AI માટે આવ્યા હતા.
રકમને એક સ્ટ્રિંગ રાખો; Pydanticને રેખા રાખવા દો
payments.create takes amountWei: a string of USDC base units (6 decimals), so 0.01 USDC is "10000" and 5 USDC is "5000000". Type the tool argument as str and Pydantic AI rejects a float at the boundary with a clear ValidationError, before any HTTP call. That is the whole point of doing this in Pydantic AI - the malformed value never reaches the API.
send_payment વહેલા 503 જવાબ આપી શકે છે
payments.create ડિફોલ્ટ દ્વારા પુનરાવૃત્ત નથી કરે અને તમારા નેટવર્ક માટે ચેઇન એડેપ્ટર વાયર કરવામાં આવે ત્યાં સુધી 503 પાછું આપી શકે છે. તમારા ટૂલની અંદર ભૂલને પકડો અને મોડેલ કાર્ય કરી શકે તેવા સ્પષ્ટ સંદેશાને પાછું આપો, એજન્ટને લૂપ પર મૂકવા બદલ. આપોઆપ મિન્ટ કરેલ idempotency કીનો અર્થ એ છે કે મેન્યુઅલ પુનરાવૃત્તિ ડબલ-ચુકવણી નહીં કરે.
પ્રદાતા-પ્રિફિક્સ મોડેલ સ્ટ્રિંગ્સ
Pydantic AI પ્રદાતા-પ્રિફિક્સ મોડેલ સ્ટ્રિંગ્સનો ઉપયોગ કરે છે: 'openai:gpt-4o', 'anthropic:claude-3-5-sonnet-latest', 'google-gla:gemini-1.5-pro'. પ્રિફિક્સને છોડી દેવું એક રહસ્યમય 'અજ્ઞાત મોડેલ' ભૂલ આપે છે. તમારું સાધન પ્રદાતા-અજ્ઞાત છે કારણ કે તે HTTP API ને કૉલ કરે છે, મોડેલને નહીં - પ્રિફિક્સને સ્વતંત્ર રીતે બદલો અને વૉલેટ કાર્ય અપરિવર્તિત રહે છે.
tool_plain સામે tool (RunContext)
જો કાર્યને ઉપર મુજબ ચલાવામાંથી કશું જ નથી જોઈએ, ત્યારે @agent.tool_plain નો ઉપયોગ કરો. જો તમે ટૂલમાં એજન્ટની નિર્ભરતાઓ (એક પ્રતિ-વિનંતી ક્લાયન્ટ આઈડી, એક ખર્ચ કૅપ) જોઈએ છો, તો @agent.tool નો ઉપયોગ કરો અને પ્રથમ દલીલને ctx: RunContext[Deps] તરીકે જાહેર કરો જેથી ctx.deps વાંચી શકાય. બંનેને મિશ્રિત કરવું - tool_plain પર ctx દલીલ જાહેર કરવું - નોંધણીમાં TypeError ઉઠાવે છે.