AI/ML API
AI/ML API
LiteLLM에서 AI/ML API 프로바이더를 사용하는 방법을 소개할게요. AI/ML API는 flux-pro/v1.1과 같은 최신 AI 모델(고품질 이미지 생성 포함)에 대한 접근을 제공해요.
| 속성 | 세부 내용 |
|---|---|
| 설명 | AI/ML API는 고품질 이미지 생성을 위한 flux-pro/v1.1을 포함한 최첨단 AI 모델 접근을 제공해요. |
| LiteLLM 프로바이더 라우트 | aiml/ |
| 프로바이더 문서 링크 | AI/ML API ↗ |
| 지원 작업 | [/chat/completions], /images/generations |
출처: 문서
본문
LiteLLM은 AI/ML API 이미지 생성 호출을 지원해요.
API Base, Key
# env variable
os.environ['AIML_API_KEY'] = "your-api-key"
os.environ['AIML_API_BASE'] = "https://api.aimlapi.com" # [optional]
AI/ML API를 시작하는 것은 간단해요. 설정 통합 단계는 다음과 같아요.
1. API 키 받기
먼저 API 키가 필요해요. 여기에서 받을 수 있어요.
2. 사용 가능한 모델 둘러보기
다른 모델이 필요하다면 전체 지원 모델 목록을 확인해 주세요.
3. 문서 읽기
자세한 설정 방법은 공식 AI/ML API 문서를 확인해 주세요.
4. 도움이 필요할 때
질문이 있다면 Discord에서 도움을 받을 수 있어요 🚀
사용법 (Usage)
aimlapi.com/models에서 LLama, Qwen, Flux 및 200개 이상의 오픈·폐쇄 소스 모델을 선택할 수 있어요. 예를 들어:
import litellm
response = litellm.completion(
model="aiml/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo", # The model name must include prefix "openai" + the model name from ai/ml api
api_key="", # your aiml api-key
api_base="https://api.aimlapi.com/v2",
messages=[
{
"role": "user",
"content": "Hey, how's it going?",
}
],
)
스트리밍 (Streaming)
import litellm
response = litellm.completion(
model="aiml/Qwen/Qwen2-72B-Instruct", # The model name must include prefix "openai" + the model name from ai/ml api
api_key="", # your aiml api-key
api_base="https://api.aimlapi.com/v2",
messages=[
{
"role": "user",
"content": "Hey, how's it going?",
}
],
stream=True,
)
for chunk in response:
print(chunk)
비동기 완료 (Async Completion)
import asyncio
import litellm
async def main():
response = await litellm.acompletion(
model="aiml/anthropic/claude-sonnet-5", # The model name must include prefix "openai" + the model name from ai/ml api
api_key="", # your aiml api-key
api_base="https://api.aimlapi.com/v2",
messages=[
{
"role": "user",
"content": "Hey, how's it going?",
}
],
)
print(response)
if __name__ == "__main__":
asyncio.run(main())
비동기 스트리밍 (Async Streaming)
import asyncio
import traceback
import litellm
async def main():
try:
print("test acompletion + streaming")
response = await litellm.acompletion(
model="aiml/nvidia/Llama-3.1-Nemotron-70B-Instruct-HF", # The model name must include prefix "openai" + the model name from ai/ml api
api_key="", # your aiml api-key
api_base="https://api.aimlapi.com/v2",
messages=[{"content": "Hey, how's it going?", "role": "user"}],
stream=True,
)
print(f"response: {response}")
async for chunk in response:
print(chunk)
except:
print(f"error occurred: {traceback.format_exc()}")
pass
if __name__ == "__main__":
asyncio.run(main())
비동기 임베딩 (Async Embedding)
import asyncio
import litellm
async def main():
response = await litellm.aembedding(
model="aiml/text-embedding-3-small", # The model name must include prefix "openai" + the model name from ai/ml api
api_key="", # your aiml api-key
api_base="https://api.aimlapi.com/v1", # 👈 the URL has changed from v2 to v1
input="Your text string",
)
print(response)
if __name__ == "__main__":
asyncio.run(main())
비동기 이미지 생성 (Async Image Generation)
import asyncio
import litellm
async def main():
response = await litellm.aimage_generation(
model="aiml/dall-e-3", # The model name must include prefix "openai" + the model name from ai/ml api
api_key="", # your aiml api-key
api_base="https://api.aimlapi.com/v1", # 👈 the URL has changed from v2 to v1
prompt="A cute baby sea otter",
)
print(response)
if __name__ == "__main__":
asyncio.run(main())