LiteLLM

LiteLLM

AgentOps로 여러 프로바이더에 걸친 LiteLLM 호출을 추적하고 분석하는 방법을 소개해요. 설치, API 키 설정, 그리고 통합된 인터페이스로 모든 LLM 호출을 자동 추적하는 방법을 알아볼게요.

출처: 문서

본문

AgentOps는 LiteLLM과 원활하게 통합되어, 통합된 인터페이스를 통해 서로 다른 프로바이더의 모든 LLM API 호출을 자동으로 추적할 수 있게 해줍니다.

설치 (Installation)

```bash pip theme={null} pip install agentops litellm ```
poetry add agentops litellm
uv pip install agentops litellm

API 키 설정 (Setting Up API Keys)

LiteLLM을 AgentOps와 함께 사용하기 전에 API 키를 설정해야 해요. 다음을 얻을 수 있습니다.

  • Provider API Keys: 선택한 LLM 프로바이더(OpenAI, Anthropic, Google 등)에서
  • AGENTOPS_API_KEY: AgentOps Dashboard에서

그런 다음 환경 변수로 내보내거나 .env 파일에 설정할 수 있어요.

```bash Export to CLI theme={null} export OPENAI_API_KEY="your_openai_api_key_here" export ANTHROPIC_API_KEY="your_anthropic_api_key_here" export AGENTOPS_API_KEY="your_agentops_api_key_here" ```
OPENAI_API_KEY="your_openai_api_key_here"
ANTHROPIC_API_KEY="your_anthropic_api_key_here"
AGENTOPS_API_KEY="your_agentops_api_key_here"

그리고 Python 코드에서 환경 변수를 로드합니다.

from dotenv import load_dotenv
import os

# Load environment variables from .env file
load_dotenv()

# Set up environment variables with fallback values
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
os.environ["ANTHROPIC_API_KEY"] = os.getenv("ANTHROPIC_API_KEY")
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY")

사용법 (Usage)

AgentOps를 LiteLLM과 통합하는 가장 간단한 방법은 success\_callback을 설정하는 것입니다.

import litellm
from litellm import completion

# Configure LiteLLM to use AgentOps
litellm.success_callback = ["agentops"]

# Make completion requests with LiteLLM
response = completion(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "Hello, how are you?"}]
)

print(response.choices[0].message.content)

예제 (Examples)

```python Streaming theme={null} import litellm from litellm import completion

Configure LiteLLM to use AgentOps

litellm.success_callback = ["agentops"]

Make a streaming completion request

response = completion( model="gpt-4", messages=[{"role": "user", "content": "Write a short poem about AI."}], stream=True )

Process the streaming response

for chunk in response: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="", flush=True) print() # Add a newline at the end


```python Multi-Provider theme={null}
import litellm
from litellm import completion

# Configure LiteLLM to use AgentOps
litellm.success_callback = ["agentops"]

# OpenAI request
openai_response = completion(
    model="gpt-4",
    messages=[{"role": "user", "content": "What are the advantages of GPT-4?"}]
)

print("OpenAI Response:", openai_response.choices[0].message.content)

# Anthropic request using the same interface
anthropic_response = completion(
    model="anthropic/claude-3-opus-20240229",
    messages=[{"role": "user", "content": "What are the advantages of Claude?"}]
)

print("Anthropic Response:", anthropic_response.choices[0].message.content)

# All requests across different providers are automatically tracked by AgentOps

더 많은 예제 (More Examples)

AgentOps를 LiteLLM과 통합하는 방법에 대한 자세한 내용은 AgentOps 통합에 관한 LiteLLM 문서를 참조하세요.

더 알아보기 (Learn more)