메인 콘텐츠로 건너뛰기
LiteLLM은 100개 이상의 LLM provider를 위한 통합 Python API를 제공합니다. CometAPI는 기본적으로 지원되며, cometapi/ 접두사를 사용해 요청을 CometAPI의 모델 카탈로그로 라우팅할 수 있습니다.

사전 요구 사항

1

LiteLLM 설치

pip install litellm
2

API 키 설정

API 키를 환경 변수로 설정하는 것(권장) 또는 인라인으로 전달할 수 있습니다:
import os
from litellm import completion

# Recommended: environment variable
os.environ["COMETAPI_KEY"] = "<COMETAPI_KEY>"

# Alternative: pass inline
api_key = "<COMETAPI_KEY>"
스크립트에 민감한 자격 증명을 하드코딩하지 않으려면 환경 변수를 사용하세요.
3

completion 호출 만들기

모델을 지정하려면 cometapi/<model-name> 형식을 사용하세요. 키는 환경 변수로 전달하거나 명시적으로 전달할 수 있습니다:
messages = [{"content": "Hello, how are you?", "role": "user"}]

# Method 1: environment variable (recommended)
response = completion(model="cometapi/your-model-id", messages=messages)

# Method 2: explicit API key
response = completion(model="cometapi/your-model-id", messages=messages, api_key=api_key)

print(response.choices[0].message.content)
4

Async 및 스트리밍 호출

논블로킹 실시간 응답을 위해 stream=True와 함께 acompletion을 사용하세요:
from litellm import acompletion
import asyncio, traceback

async def stream_call():
    try:
        response = await acompletion(
      model="cometapi/your-model-id",
            messages=[{"content": "Hello, how are you?", "role": "user"}],
            stream=True,
        )
        async for chunk in response:
            print(chunk)
    except Exception:
        print(f"Error: {traceback.format_exc()}")

asyncio.run(stream_call())
  • 모델 형식: CometAPI 모델은 cometapi/<model-name> 접두사를 사용합니다. 예: cometapi/your-model-id. 사용 가능한 모델은 CometAPI 모델 페이지에서 확인하세요.
  • 파인튜닝(Fine-tuning) 응답: LiteLLM은 temperature, max_tokens, top_p를 지원합니다 — 예를 들어 completion(..., temperature=0.7)처럼 어떤 completion() 호출에도 추가할 수 있습니다.
  • 오류 처리: 잘못된 키 오류나 네트워크 문제를 잡으려면 호출을 try/except로 감싸세요.
  • 보안: API 키를 버전 관리에 절대 커밋하지 마세요. 환경 변수나 secrets manager를 사용하세요.
  • 속도 제한: CometAPI 콘솔에서 사용량을 모니터링하세요.
  • 추가 문서: LiteLLM 문서CometAPI 빠른 시작