modelslab-chat-generation

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

ModelsLab Chat/LLM Generation

ModelsLab 对话/大语言模型生成

Send chat completions to 60+ LLM models through a single OpenAI-compatible endpoint.
通过一个兼容OpenAI的端点,向60余种大语言模型(LLM)发送对话补全请求。

When to Use This Skill

何时使用该功能

  • Chat with AI models (DeepSeek, Llama, Gemini, Qwen, Mistral)
  • Build conversational AI applications
  • Generate text completions with system prompts
  • Use function/tool calling with LLMs
  • Stream responses for real-time output
  • Get structured JSON responses
  • 与AI模型对话(DeepSeek、Llama、Gemini、Qwen、Mistral)
  • 构建对话式AI应用
  • 结合系统提示词生成文本补全内容
  • 为大语言模型(LLM)启用函数/工具调用
  • 流式响应实现实时输出
  • 获取结构化JSON响应

API Endpoint

API端点

Chat Completions:
POST https://modelslab.com/api/v7/llm/chat/completions
The endpoint follows the OpenAI chat completions format, making it easy to switch from OpenAI or use the OpenAI SDK.
Chat Completions:
POST https://modelslab.com/api/v7/llm/chat/completions
该端点遵循OpenAI对话补全格式,方便从OpenAI切换或直接使用OpenAI SDK。

Quick Start

快速开始

python
import requests

def chat(message, api_key, model="meta-llama-3-8B-instruct"):
    """Send a chat completion request.

    Args:
        message: The user message
        api_key: Your ModelsLab API key
        model: LLM model ID (use modelslab models search --feature llmaster to find models)
    """
    response = requests.post(
        "https://modelslab.com/api/v7/llm/chat/completions",
        json={
            "key": api_key,
            "model_id": model,
            "messages": [
                {"role": "user", "content": message}
            ],
            "max_tokens": 200,
            "temperature": 0.7
        }
    )

    data = response.json()

    if "choices" in data:
        return data["choices"][0]["message"]["content"]
    else:
        raise Exception(f"Error: {data.get('message', 'Unknown error')}")
python
import requests

def chat(message, api_key, model="meta-llama-3-8B-instruct"):
    """Send a chat completion request.

    Args:
        message: The user message
        api_key: Your ModelsLab API key
        model: LLM model ID (use modelslab models search --feature llmaster to find models)
    """
    response = requests.post(
        "https://modelslab.com/api/v7/llm/chat/completions",
        json={
            "key": api_key,
            "model_id": model,
            "messages": [
                {"role": "user", "content": message}
            ],
            "max_tokens": 200,
            "temperature": 0.7
        }
    )

    data = response.json()

    if "choices" in data:
        return data["choices"][0]["message"]["content"]
    else:
        raise Exception(f"Error: {data.get('message', 'Unknown error')}")

Usage

Usage

reply = chat( "Explain quantum computing in simple terms.", "your_api_key" ) print(reply)
undefined
reply = chat( "Explain quantum computing in simple terms.", "your_api_key" ) print(reply)
undefined

With System Prompt

结合系统提示词

python
def chat_with_system(system_prompt, message, api_key, model="meta-llama-3-8B-instruct"):
    """Chat with a system prompt for role/behavior control."""
    response = requests.post(
        "https://modelslab.com/api/v7/llm/chat/completions",
        json={
            "key": api_key,
            "model_id": model,
            "messages": [
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": message}
            ],
            "max_tokens": 500,
            "temperature": 0.7
        }
    )

    data = response.json()
    if "choices" in data:
        return data["choices"][0]["message"]["content"]
python
def chat_with_system(system_prompt, message, api_key, model="meta-llama-3-8B-instruct"):
    """Chat with a system prompt for role/behavior control."""
    response = requests.post(
        "https://modelslab.com/api/v7/llm/chat/completions",
        json={
            "key": api_key,
            "model_id": model,
            "messages": [
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": message}
            ],
            "max_tokens": 500,
            "temperature": 0.7
        }
    )

    data = response.json()
    if "choices" in data:
        return data["choices"][0]["message"]["content"]

Usage

Usage

reply = chat_with_system( "You are a Python expert. Give concise code examples.", "How do I read a CSV file?", "your_api_key" )
undefined
reply = chat_with_system( "You are a Python expert. Give concise code examples.", "How do I read a CSV file?", "your_api_key" )
undefined

Multi-Turn Conversation

多轮对话

python
def conversation(messages, api_key, model="meta-llama-3-8B-instruct"):
    """Send a multi-turn conversation."""
    response = requests.post(
        "https://modelslab.com/api/v7/llm/chat/completions",
        json={
            "key": api_key,
            "model_id": model,
            "messages": messages,
            "max_tokens": 500,
            "temperature": 0.7
        }
    )
    return response.json()
python
def conversation(messages, api_key, model="meta-llama-3-8B-instruct"):
    """Send a multi-turn conversation."""
    response = requests.post(
        "https://modelslab.com/api/v7/llm/chat/completions",
        json={
            "key": api_key,
            "model_id": model,
            "messages": messages,
            "max_tokens": 500,
            "temperature": 0.7
        }
    )
    return response.json()

