feat: support old title placeholder in title prompts
This commit is contained in:
+61
-1
@@ -181,7 +181,40 @@ class AITests(TempDirMixin, unittest.TestCase):
|
||||
body = json.loads(calls[-1][0].data.decode("utf-8"))
|
||||
self.assertEqual("text-model", body["model"])
|
||||
self.assertEqual(0, body["temperature"])
|
||||
self.assertNotIn("sk-text-secret", body["messages"][1]["content"])
|
||||
self.assertEqual(1, len(body["messages"]))
|
||||
self.assertIn("优化标题", body["messages"][0]["content"])
|
||||
self.assertIn("旧标题:\n旧标题", body["messages"][0]["content"])
|
||||
self.assertIn("请只返回新标题,不要解释。", body["messages"][0]["content"])
|
||||
self.assertNotIn("sk-text-secret", body["messages"][0]["content"])
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_gen_title_renders_old_title_placeholder_without_duplicate_append(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
models_path = os.path.join(temp_dir, "ai_models.json")
|
||||
self._write_models(models_path)
|
||||
calls = []
|
||||
|
||||
def fake_urlopen(request, timeout=None):
|
||||
calls.append(request)
|
||||
return _Response({"choices": [{"message": {"content": "新标题"}}]})
|
||||
|
||||
with mock.patch("app.ai.urllib.request.urlopen", side_effect=fake_urlopen):
|
||||
title = ai.gen_title(
|
||||
"请基于{旧标题}重写标题,保留 {商品id}",
|
||||
"原始标题A",
|
||||
config=self._config(),
|
||||
models_path=models_path,
|
||||
)
|
||||
|
||||
self.assertEqual("新标题", title)
|
||||
body = json.loads(calls[-1].data.decode("utf-8"))
|
||||
content = body["messages"][0]["content"]
|
||||
self.assertIn("请基于原始标题A重写标题", content)
|
||||
self.assertEqual(1, content.count("原始标题A"))
|
||||
self.assertNotIn("旧标题:", content)
|
||||
self.assertIn("{商品id}", content)
|
||||
self.assertIn("请只返回新标题,不要解释。", content)
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
@@ -339,6 +372,33 @@ class AITests(TempDirMixin, unittest.TestCase):
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_cmhub_gen_title_renders_old_title_placeholder_once(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
cfg, key_path = self._cmhub_config(temp_dir)
|
||||
calls = []
|
||||
|
||||
def fake_request(method, url, **kwargs):
|
||||
calls.append((method, url, kwargs))
|
||||
return _RequestsResponse({"titles": ["新标题"]})
|
||||
|
||||
with mock.patch.object(ai._cmhub_session(), "request", side_effect=fake_request):
|
||||
title = ai.gen_title(
|
||||
"请参考 {旧标题} 输出更短标题,店铺变量 {店铺}",
|
||||
"原始标题B",
|
||||
config=cfg,
|
||||
cmhub_config_path=key_path,
|
||||
)
|
||||
|
||||
self.assertEqual("新标题", title)
|
||||
prompt = calls[0][2]["json"]["prompt"]
|
||||
self.assertIn("请参考 原始标题B 输出更短标题", prompt)
|
||||
self.assertEqual(1, prompt.count("原始标题B"))
|
||||
self.assertNotIn("旧标题:", prompt)
|
||||
self.assertIn("{店铺}", prompt)
|
||||
self.assertIn("请只返回新标题,不要解释。", prompt)
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_cmhub_missing_config_raises_clear_error(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
cfg = self._config()
|
||||
|
||||
Reference in New Issue
Block a user