> ## Documentation Index
> Fetch the complete documentation index at: https://docs.geekai.co/llms.txt
> Use this file to discover all available pages before exploring further.

# 流式响应

以下是 Message API 最简单的流式响应（边回答边输出）请求示例，只需要将 `stream` 设置为 `true` 即可：

<CodeGroup>
  ```bash curl theme={null}
  curl --location --request POST 'https://geekai.co/api/v1/messages' \
  --header 'Authorization: Bearer $GEEKAI_API_KEY' \
  --header 'Content-Type: application/json' \
  --data-raw '{
      "model": "claude-sonnet-4-20250514",
      "max_tokens": 1024,
      "messages": [
          {"role": "user", "content": "你好"}
      ]
      "stream": true
  }'
  ```

  \`

  ```bash python theme={null}
  # 先安装 Anthropic SDK: `pip install anthropic`
  import anthropic

  client = anthropic.Anthropic(
      auth_token="$GEEKAI_API_KEY",
      base_url="https://geekai.co/api/v1"
  )

  message = client.messages.create(
      model="claude-sonnet-4-20250514",
      max_tokens=1024,
      messages=[{"role": "user", "content": "你好"}],
      stream=True
  )
  print(message.content)
  ```

  ```bash javascript theme={null}
  # 先安装 Anthropic SDK: `npm install @anthropic-ai/sdk`
  import Anthropic from "@anthropic-ai/sdk";

  const anthropic = new Anthropic({
      baseURL: "https://geekai.co/api/v1",
      authToken: "$GEEKAI_API_KEY"
  });

  const msg = await anthropic.messages.create({
    model: "claude-sonnet-4-20250514",
    max_tokens: 1024,
    messages: [{ role: "user", content: "你好" }],
    stream: true
  });
  console.log(msg);
  ```

  ```bash go theme={null}
  package main

  import (
  	"bytes"
  	"encoding/json"
  	"fmt"
  	"io"
  	"net/http"
  )

  func main() {
  	requestBody := map[string]any{
  		"model": "gpt-4o-mini",
  		"input": "你好", 
          "stream": true,
  	}
  	
  	jsonData, err := json.Marshal(requestBody)
  	if err != nil {
  		panic(err)
  	}

  	client := &http.Client{}
  	req, err := http.NewRequest("POST", "https://geekai.co/api/v1/responses", bytes.NewBuffer(jsonData))
  	if err != nil {
  		panic(err)
  	}

  	req.Header.Set("Authorization", "Bearer $GEEKAI_API_KEY")
  	req.Header.Set("Content-Type", "application/json")

  	resp, err := client.Do(req)
  	if err != nil {
  		panic(err)
  	}
  	defer resp.Body.Close()

  	body, err := io.ReadAll(resp.Body)
  	if err != nil {
  		panic(err)
  	}

  	fmt.Println(string(body))
  }
  ```
</CodeGroup>

对于流式请求，需要在客户端接收响应时进行特殊处理才能获取到，以 JavaScript 为例，需要通过 `EventSource` 进行监听获取。