Multi-turn example

Multi-turn example

messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is Python?"}, {"role": "assistant", "content": "Python is a high-level programming language..."}, {"role": "user", "content": "Show me a hello world example."} ]
result = conversation(messages, "your_api_key") print(result["choices"][0]["message"]["content"])
undefined
messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is Python?"}, {"role": "assistant", "content": "Python is a high-level programming language..."}, {"role": "user", "content": "Show me a hello world example."} ]
result = conversation(messages, "your_api_key") print(result["choices"][0]["message"]["content"])
undefined

OpenAI SDK Compatibility

兼容OpenAI SDK

You can use the official OpenAI Python SDK by changing the base URL and API key:
python
from openai import OpenAI

client = OpenAI(
    base_url="https://modelslab.com/api/v7/llm",
    api_key="your_modelslab_api_key"
)

response = client.chat.completions.create(
    model="meta-llama-3-8B-instruct",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ],
    max_tokens=100,
    temperature=0.7
)

print(response.choices[0].message.content)
您可以通过修改基础URL和API密钥,使用官方OpenAI Python SDK:
python
from openai import OpenAI

client = OpenAI(
    base_url="https://modelslab.com/api/v7/llm",
    api_key="your_modelslab_api_key"
)

response = client.chat.completions.create(
    model="meta-llama-3-8B-instruct",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ],
    max_tokens=100,
    temperature=0.7
)

print(response.choices[0].message.content)

cURL Example

cURL示例

bash
curl -X POST "https://modelslab.com/api/v7/llm/chat/completions" \
  -H "Content-Type: application/json" \
  -d '{
    "key": "your_api_key",
    "model_id": "meta-llama-3-8B-instruct",
    "messages": [
      {"role": "user", "content": "Say hello in one sentence."}
    ],
    "max_tokens": 50,
    "temperature": 0.7
  }'
bash
curl -X POST "https://modelslab.com/api/v7/llm/chat/completions" \
  -H "Content-Type: application/json" \
  -d '{
    "key": "your_api_key",
    "model_id": "meta-llama-3-8B-instruct",
    "messages": [
      {"role": "user", "content": "Say hello in one sentence."}
    ],
    "max_tokens": 50,
    "temperature": 0.7
  }'

Discovering LLM Models

查找大语言模型(LLM)

bash
undefined
bash
undefined

Search all chat/LLM models

Search all chat/LLM models

modelslab models search --feature llmaster
modelslab models search --feature llmaster

Search by provider

Search by provider

modelslab models search --search "deepseek" modelslab models search --search "llama" modelslab models search --search "gemini"
modelslab models search --search "deepseek" modelslab models search --search "llama" modelslab models search --search "gemini"

Get model details

Get model details

modelslab models detail --id meta-llama-3-8B-instruct
undefined
modelslab models detail --id meta-llama-3-8B-instruct
undefined

Popular LLM Model IDs

热门大语言模型(LLM)ID

Meta Llama

Meta Llama

  • meta-llama-3-8B-instruct
    — Llama 3 8B (fast, efficient)
  • meta-llama-Llama-3.3-70B-Instruct-Turbo
    — Llama 3.3 70B (powerful)
  • meta-llama-Meta-Llama-3.1-405B-Instruct-Turbo
    — Llama 3.1 405B (largest)
  • meta-llama-3-8B-instruct
    — Llama 3 8B(快速、高效)
  • meta-llama-Llama-3.3-70B-Instruct-Turbo
    — Llama 3.3 70B(功能强大)
  • meta-llama-Meta-Llama-3.1-405B-Instruct-Turbo
    — Llama 3.1 405B(参数规模最大)

DeepSeek

DeepSeek

  • deepseek-ai-DeepSeek-R1-Distill-Llama-70B
    — DeepSeek R1 (reasoning)
  • deepseek-ai-DeepSeek-V3
    — DeepSeek V3 (general purpose)
  • deepseek-ai-DeepSeek-R1-Distill-Llama-70B
    — DeepSeek R1(擅长推理)
  • deepseek-ai-DeepSeek-V3
    — DeepSeek V3(通用型)

Google

Google

  • gemini-2.0-flash-001
    — Gemini 2.0 Flash (fast)
  • gemini-2.5-pro
    — Gemini 2.5 Pro (capable)
  • gemini-2.0-flash-001
    — Gemini 2.0 Flash(快速响应)
  • gemini-2.5-pro
    — Gemini 2.5 Pro(能力全面)

Qwen

Qwen

  • Qwen-Qwen2.5-72B-Instruct-Turbo
    — Qwen 2.5 72B
  • Qwen-Qwen2.5-Coder-32B-Instruct
    — Qwen 2.5 Coder
  • Qwen-Qwen2.5-72B-Instruct-Turbo
    — Qwen 2.5 72B
  • Qwen-Qwen2.5-Coder-32B-Instruct
    — Qwen 2.5 代码模型

