Using OpenAI SDK in Python
This guide explains how to install and use the OpenAI SDK in Python to connect to Kakoti’s API.
π§ Installing the SDK
You can install the OpenAI SDK using either pip or uv.
Install with pip
pip install openaiInstall with uv
uv pip install openaiπ‘ About uv
uvis a new and fast tool for managing Python packages, built by Astral. Key features include:
- High Speed: Package installation is 10-100x faster than pip
- Virtual Environment Management: Automatic creation and management of virtual environments
- Full Compatibility: Direct replacement for pip and pip-tools
- Fast Installation: Written in Rust for optimal performance
- Dependency Management: Fast and accurate dependency resolution
To install
uv, you can use the following command:curl -LsSf https://astral.sh/uv/install.sh | sh
π Using the SDK
To use the OpenAI SDK with Kakoti’s API, you need to configure the Base URL and API Key.
Complete Example
from openai import OpenAI
# Create client with Kakoti Base URL and API Key
client = OpenAI(
base_url="https://llm-api.kakoti.com/v1",
api_key="YOUR_API_KEY"
)
# Send request
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": "Hello! How can I help you?"}
]
)
# Display response
print(response.choices[0].message.content)π Explanation
- base_url: Kakoti’s Base URL (
https://llm-api.kakoti.com/v1) - api_key: API key received from the Kakoti panel
- model: Name of the model to use (e.g.,
gpt-4o-mini,gpt-4o) - messages: Array of messages for conversation
π Using Environment Variables
For better security, you can store the API Key in environment variables:
import os
from openai import OpenAI
# Read API Key from environment variable
api_key = os.getenv("KAKOTI_API_KEY")
client = OpenAI(
base_url="https://llm-api.kakoti.com/v1",
api_key=api_key
)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": "Hello"}
]
)
print(response.choices[0].message.content)Then set the environment variable:
export KAKOTI_API_KEY="your-api-key-here"π‘ Advanced Example
from openai import OpenAI
client = OpenAI(
base_url="https://llm-api.kakoti.com/v1",
api_key="YOUR_API_KEY"
)
# Conversation with multiple messages - Wealth Management Assistant
messages = [
{
"role": "system",
"content": "You are a professional financial advisor and wealth management specialist. You specialize in financial market analysis, investment planning, risk management, and portfolio optimization. You always provide recommendations based on conservative financial principles and aligned with long-term client goals."
},
{
"role": "user",
"content": "I'm 35 years old and want to plan for retirement in 30 years. I currently have $50,000 in savings and can save $1,000 per month. Please suggest a diversified and balanced investment strategy with moderate risk consideration."
}
]
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
temperature=0.7,
max_tokens=500
)
print(response.choices[0].message.content)