Trans_for_doctors / CURL_EXAMPLES.md
Mintik24's picture
🎉 Полный рефакторинг проекта Medical Transcriber
e275025

Примеры Curl команд для OpenRouter API

Установка переменных окружения

# Установите ваш API ключ
export OPENROUTER_API_KEY="sk-or-v1-..."

# Опционально: выберите модель (по умолчанию gemini-3-flash-preview)
export OPENROUTER_MODEL="google/gemini-3-flash-preview"

1. Базовый запрос

curl https://openrouter.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -d '{
    "model": "google/gemini-3-flash-preview",
    "messages": [
      {
        "role": "user",
        "content": "Hello, how are you?"
      }
    ]
  }'

2. Запрос с reasoning mode (для Gemini)

curl https://openrouter.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -d '{
    "model": "google/gemini-3-flash-preview",
    "messages": [
      {
        "role": "user",
        "content": "How many r'\''s are in the word strawberry?"
      }
    ],
    "reasoning": {
      "enabled": true
    }
  }'

3. Медицинская коррекция (простая)

curl https://openrouter.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -d '{
    "model": "google/gemini-3-flash-preview",
    "messages": [
      {
        "role": "system",
        "content": "Ты медицинский помощник. Исправь ошибки в медицинской транскрипции."
      },
      {
        "role": "user",
        "content": "Пациент жалуется на боль в животе, тошнота и рвота"
      }
    ],
    "temperature": 0.1,
    "reasoning": {
      "enabled": true
    }
  }'

4. Медицинская коррекция с терминами

curl https://openrouter.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -d '{
    "model": "google/gemini-3-flash-preview",
    "messages": [
      {
        "role": "system",
        "content": "Ты медицинский помощник. Исправь ошибки в транскрипции, используя правильную медицинскую терминологию.\n\nМедицинские термины: аппендицит, гастрит, энцефалопатия, кардиомиопатия, артериальная гипертензия, сахарный диабет"
      },
      {
        "role": "user",
        "content": "У пациента подозрение на апендицит и гастрит. Также отмечается повышенное давление."
      }
    ],
    "temperature": 0.1,
    "max_tokens": 2000,
    "reasoning": {
      "enabled": true
    }
  }'

5. Запрос с дополнительными заголовками

curl https://openrouter.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -H "HTTP-Referer: http://localhost" \
  -H "X-Title: Trans_for_doctors" \
  -d '{
    "model": "google/gemini-3-flash-preview",
    "messages": [
      {
        "role": "user",
        "content": "Привет!"
      }
    ]
  }'

6. Использование другой модели (GPT-4o)

curl https://openrouter.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -d '{
    "model": "openai/gpt-4o",
    "messages": [
      {
        "role": "system",
        "content": "Ты медицинский эксперт. Исправь транскрипцию."
      },
      {
        "role": "user",
        "content": "Пациент жалуется на боль в животе"
      }
    ],
    "temperature": 0.1
  }'

7. Использование Claude

curl https://openrouter.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -d '{
    "model": "anthropic/claude-3.5-sonnet",
    "messages": [
      {
        "role": "user",
        "content": "Исправь медицинскую транскрипцию: Пациент с диагнозом апендицит"
      }
    ],
    "temperature": 0.1
  }'

8. Форматированный вывод (с jq)

curl -s https://openrouter.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -d '{
    "model": "google/gemini-3-flash-preview",
    "messages": [
      {
        "role": "user",
        "content": "Hello!"
      }
    ]
  }' | jq '.choices[0].message.content'

9. Сохранение ответа в файл

curl https://openrouter.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -d '{
    "model": "google/gemini-3-flash-preview",
    "messages": [
      {
        "role": "user",
        "content": "Исправь: Пациент жалуется на боль"
      }
    ]
  }' > response.json

10. Batch обработка (скрипт)

#!/bin/bash

TEXTS=(
    "Пациент жалуется на боль в животе"
    "Диагноз апендицит"
    "Высокая температура и кашель"
)

for text in "${TEXTS[@]}"; do
    echo "Обработка: $text"
    
    curl -s https://openrouter.ai/api/v1/chat/completions \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer $OPENROUTER_API_KEY" \
      -d "{
        \"model\": \"google/gemini-3-flash-preview\",
        \"messages\": [
          {
            \"role\": \"system\",
            \"content\": \"Исправь медицинский текст\"
          },
          {
            \"role\": \"user\",
            \"content\": \"$text\"
          }
        ],
        \"temperature\": 0.1
      }" | jq -r '.choices[0].message.content'
    
    echo "---"
done

11. Проверка статуса API

curl -s https://openrouter.ai/api/v1/models \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" | jq

12. Получение информации о модели

curl -s https://openrouter.ai/api/v1/models \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" | \
  jq '.data[] | select(.id == "google/gemini-3-flash-preview")'

13. Multiline текст (heredoc)

curl https://openrouter.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -d @- <<EOF
{
  "model": "google/gemini-3-flash-preview",
  "messages": [
    {
      "role": "system",
      "content": "Ты медицинский помощник"
    },
    {
      "role": "user",
      "content": "Пациент жалуется на:\n- боль в животе\n- тошноту\n- рвоту"
    }
  ],
  "temperature": 0.1
}
EOF

14. С таймаутом

curl --max-time 30 https://openrouter.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -d '{
    "model": "google/gemini-3-flash-preview",
    "messages": [
      {
        "role": "user",
        "content": "Quick test"
      }
    ]
  }'

15. Подробный вывод (verbose)

curl -v https://openrouter.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -d '{
    "model": "google/gemini-3-flash-preview",
    "messages": [
      {
        "role": "user",
        "content": "Test"
      }
    ]
  }'

Использование готового скрипта

Проект включает готовый bash скрипт для тестирования:

# Базовое использование
./test_openrouter_curl.sh

# С кастомным текстом
./test_openrouter_curl.sh "Ваш текст для обработки"

# С переменной окружения для модели
OPENROUTER_MODEL="openai/gpt-4o" ./test_openrouter_curl.sh "Текст"

Обработка ошибок

response=$(curl -s -w "\n%{http_code}" https://openrouter.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -d '{
    "model": "google/gemini-3-flash-preview",
    "messages": [{"role": "user", "content": "Test"}]
  }')

http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | head -n-1)

if [ "$http_code" -eq 200 ]; then
    echo "Success: $body"
else
    echo "Error ($http_code): $body"
fi

Полезные советы

  1. Сохраните API ключ в переменной окружения:

    echo "export OPENROUTER_API_KEY='your-key'" >> ~/.bashrc
    source ~/.bashrc
    
  2. Установите jq для форматирования JSON:

    # Ubuntu/Debian
    sudo apt-get install jq
    
    # macOS
    brew install jq
    
  3. Используйте файлы для больших промптов:

    curl https://openrouter.ai/api/v1/chat/completions \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer $OPENROUTER_API_KEY" \
      -d @request.json
    
  4. Логируйте запросы для отладки:

    curl https://openrouter.ai/api/v1/chat/completions \
      -H "Authorization: Bearer $OPENROUTER_API_KEY" \
      -d '...' | tee response.log
    

Дополнительные ресурсы