Skip to main content

Text Completion

Overview

The Text Completion endpoint extends any given text prompt with coherent, stylistically-aligned continuations. Whether you need to finish a sentence, expand a paragraph, populate a table cell, or polish existing prose, one lightweight API call delivers the missing piece—fully compatible with the OpenAI completions format for zero-friction integration.

Highlights:

  • Context-aware: Mirrors tone, domain vocabulary and logical flow of the original text.
  • Multi-scenario: Sentence completion → long-form drafting → structured filling → style refinement.
  • Style lock-in: Optional parameters keep register (formal/casual), voice or authorial style intact.
  • Seamless integration: Works out-of-the-box with LangChain, LlamaIndex and other OpenAI-compatible stacks.
  • Streaming ready: Real-time token streaming (stream=true) for responsive UX.

Browse high-performance completion models in our AI Model Hub or jump straight into the Text Completion Quickstart to get started.

Quick Start: Basic Text Completion Example

Example 1: Quickly Use Text Completion with curl

Using curl allows you to clearly understand the raw structure of the text completion request path and parameters:

curl https://moark.ai/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_access_token" \
-d '{
"model": "Qwen2.5-72B-Instruct",
"prompt": "Artificial intelligence is a discipline that studies how to enable computers to",
"stream": false,
"max_tokens": 100,
"temperature": 0.7
}'

AI Model Response:

