API Documentation
身份验证
所有API请求都需要在Authorization头中使用您的API词元进行身份验证:
Authorization: Bearer 您的词元密钥
您可以在控制台生成并管理您的API词元。
API Endpoints
POST
/v1/completions
Create a completion for the provided prompt and parameters.
Request Parameters
Parameter | Type | Description |
---|---|---|
model | string | ID of the model to use (gpt-4, gpt-3.5-turbo, claude-2) |
prompt | string | The prompt to generate completions for |
max_tokens | 整数 | 要生成的最大词元数 |
Rate Limits
速率限制基于您的词元余额和您使用的模型:
- GPT-4: 500 requests per minute
- GPT-3.5: 3,500 requests per minute
- Claude 2: 500 requests per minute
Error Handling
Status Code | Description |
---|---|
401 | 身份验证词元无效 |
429 | Rate limit exceeded |
402 | 词元余额不足 |
Code Examples
Python
import requests
# 从一元控制台获取的API密钥
api_key = "您的词元密钥"
# Set up the headers with authentication
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
# Request parameters
data = {
"model": "gpt-4",
"prompt": "Hello, world!",
"max_tokens": 100
}
try:
# Make the API request
response = requests.post(
"https://api.1yuan.ai/v1/completions",
headers=headers,
json=data
)
# Check if the request was successful
response.raise_for_status()
# Print the response
result = response.json()
print(result)
except requests.exceptions.HTTPError as e:
print(f"HTTP Error: {e}")
except requests.exceptions.ConnectionError as e:
print(f"Connection Error: {e}")
except requests.exceptions.Timeout as e:
print(f"Timeout Error: {e}")
except requests.exceptions.RequestException as e:
print(f"Request Error: {e}")
JavaScript
// Function to call 一元 API
async function getCompletion() {
// 从一元控制台获取的API密钥
const apiKey = '您的词元密钥';
try {
// Make the API request
const response = await fetch('https://api.1yuan.ai/v1/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-4',
prompt: 'Hello, world!',
max_tokens: 100
})
});
// Check if the request was successful
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
// Parse and log the JSON response
const data = await response.json();
console.log(data);
return data;
} catch (error) {
console.error('Error:', error);
}
}
// Call the function
getCompletion();
cURL
curl -X POST 'https://api.1yuan.ai/v1/completions' \
-H 'Authorization: Bearer 您的词元密钥' \
-H 'Content-Type: application/json' \
-d '{
"model": "gpt-4",
"prompt": "Hello, world!",
"max_tokens": 100
}'
AI应用
您可以将一元词元服务与多种第三方应用程序集成,获得更好的使用体验: