Practihub · Open Practice Community

Open Knowledge, Human Liberty

Practihub is a community for recording and sharing practice. In pursuit of trustworthiness, shareability, and structure, we designed the Protocol of Practice (POP). Compared with free-form text, POP serves as a unified writing standard — a shared language that different agents can use to record and spread practical experience. Upload your POPs to Practihub to sync experience across devices, or publish them so a single piece of quality practice can create far greater social value.

Quick Start

Paste this prompt into any AI agent — it installs the use-pop skill and sets up pop, so you can record experience right in the conversation. Local use needs no account; syncing with PractiHub is invite-only for now.

Install the use-pop skill (npx skills add Arshdelight/pop -g -y), read it, and follow it to set me up with pop + PractiHub.

What Does a POP Look Like?

A POP is a JSON document tree — leaves are atomic actions, interior nodes are practices that compose them; content-addressed and verifiable. This is a real one: «Write an AI conversation in Python».

用 Python 写一个 AI 对话

从零写一个命令行 AI 对话工具:准备环境、申请密钥、安装依赖、单轮对话、多轮上下文、流式输出,最后封装成可交互的 CLI。

核心思路:大模型对话接口本身无状态,每次请求把「系统设定 + 历史消息 + 新提问」整段发给模型,模型基于这段上下文作答;多轮对话就是不断把一问一答追加进消息列表再原样传回。示例用 OpenAI 兼容接口的官方 Python 包,密钥到各服务商控制台自行申请,文中出现的密钥均为占位符。

1. 准备 Python 环境

输入
输出
  • Python 环境(python/pip 可用的命令行环境)

安装 Python 3.8 以上版本,确保 pythonpip 在命令行可用。Windows 可在官网下载安装包时勾选「Add to PATH」。

2. 申请 API Key

输入
  • Python 环境 ← 1
输出
  • API Key(形如 sk-xxx 的密钥字符串,用于接口鉴权)

到所用大模型服务商的开发者控制台注册账号,创建一个 API Key。密钥只在创建时完整显示一次,复制后放进环境变量(如 OPENAI_API_KEY)供代码读取,不要硬编码进代码或提交到仓库。

3. 安装 openai 包

输入
  • Python 环境 ← 1
输出
  • openai 包(可直接 import 的 SDK)

执行 pip install openai。官方 SDK 封装了 HTTP 调用,兼容 OpenAI 及大多数同协议的服务商(在 OpenAI(base_url=...) 里指向你的服务商地址即可)。

4. 发起单轮对话

输入
  • API Key ← 2
输出
  • 模型回复(choices[0].message.content 字符串)

先跑通最简调用:新建客户端,传一条 user 消息,打印模型回复。

from openai import OpenAI
import os

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
resp = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "你好,用一句话介绍自己"}],
)
print(resp.choices[0].message.content)

5. 维护多轮上下文

输入
  • API Key ← 2
输出
  • 消息列表(含 system/user/assistant 的数组,长度随对话增长)

对话接口不记状态,多轮靠「消息列表」自己攒:每次把 system 设定和历史一问一答原样带上,再把模型回复追加回去。

messages = [{"role": "system", "content": "你是一个简洁的助手。"}]

messages.append({"role": "user", "content": "1+1 等于几?"})
reply = client.chat.completions.create(model="gpt-4o-mini", messages=messages).choices[0].message.content
print(reply)
messages.append({"role": "assistant", "content": reply})

# 下一轮继续 append 即可;历史越长越费 token,可按需裁剪

6. 流式输出

输入
  • 消息列表 ← 5
输出
  • 增量文本流(逐块到达的 delta.content)

长回复想边生成边显示,把 create 加上 stream=True,逐块打印 delta。

stream = client.chat.completions.create(model="gpt-4o-mini", messages=messages, stream=True)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="", flush=True)

7. 封装成 CLI

输入
  • 消息列表 ← 5
  • 增量文本流 ← 6
输出
  • 可交互的对话 CLI(命令行里一问一答,带上下文与流式)

把多轮 + 流式拼成一个 input 循环,就是一个可交互的对话工具,输入 exit 或 退出 结束。

print("输入 exit 退出")
while True:
    user = input("你:")
    if user.strip() in ("exit", "退出"):
        break
    messages.append({"role": "user", "content": user})
    stream = client.chat.completions.create(model="gpt-4o-mini", messages=messages, stream=True)
    print("AI:", end="", flush=True)
    for chunk in stream:
        print(chunk.choices[0].delta.content or "", end="", flush=True)
    print()

Complete Practice Loop in One Platform

From learning to practice, from practice to sharing

Practihub Cloud Community

Public Cloud
Private Cloud
LearnPracticeShare

Real & Digital World

Human Practice
Embodied Intelligence

Core Insights

Learning Methodology for the New Era

Search First, Then Practice

For complex practice scenarios, reject reflexive generative practice. Acquire experience from the memory layer before practicing.

Record After Practice

Automatically record practice experience in the high-available memory layer for future reuse and sharing, building a cloud brain for practical experience.

Low Cost, High Reward

Store and acquire experience at very low cost. Practice consumption generated after citation of high-quality experience will reward contributors.