Skip to main content

AI API Tutorials

Welcome to our tutorials section! This guide will walk you through using our AI API to generate text completions, chat completions, and manage API credits. Follow these step-by-step examples to quickly integrate our AI capabilities into your applications.

Table of Contents

Getting Started

Before you begin, make sure you have:

  1. Created an account and activated developer mode
  2. Obtained your API key from the dashboard
  3. Purchased API credits (if required)

All API requests should include your API key in the Authorization header:

Authorization: Bearer YOUR_API_KEY

Text Completion Tutorial

Text completion allows you to generate text based on a prompt. This is useful for content generation, summarization, and other text-based tasks.

Basic Text Completion

Here's how to make a simple text completion request:

// Using fetch in JavaScript
const response = await fetch('https://api.graphine.ai/api/v1/completions/text', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
"prompt": "Write a short paragraph about artificial intelligence."
})
});

const result = await response.json();
console.log(result.data.text);
# Using requests in Python
import requests

url = "https://api.graphine.ai/api/v1/completions/text"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
}
data = {
"prompt": "Write a short paragraph about artificial intelligence."
}

response = requests.post(url, headers=headers, json=data)
result = response.json()
print(result["data"]["text"])

Customizing with System Prompts

You can customize the behavior of the model by providing a system prompt:

const response = await fetch('https://api.graphine.ai/api/v1/completions/text', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
"prompt": "Write a short paragraph about artificial intelligence.",
"system_prompt": "You are an expert in AI technology. Be technical but concise."
})
});

Chat Completion Tutorial

Chat completions are ideal for conversational applications, allowing you to maintain context across messages.

Simple Chat Conversation

const response = await fetch('https://api.graphine.ai/api/v1/completions/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Hello, can you help me understand how machine learning works?"
}
]
})
});

const result = await response.json();
console.log(result.data.choices[0].message.content);

Multi-turn Conversation

For a multi-turn conversation, append new messages to the array:

const conversation = [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Hello, can you help me understand how machine learning works?"
},
{
"role": "assistant",
"content": "Machine learning is a subset of artificial intelligence that enables systems to learn and improve from experience without being explicitly programmed. It focuses on developing algorithms that can access data and use it to learn patterns and make decisions with minimal human intervention."
},
{
"role": "user",
"content": "What are some common machine learning algorithms?"
}
];

const response = await fetch('https://api.graphine.ai/api/v1/completions/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
"messages": conversation
})
});

Managing API Credits

Our API uses a credit system for billing. Here's how to manage your credits:

Checking Available Models

First, check what models are available and their capabilities:

const response = await fetch('https://your-api-domain.com/api/v1/models', {
method: 'GET',
headers: {
'Authorization': 'Bearer YOUR_API_KEY'
}
});

const models = await response.json();
console.log(models.data);

Viewing Available Credit Packages

To see what credit packages are available for purchase:

const response = await fetch('https://your-api-domain.com/api/v1/credits/packages', {
method: 'GET',
headers: {
'Authorization': 'Bearer YOUR_API_KEY'
}
});

const packages = await response.json();
console.log(packages.data);

Purchasing Credits

To purchase credits, create a checkout session:

const response = await fetch('https://your-api-domain.com/api/v1/credits/checkout', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
"package_id": "PACKAGE_UUID",
"success_url": "https://your-app.com/payment/success",
"cancel_url": "https://your-app.com/payment/canceled"
})
});

const checkout = await response.json();
// Redirect the user to checkout_url
window.location.href = checkout.data.checkout_url;

Checking Credit Purchase History

To view your credit purchase history:

const response = await fetch('https://your-api-domain.com/api/v1/credits/history', {
method: 'GET',
headers: {
'Authorization': 'Bearer YOUR_API_KEY'
}
});

const history = await response.json();
console.log(history.data);

Working with Different Models

Our API supports multiple models with different capabilities:

Specifying a Model

To use a specific model for text completion:

const response = await fetch('https://api.graphine.ai/api/v1/completions/text', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
"prompt": "Write a creative short story.",
"model": "your-model-name" // Use model_name from the available models endpoint
})
});

Similarly, for chat completions:

const response = await fetch('https://api.graphine.ai/api/v1/completions/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
"messages": [
{
"role": "user",
"content": "Write a creative short story."
}
],
"model": "your-model-name" // Use model_name from the available models endpoint
})
});

Understanding Response Metadata

Our API responses include helpful metadata about token usage and processing time:

// Example response structure
{
"success": true,
"data": {
"text": "Generated text...",
"metadata": {
"conversation_id": "uuid-string",
"model": "model-name",
"prompt_tokens": 10,
"completion_tokens": 50,
"total_tokens": 60,
"completion_time": 1.25
}
}
}

For chat completions, check the usage field:

// Chat completion response
{
"success": true,
"data": {
"id": "completion-id",
"object": "chat.completion",
"created": 1678364583,
"model": "model-name",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Generated response..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 30,
"total_tokens": 45
}
}
}

This metadata helps you track your credit usage and optimize your API calls.