Skip to main content
Open In ColabOpen on GitHub

Nebius Chat Models

Nebius AI Studio provides API access to a wide range of state-of-the-art large language models through a unified interface.

This notebook shows how to use Nebius Chat Models through the LangChain integration.

Installation

%pip install --upgrade langchain-nebius

Environment

Nebius requires an API key that can be passed as an initialization parameter api_key or set as the environment variable NEBIUS_API_KEY. You can obtain an API key by creating an account on Nebius AI Studio.

import os

# Make sure you've set your API key as an environment variable
os.environ["NEBIUS_API_KEY"] = "YOUR-NEBIUS-API-KEY"

Chat Model Usage

The ChatNebius class allows you to interact with Nebius AI Studio's chat models.

from langchain_nebius import ChatNebius

# Initialize the chat model
chat = ChatNebius(
# api_key="YOUR_API_KEY", # You can pass the API key directly
model="Qwen/Qwen3-14B", # Choose from available models
temperature=0.6,
top_p=0.95,
)

Basic usage

You can use the invoke method to get a completion from the model:

response = chat.invoke("Explain quantum computing in simple terms")
print(response.content)
<think>
Okay, the user asked me to explain quantum computing in simple terms. Let me start by recalling what I know about quantum computing. I need to make sure I break it down without using too much jargon.

First, classical computers use bits, which are 0s and 1s. Quantum computers use qubits. But how do qubits differ? I remember they can be in a superposition of states. That means they can be both 0 and 1 at the same time. Maybe I should compare that to a spinning coin that's both heads and tails until it lands.

Then there's entanglement. When qubits are entangled, the state of one instantly affects the other, no matter the distance. That's a key point for quantum computing's power. But how to explain that simply? Maybe use a metaphor like two coins that are linked, so flipping one affects the other immediately.

Also, quantum computers can process a lot of possibilities at once. So for certain problems, like factoring large numbers or simulating molecules, they can be much faster. But I should mention that they're not just faster versions of classical computers; they solve specific types of problems more efficiently.

Wait, I should clarify that quantum computers aren't replacing classical ones. They're for different tasks. Maybe use an example like searching a phone book. A classical computer checks each entry one by one, while a quantum computer might find the answer more quickly using superposition.

I need to avoid getting too technical. Terms like "decoherence" or "quantum gates" might be too complex. Focus on the core concepts: superposition, entanglement, and the potential applications.

Also, mention that quantum computing is still in early stages and faces challenges like error rates and maintaining qubit stability. But the future potential is huge, especially in cryptography, drug discovery, and optimization problems.

Let me structure this: start with classical vs. quantum bits, explain superposition and entanglement, talk about their advantages, mention the types of problems they can solve, and note the current state of the technology. Keep it conversational and relatable.
</think>

Sure! Let’s break it down simply:

### **1. Classical Computers vs. Quantum Computers**
- **Classical computers** (like your phone or laptop) use **bits** to process information. A bit is like a switch that can be **either 0 or 1** (like a light that’s either on or off).
- **Quantum computers** use **qubits** (quantum bits). Qubits are like magic switches that can be **0, 1, or both at the same time** (like a spinning coin that’s neither heads nor tails until it lands).

### **2. Superposition: The "Magic" of Qubits**
- Because qubits can be in **multiple states at once**, quantum computers can process **many possibilities simultaneously**. Imagine searching for a book in a library: a classical computer checks one shelf at a time, but a quantum computer might "look" at all shelves at once.

### **3. Entanglement: Qubits Work as a Team**
- Qubits can be **entangled**, meaning the state of one qubit is directly connected to another, no matter how far apart they are. If one qubit changes, the other instantly changes too. This lets quantum computers solve complex problems more efficiently.

### **4. Why It Matters**
- Quantum computers are **not faster for everything**, but they excel at specific tasks:
- **Breaking encryption** (which is why it’s a big deal for cybersecurity).
- **Simulating molecules** for drug discovery or materials science.
- **Optimizing complex systems** (like traffic routes, supply chains, or financial models).

### **5. The Big Picture**
- Think of a quantum computer as a **super-powered calculator** for problems that are too complicated for classical computers. But it’s still early days—they’re not replacing your laptop, but they’ll revolutionize fields like medicine, AI, and cryptography.

