help.khb.com 对话模型 FIM 补全 (Fill In the Middle)

FIM 补全 (Fill In the Middle)

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

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 系列

  • 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 系列

  • 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

注意:支持的模型列表可能会发生变化,请查阅本文档了解最新支持的模型列表。

4. 使用示例

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

from openai import OpenAI

client = OpenAI(
    api_key="您的 APIKEY",  # 从 https://account.khb.com/account/ak 获取
    base_url="https://khb.net.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": """def quick_sort(arr):
    # 基本情况,如果数组长度小于等于 1,则返回数组
    if len(arr) <= 1:
        return arr
    else:
        """,
        "suffix": """
# 测试 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 的 completions 接口使用 FIM 补全

from openai import OpenAI

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

response = client.completions.create(
    model="Pro/zai-org/GLM-5.1",
    prompt="""def quick_sort(arr):
    # 基本情况,如果数组长度小于等于 1,则返回数组
    if len(arr) <= 1:
        return arr
    else:
        """,
    suffix="""
# 测试 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='')

Related Post