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

支持的模型

目前平台上的 DeepSeek-V3 模型支持。

注意事项

用户需提前在 API 密钥管理中创建和获取 API KEY 用于替换示例代码中的 api_key 参数。

使用方法

/v1/completions 接口中使用:

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

代码示例

示例一

from openai import OpenAI

client = OpenAI(
    api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    base_url="https://openapi.coreshub.cn/v1"
)

response = client.completions.create(
    model="DeepSeek-V3",
    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:
    if chunk.choices:
      print(chunk.choices[0].text, end='')
print("\n")

运行示例代码的回显示例:

model func fim 1

示例二

from openai import OpenAI

client = OpenAI(
    api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    base_url="https://openapi.coreshub.cn/v1"
)

response = client.completions.create(
    model="DeepSeek-V3",
    prompt="def fib(a):",
    suffix="        return fib(a-1) + fib(a-2)",
    stream=True,
    max_tokens=4096
)

for chunk in response:
    if chunk.choices:
      print(chunk.choices[0].text, end='')
print("\n")

运行示例代码的回显示例:

    if a <= 1:
        return a
    else: