Documentation Index
Fetch the complete documentation index at: https://wb-21fd5541-john-wbdocs-2044-rename-serverless-products.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
이 문서는 대화형 노트북입니다. 로컬에서 실행하거나 아래 링크를 사용할 수 있습니다:
Weave에서 트레이싱을 시작하기 전에 다음 사전 요구 사항을 충족하세요.
- W&B Weave SDK를 설치하고 API 키로 로그인합니다.
- OpenAI SDK를 설치하고 API 키로 로그인합니다.
- W&B 프로젝트를 초기화합니다.
# 의존성 설치 및 임포트
!pip install wandb weave openai -q
import json
import os
from getpass import getpass
from openai import OpenAI
import weave
# 🔑 API 키 설정
# 이 셀을 실행하면 `getpass`를 통해 API 키를 입력하라는 메시지가 표시되며, 터미널에 입력 내용이 출력되지 않습니다.
#####
print("---")
print(
"Create a W&B API key at: https://wandb.ai/settings#apikeys"
)
os.environ["WANDB_API_KEY"] = getpass("Enter your W&B API key: ")
print("---")
print("You can generate your OpenAI API key here: https://platform.openai.com/api-keys")
os.environ["OPENAI_API_KEY"] = getpass("Enter your OpenAI API key: ")
print("---")
#####
# 🏠 W&B 프로젝트 이름 입력
weave_client = weave.init("MY_PROJECT_NAME") # 🐝 W&B 프로젝트 이름
다음 코드 예제는 @weave.op 데코레이터를 사용해 Weave에서 트레이스를 캡처하고 시각화하는 방법을 보여줍니다. 이 예제에서는 문장에서 구조화된 데이터(과일, 색상, 맛)를 추출하기 위해 OpenAI의 GPT-4o에 프롬프트를 보내는 extract_fruit 함수를 정의합니다. 함수에 @weave.op를 데코레이트하면 Weave가 입력, 출력, 중간 step을 포함한 함수 실행을 자동으로 추적합니다. 샘플 문장으로 이 함수를 호출하면 전체 트레이스가 저장되며 Weave UI에서 확인할 수 있습니다.
@weave.op() # 🐝 요청을 추적하는 데코레이터
def extract_fruit(sentence: str) -> dict:
client = OpenAI()
system_prompt = (
"Parse sentences into a JSON dict with keys: fruit, color and flavor."
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": sentence},
],
temperature=0.7,
response_format={"type": "json_object"},
)
extracted = response.choices[0].message.content
return json.loads(extracted)
sentence = "There are many fruits that were found on the recently discovered planet Goocrux. There are neoskizzles that grow there, which are purple and taste like candy."
extract_fruit(sentence)