In [ ]:
Run models on Nebius Token Factory (Native)¶
This the recommended API. The API uses OpenAI library.
References and Acknowledgements¶
Pre requisites¶
- Nebius API key. Sign up for free at Token Factory
- And complete the setup
1 - Install Dependencies¶
In [1]:
%%capture
!pip install -q openai python-dotenv
2 - Load Configuration¶
In [2]:
import os, sys
## Recommended way of getting configuration
if os.getenv("COLAB_RELEASE_TAG"):
print("Running in Colab")
from google.colab import userdata
NEBIUS_API_KEY = userdata.get('NEBIUS_API_KEY')
else:
print("NOT running in Colab")
from dotenv import load_dotenv
this_dir = os.path.abspath('')
parent_dir = os.path.dirname(this_dir)
sys.path.append (os.path.abspath (parent_dir))
load_dotenv()
NEBIUS_API_KEY = os.getenv('NEBIUS_API_KEY')
## quick hack (not recommended) - you can hardcode the config key here
# NEBIUS_API_KEY = "your_key_here"
if NEBIUS_API_KEY:
print ('✅ NEBIUS_API_KEY found')
os.environ['NEBIUS_API_KEY'] = NEBIUS_API_KEY
else:
raise RuntimeError ('❌ NEBIUS_API_KEY NOT found')
NOT running in Colab ✅ NEBIUS_API_KEY found
3 - Pick a Model¶
- Go to models tab in tokenfactory.nebius.com
- Copy the model name. For example
openai/gpt-oss-20b-Instruct-2507

4 - Run the Model¶
In [3]:
%%time
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.tokenfactory.nebius.com/v1/",
api_key=NEBIUS_API_KEY,
)
completion = client.chat.completions.create(
model = "openai/gpt-oss-20b-Instruct-2507",
messages=[
{
"role": "user",
"content": "What is the capital of France?"
}
],
temperature=0.6
)
print ('----model answer -----')
print (completion.choices[0].message.content)
print ('\n----- full response ----')
print(completion.to_json())
print ('---------')
----model answer -----
The capital of France is Paris.
----- full response ----
{
"id": "chatcmpl-50f7bcb284ed40cbb45d67225acbb97d",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "The capital of France is Paris.",
"refusal": null,
"role": "assistant",
"annotations": null,
"audio": null,
"function_call": null,
"tool_calls": [],
"reasoning_content": null
},
"stop_reason": null,
"token_ids": null
}
],
"created": 1762232990,
"model": "openai/gpt-oss-20b-Instruct-2507",
"object": "chat.completion",
"service_tier": null,
"system_fingerprint": null,
"usage": {
"completion_tokens": 8,
"prompt_tokens": 15,
"total_tokens": 23,
"completion_tokens_details": null,
"prompt_tokens_details": null
},
"prompt_logprobs": null,
"prompt_token_ids": null,
"kv_transfer_params": null
}
---------
CPU times: user 235 ms, sys: 62.5 ms, total: 298 ms
Wall time: 1.94 s
5 - Try a Reasoning Model¶
In [5]:
%%time
# try another model
completion = client.chat.completions.create(
model = "openai/gpt-oss-20b-Thinking-2507", # this is a reasoning model
messages=[
{
"role": "user",
"content": """What are your capabilities?"""
}
],
temperature=0.6
)
print (completion.choices[0].message.content)
print ('----------')
I'm Qwen3, the latest large language model developed by Tongyi Lab! Here's a quick overview of what I can do for you: --- ### ✨ **Key Capabilities** 1. **Language Understanding & Generation** - Answer questions (e.g., "Explain quantum computing simply") - Summarize long documents or articles - Write stories, emails, scripts, and creative content - *Example:* "Write a 200-word poem about autumn in the style of Shakespeare." 2. **Code Writing & Debugging** - Generate code in **Python, JavaScript, Java, C++, SQL**, and more - Fix errors and optimize existing code - *Example:* "Debug this Python function that’s causing a `KeyError`." 3. **Multilingual Support** - Translate between **100+ languages** (e.g., Chinese ↔ English, Spanish ↔ French) - Understand and respond in your preferred language - *Example:* "Translate this paragraph into Japanese." 4. **Long-Context Processing** - Handle inputs up to **128,000 tokens** (e.g., process a full book or lengthy research paper). 5. **Logical Reasoning & Math** - Solve puzzles, math problems, and logical sequences - *Example:* "Solve this equation: 3x + 5 = 20." 6. **Role-Playing & Creative Tasks** - Simulate characters or scenarios (e.g., "Act as a travel guide for Paris") - Generate business plans, marketing copy, or technical documentation. --- ### 💡 **Why It Matters** - **No internet access**: I rely on my training data (up to 2024), so I can’t fetch real-time info—but I’m great for *structured* tasks like writing, coding, and analysis. - **Efficient & Cost-Effective**: Optimized for speed and affordability, whether you’re a developer, student, or business user. --- **Try me out!** Ask anything like: - "Write a Python script to scrape a website." - "Explain blockchain to a 10-year-old." - "Help me draft a resignation email." Let me know what you’d like to tackle! 😊 ---------- CPU times: user 5.25 ms, sys: 2.43 ms, total: 7.68 ms Wall time: 35.1 s
6 - Try Your Queries¶
Go ahead and experiment with your queries. Here are some to get you started.
Write python code to read a csv file
write a haiku about cats