Skip to main content

Overview

Chat completions allow you to generate human-like text responses using state-of-the-art language models. CyrionAI provides access to multiple models including GPT-4, Claude-3, and others through a unified API.

Basic Usage

Simple Chat Completion

import openai

client = openai.OpenAI(
    api_key="your-api-key",
    base_url="https://ai.cyrionlabs.org/v1"
)

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "user", "content": "What are the benefits of AI for nonprofits?"}
    ]
)

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

Multi-turn Conversation

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant for nonprofit organizations."},
        {"role": "user", "content": "How can we improve our fundraising strategy?"},
        {"role": "assistant", "content": "There are several ways to improve fundraising..."},
        {"role": "user", "content": "Can you provide specific examples for small nonprofits?"}
    ]
)

Message Roles

System Message

Sets the behavior and context for the conversation:
{"role": "system", "content": "You are an expert grant writer helping nonprofits secure funding."}

User Message

Represents input from the user:
{"role": "user", "content": "Write a grant proposal for a youth education program."}

Assistant Message

Represents responses from the AI model:
{"role": "assistant", "content": "Here's a draft grant proposal..."}

Parameters

Required Parameters

ParameterTypeDescription
modelstringThe model to use (e.g., “gpt-4o-mini”, “claude-3-sonnet”)
messagesarrayArray of message objects with role and content

Optional Parameters

ParameterTypeDefaultDescription
max_tokensintegernullMaximum number of tokens to generate
temperaturenumber1.0Controls randomness (0-2)
top_pnumber1.0Controls diversity via nucleus sampling (0-1)
ninteger1Number of completions to generate
stopstring/arraynullStop sequences
presence_penaltynumber0.0Penalty for new topics (-2 to 2)
frequency_penaltynumber0.0Penalty for repetition (-2 to 2)
userstringnullUser identifier for tracking

Temperature

Controls the randomness of the output:
# More creative and varied responses
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Write a creative story"}],
    temperature=0.8
)

# More focused and deterministic responses
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Explain quantum physics"}],
    temperature=0.1
)

Max Tokens

Limit the length of the response:
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Summarize the benefits of AI"}],
    max_tokens=150  # Limit to 150 tokens
)

Supported Models

GPT Models

ModelDescriptionBest For
gpt-4o-miniFast and efficientGeneral tasks, quick responses
gpt-4oMost capableComplex reasoning, creative writing
gpt-3.5-turboLegacy modelSimple tasks, cost-effective

Claude Models

ModelDescriptionBest For
claude-3-haikuFast and efficientQuick responses, simple tasks
claude-3-sonnetBalanced performanceGeneral purpose, good reasoning
claude-3-opusMost capableComplex analysis, creative writing

Other Models

ModelDescriptionBest For
llama-3-8bOpen sourceCustom applications, fine-tuning
llama-3-70bLarge open sourceComplex reasoning, research

Advanced Features

Function Calling

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "What's the weather in San Francisco?"}],
    tools=[
        {
            "type": "function",
            "function": {
                "name": "get_weather",
                "description": "Get current weather for a location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {"type": "string", "description": "City name"}
                    },
                    "required": ["location"]
                }
            }
        }
    ]
)
Enable web search for up-to-date information:
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "What are the latest AI developments?"}],
    web_search=True
)

Image Understanding

Include images in your conversations:
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What's in this image?"},
                {"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,..."}}
            ]
        }
    ]
)

Best Practices

1. Use System Messages Effectively

# Good: Clear, specific system message
messages = [
    {"role": "system", "content": "You are a grant writing expert. Provide specific, actionable advice for nonprofit organizations."},
    {"role": "user", "content": "Help me write a grant proposal"}
]

# Avoid: Vague or overly complex system messages
messages = [
    {"role": "system", "content": "Be helpful and nice"},
    {"role": "user", "content": "Help me write a grant proposal"}
]

2. Manage Conversation Context

# Keep conversations focused and within token limits
messages = [
    {"role": "system", "content": "You are a nonprofit consultant."},
    {"role": "user", "content": "How can we improve our volunteer program?"}
]

# Add follow-up questions as new user messages
messages.append({"role": "assistant", "content": "Here are some strategies..."})
messages.append({"role": "user", "content": "Can you elaborate on the training aspect?"})

3. Use Appropriate Temperature

# Low temperature for factual, consistent responses
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Explain the tax benefits for 501(c)(3) organizations"}],
    temperature=0.1
)

# Higher temperature for creative tasks
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Write a creative fundraising campaign slogan"}],
    temperature=0.8
)

4. Handle Long Conversations

# For long conversations, consider summarizing or truncating context
def summarize_conversation(messages):
    # Implement conversation summarization logic
    pass

# Use the summary as context for new requests
summary = summarize_conversation(previous_messages)
messages = [
    {"role": "system", "content": f"Previous conversation summary: {summary}"},
    {"role": "user", "content": "Continue our discussion about fundraising"}
]

Error Handling

try:
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Hello"}]
    )
except openai.RateLimitError:
    print("Rate limit exceeded. Please wait before making more requests.")
except openai.AuthenticationError:
    print("Invalid API key. Please check your credentials.")
except openai.APIError as e:
    print(f"API error: {e}")

Response Format

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello"}]
)

# Access response data
print(response.id)  # Unique response ID
print(response.model)  # Model used
print(response.choices[0].message.content)  # Generated text
print(response.usage.prompt_tokens)  # Input tokens
print(response.usage.completion_tokens)  # Output tokens
print(response.usage.total_tokens)  # Total tokens

Examples

Grant Writing Assistant

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are an expert grant writer with 20 years of experience helping nonprofits secure funding."},
        {"role": "user", "content": "Write a compelling needs statement for a youth mentoring program seeking $50,000 in funding."}
    ],
    temperature=0.3,
    max_tokens=500
)

Donor Communication

response = client.chat.completions.create(
    model="claude-3-sonnet",
    messages=[
        {"role": "system", "content": "You are a nonprofit communications specialist. Write warm, personal messages that connect with donors."},
        {"role": "user", "content": "Write a thank you email to a donor who just gave $1,000 to our animal shelter."}
    ],
    temperature=0.7
)

Program Evaluation

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a nonprofit evaluation expert. Help organizations measure their impact effectively."},
        {"role": "user", "content": "Design an evaluation framework for a literacy program serving elementary school students."}
    ],
    temperature=0.2,
    max_tokens=800
)

Next Steps