json
{
"id": "cmpl-6d4f8a9b9c7e6f5d4c3b2a1",
"object": "text_completion",
"created": 1731402000,
"model": "Qwen2.5-72B-Instruct",
"choices": [
{
"text": "think, learn, and perform various intelligent tasks like humans. It integrates knowledge from multiple fields such as computer science, mathematics, psychology, and linguistics, aiming to build systems with intelligent behaviors. These systems can perceive the environment, process information, make reasoning and decisions, and continuously improve their performance through experience. They are widely applied in numerous fields including image recognition, natural language processing, and robotics.",
"index": 0,
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 120,
"total_tokens": 135
}
}

Example 2: Calling Text Completion API with OpenAI Client

MoArk's Serverless API is compatible with the popular OpenAI-style API that developers love and the community favors.

All tools supporting OpenAI API can work directly with MoArk's Serverless API.

  1. Get Access Credentials: Log in to the MoArk console and create an access token in Workbench -> Access Tokens .
  2. Install Client: Taking the OpenAI client as an example, first install the dependency:
pip install openai -i https://mirrors.cloud.tencent.com/pypi/simple

If you have JavaScript experience, you can use the OpenAI Node.js client

Generate specified content through simple API calls:

python
from openai import OpenAI

client = OpenAI(
base_url="https://moark.ai/v1",
api_key="your_access_token"
)

response = client.completions.create(
model="Qwen2.5-72B-Instruct",
prompt="Spring has arrived, and the flowers in the park",
max_tokens=50,
temperature=0.8
)
print(response.choices[0].text)

Streaming Responses

For longer text completions, enabling streaming responses with stream=True allows real-time content delivery, enhancing user experience:

python
from openai import OpenAI

client = OpenAI(
base_url="https://moark.ai/v1",
api_key="your_access_token"
)

stream = client.completions.create(
model="Qwen2.5-72B-Instruct",
prompt="In the distant future, humanity has been able to",
stream=True,
max_tokens=200,
temperature=0.9
)

for chunk in stream:
if chunk.choices[0].text:
print(chunk.choices[0].text, end="")

Sample Output:

In the distant future, humanity has been able to freely travel among the stars, establishing prosperous colonies on different planets. They have unraveled many mysteries of the universe, mastered advanced energy technologies, and can utilize stellar energy to provide an endless power source for civilization. Meanwhile, humans have established friendly exchanges and cooperative relationships with other extraterrestrial civilizations, jointly exploring the unknown realms of the universe and promoting the progress of civilizations across the entire galaxy...

Parameter Description

Parameter descriptions are available in the API documentation

Model Selection

Choosing the right model for text completion requires considering both the characteristics of your completion scenario and the model's capabilities. Here are key factors to consider:

Task Scenario Adaptation

  • Short Sentence/Phrase Completion: For tasks like search suggestions or sentence completion, choose lightweight models (e.g., Qwen3-4B, Qwen3-8B) to balance speed and cost.
  • Long Text Segment Completion: For article continuation or paragraph expansion, select models with large context windows (e.g., 128K) and strong long-text comprehension abilities (e.g., kimi-k2-instruct).
  • Domain-Specific Completion: For academic paper fragments or professional report continuation, prioritize models trained in relevant domains (e.g., ERNIE-4.5-Turbo excels in Chinese professional fields, DeepSeek-R1 in reasoning tasks).

Functional Requirement Considerations

  • Style Consistency: For strict style adherence (e.g., classical Chinese completion or specific author style imitation), choose models sensitive to stylistic nuances (e.g., Qwen2.5-72B-Instruct).
  • Fact Accuracy: For completions requiring factual support (e.g., technical documentation), focus on models with strong factual performance.

Practical Selection Recommendations

  • Rapid Validation: Test completion logic and prompt effectiveness with smaller parameter models (e.g., Qwen3-4B) to reduce trial costs.
  • Performance Optimization: Switch to better models based on completion quality feedback (e.g., coherence, relevance). For example, if short sentence completion quality is unsatisfactory, try Qwen2.5-32B-Instruct.

By considering these factors and selecting the most suitable model from MoArk's model marketplace, you can achieve more efficient and higher-quality completion results.

Prompt Engineering

In text completion, prompt design is crucial as it directly impacts the coherence and relevance between completed content and the original text. A well-crafted prompt can guide the model to generate results that meet expectations.

Clarify Completion Boundaries and Direction

Clearly implying the direction and scope of completion in your prompt helps the model generate more relevant content.

Examples:

  • Poor prompt: "Uses of computers" (tends to generate entirely new content)
  • Better prompt: "Computers are used in various ways, such as in work, education, and entertainment." (clearly indicates completion of "work-related uses")

Maintain Style Consistency

If you want completion content to match the style of input text (e.g., formal/informal, professional/casual, specific genres), you can indicate this in the prompt or specify it through the instructions parameter.

Example: Setting style through instructions

python
from openai import OpenAI

client = OpenAI(
base_url="https://moark.ai/v1",
api_key="your_access_token"
)

client.completions.create(
model="Qwen2.5-72B-Instruct",
prompt="I once heard that the most beautiful things in the world are those that",
instructions="Continue in a reflective and poetic style, with elegant language and vivid imagery",
max_tokens=100
)

Leveraging Contextual Prompts

For the completion of long text segments, ensure that sufficient context information is provided to help the model understand the theme, logic, and style of the text. Example:

python
from openai import OpenAI

client = OpenAI(
base_url="https://moark.ai/v1",
api_key="your_access_token"
)

prompt = """
The following is a fragment of an article on environmental protection. Please continue it:

"With the acceleration of industrialization, environmental problems have become increasingly prominent. Air pollution, water pollution, soil pollution, etc., not only threaten ecological balance but also seriously affect human health. To address these challenges, each of us has a responsibility to take action,"
"""

client.completions.create(
model="Qwen2.5-72B-Instruct",
prompt=prompt,
max_tokens=150
)

Few-Shot Completion Example

By providing "input fragment + completion example" in your prompt, you can guide the model to quickly grasp specific completion patterns. Example: Email Response Fragment Completion

python
prompt = """
Please complete the email response fragment following the style of the examples below:

Example 1:
Input fragment: "Could we adjust the time of next Monday's meeting?"
Completed content: "Hello, we can adjust the time of next Monday's meeting. Would you be available at 2 PM next Monday or 10 AM on Wednesday? Looking forward to your reply."

Example 2:
Input fragment: "Regarding the project delay issue, I'd like to discuss solutions with you."
Completed content: "Hello, regarding the project delay issue, I'm happy to discuss solutions with you. I'm available this afternoon and all day tomorrow. What time works best for you?"

Fragment to complete: "Could you provide the financial statements from last month?"
Completed content:
"""

from openai import OpenAI

client = OpenAI(
base_url="https://moark.ai/v1",
api_key="your_access_token"
)

client.completions.create(
model="Qwen2.5-72B-Instruct",
prompt=prompt,
max_tokens=50
)

Possible Output:

"Hello, I've prepared last month's financial statements and will send them to your email shortly. If you have any questions, feel free to contact me anytime." By using the above prompt engineering techniques, you can improve the quality and accuracy of text completion, making the completed content more suitable for actual needs.