Overview
Text Generation (Chat Completions) is one of the core capabilities of the APIYi platform, supporting 400+ popular AI models for intelligent conversations and text generation. Through a unified OpenAI-compatible interface, you can easily implement:- Intelligent Dialogue: Build chatbots and virtual assistants
- Content Creation: Article writing, creative generation, copywriting
- Code Assistance: Code generation, debugging, refactoring suggestions
- Knowledge Q&A: Answer questions, knowledge retrieval, information extraction
- Role-Playing: Customized AI characters and scenario simulation
Supports OpenAI GPT-4, Claude, Gemini, DeepSeek, Qwen, and 400+ mainstream models with a single API key.
Quick Start
Basic Conversation Example
Simple single-turn conversation using Chat Completions API:Multi-Turn Conversation Example
Maintain conversation history through themessages array for context-aware dialogue:
Core Parameters
model (required)
Specify the model name. See Model Information for details.messages (required)
Array of conversation messages, each containingrole and content fields:
system
System prompt defining AI behavior and role
user
User message representing user input
assistant
Assistant message representing AI response
temperature (optional)
Controls output randomness, range0.0 ~ 2.0, default 1.0:
- 0.0 ~ 0.3: More deterministic and consistent, suitable for factual tasks (translation, summarization, code generation)
- 0.7 ~ 1.0: Balanced creativity and accuracy, suitable for daily conversation
- 1.0 ~ 2.0: More creative and diverse, suitable for creative writing and brainstorming
max_tokens (optional)
Limit maximum number of tokens generated to control cost and response length:top_p (optional)
Nucleus sampling parameter, range0.0 ~ 1.0, controls output diversity:
- Lower values (e.g.,
0.5): More focused and deterministic output - Higher values (e.g.,
0.9): More diverse and random output
It’s recommended to adjust only
temperature or top_p, not both simultaneously.stream (optional)
Enable streaming output to return results token by token, improving user experience:Advanced Usage
System Prompt
Define AI behavior, role, knowledge scope, and response style through thesystem role:
Role-Playing
Create AI assistants with specific personalities and expertise:Context Management
For long conversations, properly manage context length to avoid exceeding model token limits:JSON Mode Output
Some models support forcing JSON format output:Best Practices
1. Choose the Right Model
Select the most cost-effective model based on task requirements:2. Optimize Prompts
Good prompts significantly improve output quality:Clear Task
Clearly state what AI needs to do with necessary context
Specify Format
Define output format, length, tone, etc.
Provide Examples
Give input-output examples to help AI understand expectations
Step-by-Step
Break complex tasks into multiple steps
3. Cost Control
Use parameters wisely to reduce API costs:4. Error Handling
Add exception handling to improve application stability:5. Use Streaming Output
For long text generation, streaming output improves user experience:FAQ
How to count tokens?
Different models use different tokenizers. Use thetiktoken library for estimation:
Why is the output truncated?
Possible reasons:- Reached
max_tokenslimit - Model’s context window is insufficient
- Content safety policy triggered
- Increase
max_tokensparameter - Choose models with longer context support
- Check
finish_reasonfield to determine cause
How to implement conversation memory?
Maintain conversation history at the application layer:Related Documentation
Model Information
View all supported models and pricing
Text Embedding
Convert text to vector representations