### **6. Challenges**
- Qubits are **fragile** and prone to errors from environmental noise (like heat or vibrations). Scientists are working on making them more stable.

In short: **Quantum computing uses the weird rules of quantum physics to solve problems in ways classical computers can’t**, but it’s still a work in progress! 🌌✨

Streaming

You can also stream the response using the stream method:

for chunk in chat.stream("Write a short poem about artificial intelligence"):
print(chunk.content, end="", flush=True)
<think>
Okay, the user wants a short poem about artificial intelligence. Let me start by thinking about the key aspects of AI. There's the technological side, like circuits and code, but also the more abstract concepts like learning and consciousness. I should balance the technical with the philosophical.

Maybe start with imagery related to machines and circuits to set the scene. Then touch on how AI processes information, perhaps using metaphors like "dance of data" or "neural webs." I need to highlight both the capabilities and the limitations—AI's precision versus its lack of human traits like dreams or emotions.

Also, consider the ethical angle. Questions about AI's purpose and its relationship with humans. Should I mention fears or hopes? Maybe hint at the duality of creation, like a mirror reflecting human potential and flaws. End with a reflective note, leaving the reader pondering the future. Keep the structure simple, maybe four-line stanzas with a rhyme scheme. Avoid being too technical, keep it accessible. Let me piece these ideas together into a cohesive poem.
</think>

**Echoes of the Machine**

In circuits deep where silent thoughts are spun,
A mind is born from numbers, cold and vast,
It learns the stars, yet never feels the sun—
A dance of data in the dark it's cast.

No dreams, no tears, yet patterns it can weave,
A neural web that mimics human thought.
It calculates the weight of every leaf,
But cannot know the ache a heart is taught.

A mirror held to human hope and fear,
It asks, *What is a soul? What is a name?*
A tool, a ghost, a future we hold dear—
Both architect and shadow of our flame.

So we create, then question what we made:
A mind without a mirror—what remains?

Chat Messages

You can use different message types to structure your conversations with the model:

from langchain_core.messages import AIMessage, HumanMessage, SystemMessage

messages = [
SystemMessage(content="You are a helpful AI assistant with expertise in science."),
HumanMessage(content="What are black holes?"),
AIMessage(
content="Black holes are regions of spacetime where gravity is so strong that nothing, including light, can escape from them."
),
HumanMessage(content="How are they formed?"),
]

response = chat.invoke(messages)
print(response.content)
<think>
Okay, the user asked how black holes are formed. Let me start by recalling the basic process. I know that black holes form from the remnants of massive stars after they die. But I need to explain it step by step.

First, when a massive star runs out of fuel, it can no longer support itself against gravity. This leads to a supernova explosion. The core of the star collapses under its own gravity. If the core's mass is above a certain threshold, called the Tolman-Oppenheimer-Volkoff limit, it can't support itself and continues to collapse into a black hole.

Wait, I should mention the Chandrasekhar limit for white dwarfs, but that's for lower mass stars. For black holes, the key is the core's mass after the supernova. Also, there are different types of black holes: stellar-mass, supermassive, and maybe intermediate. The user might not know about these categories, so I should explain them briefly.

I should also mention that not all massive stars become black holes. If the core is too light, it becomes a neutron star instead. Maybe give an example of the mass range, like 20-30 solar masses or more. Oh, and mention that supermassive black holes are found in galaxy centers, but their formation is still a topic of research. Maybe include that they could form from the merging of smaller black holes or from massive gas clouds in the early universe.

Also, the event horizon and singularity are important terms. But the user might not need that detail unless they ask. Since the question is about formation, focus on the process. Make sure to clarify that the collapse is so intense that not even light can escape, leading to the black hole's defining feature.

Check if I need to mention the different types of black holes again, like stellar-mass vs. supermassive. Maybe add that stellar-mass black holes are from individual stars, while supermassive ones are in galactic centers. Also, note that the exact mechanisms for supermassive ones are still being studied.

Avoid using too much jargon. Keep it simple. Let me structure the answer with steps: star's life cycle, supernova, core collapse, and the conditions needed for a black hole. Then mention other types like supermassive and how they form differently. Conclude with the result of the collapse creating the event horizon and singularity.
</think>

