几分钟内开始使用 LinkHarbor API。只需几行代码,即可完成第一次 AI 请求。
注册免费的 LinkHarbor 账户以获取 API 密钥。您的密钥可访问 OpenAI、Anthropic、Google 等供应商的模型。
选择要使用的接口,并安装对应的 SDK。
pip install anthropicpip install openai将 API 密钥导出为环境变量。不要将它硬编码到源码中。
export OPENAI_API_KEY="YOUR_API_KEY"
export OPENAI_BASE_URL="https://api.linkharbor.ai/v1"可使用 Anthropic Messages API,或使用 OpenAI 兼容的 Chat Completions 端点调用 LinkHarbor 模型目录中的模型。
from anthropic import Anthropic
import os
client = Anthropic(
base_url="https://api.linkharbor.ai/anthropic",
api_key=os.environ["ANTHROPIC_AUTH_TOKEN"]
)
message = client.messages.create(
model="your-model-name",
max_tokens=1024,
messages=[{"role": "user", "content": "What is the capital of France?"}]
)
print(message.content[0].text)from openai import OpenAI
import os
client = OpenAI(
base_url="https://api.linkharbor.ai/v1",
api_key=os.environ["OPENAI_API_KEY"]
)
response = client.chat.completions.create(
model="your-model-name",
messages=[{"role": "user", "content": "What is the capital of France?"}]
)
print(response.choices[0].message.content)启用流式输出以实时接收生成的 token,非常适合需要实时反馈的聊天界面。
stream = client.chat.completions.create(
model="your-model-name",
messages=[{"role": "user", "content": "Write a haiku about APIs"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)您已经完成第一次请求。继续了解完整 API 能力。