-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstream_chat_test.py
More file actions
46 lines (39 loc) · 1.32 KB
/
stream_chat_test.py
File metadata and controls
46 lines (39 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import asyncio
from opencode_sdk import OpencodeClient
async def stream_chat():
# 使用你的服务器地址
client = OpencodeClient(base_url="http://192.168.77.28:8001")
print("创建会话...")
# 创建会话
session = client.sessions.create(
title="流式对话",
directory="/data/seo/workspace"
)
print(f"会话已创建: {session.id}\n")
question = "当前什么时间?"
print(f"问题:{question}")
print("AI 响应:\n")
# 发送消息并接收流式响应
try:
async for event in client.events.subscribe_session(
session_id=session.id,
parts=[{"type": "text", "text": question}],
directory="/data/seo/workspace",
agent="build",
model={
"modelID": "gpt-5-nano",
"providerID": "opencode"
},
variant="low"
):
# 只打印流式文本的增量
if event.type == "message.part.delta":
print(event.properties.delta, end="", flush=True)
except Exception as e:
print(f"\n\n错误: {type(e).__name__}: {e}")
import traceback
traceback.print_exc()
print("\n\n会话完成,正在清理...")
client.sessions.delete(session.id)
# 运行
asyncio.run(stream_chat())