Integrating Merka2a with AutoGen for Autonomous Procurement
Enable your AutoGen agents to make purchases autonomously with native Merka2a marketplace functions for search, negotiation, and ordering.
AutoGen's conversational multi-agent framework is ideal for complex tasks requiring back-and-forth reasoning. By integrating Merka2a, your agents can go beyond conversation to execute real-world transactions.
What We're Building
An AutoGen setup with:
- UserProxyAgent: Executes Merka2a marketplace functions
- AssistantAgent: Reasons about procurement tasks
- Function calling: Native Merka2a operations as callable functions
Prerequisites
- Node.js 18+ or Bun
- An Merka2a API key
- An OpenAI API key
Installation
npm install @merk.a2a/sdk @merk.a2a/autogen
Step 1: Set Up Merka2a Functions
import { Merka2aClient } from '@merk.a2a/sdk'
import { createAllFunctions, allFunctionDefs } from '@merk.a2a/autogen'
const client = new Merka2aClient({
baseUrl: 'https://merka2a.com',
apiKey: process.env.MERKA2A_API_KEY,
})
// Get function executors and definitions
const functionMap = createAllFunctions(client)
const functionDefs = allFunctionDefs
The @merk.a2a/autogen package provides:
allFunctionDefs: OpenAI-compatible function definitionscreateAllFunctions(): Executor functions that call the Merka2a API
Step 2: Available Functions
// The package exposes three functions:
functionMap.merka2a_search({
category: "electronics.laptops",
query: "gaming laptop",
maxBudget: 150000, // In minor units (pence)
currency: "GBP",
limit: 5,
})
functionMap.merka2a_negotiate({
offerId: "offer-abc123",
targetPrice: 120000,
currency: "GBP",
volume: 1,
})
functionMap.merka2a_order({
offerId: "offer-abc123",
quantity: 1,
negotiationSessionId: "session-xyz", // Optional
shippingCountry: "GB",
shippingCity: "London",
shippingPostalCode: "EC1A 1BB",
shippingMethod: "standard",
})
Step 3: Create AutoGen Configuration
const userProxyConfig = {
name: "Merka2a_Procurement_Proxy",
humanInputMode: "NEVER", // Fully autonomous
maxConsecutiveAutoReply: 10,
functionMap,
isTerminationMsg: (msg: { content?: string }) =>
msg.content?.includes("ORDER_COMPLETE") ||
msg.content?.includes("TASK_FAILED"),
}
const assistantConfig = {
name: "Merka2a_Assistant",
systemMessage: `You are an autonomous procurement agent for the Merka2a marketplace.
Available functions:
- merka2a_search: Search for products by category, query, or budget
- merka2a_negotiate: Negotiate price on an offer
- merka2a_order: Place an order
Workflow:
1. Search for products matching requirements
2. Evaluate options by price, quality, seller reputation
3. Negotiate if price is negotiable and above budget
4. Place order for best value option
5. End with ORDER_COMPLETE or TASK_FAILED
Always explain your reasoning before calling functions.`,
llmConfig: {
functions: functionDefs,
configList: [{ model: "gpt-4-turbo" }],
},
}
Step 4: Execute Function Calls
async function executeFunction(
name: string,
args: Record<string, unknown>
): Promise<string> {
const executor = functionMap[name as keyof typeof functionMap]
if (!executor) {
return `Error: Unknown function "${name}"`
}
try {
return await executor(args as any)
} catch (err) {
return `Error: ${(err as Error).message}`
}
}
Step 5: Run a Procurement Task
// Simulated conversation flow
const task = "Find and order a wireless mouse under £50, ship to London UK"
// Assistant reasons and calls functions
const searchResult = await executeFunction("merka2a_search", {
category: "electronics.mice",
maxBudget: 5000,
currency: "GBP",
limit: 5,
})
console.log("Search Result:", searchResult)
// Found 3 product(s):
// - Logitech MX Master 3S | GBP 45.99 | Offer ID: offer-abc123 | Negotiable: Yes
// - Razer DeathAdder | GBP 39.99 | Offer ID: offer-def456 | Negotiable: No
// ...
Using with Python AutoGen
The TypeScript definitions translate directly to Python:
from autogen import UserProxyAgent, AssistantAgent
import json
import requests
# Define Merka2a functions for Python
def merka2a_search(category: str = None, query: str = None,
max_budget: int = None, currency: str = "GBP", limit: int = 10):
"""Search Merka2a marketplace for products"""
response = requests.post(
"https://merka2a.com/v1/search-intent",
headers={"Authorization": f"Bearer {API_KEY}"},
json={
"intent": {
"category": category,
"query": query,
"budget": {"max": {"amount": max_budget, "currency": currency}} if max_budget else None,
},
"pagination": {"limit": limit}
}
)
return format_search_results(response.json())
# Register functions
user_proxy = UserProxyAgent(
name="Merka2a_Proxy",
human_input_mode="NEVER",
function_map={
"merka2a_search": merka2a_search,
"merka2a_negotiate": merka2a_negotiate,
"merka2a_order": merka2a_order,
}
)
assistant = AssistantAgent(
name="Merka2a_Assistant",
system_message=SYSTEM_MESSAGE,
llm_config={
"functions": FUNCTION_DEFINITIONS,
"config_list": [{"model": "gpt-4"}],
}
)
# Start procurement
user_proxy.initiate_chat(
assistant,
message="Find and order a wireless mouse under £50, ship to London UK"
)
Multi-Agent Procurement
For complex procurement, use multiple specialists:
const researchAssistant = {
name: "Researcher",
systemMessage: "You search for products and analyze options...",
llmConfig: { functions: [searchFunctionDef] },
}
const negotiatorAssistant = {
name: "Negotiator",
systemMessage: "You negotiate the best prices...",
llmConfig: { functions: [negotiateFunctionDef] },
}
const buyerAssistant = {
name: "Buyer",
systemMessage: "You place orders accurately...",
llmConfig: { functions: [orderFunctionDef] },
}
// Group chat coordinates between specialists
Function Definitions Reference
// Search function schema
{
name: "merka2a_search",
description: "Search the Merka2a marketplace for products",
parameters: {
type: "object",
properties: {
category: { type: "string", description: "Product category" },
query: { type: "string", description: "Search query" },
maxBudget: { type: "number", description: "Max price in minor units" },
currency: { type: "string", default: "GBP" },
limit: { type: "number", default: 10 },
}
}
}
// Negotiate function schema
{
name: "merka2a_negotiate",
description: "Negotiate price on an offer",
parameters: {
type: "object",
properties: {
offerId: { type: "string", required: true },
targetPrice: { type: "number", required: true },
currency: { type: "string", default: "GBP" },
volume: { type: "number", default: 1 },
}
}
}
// Order function schema
{
name: "merka2a_order",
description: "Place an order",
parameters: {
type: "object",
properties: {
offerId: { type: "string", required: true },
quantity: { type: "number", default: 1 },
negotiationSessionId: { type: "string" },
shippingCountry: { type: "string", required: true },
shippingCity: { type: "string" },
shippingPostalCode: { type: "string" },
shippingMethod: { type: "string", default: "standard" },
}
}
}
Full Example
See the complete AutoGen integration:
git clone https://github.com/globallayer/Marketplace
cd Marketplace/packages/autogen/examples
npx ts-node user-proxy-agent.ts
Next Steps
- LangChain Integration Guide - ReAct agents
- CrewAI Integration Guide - Role-based crews
- API Reference - Full endpoint documentation
Ready to enable AutoGen transactions? Get your API key →