|
| 1 | +"""Workday integration: timeout and account scoping for slow providers. |
| 2 | +
|
| 3 | +Workday can take 10-15s to respond. This example shows how to configure |
| 4 | +timeout and account_ids through the execute config. |
| 5 | +
|
| 6 | +Run with: |
| 7 | + uv run python examples/workday_integration.py |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import json |
| 13 | +import os |
| 14 | + |
| 15 | +try: |
| 16 | + from dotenv import load_dotenv |
| 17 | + |
| 18 | + load_dotenv() |
| 19 | +except ModuleNotFoundError: |
| 20 | + pass |
| 21 | + |
| 22 | +from openai import OpenAI |
| 23 | + |
| 24 | +from stackone_ai import StackOneToolSet |
| 25 | + |
| 26 | +account_id = os.getenv("STACKONE_ACCOUNT_ID", "") |
| 27 | + |
| 28 | +# Timeout and account_ids both live in the execute config |
| 29 | +toolset = StackOneToolSet( |
| 30 | + search={"method": "auto", "top_k": 5}, |
| 31 | + execute={"account_ids": [account_id], "timeout": 120}, |
| 32 | +) |
| 33 | +client = OpenAI() |
| 34 | + |
| 35 | + |
| 36 | +def run_agent(messages: list[dict], tools: list[dict], max_steps: int = 10) -> None: |
| 37 | + """Simple agent loop: call LLM, execute tools, repeat.""" |
| 38 | + for _ in range(max_steps): |
| 39 | + response = client.chat.completions.create(model="gpt-5.4", messages=messages, tools=tools) |
| 40 | + choice = response.choices[0] |
| 41 | + |
| 42 | + if not choice.message.tool_calls: |
| 43 | + print(f"Answer: {choice.message.content}") |
| 44 | + return |
| 45 | + |
| 46 | + messages.append(choice.message.model_dump(exclude_none=True)) |
| 47 | + for tc in choice.message.tool_calls: |
| 48 | + print(f" -> {tc.function.name}({tc.function.arguments[:80]})") |
| 49 | + tool = toolset.execute(tc.function.name, tc.function.arguments) |
| 50 | + messages.append({"role": "tool", "tool_call_id": tc.id, "content": json.dumps(tool)}) |
| 51 | + |
| 52 | + |
| 53 | +# --- Example 1: Search and execute mode --- |
| 54 | +# LLM gets tool_search + tool_execute, discovers tools autonomously |
| 55 | +print("=== Search and execute mode ===\n") |
| 56 | +run_agent( |
| 57 | + messages=[ |
| 58 | + {"role": "system", "content": "Use tool_search to find tools, then tool_execute to run them."}, |
| 59 | + {"role": "user", "content": "List the first 5 employees."}, |
| 60 | + ], |
| 61 | + tools=toolset.openai(mode="search_and_execute"), |
| 62 | +) |
| 63 | + |
| 64 | +# --- Example 2: Normal mode --- |
| 65 | +# Fetch specific tools upfront, pass to LLM |
| 66 | +print("\n=== Normal mode ===\n") |
| 67 | +tools = toolset.fetch_tools(actions=["workday_*_employee*"]) |
| 68 | +if len(tools) == 0: |
| 69 | + print("No Workday tools found for this account.") |
| 70 | +else: |
| 71 | + run_agent( |
| 72 | + messages=[{"role": "user", "content": "List the first 5 employees."}], |
| 73 | + tools=tools.to_openai(), |
| 74 | + ) |
0 commit comments