文本对话(LLM)
本页教你向模型提问,并在程序中读到回答。你可以用它做学习问答、文字整理、写作或编程练习。
先完成快速开始,从模型广场复制一个支持文本对话的模型名称。
选择一种方式
| 你习惯用什么 | 看哪一节 |
|---|---|
| Python | Python 示例 |
| macOS / Linux 的 Bash 终端 | cURL 示例 |
| Windows PowerShell | PowerShell 示例 |
| WorkBuddy | 软件配置步骤 |
本页使用的完整接口地址:
text
https://www.taokennet.com/v1/chat/completionsPython 示例
先按 Python 准备教程安装 requests。复制以下完整代码,保存为 chat_demo.py,只需把 YOUR_LLM_MODEL_ID 换成实际模型名称。
python
import getpass
import requests
API_BASE_URL = "https://www.taokennet.com"
MODEL_ID = "YOUR_LLM_MODEL_ID"
api_key = getpass.getpass("粘贴你的 API Key,然后按回车:").strip()
response = requests.post(
f"{API_BASE_URL}/v1/chat/completions",
headers={"Authorization": f"Bearer {api_key}"},
json={
"model": MODEL_ID,
"messages": [
{"role": "user", "content": "请用三句话解释什么是机器学习。"}
],
},
timeout=180,
)
if not response.ok:
print("请求失败:", response.status_code, response.text)
raise SystemExit(1)
result = response.json()
print(result["choices"][0]["message"]["content"])运行:
bash
python chat_demo.py按提示粘贴 API Key,再按回车。成功时,终端会显示模型给出的解释;具体回答每次可能不同。
cURL 示例
下面按 Bash 写法提供。Windows PowerShell 用户请使用下一节,不要混用两种换行符。
把 YOUR_LLM_MODEL_ID 换成模型名称,整段粘贴到 Bash 中执行:
bash
API_BASE_URL='https://www.taokennet.com'
printf '粘贴你的 API Key,然后按回车:'
read -r -s API_KEY
printf '\n'
curl -sS "$API_BASE_URL/v1/chat/completions" \
-H "Authorization: Bearer $API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "YOUR_LLM_MODEL_ID",
"messages": [
{"role": "user", "content": "请用三句话解释什么是机器学习。"}
]
}'粘贴密钥时屏幕不显示输入是正常现象。请求成功后会显示一段 JSON,回答在 choices[0].message.content。
PowerShell 示例
打开 Windows PowerShell,替换模型名称,再复制运行:
powershell
$ErrorActionPreference = 'Stop'
$apiBaseUrl = 'https://www.taokennet.com'
$apiKey = Read-Host '粘贴你的 API Key,然后按回车'
$modelId = 'YOUR_LLM_MODEL_ID'
$headers = @{ Authorization = "Bearer $($apiKey.Trim())" }
$body = @{
model = $modelId
messages = @(
@{ role = 'user'; content = '请用三句话解释什么是机器学习。' }
)
} | ConvertTo-Json -Depth 8
$result = Invoke-RestMethod `
-Uri "$apiBaseUrl/v1/chat/completions" `
-Method Post `
-Headers $headers `
-ContentType 'application/json' `
-Body ([Text.Encoding]::UTF8.GetBytes($body))
$result.choices[0].message.content成功时,最后一行会直接显示模型回答,不需要自己从整段 JSON 中找文字。
示例里的字段是什么意思?
先保留示例的写法,只替换模型名称和你想问的问题。常用字段在这里就可以查到:
| 字段 | 是什么 | 怎么填写 |
|---|---|---|
model | 本次使用的模型 | 从平台模型广场复制,替换 YOUR_LLM_MODEL_ID |
messages | 这次对话的消息列表 | 第一次提问放一条消息,继续追问时带上前面的对话 |
role | 这条消息是谁说的 | 你的问题用 user,模型之前的回答用 assistant |
content | 消息的文字内容 | 把示例问题换成自己的问题 |
stream | 是否边生成边返回 | 不填或填 false 就等待完整回答;填 true 时使用下面的流式读取示例 |
请求头中的 Authorization: Bearer ... 用来填写平台 API Key,Content-Type: application/json 表示发送 JSON。代码示例已经写好这些部分,按提示输入 Key 即可。
Python 的 timeout 是本次网络请求的等待设置,不是回答字数。第一次调用不需要额外添加其他参数。
成功结果怎么看?
cURL 通常显示完整 JSON,下面是简化后的结构示意,文字只是示例:
json
{
"choices": [
{
"message": {
"role": "assistant",
"content": "机器学习是让计算机从数据中学习规律的方法。"
}
}
]
}找到 choices 中第一项的 message.content,就是这次回答。上面的 Python 和 PowerShell 示例已经替你取出了这段文字。
流式回答会分段到达,文字在每段的 choices[0].delta.content 中,收到 [DONE] 表示结束。下面的完整示例已经包含这些读取步骤。
继续追问怎么写?
希望模型知道前面聊过什么,就把相关对话一起放进 messages。例如,把 Python 示例里的消息列表换成:
json
[
{"role": "user", "content": "什么是机器学习?"},
{"role": "assistant", "content": "机器学习是让计算机从数据中学习规律的方法。"},
{"role": "user", "content": "再举一个生活中的例子。"}
]user 表示你的问题,assistant 表示模型之前的回答。自己的程序中应保存真实的对话内容。
想让回答逐字显示?
先确认普通对话已经成功,再尝试下面的 Python 示例,保存为 chat_stream.py。它会边收到文字边打印:
python
import getpass
import json
import requests
API_BASE_URL = "https://www.taokennet.com"
MODEL_ID = "YOUR_LLM_MODEL_ID"
api_key = getpass.getpass("粘贴你的 API Key,然后按回车:").strip()
with requests.post(
f"{API_BASE_URL}/v1/chat/completions",
headers={"Authorization": f"Bearer {api_key}"},
json={
"model": MODEL_ID,
"messages": [{"role": "user", "content": "介绍三种有效的学习方法。"}],
"stream": True,
},
stream=True,
timeout=180,
) as response:
if not response.ok:
print("请求失败:", response.status_code, response.text)
raise SystemExit(1)
for line in response.iter_lines():
if not line:
continue
text = line.decode("utf-8")
if not text.startswith("data:"):
continue
data = text[5:].strip()
if data == "[DONE]":
break
if not data:
continue
chunk = json.loads(data)
if chunk.get("error"):
print("\n请求失败:", chunk["error"])
break
choices = chunk.get("choices", [])
if choices:
print(choices[0].get("delta", {}).get("content") or "", end="", flush=True)
print()运行 python chat_stream.py。看到文字陆续出现,而不是等全部完成后一起出现,就说明流式显示已生效。所选模型需要支持流式回答。
没有得到回答?
先检查三处:地址是否照抄本页、API Key 是否完整、模型占位文字是否已经替换。再看常见问题。
本页已包含完成文本对话所需的地址、字段、代码和结果说明,照着操作即可,不需要另外查找接口文档。
