AI21

AI21

AgentOps로 AI21의 최신 Jamba 모델 계열을 관찰(observe)할 수 있어요. Python SDK를 통한 AI21 통합 방법을 함께 알아볼게요.

출처: 문서

본문

AgentOps는 Python SDK를 통해 AI21을 일급(first party)으로 지원해요. AI21로 개발을 탐구하고 싶다면 docs를 방문해보세요.

AI21을 AgentOps에 통합하는 단계 (Steps to integrate AI21 with AgentOps)

1. AgentOps SDK 설치하기 (Install the AgentOps SDK)

pip install agentops
poetry add agentops

2. AI21 SDK 설치하기 (Install the AI21 SDK)

참고: 현재 AI21 SDK의 v2.x.x를 지원하며, 향후 v3.x.x를 지원할 계획이에요.

pip install "ai21<3.0.0"
poetry add "ai21<3.0.0"

3. 코드에 AgentOps 추가하기 (Add AgentOps to your code)

참고: openai, cohere, crew 등 어떤 모델을 호출하기 전에 반드시 agentops.init을 먼저 호출해야 해요.

from ai21 import AI21Client
from ai21.models.chat import ChatMessage
import agentops

# Initialize clients
agentops.init(<INSERT YOUR API KEY HERE>)
client = AI21Client(api_key="your-api-key")

# Your AI21 code here...

agentops.end_session("Success")

팁: API key를 .env 변수로 설정하면 쉽게 사용할 수 있어요.

AGENTOPS_API_KEY=<YOUR API KEY>

환경 변수에 대한 자세한 내용은 Advanced Configuration에서 확인할 수 있어요.

4. 에이전트 실행하기 (Run your Agent)

프로그램을 실행하고 app.agentops.ai/drilldown을 방문해서 여러분의 에이전트를 관찰해보세요! 🕵️

팁: 실행 후 AgentOps는 대시보드의 세션으로 바로 연결되는 클릭 가능한 URL을 콘솔에 출력해요.

전체 예제 (Full Examples)

from ai21 import AI21Client
from ai21.models.chat import ChatMessage
import agentops

agentops.init(<INSERT YOUR API KEY HERE>)
client = AI21Client(api_key="your-api-key")

messages = [
    ChatMessage(
        content="You are a world renowned poet in the style of Edgar Allan Poe.",
        role="system",
    ),
    ChatMessage(
        content="Write me a short poem about the AI agents co-existing within the human brain.",
        role="user",
    ),
]

response = client.chat.completions.create(
    messages=messages,
    model="jamba-1.5-mini",
)

print(response.choices[0].message.content)
agentops.end_session('Success')
from ai21 import AsyncAI21Client
import agentops
import asyncio

async def main():
    agentops.init(<INSERT YOUR API KEY HERE>)
    client = AsyncAI21Client(api_key="your-api-key")

    messages = [
        ChatMessage(
            content="You are a world renowned poet in the style of Edgar Allan Poe.",
            role="system",
        ),
        ChatMessage(
            content="Write me a short poem about the AI agents co-existing within the human brain.",
            role="user",
        ),
    ]

    response = await client.chat.completions.create(
        messages=messages,
        model="jamba-1.5-mini",
    )

    print(response.choices[0].message.content)
    agentops.end_session('Success')

asyncio.run(main())

스트리밍 예제 (Streaming Examples)

from ai21 import AI21Client
from ai21.models.chat import ChatMessage
import agentops

agentops.init(<INSERT YOUR API KEY HERE>)
client = AI21Client(api_key="your-api-key")

messages = [
    ChatMessage(
        content="You are a world renowned poet in the style of Edgar Allan Poe.",
        role="system",
    ),
    ChatMessage(
        content="Write me a short poem about the AI agents co-existing within the human brain.",
        role="user",
    ),
]

complete_response = ""

response = client.chat.completions.create(
    messages=messages,
    model="jamba-1.5-mini",
    stream=True,
)

for chunk in response:
  complete_response += str(chunk.choices[0].delta.content)

print(complete_response)
agentops.end_session('Success')
from ai21 import AsyncAI21Client
import agentops
import asyncio

async def main():
    agentops.init(<INSERT YOUR API KEY HERE>)
    client = AsyncAI21Client(api_key="your-api-key")

    messages = [
        ChatMessage(
            content="You are a world renowned poet in the style of Edgar Allan Poe.",
            role="system",
        ),
        ChatMessage(
            content="Write me a short poem about the AI agents co-existing within the human brain.",
            role="user",
        ),
    ]

    complete_response = ""

    response = await client.chat.completions.create(
        messages=messages,
        model="jamba-1.5-mini",
        stream=True,
    )

    async for chunk in response:
      complete_response += str(chunk.choices[0].delta.content)

    print(complete_response)
    agentops.end_session('Success')

asyncio.run(main())

작업별 모델 (Task-Specific Models)

AI21 v2.x.x는 특정 작업을 위한 Task Specific Models라고 하는 특화된 모델을 제공해요.

다음은 contextual answers 엔드포인트를 사용하는 예시예요:

from ai21 import AI21Client
import agentops

agentops.init(<INSERT YOUR API KEY HERE>)
client = AI21Client(api_key="your-api-key")

CONTEXT = """
In 2020 and 2021, enormous QE — approximately $4.4 trillion, or 18%, of 2021 gross
domestic product (GDP) — and enormous fiscal stimulus (which has been and
always will be inflationary) — approximately $5 trillion, or 21%, of 2021 GDP
— stabilized markets and allowed companies to raise enormous amounts of
capital. In addition, this infusion of capital saved many small businesses and
put more than $2.5 trillion in the hands of consumers and almost $1 trillion into
state and local coffers. These actions led to a rapid decline in unemployment, 
dropping from 15% to under 4% in 20 months — the magnitude and speed of which were both
unprecedented. Additionally, the economy grew 7% in 2021 despite the arrival of
the Delta and Omicron variants and the global supply chain shortages, which were
largely fueled by the dramatic upswing in consumer spending and the shift in
that spend from services to goods.
"""

response = client.answer.create(
    context=CONTEXT,
    question="Did the economy shrink after the Omicron variant arrived?",
)

print(response.answer)
agentops.end_session('Success')

참고: 응답을 스트리밍하려면 stream=True 플래그를 사용할 수 있어요. 스트리밍 응답은 AI21 SDK가 처리해요.

이 모든 예제는 이 notebook에서 찾을 수 있어요.

더 알아보기 (Learn more)