Mistral

Mistral

  • mistralai-Mixtral-8x7B-Instruct-v0.1
    — Mixtral 8x7B
  • mistralai-Mistral-Small-24B-Instruct-2501
    — Mistral Small
  • mistralai-Mixtral-8x7B-Instruct-v0.1
    — Mixtral 8x7B
  • mistralai-Mistral-Small-24B-Instruct-2501
    — Mistral Small

Key Parameters

关键参数

ParameterTypeRequiredDescription
model_id
stringYesLLM model identifier (alias:
model
)
messages
arrayYesArray of message objects with
role
and
content
temperature
floatNoSampling temperature (0-2, default varies by model)
max_tokens
integerNoMaximum tokens to generate
top_p
floatNoNucleus sampling (0-1)
top_k
integerNoTop-k sampling
frequency_penalty
floatNoFrequency penalty (-2 to 2)
presence_penalty
floatNoPresence penalty (-2 to 2)
streaming
booleanNoEnable streaming responses (alias:
stream
)
n
integerNoNumber of completions (1-10)
stop
arrayNoStop sequences (max 4)
seed
integerNoRandom seed for reproducibility
tools
arrayNoFunction/tool definitions
tool_choice
stringNoTool selection strategy
response_format
objectNo
{"type": "json_object"}
for JSON mode
参数类型是否必填描述
model_id
字符串大语言模型(LLM)标识符(别名:
model
messages
数组包含
role
content
的消息对象数组
temperature
浮点数采样温度(0-2,默认值因模型而异)
max_tokens
整数生成的最大token数
top_p
浮点数核采样参数(0-1)
top_k
整数Top-k采样参数
frequency_penalty
浮点数频率惩罚(-2到2)
presence_penalty
浮点数存在惩罚(-2到2)
streaming
布尔值启用流式响应(别名:
stream
n
整数生成补全结果的数量(1-10)
stop
数组停止序列(最多4个)
seed
整数用于结果复现的随机种子
tools
数组函数/工具定义
tool_choice
字符串工具选择策略
response_format
对象指定为
{"type": "json_object"}
可启用JSON模式

Response Format

响应格式

The response follows the OpenAI chat completions format:
json
{
  "id": "gen-...",
  "model": "meta-llama-3-8B-instruct",
  "object": "chat.completion",
  "created": 1771658583,
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      }
    }
  ],
  "usage": {
    "prompt_tokens": 17,
    "completion_tokens": 10,
    "total_tokens": 27
  }
}
响应遵循OpenAI对话补全格式:
json
{
  "id": "gen-...",
  "model": "meta-llama-3-8B-instruct",
  "object": "chat.completion",
  "created": 1771658583,
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      }
    }
  ],
  "usage": {
    "prompt_tokens": 17,
    "completion_tokens": 10,
    "total_tokens": 27
  }
}

Error Handling

错误处理

python
def safe_chat(message, api_key, model="meta-llama-3-8B-instruct"):
    """Chat with error handling."""
    try:
        response = requests.post(
            "https://modelslab.com/api/v7/llm/chat/completions",
            json={
                "key": api_key,
                "model_id": model,
                "messages": [{"role": "user", "content": message}],
                "max_tokens": 200
            }
        )
        data = response.json()

        if "choices" in data:
            return data["choices"][0]["message"]["content"]
        elif data.get("status") == "error":
            raise Exception(data.get("message", "Unknown error"))
        else:
            raise Exception(f"Unexpected response: {data}")

    except requests.exceptions.RequestException as e:
        raise Exception(f"Network error: {e}")

try:
    reply = safe_chat("Hello!", "your_api_key")
    print(reply)
except Exception as e:
    print(f"Chat failed: {e}")
python
def safe_chat(message, api_key, model="meta-llama-3-8B-instruct"):
    """Chat with error handling."""
    try:
        response = requests.post(
            "https://modelslab.com/api/v7/llm/chat/completions",
            json={
                "key": api_key,
                "model_id": model,
                "messages": [{"role": "user", "content": message}],
                "max_tokens": 200
            }
        )
        data = response.json()

        if "choices" in data:
            return data["choices"][0]["message"]["content"]
        elif data.get("status") == "error":
            raise Exception(data.get("message", "Unknown error"))
        else:
            raise Exception(f"Unexpected response: {data}")

    except requests.exceptions.RequestException as e:
        raise Exception(f"Network error: {e}")

try:
    reply = safe_chat("Hello!", "your_api_key")
    print(reply)
except Exception as e:
    print(f"Chat failed: {e}")

Resources

相关资源

Related Skills

相关功能

  • modelslab-model-discovery
    - Find and filter models
  • modelslab-account-management
    - Manage API keys and billing
  • modelslab-image-generation
    - Generate images from text
  • modelslab-model-discovery
    - 查找和筛选模型
  • modelslab-account-management
    - 管理API密钥和账单
  • modelslab-image-generation
    - 根据文本生成图像