1. 使用场景

FIM (Fill In the Middle) 补全中,用户提供希望输入的前后内容,模型来补全中间的内容,典型用于代码补全、文本中间内容补全等场景中。

2. 使用方式

2.1 在 chat/completions 接口中使用

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

2.2 在 completions 接口中使用

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

3. 支持模型列表

  • Deepseek 系列:

    • deepseek-ai/DeepSeek-V2.5
    • deepseek-ai/DeepSeek-Coder-V2-Instruct
  • Qwen系列:

    • Qwen/Qwen2.5-Coder-7B-Instruct
注意:支持的模型列表可能会发生变化,请查阅本文档了解最新支持的模型列表。

4. 使用示例

4.1 基于 OpenAI 的 chat.completions 接口使用FIM补全:

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 基于 OpenAI 的 chat.completions 接口使用 FIM 补全:

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='')