Agent Orchestration

Introduction #

Agent orchestration is SmartChart’s capability for handling multi-step, multi-tool collaboration in complex AI scenarios. Through Python datasets, you can chain multiple agents, LLM calls, database queries, and data writes like writing a script, implementing complete AI business processes.

Use Cases #

  • Connecting LLM + data + charts + forms + reach
  • Complex logic flows needed
  • Invoice review example

Key Functions #

Available in Python datasets:

Data Read Functions #

# Get dataset via connection name and SQL
ds_sql(conn_name, sql)

# Read any file or webpage
ds_read(file_or_url, encoding="utf-8", clean=True, max_length=None, as_base64=False, selector='')
# When selector is not empty (e.g., 'body'), uses browser to fetch data

# Write markdown to Word
ds_save_doc(markdown_string, filename)

Data Write Functions #

# Write data to data source
ds_save(config, content, update=0)
# config: {'conn':'connection_name','table':'table_name(field,..)'}
# content: dict or 2D array
# Returns {'status':200,'msg':'success'} on success

LLM Call Functions #

# Get LLM generated result
ds_gpt(conn_name, prompt, system='', stream=False, his='', tool=False, remark={}, files='')
# system: system prompt
# stream: enable streaming output
# his: history
# tool: output tool call intent (returns {'tool':..., 'sql'/'msg'/...})
# files: file list (multimodal input)

Agent Tool Call Functions #

# Execute agent
ds_tool('agent_name', param=None, deep=0, safe=False)
# deep: max recursive agent calls
# safe: safe execution mode

# Examples
ds_tool('PlaceOrder', {'account': 'xxx', 'product': 'xxxx'})
ds_tool({'tool': 'agent_name', 'param': {'xx': 'xxx'}})   # Execute ds_gpt tool intent
ds_tool({'tool': 'agent_name', 'msg': 'xxxx'})             # Pass msg as prompt
ds_tool('agent_name', 'prompt_string')                       # Shorthand

Agent Return Value Formats #

Agent Type Return Format
SQL Agent [['Province','Count'],['Guangdong',123]]
Component Agent {'msg': name, 'ds': dataset, 'chart': chart, 'token': 0, 'status': 200}
LLM Agent {'msg': 'xxxxxx', 'token': 0, 'status': 200}
Python Agent {'msg': 'xxxxxx', 'token': 0, 'status': 200}

Complex Agent Example (Invoice Review) #

  • Create an LLM data source, using dashAI to call Alibaba DashScope

dashAI supports qwen model, auto-switches to vl model for image uploads (default qwen-vl, changeable via {"vmodel":"qwen-vl-max"})

  • Create agent dataset with Python data source, named “Invoice Review”:
import json
gpt_dict = json.loads("""$gpt_dict""")
history = gpt_dict['his']
prompt = """$prompt"""
p0 = """
Judge user intent:
1.Invoice review -> {"status":1}
2.Data submission -> {"status":2}
3.Query/aggregate data -> {"status":3}
4.If none apply, ask user for intent
"""

p1=f"""
If user needs invoice review, identify attachment content and review.
Rules: amount cannot exceed 150; invoice info must be genuine, no fake reimbursement.
If approved, ask user to confirm submission.
If rejected, provide reason.
Output format:
Identified content: xxxxx
Approved: Yes or No
Reason: xxx
My question:
{prompt}
"""

p2="""
Based on the last review result (ignore previous results):
If not approved, do not allow submission, provide reason.
If approved, output insert SQL JSON:
{"tool":"local","sql":"INSERT INTO fpshjl (approved, reason) VALUES ('Yes/No', 'your reason')"}
"""

p3="""
Data stored in SQLite table with structure:
create table fpshjl(approved varchar(100), reason text, create_time DATETIME DEFAULT (datetime(CURRENT_TIMESTAMP,'localtime')))
Output JSON format:
{"tool":"local","sql":"your query sql"}
"""

ds = ds_gpt('Qwen',prompt,his=history+p0,tool=True)
yield f"Intent identified{ds}, processing<br>"
# ---------- Intent 1: file upload -> dashAI for invoice recognition -----------
if ds['status']==1:
    if gpt_dict['files']:
        res = ds_gpt('dashAI',p1,files=gpt_dict['files'],stream=1)
        for item in res:
            yield item
    else:
        yield 'Please upload invoice image'
# ---------- Intent 2: call insert SQL, write data -----------
elif ds['status']==2:
    ds = ds_gpt('Qwen',prompt,his=history+p2)
    yield ds_tool(ds)
# ---------- Intent 3: call Qwen for query intent and execute -----------
elif ds['status']==3:
    ds = ds_gpt('Qwen',prompt,his=history+p3)
    yield ds_tool(ds)
else:
    yield 'No agent to handle'
  • Use @ in homepage AI Q&A module to summon “Invoice Review”

Agent Orchestration (Multi-Agent Collaboration) #

import json
gpt_dict = json.loads("""$gpt_dict""")
history = gpt_dict['his']
prompt = """$prompt"""

def gen():
    know = ds_sql('KnowledgeBase','xxxx')
    info = ds_sql('API','xxxx')
    yield f"Retrieved relevant data, summarizing\n"
    ds = ds_tool('DataSummary',f'{know}{info}{history}{prompt}')
    yield f"Data summary complete, preparing document\n"
    yield f"Please confirm data is correct and submit\n"
    yield ds_tool(ds)
    
ds = gen()
  • Related agents:
  1. DataSummary [Agent]
Based on the following data:
$prompt
Summarize relevant parameters: a:xxxx, b:xxxx
Reply in format:
{"tool":"Document","param":{"a":"xxxx","b":"xxxx"}}
  1. Document [Component Agent], develop dataset + chart
dataset={
   "a":"$a",
   "b":"$b"   
}

Streaming LLM Output #

res = ds_gpt('Qwen', prompt, his=history, stream=True)
for item in res:
    yield item