

























研究发现,睡前看手机会影响睡眠,这不是废话么,当然影响了.
所以作为一名马上中年马上再成为爷爷辈儿的人(刚才视频号有人说他38岁朋友都当爷爷了).
我是要照顾好自己,照顾好自己的精神和身体.
最近不仅又着迷一个文生图的应用,也开始大量的使用token来编写代码.
有一段时间甚至有些恍惚,我还究竟是不是会写代码来着?

最近打算的计划是,将自己使用AI编辑器和包括openclaw还有hermes的经验大概记录下.然后分享出来.
比如现在我本地项目调用WSL里的Hermes:
```python
@csrf_exempt
def testcase_generate(request, project_id, issue_id):
"""AI 生成测试用例 — 调用本地 Hermes Agent (OpenAI 兼容协议) 直接落库。
隔离原则: Hermes 任何异常都被吞掉, 转成业务级 JSON 返回。
- 永远返回 HTTP 200 + 业务 code (200/4xx/5xx 都在 body.code 里)
- 不抛出未捕获异常, 不污染 Django 进程, 不影响其他请求
- 单条 case 入库失败不影响其他 case
"""
if request.method != 'POST':
return JsonResponse({'code': 405, 'msg': 'Method not allowed'})
# ───── 顶层 try: 任何意外都不冒到 Django 500 模板 ─────
try:
issue = get_object_or_404(models.Issues, id=issue_id, project_id=project_id)
desc = (getattr(issue, 'description', None)
or getattr(issue, 'remark', None)
or '').strip()
system_prompt = (
"你是资深测试工程师。你的全部回复必须是一个 JSON 对象, 不允许任何其他文字。\n"
"硬性规则:\n"
"1. 回复的第一个字符必须是 `{`, 最后一个字符必须是 `}`。\n"
"2. 不允许出现任何 Markdown 表格、列表、标题、总结、前言、后记。\n"
"3. 不允许出现 ``` 代码围栏。\n"
"4. 不允许出现「以下是」「输出如下」「覆盖了」等任何说明性句子。\n"
"5. 哪怕只多一个字符,系统都会判定失败。\n\n"
"JSON schema (严格遵守这个结构):\n"
"{\n"
' "cases": [\n'
" {\n"
' "name": "用例标题, 中文, <= 80 字",\n'
' "description": "用例目的简述",\n'
' "test_steps": "1. xxx\\n2. xxx\\n3. xxx",\n'
' "expected_result": "预期结果",\n'
' "test_py_content": "可选, unittest 脚本; 没有就给空字符串"\n'
" }\n"
" ]\n"
"}\n\n"
"cases 数组长度必须在 3-5 之间, 覆盖: 正常流程 / 边界 / 异常 三类场景。"
)
user_prompt = (
f"需求标题: {issue.subject}\n\n"
f"需求描述:\n{desc or '(无)'}\n\n"
"立即输出 JSON 对象, 第一个字符必须是 `{`。"
)
# 抽 markdown 里的图片 URL → OpenAI vision 兼容 content 数组(没图就退回纯文本)
try:
from web.api.views import _build_multimodal_text
user_content = _build_multimodal_text(user_prompt, request=request)
except Exception:
user_content = user_prompt
# ───── 1) 调 Hermes ─────
try:
raw = chat_complete(
[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_content},
],
temperature=0.1,
extra={"response_format": {"type": "json_object"}},
)
except LLMError as e:
logger.warning("Hermes 调用失败 (issue=%s): %s", issue_id, e)
return JsonResponse({
'code': 502,
'msg': f'Hermes 调用失败(已自动忽略, 不影响主流程): {e}',
})
except Exception as e:
logger.exception("Hermes 未知错误 (issue=%s)", issue_id)
return JsonResponse({
'code': 502,
'msg': f'Hermes 未知错误(已自动忽略): {e}',
})
# ───── 2) 解析 JSON ─────
try:
parsed = extract_json(raw)
except LLMError as e:
logger.warning("AI 返回无法解析 (issue=%s): %s", issue_id, e)
return JsonResponse({
'code': 422,
'msg': f'AI 返回内容无法解析: {e}',
'raw': (raw or '')[:1000],
})
except Exception as e:
logger.exception("解析阶段未知错误 (issue=%s)", issue_id)
return JsonResponse({
'code': 422,
'msg': f'AI 返回解析时意外错误: {e}',
'raw': (raw or '')[:1000],
})
cases = parsed.get('cases') if isinstance(parsed, dict) else parsed
if not isinstance(cases, list) or not cases:
return JsonResponse({
'code': 422,
'msg': '模型返回的 JSON 里没有 cases 数组',
'raw': (raw or '')[:1000],
})
# ───── 3) 批量落库 — 单条失败不影响其他 ─────
creator = getattr(getattr(request, 'web', None), 'user', None)
created = []
skipped = 0
for c in cases:
try:
if not isinstance(c, dict):
skipped += 1
continue
name = (c.get('name') or '').strip()[:120]
if not name:
skipped += 1
continue
obj = models.TestCase.objects.create(
project_id=project_id,
issue_id=issue.id,
name=name,
description=(c.get('description') or '').strip(),
test_steps=(c.get('test_steps') or '').strip(),
expected_result=(c.get('expected_result') or '').strip(),
test_py_content=(c.get('test_py_content') or '').strip(),
status=1,
ai_generated=True,
creator=creator,
)
created.append({
'id': obj.id,
'name': obj.name,
'description': (obj.description or '')[:200],
})
except Exception:
logger.exception("单条 TestCase 入库失败 (issue=%s), 跳过", issue_id)
skipped += 1
continue
if not created:
return JsonResponse({
'code': 422,
'msg': f'解析到 {len(cases)} 条 cases 但全部入库失败/缺字段',
'raw': (raw or '')[:1000],
})
msg = f'已生成 {len(created)} 条测试用例'
if skipped:
msg += f' (跳过 {skipped} 条无效)'
return JsonResponse({
'code': 200,
'msg': msg,
'created': created,
})
except Exception as e:
# 最外层兜底: 哪怕上面逻辑有 bug, 也保证主页不崩
logger.exception("testcase_generate 意外异常 (issue=%s)", issue_id)
return JsonResponse({
'code': 500,
'msg': f'生成测试用例时发生意外异常(已自动忽略, 主流程不受影响): {e}',
})
```
像这种我能做之后,我就感觉再对接其他的都不是梦.
比如做企业AI知识库,知识图谱,智能机器人,自动回复机器人等等.
当然了还可以让它做得再深度点.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。