跳转到主要内容

SDK 示例

OpenAI SDK

import os
from openai import OpenAI

client = OpenAI(
    api_key="your-pipellm-key",
    base_url="https://api.pipellm.com/v1"
)

# 聊天补全
response = client.chat.completions.create(
    model="gpt-4",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ]
)
print(response.choices[0].message.content)

# 文本补全
response = client.completions.create(
    model="gpt-3.5-turbo-instruct",
    prompt="Once upon a time",
    max_tokens=100
)
print(response.choices[0].text)

# 嵌入
response = client.embeddings.create(
    model="text-embedding-ada-002",
    input=["Hello world", "Foo bar"]
)
print(response.data[0].embedding)

# 图像生成
response = client.images.generate(
    model="dall-e-3",
    prompt="A futuristic city skyline",
    size="1024x1024"
)
print(response.data[0].url)

# 音频转录
with open("audio.mp3", "rb") as audio_file:
    response = client.audio.transcriptions.create(
        model="whisper-1",
        file=audio_file
    )
print(response.text)

Anthropic SDK

import os
from anthropic import Anthropic

client = Anthropic(
    api_key="your-pipellm-key",
    base_url="https://api.pipellm.com/v1"
)

# 消息 API
response = client.messages.create(
    model="claude-3-sonnet",
    max_tokens=1000,
    messages=[
        {"role": "user", "content": "Hello, Claude!"}
    ]
)
print(response.content[0].text)

# 流式响应
with client.messages.stream(
    model="claude-3-haiku",
    max_tokens=100,
    messages=[{"role": "user", "content": "Count to 10:"}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Google Gemini SDK

import os
import google.generativeai as genai

genai.configure(
    api_key="your-pipellm-key",
    base_url="https://api.pipellm.com/v1"
)

# 加载模型
model = genai.GenerativeModel('gemini-pro')

# 生成内容
response = model.generate_content("Write a Python function to calculate factorial")
print(response.text)

# 流式响应
for chunk in model.generate_content("Tell me a story", stream=True):
    print(chunk.text, end="")

LangChain

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate

# OpenAI 模型
llm = ChatOpenAI(
    model="gpt-4",
    api_key="your-pipellm-key",
    base_url="https://api.pipellm.com/v1"
)

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    ("user", "{input}")
])

chain = prompt | llm
response = chain.invoke({"input": "What is AI?"})
print(response.content)

# Anthropic 模型
from langchain_anthropic import ChatAnthropic

llm = ChatAnthropic(
    model="claude-3-sonnet",
    api_key="your-pipellm-key",
    base_url="https://api.pipellm.com/v1"
)

response = llm.invoke("Hello, Claude!")
print(response.content)

cURL 示例

聊天补全

curl https://api.pipellm.com/v1/chat/completions \
  -H "Authorization: Bearer your-pipellm-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Hello!"}
    ],
    "temperature": 0.7,
    "max_tokens": 1000
  }'

流式响应

curl https://api.pipellm.com/v1/chat/completions \
  -H "Authorization: Bearer your-pipellm-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4",
    "messages": [{"role": "user", "content": "Count to 5:"}],
    "stream": true
  }'

文本补全

curl https://api.pipellm.com/v1/completions \
  -H "Authorization: Bearer your-pipellm-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-3.5-turbo-instruct",
    "prompt": "Once upon a time",
    "max_tokens": 100,
    "temperature": 0.8
  }'

嵌入

curl https://api.pipellm.com/v1/embeddings \
  -H "Authorization: Bearer your-pipellm-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "text-embedding-ada-002",
    "input": ["Hello world", "Foo bar"]
  }'

图像生成

curl https://api.pipellm.com/v1/images/generations \
  -H "Authorization: Bearer your-pipellm-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "dall-e-3",
    "prompt": "A futuristic city skyline at sunset",
    "size": "1024x1024"
  }'

音频转录

curl https://api.pipellm.com/v1/audio/transcriptions \
  -H "Authorization: Bearer your-pipellm-key" \
  -F "[email protected]" \
  -F "model=whisper-1" \
  -F "language=en"

认证

所有请求都需要 API 密钥:
curl https://api.pipellm.com/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-4","messages":[{"role":"user","content":"Hello"}]}'

错误代码

代码错误描述
401Unauthorized无效或缺失的 API 密钥
429Too Many Requests超出速率限制
502Bad Gateway上游服务错误
503Service Unavailable服务暂时不可用