Skip to main content

AlgoceanGrokOAuth

LangChain / LangGraph에서 ChatOpenAI 자리에 그대로 꽂는 Grok 래퍼입니다.

  • auth=oauth (기본) — SuperGrok / X Premium+ 브라우저 로그인 (Hermes와 같은 xAI device-code)
  • auth=api_key — xAI API key 과금 (langchain_openai.ChatOpenAI → https://api.x.ai/v1)

GitHub: algocean1204/AlgoceanGrokOAuth

OAuth 프로토콜(엔드포인트, client_id, device-code, refresh)은 Hermes Agent의 공식 xAI Grok OAuth 코드를 거의 그대로 가져왔습니다.


1분 시작

pip install -U algocean-grok-oauth langgraph langchain-core
python -m algocean_grok_oauth login   # oauth 사용 시 1회 (브라우저)
from algocean_grok_oauth import AlgoceanGrokOAuth
from langchain_core.messages import HumanMessage

llm = AlgoceanGrokOAuth.chat(model="grok-4.6")
print(llm.invoke([HumanMessage(content="Hello")]).content)

AlgoceanGrokOAuth.print_models()

LangGraph 노드에도 동일한 llm 객체를 넣으면 됩니다.

하네스는 적용되지 않습니다. Hermes / Codex / AGENTS.md / 레포 룰 / 사용자 하네스를 읽지 않습니다.
oauth는 구독 토큰을 https://api.x.ai/v1에 넣는 것 외에는 API key와 같습니다. 로컬 개인 자동화·개인 서비스에 그대로 쓰면 됩니다.


어떤 auth를 쓸까?

auth=oauth (기본) auth=api_key
언제 로컬 개발 PC 배포 서버, CI
인증 python -m algocean_grok_oauth login ALGOCEANGROKOAUTH_API 또는 XAI_API_KEY
과금 SuperGrok / X Premium+ 구독 xAI API
설치 pip install algocean-grok-oauth (동일) 동일
tool calling (bind_tools) ✅ ✅
structured output ✅ ✅
토큰 스트리밍 ✅ ✅

그래프 코드는 그대로 — llm을 만드는 줄만 auth와 model을 바꾸면 됩니다.


설치

pip install -U algocean-grok-oauth

oauth — 로컬 개발 (추가 1회)

python -m algocean_grok_oauth login
python -m algocean_grok_oauth status

브라우저에서 accounts.x.ai 코드를 승인하면 됩니다.
토큰은 ~/.algocean_grok_oauth/auth.json에 저장되고, 만료 전에 자동 refresh 됩니다.

원격/SSH에서는 --no-browser로 URL만 출력합니다.

python -m algocean_grok_oauth login --no-browser

구독: SuperGrok 또는 X Premium+

api_key — 배포 / 서버

export ALGOCEANGROKOAUTH_API=<your-xai-api-key>
# 또는 export XAI_API_KEY=<your-xai-api-key>

기본 사용

from algocean_grok_oauth import AlgoceanGrokOAuth, oauth, api_key
from langchain_core.messages import HumanMessage

llm = AlgoceanGrokOAuth(model="grok-4.6")
llm = AlgoceanGrokOAuth(auth=api_key, model="grok-4.6")

response = llm.invoke([HumanMessage(content="FastAPI Depends를 짧게 설명해줘.")])
await llm.ainvoke([HumanMessage(content="...")])

환경 변수 / .env로 모드 선택

llm = AlgoceanGrokOAuth.from_env()
상황 결과
ALGOCEANGROKOAUTH_AUTH 지정 그 값 (oauth / api_key)
ALGOCEANGROKOAUTH_API 또는 XAI_API_KEY만 있음 api_key
둘 다 없음 oauth (구독)

LangGraph

from langchain_core.messages import HumanMessage, SystemMessage
from langgraph.graph import StateGraph, END
from typing_extensions import TypedDict
from algocean_grok_oauth import AlgoceanGrokOAuth

class State(TypedDict):
    user_input: str
    answer: str

llm = AlgoceanGrokOAuth(model="grok-4.6")

async def assistant_node(state: State) -> State:
    messages = [
        SystemMessage(content="간결한 개인 비서."),
        HumanMessage(content=state["user_input"]),
    ]
    ai = await llm.ainvoke(messages)
    return {"answer": ai.content}

graph = StateGraph(State)
graph.add_node("assistant", assistant_node)
graph.set_entry_point("assistant")
graph.add_edge("assistant", END)
app = graph.compile()

ReAct Agent / Structured Output

from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
from algocean_grok_oauth import AlgoceanGrokOAuth

@tool
def add(a: int, b: int) -> int:
    """Add two numbers."""
    return a + b

llm = AlgoceanGrokOAuth.chat(model="grok-4.6")
agent = create_react_agent(llm, tools=[add])

xAI는 OpenAI-compatible API라 bind_tools / with_structured_output / 스트리밍이 ChatOpenAI와 동일합니다.


생성자 옵션

AlgoceanGrokOAuth(
    model="grok-4.6",
    reasoning_effort=None,
    temperature=None,
    max_tokens=None,
    top_p=None,
    stop=None,
    auth=oauth,          # oauth | api_key
    timeout=180,
    base_url=None,       # 기본 https://api.x.ai/v1
)

환경 변수

변수 설명
ALGOCEANGROKOAUTH_API api_key 모드 xAI API key
XAI_API_KEY 위와 동일 (fallback)
ALGOCEANGROKOAUTH_AUTH oauth 또는 api_key
XAI_BASE_URL inference URL 오버라이드 (*.x.ai만 허용)
ALGOCEANGROKOAUTH_HOME 토큰 저장 디렉터리 (기본 ~/.algocean_grok_oauth)

제한 사항

  • oauth — 개인 로컬 / 개인 구독 용도. SaaS 서버 배포에는 부적합.
  • OAuth 로그인 후 inference가 HTTP 403이면 xAI가 티어를 막는 경우입니다. auth=api_key로 전환하세요.
  • Codex 라이브러리의 repo_read / repo_write 같은 CLI 샌드박스는 없습니다. Grok은 일반 LLM API입니다.

License

MIT

OAuth 프로토콜 일부는 Hermes Agent (Nous Research, MIT)에서 가져왔습니다. NOTICE 참고.

Metadata

Release files for algocean-grok-oauth 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for algocean-grok-oauth 0.1.0
File Size Uploaded
algocean_grok_oauth-0.1.0.tar.gz 16.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for algocean-grok-oauth 0.1.0
File Interpreter ABI Platform
algocean_grok_oauth-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 37.0 kB

Release files / algocean_grok_oauth-0.1.0.tar.gz

Download URL algocean_grok_oauth-0.1.0.tar.gz
Size 16.4 kB
Tags Source
SHA-256 checksum
How to use checksums
840eb4a8083cbcabeb277f8300ca791c92974943ccb795ba5444d8943bee08f0
BLAKE2b-256 checksum
How to use checksums
01c13c579d32f3198d6c8def93e5ca9535828bacd3e78db35f4fa2a811ea803d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.5 {"installer":{"name":"uv","version":"0.11.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / algocean_grok_oauth-0.1.0-py3-none-any.whl

Download URL algocean_grok_oauth-0.1.0-py3-none-any.whl
Size 20.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
afd9d73040fdd5932377958d5590e91fee29f3bc74f9ffe18e84567eab30bb71
BLAKE2b-256 checksum
How to use checksums
c390360cc8ce7d536ee3d95741a7f58288df159b4a72bb7dc73b7034fc630bc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.5 {"installer":{"name":"uv","version":"0.11.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page