Together AI 통합

Together AI 통합 (채팅/코드 모델)

한 줄 정도의 코드만으로 수십 개의 오픈소스 모델을 쓸 수 있다면 얼마나 편할까요? Together AI가 바로 그런 API를 제공해요. LangChain에서 langchain-together 패키지를 깔고 모델만 지정하면 채팅 모델과 코드 생성 모델을 모두 다룰 수 있어요.

출처: 공식문서

Together AI50개 이상의 리딩 오픈소스 모델을 몇 줄의 코드로 조회할 수 있는 API를 제공해요. 여기서는 LangChain으로 이런 모델들과 어떻게 상호작용하는지 살펴볼게요.

설치

pip install -U langchain-together

환경 변수

API 키는 api.together.ai/settings/api-keys에서 확인할 수 있어요. 이 키는 초기화 파라미터 together_api_key로 넘기거나, 환경 변수 TOGETHER_API_KEY로 설정할 수 있어요.

예제

채팅 모델을 쿼리하고 싶으면 이렇게 하면 돼요. 스트리밍을 쓸 수도 있고, 원하지 않으면 invoke 메서드를 쓰면 돼요.

# Querying chat models with Together AI

from langchain_together import ChatTogether

# choose from our 50+ models here: https://docs.together.ai/docs/inference-models
chat = ChatTogether(
    # together_api_key="YOUR_API_KEY",
    model="meta-llama/Llama-3-70b-chat-hf",
)

# stream the response back from the model
stream = chat.stream_events("Tell me fun things to do in NYC", version="v3")
for token in stream.text:
    print(token, end="", flush=True)

# if you don't want to do streaming, you can use the invoke method
# chat.invoke("Tell me fun things to do in NYC")

코드 생성 모델은 Together 클래스로 사용해요.

# Querying code and language models with Together AI

from langchain_together import Together

llm = Together(
    model="codellama/CodeLlama-70b-Python-hf",
    # together_api_key="..."
)

print(llm.invoke("def bubble_sort(): "))

채팅 모델(ChatTogether)과 코드/언어 모델(Together)을 상황에 맞게 골라 쓰면 돼요. API 키만 환경 변수로 잘 잡아두면 둘 다 그대로 동작해요.

더 알아보기 (Learn more)