30 lines
959 B
Python
30 lines
959 B
Python
import requests
|
|
import json
|
|
|
|
# API 설정
|
|
api_key = "xai-7qV3DzJF1SOGxlPMmhdUhsVihEyF3lwNUPiNU95ZLjadgrFYWm05QmQH6llbE1BvOPzfkLPcFcrNRtFB" # 발급받은 API 키로 교체
|
|
url = "https://api.x.ai/v1/chat/completions"
|
|
headers = {
|
|
"Authorization": f"Bearer {api_key}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
# 요청 데이터
|
|
data = {
|
|
"model": "grok-4-fast-reasoning", # 또는 grok-4-fast-non-reasoning
|
|
"messages": [
|
|
{"role": "system", "content": "You are a helpful assistant."},
|
|
{"role": "user", "content": "인공지능의 미래에 대해 간단히 설명해."}
|
|
],
|
|
"max_tokens": 100, # 출력 토큰 제한
|
|
"temperature": 0.7 # 창의성 조절
|
|
}
|
|
|
|
# API 호출
|
|
response = requests.post(url, headers=headers, data=json.dumps(data))
|
|
|
|
# 응답 확인
|
|
if response.status_code == 200:
|
|
print(response.json()['choices'][0]['message']['content'])
|
|
else:
|
|
print(f"Error: {response.status_code}, {response.text}") |