Developer Documentation
Welcome to the TIFA AI API. This documentation will guide you on how to integrate and use our services. TIFA AI provides powerful natural language processing capabilities, supporting various application scenarios.
Main Features
- ✨ Supports streaming output, real-time response
- ✨ Long context support, up to 100K tokens
- ✨ Multi-language support, including Chinese, English, Japanese, Korean, and 40+ other languages
- ✨ 99.9% service availability guarantee
Authentication
All API requests require authentication using an API key. You can obtain your API key from the console.
Notes:
- API keys have full account permissions, do not disclose them
- It is recommended to store API keys in environment variables or configuration files
- Regularly rotate API keys to enhance security
- You can create different API keys for different applications
Rate Limits
To ensure service quality, API access has the following limits:
- Each API key can make up to 60 requests per minute
- Maximum input of 4000 tokens per request
- Monthly usage limits vary based on the plan
💡 For higher quotas, please contact the business team to upgrade your plan
Best Practices
1. Error Handling
- Implement comprehensive error handling mechanisms
- Retry network errors
- Set reasonable timeout periods
- Log detailed error logs
2. Performance Optimization
- Use connection pooling to reuse connections
- Enable gzip compression
- Implement request caching
- Adopt asynchronous processing methods
3. Security Recommendations
- Store keys in environment variables
- Enable HTTPS transmission
- Implement request signing mechanisms
- Regularly update API keys
4. Development Recommendations
- Use SDKs instead of directly calling the API
- Isolate development and production environments
- Set model parameters appropriately
- Implement graceful degradation mechanisms
💡 These best practices will help you build more stable and reliable applications
API Endpoint
POST http://visionsic.com:8001/v1/chat/completions
Request Notes:
- Only supports HTTPS protocol
- Request body must be in JSON format
- Response encoding is UTF-8
- Supports cross-origin requests (CORS)
Example Code
import requests
import json
url = "http://visionsic.com:8001/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
}
data = {
"model": "/home/fangxin/claude",
"messages": [
{"role": "system", "content": "You are an assistant"},
{"role": "user", "content": "Hello"}
],
"temperature": 0.4,
"stream": True
}
# Error Handling
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status() # Check for HTTP errors
for line in response.iter_lines():
if line:
print(json.loads(line))
except requests.exceptions.RequestException as e:
print(f"API request error: {e}")
except json.JSONDecodeError as e:
print(f"JSON parsing error: {e}")
const url = 'http://visionsic.com:8001/v1/chat/completions';
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
};
const data = {
model: '/home/fangxin/claude',
messages: [
{role: 'system', content: 'You are an assistant'},
{role: 'user', content: 'Hello'}
],
temperature: 0.4,
stream: true
};
// Using async/await and error handling
async function chatWithAI() {
try {
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
console.log(result);
} catch (error) {
console.error('Request error:', error);
}
}
chatWithAI();
# Basic Request
curl -X POST http://visionsic.com:8001/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "/home/fangxin/claude",
"messages": [
{"role": "system", "content": "You are an assistant"},
{"role": "user", "content": "Hello"}
],
"temperature": 0.4,
"stream": true
}'
# Request with Timeout Settings
curl -X POST http://visionsic.com:8001/v1/chat/completions \
--connect-timeout 10 \
--max-time 30 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d @request.json
Code Notes:
- The example includes basic error handling
- It is recommended to add retry mechanisms in production environments
- Adjust timeout settings as needed
- Use asynchronous processing for stream mode
Chat API
Endpoint: http://visionsic.com:8001/v1/chat/completions
Method: POST
Content-Type: application/json
The chat API supports single and multi-turn conversations, allowing control over AI behavior through various parameters. It supports both streaming and regular output modes.
Parameter Explanation
Parameter Name | Type | Required | Description |
---|---|---|---|
model | string | Yes | Model path, e.g., "/home/fangxin/claude" |
messages | array | Yes | Conversation history, containing role and content fields |
temperature | number | No | Temperature parameter, controls randomness, range 0-1, default 0.7 |
stream | boolean | No | Whether to enable streaming output, default false |
max_tokens | integer | No | Maximum length of generated text, default 2048 |
Response Format:
{
"id": "chat_12345678",
"object": "chat.completion",
"created": 1679478935,
"model": "/home/fangxin/claude",
"choices": [{
"message": {
"role": "assistant",
"content": "AI's response content"
},
"finish_reason": "stop",
"index": 0
}]
}
Error Handling
Common Error Codes:
Error Code | Description | Handling Suggestion |
---|---|---|
401 | Unauthorized | Check if the API key is correct |
429 | Too Many Requests | Implement request rate limiting |
500 | Server Error | Retry later or contact support |
Advanced Features
1. Streaming Output
Supports real-time streaming of AI responses, providing a better interactive experience.
// Enable streaming output
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify({...data, stream: true})
});
const reader = response.body.getReader();
while(true) {
const {done, value} = await reader.read();
if (done) break;
console.log(new TextDecoder().decode(value));
}
2. Function Calling
Supports custom function capabilities, expanding the functionality of the AI assistant.
const functions = [{
"name": "get_weather",
"description": "Get weather information",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name"
}
}
}
}];
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify({
...data,
functions: functions,
function_call: "auto"
})
});
3. Context Memory
Supports multi-turn conversation memory, achieving a coherent conversation experience.
const messages = [
{"role": "system", "content": "You are an assistant"},
{"role": "user", "content": "How's the weather today?"},
{"role": "assistant", "content": "It's sunny in Beijing today"},
{"role": "user", "content": "What about the temperature?"} // Continuing the context
];
4. Model Parameter Tuning
Provides rich parameter configurations for personalized output control.
- temperature: Controls output randomness (0-1)
- top_p: Controls output diversity
- max_tokens: Controls output length
- presence_penalty: Controls topic freshness
💡 Advanced features may require higher API permissions, please contact customer service to upgrade