Service API Integration · 05

SDK Integration Examples

4.1 Python (official openai SDK)

bash
pip install openai
python
import os
from openai import OpenAI

client = OpenAI(
    base_url=os.environ["JF_BASE_URL"],   # Base URL from your deployer
    api_key=os.environ["JF_API_KEY"],     # sk-svc-... from your deployer
)

# Streaming
stream = client.chat.completions.create(
    model="ignored",                                # required field, but server ignores it
    messages=[{"role": "user", "content": "Explain deep learning in 5 sentences"}],
    stream=True,
    extra_body={"conversation_id": None},           # None for first turn; fill with returned id later
)
for chunk in stream:
    delta = chunk.choices[0].delta.content
    if delta:
        print(delta, end="", flush=True)

# Non-streaming + continue session
resp = client.chat.completions.create(
    model="x",
    messages=[{"role": "user", "content": "My name is Alex"}],
    stream=False,
)
conv_id = resp.model_extra.get("conversation_id")   # ⚠️ top-level extension field

resp = client.chat.completions.create(
    model="x",
    messages=[{"role": "user", "content": "What's my name?"}],
    stream=False,
    extra_body={"conversation_id": conv_id},
)
print(resp.choices[0].message.content)

4.2 Node.js (openai npm)

bash
npm i openai
javascript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: process.env.JF_BASE_URL,
  apiKey: process.env.JF_API_KEY,
});

// Streaming
const stream = await client.chat.completions.create({
  model: "ignored",
  messages: [{ role: "user", content: "Explain deep learning in 5 sentences" }],
  stream: true,
  // @ts-expect-error extension field
  conversation_id: null,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || "");
}

4.3 curl (streaming)

bash
curl -N -sS "$JF_BASE_URL/chat/completions" \
  -H "Authorization: Bearer $JF_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "stream": true,
    "messages": [{"role": "user", "content": "Explain deep learning in 5 sentences"}]
  }'

-N disables curl's own buffering so you see data: ... chunks as they arrive.

4.4 LangChain / LlamaIndex / Cursor / any OpenAI-compatible client

Point OPENAI_API_BASE or the client's base URL to your deployer's $JF_BASE_URL, set OPENAI_API_KEY to sk-svc-..., and put anything in the model field (it's ignored). Your app should persist conversation_id for session continuation.