Get API key

OpenAI SDK

View as Markdown

Use the official OpenAI Python or JavaScript client. Set base_url and api_key.

Point any official OpenAI client at PrivateMind by setting its base_url and api_key. The rest of your code stays the same.

Python

Python
from openai import OpenAI

client = OpenAI(
    base_url="https://api.privatemind.com/v1",
    api_key="PMIND...:abcdef...",
)

resp = client.chat.completions.create(
    model="<model-id>",
    messages=[{"role": "user", "content": "Hello"}],
)
print(resp.choices[0].message.content)

TypeScript / Node.js

TypeScript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.privatemind.com/v1",
  apiKey: "PMIND...:abcdef...",
});

const resp = await client.chat.completions.create({
  model: "<model-id>",
  messages: [{ role: "user", content: "Hello" }],
});
console.log(resp.choices[0].message.content);

Responses API

The same clients call the Responses API through client.responses. PrivateMind stores no responses, so leave out previous_response_id and send the whole conversation in input.

Python
from openai import OpenAI

client = OpenAI(
    base_url="https://api.privatemind.com/v1",
    api_key="PMIND...:abcdef...",
)

resp = client.responses.create(
    model="<model-id>",
    input="Hello",
)
print(resp.output_text)

Streaming from Node.js:

TypeScript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.privatemind.com/v1",
  apiKey: "PMIND...:abcdef...",
});

const stream = client.responses.stream({
  model: "<model-id>",
  input: "Write a haiku about TCP.",
});
for await (const event of stream) {
  if (event.type === "response.output_text.delta") process.stdout.write(event.delta);
}

In Python, client.responses.parse with a pydantic model works too: see Structured output.

OpenAI Agents SDK

The OpenAI Agents SDK runs on the Responses API. Give it a client pointed at PrivateMind through OpenAIResponsesModel:

Python
from agents import Agent, OpenAIResponsesModel, Runner, function_tool, set_tracing_disabled
from openai import AsyncOpenAI

client = AsyncOpenAI(
    base_url="https://api.privatemind.com/v1",
    api_key="PMIND...:abcdef...",
)
set_tracing_disabled(True)


@function_tool
def get_weather(city: str) -> str:
    """Get current weather for a city."""
    return "18C, overcast"


agent = Agent(
    name="Assistant",
    instructions="You are a concise assistant.",
    model=OpenAIResponsesModel(model="<model-id>", openai_client=client),
    tools=[get_weather],
)
result = Runner.run_sync(agent, "What is the weather in Paris?")
print(result.final_output)

Tools and output_type work together: if the agent's final output doesn't match output_type, it is generated again with the format enforced (see With tools on offer).

By default the SDK sends a trace of every run, model inputs and outputs included, to OpenAI whenever it has a key to send it with. It uses OPENAI_API_KEY, or a key or client you passed to set_default_openai_key or set_default_openai_client. If you pass your PrivateMind key or client to either function, the SDK sends that key to OpenAI too. Keep set_tracing_disabled(True).

Go

Go
import (
  "context"
  "fmt"
  openai "github.com/openai/openai-go"
  "github.com/openai/openai-go/option"
)

client := openai.NewClient(
  option.WithBaseURL("https://api.privatemind.com/v1"),
  option.WithAPIKey("PMIND...:abcdef..."),
)

resp, _ := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
  Model: openai.F("<model-id>"),
  Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
    openai.UserMessage("Hello"),
  }),
})
fmt.Println(resp.Choices[0].Message.Content)

LangChain

Python
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    base_url="https://api.privatemind.com/v1",
    api_key="PMIND...:abcdef...",
    model="<model-id>",
)

print(llm.invoke("Hello").content)

LlamaIndex

Python
from llama_index.llms.openai_like import OpenAILike

llm = OpenAILike(
    api_base="https://api.privatemind.com/v1",
    api_key="PMIND...:abcdef...",
    model="<model-id>",
    is_chat_model=True,
)

Use OpenAILike because PrivateMind speaks the OpenAI wire format from a host other than api.openai.com.

What works, what doesn't

Anything that calls /v1/chat/completions, /v1/responses, /v1/embeddings, /v1/models, /v1/images/generations or /v1/audio/* works. The Files and Batch APIs are there too, so client.files.* and client.batches.* work unchanged.

SDK features that call endpoints PrivateMind doesn't have, such as the Assistants API and fine-tuning, return 404. To fine-tune a model on your org's GPUs, use pm.finetune in the PrivateMind SDK. It takes OpenAI's chat and preference file formats.

Training jobs, image generation and batch work also have a native SDK: the PrivateMind Python SDK.

Code written for the Anthropic SDKs works too, through the Messages API: see Anthropic SDK.

Where next