AI Text Generation API Documentation
1. Overview
This document outlines the usage and specifications of the AI Text Generation API. Powered by advanced artificial intelligence models, this API can generate high-quality text based on user-provided prompts. It is ideal for use cases such as intelligent writing assistants, content creation tools, and chatbots.
2. Basic Information
API Name: AI Text Generation API
Version: 1.0
HTTP Method: POST
Endpoint:
https://api.example.com/ai/text/generate
3. Request Parameters
Parameter | Type | Required | Description |
prompt | string | Yes | Input prompt to guide the text generation |
max_tokens | int | No | Maximum length of generated text, default is 100 |
temperature | float | No | Controls randomness of output; range: 0–1, default is 0.7 |
4. Response Parameters
Parameter | Type | Description |
code | int | Response status code (200 = success) |
message | string | Response message, empty on success |
data | object | Contains the generated text |
- text | string | The generated text content |
5. Request Example
{
"prompt": "Write an introduction about artificial intelligence.",
"max_tokens": 200,
"temperature": 0.8
}
6. Response Example
Success Response
{
"code": 200,
"message": "",
"data": {
"text": "Artificial intelligence is a technology that simulates human intelligence, enabling computers to perform tasks such as learning, reasoning, and self-correction."
}
}
Error Response
{
"code": 400,
"message": "Invalid request parameters",
"data": null
}
7. Error Handling
Error Code | Message | Cause | Solution |
400 | Bad Request | Invalid request parameters | Verify and correct parameters before resubmitting |
401 | Unauthorized | Missing or invalid credentials | Ensure valid API key is used in the Authorization header |
500 | Internal Server Error | Server-side issue | Contact support or retry after some time |
8. Security and Authentication
Security: Data transmission is secured via HTTPS.
Authentication: API key required. Add the
Authorization
header in the formatBearer your_api_key
.
9. Example Code
Python Example
import requests
url = "https://api.example.com/ai/text/generate"
headers = {
"Authorization": "Bearer your_api_key",
"Content-Type": "application/json"
}
data = {
"prompt": "Write an introduction about artificial intelligence.",
"max_tokens": 200,
"temperature": 0.8
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
print(response.json()["data"]["text"])
else:
print("Error:", response.status_code, response.json()["message"])
Java Example
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class ApiExample {
public static void main(String[] args) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/ai/text/generate"))
.header("Authorization", "Bearer your_api_key")
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"prompt\": \"Write an introduction about artificial intelligence.\", \"max_tokens\": 200, \"temperature\": 0.8}"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
System.out.println(response.body());
} else {
System.out.println("Error: " + response.statusCode() + ", " + response.body());
}
}
}
10. Appendix
Performance Metrics: Average response time per request is approximately 1 second.
Release Notes: Version 1.0 released on June 19, 2025.
Reference: OpenAI Official Documentation
Note: This is a sample API reference. Please adjust the details to match the actual specifications of your API when implementing it in a production environment.
评论
发表评论