In the FIM (Fill In the Middle) completion task, users input the prefix and suffix content they want to keep, and the model generates the missing part based on these prompts. This completion method is common in applications such as code auto-completion and text middle generation.
In the chat/completions interface, simply set the model id to the model id on the Model Gallery, which is available in the top right corner of the module card.
3.1 Using FIM Completion Based on OpenAI’s chat.completions Interface:
Copy
from openai import OpenAIclient = OpenAI( api_key="AIHUBMIX_API_KEY", # Replace with the key you generated in AiHubMix base_url="https://aihubmix.com/v1")messages = [ {"role": "user", "content": "Please write a sum function code"},]response = client.chat.completions.create( model="gpt-4o-mini", messages=messages, extra_body={ "prefix": f"""def sum_numbers(numbers): # If the list is empty, return 0 if not numbers: return 0""", "suffix": f"""# Run Testnumbers = [1, 2, 3, 4, 5]result = sum_numbers(numbers)print("Sum of numbers:", result)""" }, stream=True, max_tokens=4096)for chunk in response: if chunk.choices and len(chunk.choices) > 0 and chunk.choices[0].delta.content is not None: print(chunk.choices[0].delta.content, end='')