Code Examples
Ready-to-use examples in your preferred language. All examples use the OpenAI SDK with our base URL.
💡 Before you begin
Replace YOUR_API_KEY with your actual API key. Get one free with code DEVSTARTER20.
Chat Completions
The most common use case: send a conversation and get a response.
python
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://bb71d3f3506709ed-101-126-19-34.serveousercontent.com/v1"
)
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V4-Pro",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain quantum computing in simple terms."}
],
temperature=0.7,
max_tokens=500
)
print(response.choices[0].message.content)
print(f"Tokens used: {response.usage.total_tokens}")
javascript
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'YOUR_API_KEY',
baseURL: 'https://bb71d3f3506709ed-101-126-19-34.serveousercontent.com/v1'
});
async function main() {
const response = await client.chat.completions.create({
model: 'deepseek-ai/DeepSeek-V4-Pro',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Explain quantum computing in simple terms.' }
],
temperature: 0.7,
max_tokens: 500
});
console.log(response.choices[0].message.content);
console.log(`Tokens used: ${response.usage.total_tokens}`);
}
main();
java
// Add to pom.xml: com.openai:openai-java:0.8.0
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.*;
public class ChatExample {
public static void main(String[] args) {
OpenAIClient client = OpenAIOkHttpClient.builder()
.apiKey("YOUR_API_KEY")
.baseUrl("https://bb71d3f3506709ed-101-126-19-34.serveousercontent.com/v1")
.build();
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
.model("deepseek-ai/DeepSeek-V4-Pro")
.addSystemMessage("You are a helpful assistant.")
.addUserMessage("Explain quantum computing in simple terms.")
.temperature(0.7)
.maxTokens(500)
.build();
ChatCompletion completion = client.chat().completions().create(params);
System.out.println(completion.choices().get(0).message().content());
}
}
go
package main
import (
"context"
"fmt"
"log"
openai "github.com/sashabaranov/go-openai"
)
func main() {
config := openai.DefaultConfig("YOUR_API_KEY")
config.BaseURL = "https://bb71d3f3506709ed-101-126-19-34.serveousercontent.com/v1"
client := openai.NewClientWithConfig(config)
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: "deepseek-ai/DeepSeek-V4-Pro",
Messages: []openai.ChatCompletionMessage{
{Role: openai.ChatMessageRoleSystem, Content: "You are a helpful assistant."},
{Role: openai.ChatMessageRoleUser, Content: "Explain quantum computing in simple terms."},
},
Temperature: 0.7,
MaxTokens: 500,
},
)
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.Choices[0].Message.Content)
}
bash
curl https://bb71d3f3506709ed-101-126-19-34.serveousercontent.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-ai/DeepSeek-V4-Pro",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain quantum computing in simple terms."}
],
"temperature": 0.7,
"max_tokens": 500
}'
Streaming Responses
Get real-time token-by-token responses using Server-Sent Events.
python
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://bb71d3f3506709ed-101-126-19-34.serveousercontent.com/v1"
)
stream = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V4-Pro",
messages=[{"role": "user", "content": "Write a short poem about AI."}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="", flush=True)
print() # Final newline
javascript
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'YOUR_API_KEY',
baseURL: 'https://bb71d3f3506709ed-101-126-19-34.serveousercontent.com/v1'
});
async function main() {
const stream = await client.chat.completions.create({
model: 'deepseek-ai/DeepSeek-V4-Pro',
messages: [{ role: 'user', content: 'Write a short poem about AI.' }],
stream: true
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}
console.log();
}
main();
bash
curl https://bb71d3f3506709ed-101-126-19-34.serveousercontent.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-ai/DeepSeek-V4-Pro",
"messages": [{"role": "user", "content": "Write a short poem about AI."}],
"stream": true
}'
Embeddings
Generate vector embeddings for semantic search, RAG, and clustering.
python
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://bb71d3f3506709ed-101-126-19-34.serveousercontent.com/v1"
)
# Single text embedding
response = client.embeddings.create(
model="Qwen/Qwen3-Embedding-8B",
input="The quick brown fox jumps over the lazy dog"
)
embedding = response.data[0].embedding
print(f"Dimensions: {len(embedding)}")
print(f"First 5 values: {embedding[:5]}")
# Batch embeddings
response = client.embeddings.create(
model="Qwen/Qwen3-Embedding-8B",
input=[
"Machine learning is a subset of AI",
"Deep learning uses neural networks",
"Natural language processing handles text"
]
)
for item in response.data:
print(f"[{item.index}] dims={len(item.embedding)}")
bash
curl https://bb71d3f3506709ed-101-126-19-34.serveousercontent.com/v1/embeddings \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen3-Embedding-8B",
"input": "The quick brown fox jumps over the lazy dog"
}'
Vision (Image Understanding)
Analyze images with Qwen-VL models. Send image URLs or base64-encoded images.
python
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://bb71d3f3506709ed-101-126-19-34.serveousercontent.com/v1"
)
response = client.chat.completions.create(
model="qwen-vl-max",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Describe what you see in this image."},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image.jpg"
}
}
]
}
]
)
print(response.choices[0].message.content)
Function Calling
Enable the model to call external tools and functions.
python
import json
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://bb71d3f3506709ed-101-126-19-34.serveousercontent.com/v1"
)
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location"]
}
}
}
]
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V4-Pro",
messages=[{"role": "user", "content": "What's the weather in Shanghai?"}],
tools=tools,
tool_choice="auto"
)
message = response.choices[0].message
if message.tool_calls:
for call in message.tool_calls:
args = json.loads(call.function.arguments)
print(f"Calling: {call.function.name}({args})")
# Execute your function here and return result
Multi-Turn Conversation
Maintain context across multiple exchanges by including previous messages.
python
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://bb71d3f3506709ed-101-126-19-34.serveousercontent.com/v1"
)
messages = [
{"role": "system", "content": "You are a Python programming tutor."}
]
# Simulate a conversation
questions = [
"What is a list comprehension?",
"Can you show an example?",
"How is it different from map()?"
]
for question in questions:
messages.append({"role": "user", "content": question})
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V4-Pro",
messages=messages
)
reply = response.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
print(f"Q: {question}")
print(f"A: {reply}\n")
print("---")