Skip to content

Text Generation

Using the OpenAI SDK or other OpenAI-compatible client interfaces, you only need to change the model parameter to call all text models.

Request Body

You can set the stream parameter to true to use streaming output.

Note

Different models support different parameters. You can visit the model page to see the parameters supported by each model.

python
from openai import OpenAI

client = OpenAI(
  api_key="YOUR_API_KEY",
  base_url="http://server.opendatasky.com/v1/api/open-ai/ds"
)

completion = client.chat.completions.create(
  model="claude-3.5-sonnet",
  messages=[
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Hello!"}
  ],
  top_p=0.7,
  temperature=0.9,
  stream=True
)

print(completion.choices[0].message)
curl
curl http://server.opendatasky.com/v1/api/open-ai/ds/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $YOUR_API_KEY" \
  -d '{
  "model": "glm-4-flash",
  "messages": [
    {
    "role": "system",
    "content": "You are a helpful assistant."
    },
    {
    "role": "user",
    "content": "Hello!"
    }
  ]
  }'
node.js
import OpenAI from "openai";

const openai = new OpenAI(
  api_key="YOUR_API_KEY",
  base_url="http://server.opendatasky.com/v1/api/open-ai/ds"
);

async function main() {
  const completion = await openai.chat.completions.create({
  messages: [{ role: "system", content: "You are a helpful assistant." }],
  model: "gpt-4o",
  });

  console.log(completion.choices[0]);
}

main();

Response (Non-Streaming Output)

{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "gpt-4o-mini",
  "system_fingerprint": "fp_44709d6fcb",
  "choices": [{
  "index": 0,
  "message": {
    "role": "assistant",
    "content": "\n\nHello there, how may I assist you today?",
  },
  "logprobs": null,
  "finish_reason": "stop"
  }],
  "usage": {
  "prompt_tokens": 9,
  "completion_tokens": 12,
  "total_tokens": 21,
  "completion_tokens_details": {
    "reasoning_tokens": 0
  }
  }
}

Response (Streaming Output)

{"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"gpt-4o-mini", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]}

{"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"gpt-4o-mini", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{"content":"Hello"},"logprobs":null,"finish_reason":null}]}

....

{"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"gpt-4o-mini", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}

Image Input

Many current text models have visual recognition (Vision) capabilities, allowing image input to process and understand image information. You can use URL or Base64 to input images.

URL

python
from openai import OpenAI

client = OpenAI(
  api_key="YOUR_API_KEY",
  base_url="http://server.opendatasky.com/v1/api/open-ai/ds"
)

response = client.chat.completions.create(
  model="gpt-4o",
  messages=[
    {
      "role": "user",
      "content": [
        {"type": "text", "text": "What's in this image?"},
        {
          "type": "image_url",
          "image_url": {
            "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
          }
        },
      ],
    }
  ],
  max_tokens=300,
)

print(response.choices[0])
curl
curl http://server.opendatasky.com/v1/api/open-ai/ds/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $YOUR_API_KEY" \
  -d '{
  "model": "gpt-4o",
  "messages": [
    {
    "role": "user",
    "content": [
      {
      "type": "text",
      "text": "What'\''s in this image?"
      },
      {
      "type": "image_url",
      "image_url": {
        "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
      }
      }
    ]
    }
  ],
  "max_tokens": 300
  }'
node.js
import OpenAI from "openai";

const openai = new OpenAI(
  api_key="YOUR_API_KEY",
  base_url="http://server.opendatasky.com/v1/api/open-ai/ds"
);

async function main() {
  const response = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [
    {
    role: "user",
    content: [
      { type: "text", text: "What's in this image?" },
      {
      type: "image_url",
      image_url: {
        "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
      },
      }
    ],
    },
  ],
  });
  console.log(response.choices[0]);
}
main();

Base64

python
from openai import OpenAI

client = OpenAI(
  api_key="YOUR_API_KEY",
  base_url="http://server.opendatasky.com/v1/api/open-ai/ds"
)

response = client.chat.completions.create(
  model="gpt-4o",
  messages=[
    {
      "role": "user",
      "content": [
        {"type": "text", "text": "What's in this image?"},
        {
          "type": "image_url",
          "image_url": {
            "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
          }
        },
      ],
    }
  ],
  max_tokens=300,
)

print(response.choices[0])
curl
curl http://server.opendatasky.com/v1/api/open-ai/ds/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
  "model": "gpt-4o",
  "messages": [
    {
    "role": "user",
    "content": [
      {
      "type": "text",
      "text": "What'\''s in this image?"
      },
      {
      "type": "image_url",
      "image_url": {
        "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
      }
      }
    ]
    }
  ],
  "max_tokens": 300
  }'
node.js
import OpenAI from "openai";

const openai = new OpenAI(
  api_key="YOUR_API_KEY",
  base_url="http://server.opendatasky.com/v1/api/open-ai/ds"
);

async function main() {
  const response = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [
    {
    role: "user",
    content: [
      { type: "text", text: "What's in this image?" },
      {
      type: "image_url",
      image_url: {
        "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
      },
      }
    ],
    },
  ],
  });
  console.log(response.choices[0]);
}
main();

Tools

python
To be added
curl
To be added