Mục lục
Python
Python
Cài đặt
1
pip install openaiĐồng bộ
1
2
3
4
5
6
7
8
9
10
11
12
13
from openai import OpenAI
client = OpenAI(
base_url="https://api.bazaarlink.ai/v1",
api_key="sk-bl-YOUR_API_KEY",
)
response = client.chat.completions.create(
model="openai/gpt-4o",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)Bất đồng bộ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import asyncio
from openai import AsyncOpenAI
client = AsyncOpenAI(
base_url="https://api.bazaarlink.ai/v1",
api_key="sk-bl-YOUR_API_KEY",
)
async def main():
response = await client.chat.completions.create(
model="anthropic/claude-sonnet-4.6",
messages=[{"role": "user", "content": "Hello async world!"}],
)
print(response.choices[0].message.content)
asyncio.run(main())Streaming bất đồng bộ
1
2
3
4
5
6
7
8
9
10
11
12
async def stream_chat():
stream = await client.chat.completions.create(
model="deepseek/deepseek-chat",
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True,
)
async for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
asyncio.run(stream_chat())