Files
api-examples/crewai/hello.py
Jacob IrwinandClaude Fable 5.2 1d3a2908a2 Add guides for the OpenAI SDK, LangChain, LlamaIndex and CrewAI (#1)
* Add guides for the OpenAI SDK, LangChain, LlamaIndex and CrewAI

Each guide gives the settings that point the library at Oathbreaker, a
runnable example that makes one billable request, the request that
example sends, and what to watch for with that library.

- The examples send only the request fields the API lists, set an
  output budget, and switch off the libraries' automatic retries. Left
  at their defaults, a failed call was sent 3 times by the OpenAI SDK
  and LangChain, 9 times by CrewAI and 12 times by LlamaIndex.
- Each example has a test that runs it against a local stand-in server.
  The test fails if the request carries a field the API does not list,
  or if a failed request is sent more than once. A CI job runs the
  test for each library from its pinned requirements.
- The main README names both public models and lists the base URL,
  authentication and what the API does not offer.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CfDjYvdXHyHmrgmoTbM2FU

* Run the framework tests on Python 3.12

The runner's own Python is 3.14, which CrewAI 1.15.41 does not install
on. The framework job now sets Python 3.12, the version the examples
were checked with, and the CrewAI guide says which versions work.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CfDjYvdXHyHmrgmoTbM2FU

---------

Co-authored-by: Claude <[email protected]>
2026-11-13 12:40:39 -03:00

56 lines
2.0 KiB
Python

"""A one-agent CrewAI crew pointed at Oathbreaker. Run on your server; keep keys private."""
import json
import os
import sys
from crewai import LLM, Agent, Crew, Task
from openai import APIStatusError, OpenAIError
BASE_URL = "https://oathbreaker.ai/v1"
MODEL = "oathbreaker-glm-5.3"
def complete(api_key, base_url=BASE_URL):
if not api_key:
raise ValueError("Set OATHBREAKER_API_KEY in your environment.")
llm = LLM(
model=f"openai/{MODEL}", # "openai/" selects CrewAI's OpenAI-compatible client; base_url sends it to Oathbreaker
base_url=base_url,
api_key=api_key,
max_tokens=2048,
reasoning_effort="low",
timeout=650,
max_retries=0, # left at its default, the client repeats a failed request on its own
context_window_size=131072,
)
agent = Agent(
role="Connection checker",
goal="Confirm that the model answers.",
backstory="You check that an API connection works.",
llm=llm,
max_retry_limit=0, # left at its default, the agent runs a failed task again, twice
verbose=False,
)
task = Task(
description="Reply with exactly: Oathbreaker connected.",
expected_output="That sentence and nothing else.",
agent=agent,
)
try:
result = Crew(agents=[agent], tasks=[task], verbose=False).kickoff()
except APIStatusError as error:
raise RuntimeError(f"API returned HTTP {error.status_code}. Inspect your dashboard before retrying.") from None
if not result.raw:
raise RuntimeError("No text answer. Inspect usage and the output budget before retrying.")
return {"text": result.raw, "usage": result.token_usage.model_dump()}
if __name__ == "__main__":
try:
result = complete(os.environ.get("OATHBREAKER_API_KEY"))
print(result["text"])
print("Usage:", json.dumps(result["usage"]))
except (ValueError, RuntimeError, OpenAIError) as error:
print(str(error), file=sys.stderr)
sys.exit(1)