Skip to main content

Drop-in ChatOpenAI replacement for LangChain/LangGraph — local Codex OAuth or deployed OpenAI API key.

Project description

AlgoceanCodexOAuth

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

auth=oauth면 로컬 Codex CLI + ChatGPT OAuth 구독 한도를, auth=api_key면 OpenAI API key 과금 경로를 사용합니다.

LangGraph / LangChain
  → AlgoceanCodexOAuth(auth=oauth | api_key)
  → oauth: codex exec → 로컬 ChatGPT OAuth
  → api_key: langchain_openai.ChatOpenAI 위임

설치

Local — Codex OAuth (개발 PC)

npm install -g @openai/codex

unset OPENAI_API_KEY
unset CODEX_API_KEY
codex logout
codex login
codex login status

공식 인증 문서: Codex Authentication

pip install algocean-codex-oauth

Production — OpenAI API key (배포 서버)

pip install "algocean-codex-oauth[openai]"

.env 또는 환경 변수:

export ALGOCEANCODEXOAUTH_API=sk-...
# 선택: ALGOCEANCODEXOAUTH_AUTH=api_key  (기본값 oauth)

LangGraph 프로젝트:

pip install "algocean-codex-oauth[openai]" langgraph langchain-core

Quick Start

Local (OAuth — 기본)

from algocean_codex_oauth import AlgoceanCodexOAuth, oauth
from langchain_core.messages import HumanMessage

llm = AlgoceanCodexOAuth(auth=oauth, model="gpt-5.5")
# auth 생략 시 oauth가 기본값
response = llm.invoke([HumanMessage(content="FastAPI Depends를 짧게 설명해줘.")])
print(response.content)

Production (API key)

from algocean_codex_oauth import AlgoceanCodexOAuth, api_key
from langchain_core.messages import HumanMessage

llm = AlgoceanCodexOAuth(auth=api_key, model="gpt-4o")
response = llm.invoke([HumanMessage(content="FastAPI Depends를 짧게 설명해줘.")])
print(response.content)

환경 변수로 모드 선택:

from algocean_codex_oauth import AlgoceanCodexOAuth

llm = AlgoceanCodexOAuth.from_env(model="gpt-4o")

from_env()ALGOCEANCODEXOAUTH_AUTH=api_key일 때 ALGOCEANCODEXOAUTH_API가 필요합니다.

터미널 가이드 — help()

from algocean_codex_oauth import AlgoceanCodexOAuth

AlgoceanCodexOAuth.help()                  # 개요
AlgoceanCodexOAuth.help("langgraph")       # LangGraph 사용법
AlgoceanCodexOAuth.help("auth")            # oauth vs api_key
AlgoceanCodexOAuth.help("all")             # 전체 가이드

토픽: install, quickstart, langgraph, auth, multiturn, presets, all

auth 모드 비교

auth=oauth (기본) auth=api_key
용도 로컬 개발, Codex 구독 배포 서버, OpenAI 과금
인증 codex login (ChatGPT OAuth) ALGOCEANCODEXOAUTH_API=sk-...
백엔드 codex exec langchain_openai.ChatOpenAI
repo_read / repo_write 지원 미지원 (oauth 전용)
thread_mode=codex_resume 지원 미지원
OAuth 모드 API key 차단 OPENAI_API_KEY 등 제거 해당 없음
workdir=None (LangGraph Q&A) LLM-only 고정: AGENTS.md 무시 + chat preamble + read-only 해당 없음

LangGraph

단일 노드 (OAuth)

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

from algocean_codex_oauth import AlgoceanCodexOAuth

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

llm = AlgoceanCodexOAuth(model="gpt-5.5")  # auth=oauth 기본

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()

배포용 — API key로 동일 그래프

from algocean_codex_oauth import AlgoceanCodexOAuth, api_key

llm = AlgoceanCodexOAuth(auth=api_key, model="gpt-4o")
# 그래프 코드는 import / auth / model만 바꾸면 동일

ReAct Agent

from langgraph.prebuilt import create_react_agent
from langchain_core.messages import HumanMessage
from algocean_codex_oauth import AlgoceanCodexOAuth

llm = AlgoceanCodexOAuth(model="gpt-5.5")
agent = create_react_agent(llm, tools=[])
result = agent.invoke({"messages": [HumanMessage(content="hello")]})

Structured Output

from pydantic import BaseModel, Field
from langchain_core.messages import HumanMessage
from algocean_codex_oauth import AlgoceanCodexOAuth

class Analysis(BaseModel):
    summary: str = Field(description="요약")
    risk_level: str = Field(description="low | medium | high")

llm = AlgoceanCodexOAuth(model="gpt-5.5")
structured = llm.with_structured_output(Analysis)
result = structured.invoke([HumanMessage(content="위험도를 평가해줘.")])
print(result.summary, result.risk_level)
  • oauth: Codex CLI --output-schema 사용
  • api_key: ChatOpenAI.with_structured_output 위임

ChatOpenAI 기능 대응

ChatOpenAI AlgoceanCodexOAuth
llm.invoke(messages) 동일
await llm.ainvoke(messages) 동일
llm.astream(messages) 동일
llm.with_structured_output(schema) 동일
LangGraph state["messages"] 멀티턴 thread_mode="messages" (기본, oauth/api_key 공통)
Codex thread resume thread_mode="codex_resume" (oauth 전용)
새 대화 시작 llm.reset_thread() (oauth)
response.response_metadata["usage"] oauth: Codex usage / api_key: OpenAI usage

생성자

from algocean_codex_oauth import AlgoceanCodexOAuth, oauth, api_key

