Repository Public
1#!/usr/bin/env python32# =============================================================================3# SP BOUCHER | SOFTWARE & RESEARCH4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# PROJECT METADATA8# -----------------------------------------------------------------------------9# Project : API Atlas10# File : examples/xai/tools/x-search/x_search.py11# Module : examples12# Repository : https://git.spboucher.ai/doc-api13# Created : 2026-09-1914# Last Modified : 2026-09-1915#16# =============================================================================17# AUTHOR & PROFESSIONAL AFFILIATION18# =============================================================================19#20# Author : Simon-Pierre Boucher21# Position : Professor of Finance22# Institution : Université du Québec en Outaouais (UQO)23# Department : Département des sciences administratives24# Organization : SP Boucher25#26# Website : https://spboucher.ai27# Git : https://git.spboucher.ai28#29# =============================================================================30# OFFICIAL CONTACT DIRECTORY31# =============================================================================32#33# General : info@spboucher.ai34# Professional : contact@spboucher.ai35# Direct : simon@spboucher.ai36# Research : research@spboucher.ai37# Development : dev@spboucher.ai38# Support : support@spboucher.ai39# Security/Admin : admin@spboucher.ai40# Notifications : notifications@spboucher.ai41#42# Preferred Contact: contact@spboucher.ai43#44# =============================================================================45# COPYRIGHT & INTELLECTUAL PROPERTY46# =============================================================================47#48# Copyright (c) 202649# Simon-Pierre Boucher50#51# All rights reserved where applicable.52#53# This file is governed by the applicable project license,54# copyright notices, institutional agreements and relevant law.55#56# Unless expressly authorized by the applicable license or57# rights holder, copying, redistribution, sublicensing,58# publication and commercial exploitation are not permitted.59#60# Existing third-party rights and notices remain unaffected.61#62# =============================================================================63# CONFIDENTIALITY & SECURITY64# =============================================================================65#66# Classification : Public67# Distribution : Public repository; use is governed by the project license68# Security Contact : admin@spboucher.ai69#70# Access to this source does not independently grant permission71# to redistribute or disclose confidential materials.72#73# Do not embed credentials, private keys, access tokens,74# certificates or confidential personal information.75#76# =============================================================================77# LICENSE & MAINTENANCE78# =============================================================================79#80# License : None specified (no license file in this repository)81# Maintainer : Simon-Pierre Boucher82# Technical Contact: dev@spboucher.ai83#84# =============================================================================85# END OF SP BOUCHER SOURCE HEADER86# =============================================================================8788# STATUS: LIVE_VERIFIED — executed 2026-09-19 (exit 0 in 13.9s)89# x_search tool restricted to one handle. Live: server items arrive as custom_tool_call (x_keyword_search / x_semantic_search), not x_search_call.90import os, pathlib, sys91ROOT = pathlib.Path(__file__).resolve().parents[4]; sys.path.insert(0, str(ROOT))92from scripts import live # noqa: E40293from openai import OpenAI # noqa: E4029495client = OpenAI(api_key=os.environ["XAI_API_KEY"], base_url="https://api.x.ai/v1", timeout=300)96r = client.responses.create(model="grok-4.3", store=False, max_output_tokens=4000, extra_body={"max_turns": 2}, input="What is the latest post by @xai about? Max 8 words.", tools=[{"type": "x_search", "allowed_x_handles": ["xai"]}])97live.log_request("xai", "POST", "/v1/responses", 200, 0.02, "example xai/tools/x-search/x_search.py")98print("items:", [(o.type, getattr(o, "name", None)) for o in r.output])99print("text:", r.output_text)100msg = next(o for o in r.output if o.type == "message"); print("citations:", [a.url for a in msg.content[0].annotations][:3])101print("x_search_calls:", (r.usage.model_dump().get("server_side_tool_usage_details") or {}).get("x_search_calls"))102