Chat Completions 퀵스타트 — 여러 공급자로 채팅

Chat Completions 퀵스타트 — 여러 공급자로 채팅

AISuite는 하나의 API로 여러 공급자를 호출해요. 공급자를 바꾸는 건 model 문자열의 공급자 앞부분만 바꾸면 끝이에요. 채팅 컴플리션을 예로 볼게요.

출처: https://github.com/andrewyng/aisuite/blob/main/docs/chat-completions-quickstart.md

pip install aisuite
pip install 'aisuite[openai]' 'aisuite[anthropic]'
import aisuite as ai
client = ai.Client()

messages = [
    {"role": "system", "content": "Reply in a very concise way."},
    {"role": "user", "content": "Write a haiku about AI."},
]

for provider_model in ["openai:gpt-4o", "anthropic:claude-3-5-sonnet-20241022"]:
    response = client.chat.completions.create(
        model=provider_model,
        messages=messages,
    )
    print(provider_model, ":", response.choices[0].message.content)

API 키는 환경변수(예: OPENAI_API_KEY, ANTHROPIC_API_KEY)로 넣어요. 출력 객체는 공급자별로 다를 수 있지만, response.choices[0].message.content 형태로 공통 접근하도록 맞춰져 있어요.

더 알아보기