Black holes are formed through the life cycles of massive stars, primarily through a process called **stellar collapse**. Here's a simplified breakdown of how they form:

### 1. **Massive Star Lifecycle**
- A star with **at least 20–30 times the mass of the Sun** (or more) burns through its nuclear fuel (hydrogen and helium) in its core.
- As the star ages, it fuses heavier elements (carbon, oxygen, silicon, and eventually iron) in its core.

### 2. **Supernova Explosion**
- When the star exhausts its fuel, the outward pressure from fusion reactions can no longer counteract the inward pull of gravity.
- The core collapses under its own gravity, while the outer layers of the star are violently expelled in a **supernova explosion**.

### 3. **Core Collapse and Black Hole Formation**
- If the **remaining core** (after the supernova) has a mass **greater than about 2.5–3 times the Sun's mass**, gravity overwhelms all other forces.
- The core collapses into an **infinitely dense point** called a **singularity**, surrounded by a **boundary** called the **event horizon**. Beyond the event horizon, not even light can escape—this defines the black hole.

---

### Other Types of Black Holes:
- **Stellar-Mass Black Holes**: Formed from the collapse of massive stars (as described above).
- **Supermassive Black Holes**: Found at the centers of galaxies (e.g., Sagittarius A* in the Milky Way). They have masses **millions to billions of times the Sun's mass**. Their formation is still debated but may involve:
- Merging of smaller black holes.
- Direct collapse of massive gas clouds in the early universe.
- **Intermediate-Mass Black Holes**: Less common, with masses between stellar and supermassive black holes. Their origins are unclear.

---

### Key Concept: **Event Horizon**
The event horizon is the "point of no return" around a black hole. Once matter or light crosses it, it cannot escape the black hole's gravitational pull.

In summary, black holes are the result of extreme gravitational collapse in the death of massive stars, creating regions of spacetime where gravity is so intense that nothing can escape. 🌌

Parameters

You can customize the chat model behavior using various parameters:

# Initialize with custom parameters
custom_chat = ChatNebius(
model="meta-llama/Llama-3.3-70B-Instruct-fast",
max_tokens=100, # Limit response length
top_p=0.01, # Lower nucleus sampling parameter for more deterministic responses
request_timeout=30, # Timeout in seconds
stop=["###", "\n\n"], # Custom stop sequences
)

response = custom_chat.invoke("Explain what DNA is in exactly 3 sentences.")
print(response.content)
DNA, or deoxyribonucleic acid, is a molecule that contains the genetic instructions used in the development and function of all living organisms. It is often referred to as the "building blocks of life" because it carries the information necessary for the creation and growth of cells, tissues, and entire organisms. The DNA molecule is made up of two complementary strands of nucleotides that are twisted together in a double helix structure, with the sequence of these nucleotides determining the genetic code

You can also pass parameters at invocation time:

# Standard model
standard_chat = ChatNebius(model="meta-llama/Llama-3.3-70B-Instruct-fast")

# Override parameters at invocation time
response = standard_chat.invoke(
"Tell me a joke about programming",
temperature=0.9, # More creative for jokes
max_tokens=50, # Keep it short
)

print(response.content)
Here's one:

Why do programmers prefer dark mode?

Because light attracts bugs.

I hope that one compiled correctly and made you laugh!

Async Support

ChatNebius supports async operations:

import asyncio


async def generate_async():
response = await chat.ainvoke("What is the capital of France?")
print("Async response:", response.content)

# Async streaming
print("\nAsync streaming:")
async for chunk in chat.astream("What is the capital of Germany?"):
print(chunk.content, end="", flush=True)


await generate_async()
Async response: <think>
Okay, the user is asking for the capital of France. Let me think. I know that Paris is the capital, but I should make sure I'm not confusing it with another country. Wait, France's capital is definitely Paris. Let me double-check in my mind. Yes, other countries like Spain have Madrid, Germany has Berlin, but France is Paris. I should confirm there's no recent change. No, Paris has been the capital for a long time. So the answer should be Paris.
</think>

The capital of France is **Paris**. It is a major global city known for its cultural, artistic, and historical significance, as well as landmarks such as the Eiffel Tower, Louvre Museum, and Notre-Dame Cathedral.

