Google Generative AI
Google Generative AI
AgentOps로 Google Gemini API 호출을 모니터링하고 분석하는 방법을 소개해요. 설치, API 키 설정, 그리고 스트리밍·채팅 예제까지 알아볼게요.
출처: 문서
본문
AgentOps는 Google의 Generative AI API와 원활하게 통합되어, 모든 Gemini 모델 상호작용을 자동으로 모니터링하고 분석할 수 있게 해줍니다.
설치 (Installation)
poetry add agentops google-genai
uv pip install agentops google-genai
API 키 설정 (Setting Up API Keys)
Google Gemini를 AgentOps와 함께 사용하기 전에 API 키를 설정해야 해요. 다음을 얻을 수 있습니다.
- GOOGLE_API_KEY: Google AI Studio에서
- AGENTOPS_API_KEY: AgentOps Dashboard에서
그런 다음 환경 변수로 내보내거나 .env 파일에 설정할 수 있어요.
GOOGLE_API_KEY="your_google_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["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY")
사용법 (Usage)
애플리케이션 시작 시 AgentOps를 초기화하면 모든 Gemini API 호출이 자동으로 추적됩니다.
Initialize AgentOps
agentops.init()
Create a client
client = genai.Client(api_key="YOUR_GEMINI_API_KEY")
Generate streaming content
for chunk in client.models.generate_content_stream( model='gemini-2.0-flash-001', contents='Explain quantum computing in simple terms.', ): print(chunk.text, end="", flush=True)
```python Simple Chat theme={null}
import agentops
from google import genai
# Initialize AgentOps
agentops.init()
# Create a client
client = genai.Client(api_key="YOUR_GEMINI_API_KEY")
# Start a chat session
chat = client.chats.create(model='gemini-2.0-flash-001')
# Send messages and get responses
response = chat.send_message('Hello, how can you help me with AI development?')
print(response.text)
# Continue the conversation
response = chat.send_message('What are the best practices for prompt engineering?')
print(response.text)
예제 (Examples)
Google Gen AI SDK 사용에 대한 자세한 내용은 공식 문서를 참조하세요.