
LiteLLM은 100개 이상의 LLM 모델을 하나의 통일된 API로 사용할 수 있게 해주는 Python 라이브러리입니다. OpenAI 스타일의 completion API를 사용하여 다양한 모델을 쉽게 전환할 수 있습니다.
- 통일된 API: 모든 모델을 같은 방식으로 호출
- 쉬운 모델 전환: 코드 한 줄만 바꾸면 모델 변경 완료
- 비용 효율성: 상황에 맞는 모델 선택으로 비용 절약
- 스트리밍 지원: 실시간 응답으로 사용자 경험 향상
pip install litellm
기본 구조
import litellm
import os
# 모델 선택 (여기만 바꾸면 끝!)
model_name = "ollama/llama3.2:3b" # 로컬 모델
# model_name = "openai/gpt-4o" # OpenAI
# model_name = "anthropic/claude-3-sonnet-20240229" # Claude
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "파이썬으로 웹스크래핑하는 방법 알려줘"}
]
response = litellm.completion(
model=model_name,
messages=messages
)
print(response.choices[0].message.content)
* openai, anthropic 모델은 env 파일로 관리하거나 아래처럼 API 설정 후 사용가능합니다.
os.environ["OPENAI_API_KEY"]="sk-**"
os.environ["ANTHROPIC_API_KEY"] = "sk-an**"
스트리밍을 사용하면 ChatGPT처럼 글자가 하나씩 나타나게 됩니다.
response = litellm.completion(
model=model_name,
messages=messages,
stream=True #
)
print("AI 응답:")
for chunk in response:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end='', flush=True)
같은 질문을 여러 모델에게 물어보고 답변을 비교해볼 수도 있습니다.
models = [
"ollama/llama3.2:3b", # 무료, 로컬
"openai/gpt-4o", # 유료, 고성능
"anthropic/claude-3-haiku-20240307" # 유료, 빠름
]
question = "Python 리스트와 튜플의 차이점은?"
for model in models:
print(f"\n{model}의 답변:")
print("-" * 50)
try:
response = litellm.completion(
model=model,
messages=[{"role": "user", "content": question}],
max_tokens=200
)
print(response.choices[0].message.content)
except Exception as e:
print(f"오류: {e}")
response = litellm.completion(
model="openai/gpt-4o",
messages=messages,
max_tokens=500, # 최대 토큰 제한
temperature=0.7, # 창의성 조절
top_p=0.9 # 다양성 조절
)
# 사용된 토큰 확인
print(f"사용 토큰: {response.usage.total_tokens}")
개발 시간 단축: 모델별 API 학습 불필요
비용 최적화: 상황에 맞는 모델 선택
유연성 확보: 언제든 모델 변경 가능
실험 용이: 여러 모델 쉽게 비교
위 특징을 기반으로 LiteLLM을 사용하면 여러 모델로번갈아 사용하기 용이해집니다.
Anthropic | liteLLM
LiteLLM supports all anthropic models.
docs.litellm.ai
사용가능한 모델
https://docs.litellm.ai/docs/providers/anthropic
Anthropic | liteLLM
LiteLLM supports all anthropic models.
docs.litellm.ai
| LiteLLM Proxy 대시보드 설정하기 (0) | 2025.08.24 |
|---|---|
| vLLM으로 API 서버 실행하기 (5) | 2025.07.08 |
| LangChain 을 이용한 Streamlit 채팅에 Smithery MCP 추가하기 (1) | 2025.06.07 |
| LangChain MCP 와 Streamlit 으로 채팅창 만들기 (0) | 2025.06.03 |
| OpenWebUI 사용하기 (0) | 2025.06.03 |