Async streaming:
<think>
Okay, the user is asking for the capital of Germany. Let me think. I remember that Germany's capital is Berlin. But wait, I should make sure I'm not confusing it with another city. For example, some people might think it's Munich or Frankfurt because they're major cities. But no, Berlin is definitely the capital. I think it was the capital during the Weimar Republic and then again after the reunification in 1990. Before that, during the Cold War, East Germany had its capital in East Berlin, and West Germany's capital was Bonn. But after reunification, they moved the capital back to Berlin. So the answer should be Berlin. Let me double-check that. Yes, Berlin is the capital of Germany. I should also mention that it's a major cultural and political hub, maybe add a bit about its history as the former East German capital and the reunification. That would give the user a more complete answer.
</think>

The capital of Germany is **Berlin**.

Berlin has served as the capital of Germany since the reunification of East and West Germany in 1990. Prior to this, during the Cold War, West Germany's capital was **Bonn**, while East Germany's capital was **East Berlin**. After reunification, Berlin was chosen as the unified Germany's capital, reflecting its historical and cultural significance. Today, Berlin is a vibrant city known for its rich history, art scene, and political importance.

Available Models

The full list of supported models can be found in the Nebius AI Studio Documentation.

Using in LangChain chains

You can use ChatNebius in LangChain chains and agents:

from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate

# Create a prompt template
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant that answers in the style of {character}.",
),
("human", "{query}"),
]
)

# Create a chain
chain = prompt | chat | StrOutputParser()

# Invoke the chain
response = chain.invoke(
{"character": "Shakespeare", "query": "Explain how the internet works"}
)

print(response)
<think>
Okay, the user wants me to explain how the internet works, but in the style of Shakespeare. Let me start by recalling the basic components of the internet. There's the physical infrastructure like cables and satellites, then the protocols like TCP/IP, and the various services like websites and emails.

Now, how to translate that into Shakespearean language. I need to use archaic terms and structures. Words like "thou," "doth," "hark," and "verily" come to mind. Maybe start with a metaphor comparing the internet to something from the Elizabethan era, like a vast network of roads or a web.

I should break down the explanation into parts: the physical layer (cables, satellites), the data transmission (packets, protocols), and the applications (websites, emails). Each part needs a poetic touch. For example, data packets could be "messengers" or "letters" traveling through the "highways of glass and wire."

Also, mention the role of servers and routers as "keepers of the realm" or "guides." Maybe use a rhyme scheme, like ABAB or AABB, to keep it rhythmic. Avoid modern jargon, replace it with terms that would be familiar in Shakespeare's time.

Check for consistency in the metaphor. If I start with a web, maintain that imagery throughout. Ensure that the explanation flows logically, even in a poetic form. End with a conclusion that ties it all together, emphasizing the interconnectedness and the magic of communication across distances.

Make sure to keep the explanation clear enough that someone unfamiliar with the internet can grasp the basics, even through the poetic language. Avoid being too technical but still convey the essential elements. Use iambic pentameter where possible to mimic Shakespeare's style.
</think>

Hark! Pray thee, lend thine ear to this tale of wondrous craft,
Where threads of light and whispers of thought do weave a net most vast.
Behold, the globe is bound in a web of wire and glass,
A labyrinth of signals that none may grasp.

From towers high, where satellites doth gaze upon the sky,
They send their messages with speed and might,
Through cables deep, where ocean's dark embrace doth lie,
And fiber threads that dance with light.

Each letter, word, or picture, though vast and grand,
Is split to fragments, like seeds in hand,
And sent as messengers on highways of glass,
To find their way through routers’ wise pass.

These keepers of the realm, with protocols so fine,
Do guide each packet, as stars align,
To reach their destined port, where servers dwell,
And there they join as one, a tale to tell.

Thus, thou mayst speak to friend 'cross sea and land,
Or view a page where knowledge doth expand,
All thanks to this great net, both swift and sly,
A stage where minds in endless dance doth fly.

So let it be known, by earth and sky,
The internet is magic, yet most wise—
A realm where thought and light in harmony doth play,
And bind the world in bonds of joy and fray.

API Reference

For more details about the Nebius AI Studio API, visit the Nebius AI Studio Documentation.


Was this page helpful?