1. Use Cases

FIM (Fill In the Middle) completion allows users to provide the desired prefix and suffix content, enabling the model to complete the content in between. This is typically used in scenarios such as code completion and filling in missing content in text.

2. How to Use

2.1 Using the chat/completions Interface

{ 
    "model": "model info",
    "messages": "prompt message",
    "params": "params",
    "extra_body": {"prefix":"prefix content", "suffix":"suffix content"}
}

2.2 Using the completions Interface

{
    "model": "model info",
    "prompt": "prefix content",
    "suffix": "suffix content"
}

3. Supported Models

  • Deepseek Series:

    • deepseek-ai/DeepSeek-V3
  • Qwen Series:

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

4. Example Usage

4.1 Using FIM Completion with OpenAI’s chat.completions Interface:

client = OpenAI(
    api_key="Your APIKEY", # Obtain from https://cloud.siliconflow.com/account/ak
    base_url="https://api.ap.siliconflow.com/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):
    # Base case: if the array length is less than or equal to 1, return the array
    if len(arr) <= 1:
        return arr
    else:
""",
            "suffix": f"""
# Test the quick_sort function
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="Your APIKEY", # Obtain from https://cloud.siliconflow.com/account/ak
    base_url="https://api.ap.siliconflow.com/v1"
)

response = client.completions.create(
    model="deepseek-ai/DeepSeek-V2.5",
    prompt=f"""
def quick_sort(arr):
    # Base case: if the array length is less than or equal to 1, return the array
    if len(arr) <= 1:
        return arr
    else:
""",
    suffix=f"""
# Test the quick_sort function
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='')