AlgoceanCodexOAuth(
    model: str = "gpt-5.5",
    auth: str = oauth,              # oauth | api_key
    timeout: int = 180,
    sandbox: str = "read-only",     # oauth 전용
    workdir: str | None = None,     # oauth 전용
    ephemeral: bool = True,         # oauth 전용
    thread_mode: str = "messages",  # messages | codex_resume (oauth)
    codex_bin: str = "codex",
    require_chatgpt_login: bool = True,
)

Preset (oauth 전용)

llm = AlgoceanCodexOAuth.chat(model="gpt-5.5")
llm = AlgoceanCodexOAuth.repo_read(workdir="/path/to/repo")
llm = AlgoceanCodexOAuth.repo_write(workdir="/path/to/repo")
Preset auth thread_mode
chat() oauth (기본) messages
repo_read(path) oauth 필수 messages
repo_write(path) oauth 필수 codex_resume

환경 변수

변수 용도
ALGOCEANCODEXOAUTH_API auth=api_key 시 OpenAI API key (필수)
ALGOCEANCODEXOAUTH_AUTH oauth 또는 api_key (선택, 기본 oauth)

멀티턴 — ChatOpenAI와 동일하게 (messages 모드)

messages = [HumanMessage(content="코드네임은 ALPHA7")]
ai1 = await llm.ainvoke(messages)
messages += [ai1, HumanMessage(content="코드네임이 뭐야?")]
ai2 = await llm.ainvoke(messages)

LangGraph state["messages"] 패턴과 1:1 동일합니다. oauth / api_key 모두 동일.

codex_resume (oauth 전용)

llm = AlgoceanCodexOAuth(
    model="gpt-5.5",
    workdir="/path/to/repo",
    sandbox="read-only",
    ephemeral=False,
    thread_mode="codex_resume",
)

await llm.ainvoke([HumanMessage(content="첫 질문")])
await llm.ainvoke([HumanMessage(content="이어서")])
llm.reset_thread()

Streaming

async for chunk in llm.astream([HumanMessage(content="hello")]):
    print(chunk.content, end="", flush=True)

Local / Production 스위치

import os
from algocean_codex_oauth import AlgoceanCodexOAuth, oauth, api_key

def get_llm():
    if os.getenv("ALGOCEANCODEXOAUTH_API"):
        return AlgoceanCodexOAuth(auth=api_key, model="gpt-4o")
    return AlgoceanCodexOAuth(auth=oauth, model="gpt-5.5")

그래프 코드는 동일하고 auth / model만 바꾸면 됩니다.

OAuth 정책 (auth=oauth)

라이브러리는 oauth 호출마다 아래를 강제합니다.

  • OPENAI_API_KEY, CODEX_API_KEY 환경 변수 제거
  • require_chatgpt_login=Truecodex login status로 ChatGPT OAuth 확인
  • API key 인증 감지 시 AlgoceanCodexOAuthError 발생

아키텍처

algocean_codex_oauth/
├── chat_model.py    # AlgoceanCodexOAuth — oauth / api_key 라우팅
├── auth_mode.py     # oauth, api_key 상수 + env 로더
├── client.py        # codex exec / exec resume (oauth)
├── auth.py          # OAuth 검증, API key 차단 (oauth)
├── config.py
├── messages.py
├── session.py       # codex_resume 편의 (oauth)
└── errors.py

멀티턴 (Session 래퍼, oauth 전용)

AlgoceanCodexSessionthread_mode="codex_resume" 편의 래퍼입니다.

제한

  • oauth: 개인 로컬 / 개인 구독 용도. SaaS 서버에 부적합.
  • api_key: OpenAI API key 과금. repo sandbox / codex resume 미지원.
  • oauth 모드: Codex CLI(codex)가 PATH에 있어야 합니다.

개발

git clone https://github.com/algocean1204/AlgoceanCodexOAuth.git
cd AlgoceanCodexOAuth
pip install -e ".[dev]"
pytest

통합 테스트 (별도 폴더):

cd ../AlgoceanCodexOAuth_IntegrationTest
pip install -e ../AlgoceanCodexOAuth/.[dev] langgraph
python run_all_tests.py --mode mock

PyPI

pip install algocean-codex-oauth
pip install "algocean-codex-oauth[openai]"   # api_key 모드

License

MIT

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

algocean_codex_oauth-0.2.3.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

algocean_codex_oauth-0.2.3-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file algocean_codex_oauth-0.2.3.tar.gz.

File metadata

  • Download URL: algocean_codex_oauth-0.2.3.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for algocean_codex_oauth-0.2.3.tar.gz
Algorithm Hash digest
SHA256 40a34e55f9080318f23b6af40c884b0ec591f26d0ca5bbf52c0092938303bd4b
MD5 01e9fe1d854aec4c3fdd570ee77aab24
BLAKE2b-256 9a0f8462b24a1422a22316d472cf38b479399909392ea06d14367d6d6313d6c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for algocean_codex_oauth-0.2.3.tar.gz:

Publisher: publish.yml on algocean1204/AlgoceanCodexOAuth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file algocean_codex_oauth-0.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for algocean_codex_oauth-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d64e53f0d85ae8c770b970e733a380c2d5d234bb165011a03e3d2152d93143f9
MD5 db44c2a2a783543b66871db12e5a28a1
BLAKE2b-256 88ac8bf2cb9b40521622d6e5ceb14806719ca13766317e05e02beb2d6731fd69

See more details on using hashes here.

Provenance

The following attestation bundles were made for algocean_codex_oauth-0.2.3-py3-none-any.whl:

Publisher: publish.yml on algocean1204/AlgoceanCodexOAuth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page