PythonでXのLive Search APIを実行して検索してみる

初めに

先日 Live Search APIが使えるようになったので、こちらを動かしてみます

開発環境

セットアップ

簡単にPythonを動かす環境を作ります

必要なライブラリをインストールします。

pip install requests  

APIを実行する

以下のコードでLive Search APIを実行できます

import requests

url = "https://api.x.ai/v1/chat/completions"
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {API_KEY}"
}
payload = {
    "messages": [
        {
            "role": "user",
            "content": "Provide me a digest of world news in the last 24 hours."
        }
    ],
    "search_parameters": {
        "mode": "auto"
    },
    "model": "grok-3-latest"
}

response = requests.post(url, headers=headers, json=payload)
print(response.json())

結果は以下のようになります

{'id': 'e2eb631c-7455-4b06-913c-8e515f338b72', 'object': 'chat.completion', 'created': 1748069021, 'model': 'grok-3', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': "Here is a digest of world news based on information available from recent online sources and discussions on social media platforms like X, covering the last 24 hours as of May 24, 2025. Since real-time data for the exact last 24 hours may be limited or incomplete in the provided context, I’ve focused on the most recent and relevant updates while noting that some information may be based on posts or reports from slightly earlier in the week. For the most current and verified details, I recommend checking trusted news outlets directly.\n\n1. **United States - Political Developments**: \n   - Recent discussions on X highlight President Donald Trump's active role in international diplomacy, with mentions of efforts toward an India-Pakistan ceasefire, facilitating a meeting between Zelensky and Putin, and securing a trade deal with China. These claims, while widely discussed on social media, lack specific confirmation from official sources within the last 24 hours (based on posts dated May 11 and earlier on X). However, they reflect ongoing public interest in Trump's foreign policy moves.\n\n2. **Middle East - Gaza and Israel**:\n   - Tensions in Gaza remain high, with recent reports from earlier in the week (May 19-20) indicating Israeli military actions and international responses. Posts on X from May 19 mention renewed ceasefire talks between Hamas and Israel in Qatar, as well as ongoing concerns about aid delivery in Gaza. While no specific updates from the last 24 hours are detailed in the provided data, the situation continues to be a focal point of global concern, with earlier BBC reports (May 20) noting casualties from Israeli strikes and international calls for de-escalation from countries like the UK, France, and Canada (CNN, May 19).\n\n3. **Ukraine-Russia Conflict**:\n   - The war in Ukraine continues to dominate headlines, though specific updates from the last 24 hours are not detailed in the latest data. Earlier reports from May 20 (BBC and CNN) discuss a phone call between Trump and Putin, with differing perspectives on peace talks. Ukrainian President Zelensky has accused Russia of stalling negotiations, while Trump has suggested immediate talks. Sentiment on X reflects ongoing global attention to this conflict, though no new developments are confirmed for the past day.\n\n4. **Other Global Highlights**:\n   - Posts on X from the past week mention various international events, though not specifically within the last 24 hours. These include military drills by China near Australia, political instability in South Korea and Romania, and unrest in Syria. While these topics indicate a broad range of global issues, they are not timestamped to the immediate past day and should be treated as background context rather than current news.\n   - General news sources like Voice of America (VOA) continue to provide coverage of global events, with updates as recent as 8 hours ago (as of May 24), though specific stories are not detailed in the provided information.\n\n**Note**: Due to the limitations of the data provided, some of the digest includes context from slightly older reports (up to May 22-23 on X and May 20-21 in news articles). Real-time news within the exact last 24 hours (May 23-24, 2025) may not be fully captured here. Sentiment and topics trending on X suggest continued focus on geopolitical conflicts and U.S. political actions, but these should be verified with primary news outlets like BBC, CNN, or VOA for the latest developments.\n\nIf you’re looking for more specific or breaking news from the last few hours, I suggest visiting a trusted news website or app for the most up-to-date information. Is there a particular region or topic you’d like me to dive deeper into?", 'refusal': None}, 'finish_reason': 'stop'}], 'usage': {'promon': 'stop'}], 'usage': {'prompt_tokens': 2364, 'completion_tokens': 749, 'total_tokens': 3113, 'prompt_tokens_details': {'text_tokens': 2364, 'audiatus/1atus/1848065510476587353', 'https://x.com/GoodShepherd316/status/1865243223938998382', 'https://x.com/scottmelker/status/1921628456590221692']}