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

2.1 Using in chat/completions Interface

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

2.2 Using in completions Interface

{
    "model": "model info",
    "prompt": "前缀内容",
    "suffix": "后缀内容"
}

3. Supported Model List

  • Deepseek Series:

    • deepseek-ai/DeepSeek-V2.5
    • Pro/deepseek-ai/DeepSeek-R1
    • deepseek-ai/DeepSeek-R1
    • Pro/deepseek-ai/DeepSeek-V3
    • deepseek-ai/DeepSeek-V3
    • deepseek-ai/DeepSeek-R1-Distill-Qwen-32B
    • deepseek-ai/DeepSeek-R1-Distill-Qwen-14B
    • deepseek-ai/DeepSeek-R1-Distill-Qwen-7B
    • deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
    • Pro/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B
    • Pro/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
  • Qwen Series:

    • Qwen/Qwen2.5-Coder-7B-Instruct
    • Qwen/Qwen2.5-Coder-32B-Instruct
Note: The supported model list may change, please refer to this document for the latest list of supported models.

4. Usage Examples

4.1 Using FIM Completion with OpenAI’s 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="deepseek-ai/DeepSeek-V2.5",
    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='')

4.2 Using FIM Completion with OpenAI’s completions Interface:

client = OpenAI(
    api_key="您的 APIKEY", # 从https://cloud.siliconflow.cn/account/ak获取
    base_url="https://api.siliconflow.cn/v1"
)

response = client.completions.create(
    model="deepseek-ai/DeepSeek-V2.5",
    prompt=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].text, end='')