Magisterium AI

Thực Hiện Yêu Cầu API Đầu Tiên Của Bạn

Thiết Lập Khóa API Của Bạn

Cấu hình khóa API của bạn dưới dạng biến môi trường. Cách tiếp cận này giúp đơn giản hóa việc sử dụng API của bạn bằng cách loại bỏ nhu cầu phải bao gồm khóa API trong mỗi yêu cầu. Hơn nữa, nó tăng cường bảo mật bằng cách giảm thiểu rủi ro vô tình bao gồm khóa API trong mã nguồn của bạn.

Trong terminal mà bạn chọn:

bash
export MAGISTERIUM_API_KEY=<your-api-key-here>

Hoặc, trong tệp .env của dự án của bạn:

bash
MAGISTERIUM_API_KEY=<your-api-key-here>

Thay thế <your-api-key-here> bằng khóa API thực tế của bạn nhận được từ API Console.

Thực Hiện Yêu Cầu Đầu Tiên Của Bạn

Thực thi lệnh curl này trong terminal mà bạn chọn:

bash
curl -X POST https://www.magisterium.com/api/v1/chat/completions \
    -H "Authorization: Bearer $MAGISTERIUM_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
    "model": "magisterium-1",
    "messages": [
        {
        "role": "user",
        "content": "What is the Magisterium?"
        }
    ]
    }'
typescript
// npm install magisterium
import Magisterium from "magisterium";

const magisterium = new Magisterium({
  apiKey: process.env.MAGISTERIUM_API_KEY,
});

export async function getMagisteriumAnswer() {
  const results = await magisterium.chat.completions.create({
    model: "magisterium-1",
    messages: [
      {
        role: "user",
        content: "What is the Magisterium?",
      },
    ]
  });

  // Xử lý phản hồi
  console.log(results.choices[0].message);
}
python
import requests
import os

api_key = os.getenv("MAGISTERIUM_API_KEY")
url = "https://www.magisterium.com/api/v1/chat/completions"
headers = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json",
}
data = {
    "model": "magisterium-1",
    "messages": [
    {
        "role": "user",
        "content": "What is the Magisterium?",
    }
    ],
    "stream": False
}

chat_completion = requests.post(url, headers=headers, json=data)
print(chat_completion.json()["choices"][0]["message"])