SiliconFlow
User Guide

FIM completion

FIM (Fill In the Middle) completion guide for code auto-completion and mid-text generation scenarios.

1. Use Cases

In FIM (Fill In the Middle) completion, the user provides the desired beginning and ending content, and the model fills in the middle content. This is typically used in code completion, text middle content completion, etc.

2. Usage

In the /chat/completions interface, pass the prefix and suffix parameters via extra_body:

{ 
    "model": "model info",
    "messages": "prompt message",
    "params": "params",
    "extra_body": {"prefix":"前缀内容", "suffix":"后缀内容"}
}

3. Supported Model List

  • Deepseek Series:

    • Pro/zai-org/GLM-5.1
    • Pro/zai-org/GLM-5
    • Pro/deepseek-ai/DeepSeek-R1
    • deepseek-ai/DeepSeek-R1
    • Pro/deepseek-ai/DeepSeek-V3
    • deepseek-ai/DeepSeek-V3
  • Qwen Series:

    • Qwen/Qwen3-VL-30B-A3B-Instruct
    • Qwen/Qwen3-VL-30B-A3B-Thinking
    • Qwen/Qwen3-Omni-30B-A3B-Instruct
    • Qwen/Qwen3-Omni-30B-A3B-Thinking
    • Qwen/Qwen3-Omni-30B-A3B-Captioner
    • Qwen/Qwen3-Coder-30B-A3B-Instruct
Note: The supported model list may change, please filter for the latest FIM-supported models in Models.

4. Usage Examples

Using FIM completion with the OpenAI SDK via the /chat/completions interface:

client = OpenAI(
    api_key="您的 APIKEY", # 从https://cloud.siliconflow.cn/account/ak获取
    base_url="https://api.siliconflow.cn/v1"
)
 
messages = [
    {"role": "user", "content": "Please write quick sort code"},
]

response = client.chat.completions.create(
    model="Pro/zai-org/GLM-5.1",
    messages=messages,
    extra_body={
            "prefix": f"""
def quick_sort(arr):
    # 基本情况,如果数组长度小于等于 1,则返回数组
    if len(arr) <= 1:
        return arr
    else:
""",
            "suffix": f"""
# 测试 quick_sort 函数
arr = [3, 6, 8, 10, 1, 2, 1]
sorted_arr = quick_sort(arr)
print("Sorted array:", sorted_arr)
"""
    },
    stream=True,
    max_tokens=4096
)

for chunk in response:
    print(chunk.choices[0].delta.content, end='')