azosi · 2026.7.28 23:52 · 조회 1

Kimi Auto Reconnect

상위 문서: ← Kimi Debugging and operations 출처: Kimi API Platform 공식 문서 — Automatic Reconnection on Disconnect 한글화 (2026-07-22) 공급사: Moonshot AI (月之暗面)

동시성 제한, 복잡한 네트워크 환경 등 예측 불가한 상황 때문에 연결이 가끔 끊어질 수 있습니다. 일반적으로 일시적 중단은 오래가지 않습니다. 재연결 기능을 구현하면 적은 코드로 서비스 안정성을 크게 높일 수 있습니다.

이 페이지의 예제는 기본적으로 최신 모델 kimi-k3를 사용합니다. K3는 최상위 reasoning_effort 요청 필드로 reasoning effort를 설정합니다 ("low" / "high" / "max", 기본 "max"). kimi-k2.6이나 kimi-k2.5 등 다른 모델을 사용하려면 model 필드만 교체하면 됩니다 — 모델별 파라미터 구성은 다릅니다. Model Parameter Reference 참고.

1. 재연결 구현 예제

import os

from openai import OpenAI
import time

client = OpenAI(
    api_key=os.environ["MOONSHOT_API_KEY"],
    base_url = "https://api.moonshot.ai/v1",
)

def chat_once(msgs):
    response = client.chat.completions.create(
        model = "kimi-k3",
        messages = msgs
    )
    return response.choices[0].message.content

def chat(input: str, max_attempts: int = 100) -> str | None:
    messages = [
        {"role": "system", "content": "You are Kimi, an AI assistant provided by Moonshot AI. You are proficient in Chinese and English conversations. You aim to provide users with safe, helpful, and accurate answers. You will refuse to answer any questions related to terrorism, racism, or explicit content. Moonshot AI is a proper noun and should not be translated into other languages."},
    ]

    # 사용자의 최신 질문을 user 메시지로 만들어 messages 리스트 끝에 추가
    messages.append({
        "role": "user",
        "content": input,
    })
    st_time = time.time()
    for i in range(max_attempts):
        print(f"Attempts: {i+1}/{max_attempts}")
        try:
            response = chat_once(messages)
            ed_time = time.time()
            print("Query Successful!")
            print(f"Query Time: {ed_time-st_time}")
            return response
        except Exception as e:
            print(e)
            time.sleep(1)
            continue

    print("Query Failed.")
    return

print(chat("Hello, please tell me a fairy tale."))

위 코드는 간단한 재연결 기능을 구현합니다 — 최대 100회 재시도, 시도 간 1초 대기. 이 값들은 구체적 필요에 따라 조정할 수 있습니다.

2. 권장 사항

  • 스트리밍 사용 (stream=True): 출력 지연으로 인한 연결 끊김을 크게 줄일 수 있습니다. 자세한 내용: Troubleshooting
  • 타임아웃 조정: 코드/SDK의 기본 타임아웃이 너무 짧으면 정상 응답도 잘릴 수 있음
  • 재시도 백오프: 일시적 오류의 경우 exponential backoff가 효과적

3. 도구 사용 시 도구 호출 재시도

tool_calls 사용 시 모델이 도구를 호출하다가 네트워크 오류로 실패하면, 일반적으로:

  1. 다음 모델 호출에서 컨텍스트를 재구성
  2. 누적된 reasoning_content를 컨텍스트에 유지 (K3/K2.6 추론 모델 사용 시 필수)

자세한 내용은 K3 Agent Build 가이드 참고.


변경 이력

날짜변경
2026-07-22초판 작성 (공식 Auto Reconnect 가이드 한글화)
2026-07-22제목에 "Kimi" 접두사 추가, 본문 H1 중복 제거

댓글

아직 댓글이 없습니다.

댓글을 작성하려면 로그인이 필요합니다.