1. Resources
  2. Examples

​
Example 1: Simple Code Generation

This example demonstrates how to use the CoreThink API for code generation tasks.

Request:

from openai import OpenAI
import os

# Initialize the CoreThink client
client = OpenAI(
    base_url="https://api.corethink.ai/v1/code",
    api_key=os.environ.get("CORETHINK_API_KEY"),
)

# Make the API call
response = client.chat.completions.create(
    model="gpt-oss-120b",
    messages=[
        {"role": "user", "content": "Write a Python function to calculate the fibonacci sequence"}
    ],
)

print(response.choices[0].message.content)

Output:

def fibonacci(n):
    """Calculate the nth Fibonacci number."""
    if n <= 0:
        return 0
    elif n == 1:
        return 1
    else:
        a, b = 0, 1
        for _ in range(2, n + 1):
            a, b = b, a + b
        return b

# Example usage
for i in range(10):
    print(f"F({i}) = {fibonacci(i)}")

​
Example 2: Code Review and Refactoring

Ask the model to review and improve existing code.

Request:

code_to_review = """
def calc(x,y,z):
    r = x+y
    r = r*z
    return r
"""

response = client.chat.completions.create(
    model="gpt-oss-120b",
    messages=[
        {
            "role": "user",
            "content": f"Review and refactor this code with better naming and documentation:\n\n{code_to_review}"
        }
    ],
)

print(response.choices[0].message.content)

Output:

def calculate_scaled_sum(addend_a: float, addend_b: float, multiplier: float) -> float:
    """
    Calculate the sum of two numbers and multiply the result by a scaling factor.

    Args:
        addend_a: First number to add
        addend_b: Second number to add
        multiplier: Factor to multiply the sum by

    Returns:
        The product of (addend_a + addend_b) * multiplier
    """
    total = addend_a + addend_b
    scaled_result = total * multiplier
    return scaled_result

​
Example 3: Using Different Models

CoreThink supports multiple open-source models for different use cases:

# For general purpose tasks (default)
response = client.chat.completions.create(
    model="gpt-oss-120b",
    messages=[{"role": "user", "content": "Write a simple hello world in Rust"}],
)

# For advanced reasoning tasks
response = client.chat.completions.create(
    model="deepseek/deepseek-r1",
    messages=[{"role": "user", "content": "Design a rate limiter class with sliding window algorithm"}],
)

# For efficient, lightweight tasks
response = client.chat.completions.create(
    model="mistral/mistral-small-3-24b",
    messages=[{"role": "user", "content": "Write unit tests for a user authentication module"}],
)

# For multilingual and complex tasks
response = client.chat.completions.create(
    model="qwen/qwen3-235b",
    messages=[{"role": "user", "content": "Translate and optimize this function for production"}],
)

​
Example 4: Multi-Turn Conversation for Code Development

Build on previous responses to iteratively develop code.

messages = [
    {"role": "user", "content": "Create a simple REST API endpoint for user registration in Python Flask"}
]

# First response - basic implementation
response = client.chat.completions.create(
    model="gpt-oss-120b",
    messages=messages,
)

# Add response to conversation history
messages.append({"role": "assistant", "content": response.choices[0].message.content})

# Follow-up request
messages.append({"role": "user", "content": "Now add input validation and password hashing"})

# Second response - enhanced implementation
response = client.chat.completions.create(
    model="gpt-oss-120b",
    messages=messages,
)

print(response.choices[0].message.content)

​
Response Format

The CoreThink API returns responses in the standard OpenAI chat completion format:

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "gpt-oss-120b",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "def fibonacci(n):\n    if n <= 1:\n        return n\n    return fibonacci(n-1) + fibonacci(n-2)"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 45,
    "total_tokens": 70
  }
}

​
Response Fields

id
required
string

Unique identifier for the chat completion

object
required
string

Type of object returned (always chat.completion)

created
required
integer

Unix timestamp of when the response was created

model
required
string

Model used to generate the response

choices
required
array

Array containing the response choices

choices[].index
required
integer

Index of the choice in the list

choices[].message
required
object

The message object containing the response

choices[].message.role
required
string

Role of the responder (always assistant)

choices[].message.content
required
string

The generated code or response

choices[].finish_reason
required
string

Reason why generation finished (stop, length, etc.)

usage
required
object

Token usage information for the request

usage.prompt_tokens
required
integer

Number of tokens in the prompt

usage.completion_tokens
required
integer

Number of tokens in the completion

usage.total_tokens
required
integer

Total number of tokens used (prompt + completion)

​
Best Practices for Code Generation:

  • Be specific about the programming language and framework you want

  • Provide context about your use case for better results

  • Use system messages to set coding style preferences

  • Break complex tasks into smaller, focused requests

  • Use multi-turn conversations to iteratively refine code