Files

56 lines
1.9 KiB
Python

"""Minimal end-to-end example: load config, send a chat completion, stream one.
Prerequisites:
1. `raycast-api init` has been run (so `config.json` exists next to this file
or at the path you point `--config` to). See the README.
2. `RAYCAST_BEARER` is exported (your Raycast OAuth access token, `rca_...`).
3. `RAYCAST_DEVICE_ID` is exported (any stable 64-hex string; the CLI mints
one on first `ask` and persists it to `~/.config/raycast-api/device_id`,
so a one-liner is: `export RAYCAST_DEVICE_ID=$(cat ~/.config/raycast-api/device_id)`).
Run:
python examples/basic_usage.py
"""
from __future__ import annotations
import asyncio
import os
import sys
from pathlib import Path
from raycast_api import Client, Config, Message
CONFIG_PATH = Path(os.environ.get("RAYCAST_CONFIG", "config.json"))
MODEL = os.environ.get("RAYCAST_MODEL", "Claude Sonnet 4.6")
async def main() -> None:
bearer = os.environ.get("RAYCAST_BEARER")
device_id = os.environ.get("RAYCAST_DEVICE_ID")
if not bearer or not device_id:
sys.exit("set RAYCAST_BEARER and RAYCAST_DEVICE_ID before running")
if not CONFIG_PATH.is_file():
sys.exit(f"no config at {CONFIG_PATH} — run `raycast-api init` first")
config = Config.load(CONFIG_PATH)
async with Client(
config=config, bearer_token=bearer, device_id=device_id
) as client:
result = await client.chat.complete(
model=MODEL, messages=[Message.user("Reply with the single word: OK")]
)
print(f"[complete] {result.text!r}")
print("[stream] ", end="", flush=True)
async for chunk in client.chat.stream(
model=MODEL, messages=[Message.user("Write a one-line haiku about HMAC.")]
):
if chunk.text:
print(chunk.text, end="", flush=True)
print()
if __name__ == "__main__":
asyncio.run(main())