简介

我们尝试通过 k8sgtp + ollama + qwen2-7b-4q 的组合,为 k8s 集群部署诊断服务。在进行测试的时候,发现命令行可以获取中文回复,但是 API 不能获取。但是很奇怪的是,用日语和法语,它可以正常回复。

详细

正常来说,我们可以通过 k8sgpt 的命令参数指定使用中文回复,例如:

k8sgpt analyze --explain --filter=Pod  --output=json -l Chinese
{
  "provider": "localai",
  "errors": null,
  "status": "ProblemDetected",
  "problems": 1,
  "results": [
    {
      "kind": "Pod",
      "name": "default/opbox-6d595cf94-6dmx4",
      "error": [
        {
          "Text": "Back-off pulling image \"registry.my.hub/my/image-not-exist:1.1\"",
          "KubernetesDoc": "",
          "Sensitive": []
        }
      ],
      "details": "Error: \"无法拉取镜像registry.my.hub/my/image-not-exist:1.1。该镜像在注册表中不存在。\"\nSolution: \n1. 检查拼写和大小写:确保您输入的镜像名完全正确,包括域名、仓库名称和标签。\n2. 验证镜像存在:访问镜像的官方仓库或私有仓库网站,确认该镜像是否真的存在。\n3. 登录注册表:如果您使用的是私有注册表,并且需要进行身份验证,请登录您的注册表。命令通常类似于 `kubectl config view` 或 `docker login registry.my.hub`。\n4. 重新尝试拉取镜像:在完成上述步骤后,再次尝试拉取镜像。如果仍然存在问题,请查阅相关注册表的文档或联系支持团队获取帮助。",
      "parentObject": "Deployment/opbox"
    }
  ]
}

但是很奇怪的是,当我使用接口调用的时候返回的就是英文:

grpcurl -plaintext -d '{"namespace": "test", "explain" : "true", "language":"chinese" ,"filters":["Pod"]}' 10.103.82.91:80 schema.v1.ServerService/Analyze
{
  "status": "ProblemDetected",
  "problems": 1,
  "results": [
    {
      "kind": "Pod",
      "name": "test/opbox-78dbdb5679-7lr2g",
      "error": [
        {
          "text": "Back-off pulling image \"test.my.hub/my/image-not-exist:1.1\""
        }
      ],
      "details": "Error: The image \"test.my.hub/my/image-not-exist:1.1\" you're trying to pull does not exist on the registry.\nSolution: \n1. Verify the correct spelling and existence of the image name by checking the registry (`test.my.hub/my`). Ensure there's no typo in the image name.\n2. Confirm that the image version exists within the registry by querying the available images at `test.my.hub/my`.\n3. If the correct image doesn't exist, either create it or replace \"image-not-exist\" with an existing image name.\n4. After making sure everything is correct, try pulling the image again in your Kubernetes cluster."
    }
  ]
}

继续查询 k8sgpt 的源码,发现它有一段提示词是这么写的:

Simplify the following Kubernetes error message delimited by triple dashes written in --- %s --- language; --- %s ---.
    Provide the most possible solution in a step by step style in no more than 280 characters. Write the output in the following format:
    Error: {Explain error here}
    Solution: {Step by step solution here}

以上的逻辑也没有问题,当指定 language 时,k8sgpt 调用 AI 的接口获取 response 的时候,提示词会告知 AI,请使用中文进行回复,我们直接使用这段提示词,运行试试看:

ollama run qwen2
>>> Simplify the following Kubernetes error message delimited by triple dashes written in Chinese language; --- Back-off pulling image \"test.my.hub/my/image-not-exist:1.1\" ---. Provide the most
... possible solution in a step by step style in no more than 280 characters. Write the output in the following format: Error: {Explain error here} Solution: {Step by step solution here}
Error: 正试图拉取镜像 "test.my.hub/my/image-not-exist:1.1" 时,系统回滚操作并显示该镜像不存在。
Solution:
步骤一:检查提供的镜像URL和标签是否正确无误。确认所需镜像确实存在于镜像仓库中。

步骤二:确保Kubernetes配置了正确的Docker注册服务器凭据,以便能够访问所需的镜像仓库。

步骤三:如果镜像是私有的或在内部网络中,请确认防火墙规则允许容器访问必要的端口,并验证Kubernetes服务设置是否正确。

步骤四:检查是否有任何安全策略阻止访问特定的镜像URL。可能需要更新安全策略以包含所需的服务。

步骤五:若以上步骤均无问题,尝试重启相关Pod或重新部署应用以查看错误是否仍然存在。

发现提示词是生效的,它回复了中文。

但是我们使用 API 调用的时候,结果不太一样:

curl http://10.16.202.155:30870/api/generate -d '{
  "model": "qwen2-7b-q4",
  "prompt": "Simplify the following Kubernetes error message delimited by triple dashes written in Chinese language; --- Back-off pulling image \"test.my.hub/my/image-not-exist:1.1\" ---. Provide the most possible solution in a step by step style in no more than 280 characters. Write the output in the following format: Error: {Explain error here} Solution: {Step by step solution here}",
  "stream": false
}'

{"model":"qwen2-7b-q4","created_at":"2024-07-19T06:08:27.031122355Z","response":"Error: Kubernetes cannot find or pull the specified Docker image \"test.my.hub/my/image-not-exist:1.1\". \nSolution: \n1. Verify if the Docker image exists at the given registry url 'test.my.hub/my'.\n2. Check correct spelling of repository name and tag version in your YAML file.\n3. Confirm access permissions to the registry used for pulling images.\n4. If using private registry, authenticate with it using commands like `docker login test.my.hub` before executing deployment command.\n5. Use a public image instead if issue persists or correct the spelling of repository and tag in YAML file.","done":true,"done_reason":"stop","context":[151644,872,198,50,70206,279,2701,66374,1465,1943,85328,553,23725,87546,5326,304,8453,4128,26,12448,6841,12462,22266,2168,330,1944,948,287,436,14118,5071,3470,287,562,3760,23349,29169,10187,380,25,16,13,16,1,12448,13,39565,279,1429,3204,6291,304,264,3019,553,3019,1707,304,902,803,1091,220,17,23,15,5766,13,9645,279,2550,304,279,2701,3561,25,4600,25,314,840,20772,1465,1588,92,12478,25,314,8304,553,3019,6291,1588,92,151645,198,151644,77091,198,1454,25,66374,4157,1477,476,6815,279,5189,40549,2168,330,1944,948,287,436,14118,5071,3470,287,562,3760,23349,29169,10187,380,25,16,13,16,3263,715,36842,25,715,16,13,25429,421,279,40549,2168,6724,518,279,2661,19424,2515,364,1944,948,287,436,14118,5071,3470,287,562,3760,23569,17,13,4248,4396,42429,315,12542,829,323,4772,2319,304,697,53127,1034,624,18,13,33563,2615,8541,311,279,19424,1483,369,22266,5335,624,19,13,1416,1667,869,19424,11,33189,448,432,1667,11293,1075,1565,28648,5858,1273,948,287,436,14118,5071,63,1573,30220,23172,3210,624,20,13,5443,264,584,2168,4518,421,4265,66045,476,4396,279,42429,315,12542,323,4772,304,53127,1034,13],"total_duration":4282693135,"load_duration":2406464146,"prompt_eval_count":97,"prompt_eval_duration":49791000,"eval_count":140,"eval_duration":1825137000}%

可以看到回复依然是英文。

尝试法语

第一次回答的依旧是英文,第二次尝试的时候回答的是中文。

curl http://10.16.202.155:30870/api/generate -d '{
  "model": "qwen2-7b-q4",
  "prompt": "Simplify the following Kubernetes error message delimited by triple dashes written in French language; --- Back-off pulling image \"test.my.hub/my/image-not-exist:1.1\" ---. Provide the most possible solution in a step by step style in no more than 280 characters. Write the output in the following format: Error: {Explain error here} Solution: {Step by step solution here};",
  "stream": false
}'
{"model":"qwen2-7b-q4","created_at":"2024-07-19T06:25:31.929820588Z","response":"Error: Image \"test.my.hub/my/image-not-exist:1.1\" doesn't exist on the registry.\n\nSolution: \n1. Confirm Docker Hub or custom registry contains image \"my/image-not-exist:1.1\".\n2. Check spelling and format of the image name.\n3. Validate the registry URL \"test.my.hub/my/\" is correct and accessible.\n4. Pull or create the missing image using Docker commands:\n   - `docker pull test.my.hub/my/image-not-exist:1.1`\n5. Once pulled, use it in Kubernetes deployment configuration properly.","done":true,"done_reason":"stop","context":[151644,872,198,50,70206,279,2701,66374,1465,1943,85328,553,23725,87546,5326,304,8585,4128,26,12448,6841,12462,22266,2168,330,1944,948,287,436,14118,5071,3470,287,562,3760,23349,29169,10187,380,25,16,13,16,1,12448,13,39565,279,1429,3204,6291,304,264,3019,553,3019,1707,304,902,803,1091,220,17,23,15,5766,13,9645,279,2550,304,279,2701,3561,25,4600,25,314,840,20772,1465,1588,92,12478,25,314,8304,553,3019,6291,1588,11061,151645,198,151644,77091,198,1454,25,4654,330,1944,948,287,436,14118,5071,3470,287,562,3760,23349,29169,10187,380,25,16,13,16,1,3171,944,3000,389,279,19424,382,36842,25,715,16,13,33563,40549,26538,476,2526,19424,5610,2168,330,7132,562,3760,23349,29169,10187,380,25,16,13,16,22956,17,13,4248,42429,323,3561,315,279,2168,829,624,18,13,23282,279,19424,5548,330,1944,948,287,436,14118,5071,3470,287,562,3760,11225,374,4396,323,15614,624,19,13,31828,476,1855,279,7402,2168,1667,40549,11293,510,256,481,1565,28648,6815,1273,948,287,436,14118,5071,3470,287,562,3760,23349,29169,10187,380,25,16,13,16,3989,20,13,9646,13238,11,990,432,304,66374,23172,6546,10277,13],"total_duration":4065635366,"load_duration":2152220002,"prompt_eval_count":97,"prompt_eval_duration":50020000,"eval_count":140,"eval_duration":1821685000}%
➜  k8sgpt clear
➜  k8sgpt curl http://10.16.202.155:30870/api/generate -d '{
  "model": "qwen2-7b-q4",
  "prompt": "Simplify the following Kubernetes error message delimited by triple dashes written in French language; --- Back-off pulling image \"test.my.hub/my/image-not-exist:1.1\" ---. Provide the most possible solution in a step by step style in no more than 280 characters. Write the output in the following format: Error: {Explain error here} Solution: {Step by step solution here};",
  "stream": false
}'
{"model":"qwen2-7b-q4","created_at":"2024-07-19T06:25:53.819915902Z","response":"Error: L'image \"test.my.hub/my/image-not-exist:1.1\" n'existe pas dans le registre.\nSolution: \n1. Assurez-vous que l'url du registre est correcte et accessible.\n2. Vérifiez si l'image 'image-not-exist' existe bien dans le registre test.my.hub/my.\n3. Si l'image n'existe pas, créez l'image ou utilisez une image qui existe déjà.","done":true,"done_reason":"stop","context":[151644,872,198,50,70206,279,2701,66374,1465,1943,85328,553,23725,87546,5326,304,8585,4128,26,12448,6841,12462,22266,2168,330,1944,948,287,436,14118,5071,3470,287,562,3760,23349,29169,10187,380,25,16,13,16,1,12448,13,39565,279,1429,3204,6291,304,264,3019,553,3019,1707,304,902,803,1091,220,17,23,15,5766,13,9645,279,2550,304,279,2701,3561,25,4600,25,314,840,20772,1465,1588,92,12478,25,314,8304,553,3019,6291,1588,11061,151645,198,151644,77091,198,1454,25,444,77237,330,1944,948,287,436,14118,5071,3470,287,562,3760,23349,29169,10187,380,25,16,13,16,1,308,57636,16776,6368,6866,512,31247,265,624,36842,25,715,16,13,2688,552,89,44225,1709,326,86531,3845,31247,265,1788,4396,68,1842,15614,624,17,13,647,13763,56509,89,4403,326,77237,364,1805,29169,10187,380,6,28153,14370,6866,512,31247,265,1273,948,287,436,14118,5071,3470,287,562,3760,624,18,13,11832,326,77237,308,57636,16776,6368,11,1560,7888,89,326,77237,5908,68052,89,6185,2168,7774,28153,45839,13],"total_duration":1548776331,"load_duration":15919679,"prompt_eval_count":97,"prompt_eval_duration":33250000,"eval_count":111,"eval_duration":1457631000}%

尝试日语

发现日语第一次就是能够正常回复的。

curl http://10.16.202.155:30870/api/generate -d '{
  "model": "qwen2-7b-q4",
  "prompt": "Simplify the following Kubernetes error message delimited by triple dashes written in Japanese language; --- Back-off pulling image \"test.my.hub/my/image-not-exist:1.1\" ---. Provide the most possible solution in a step by step style in no more than 280 characters. Write the output in the following format: Error: {Explain error here} Solution: {Step by step solution here};",
  "stream": false
}'
{"model":"qwen2-7b-q4","created_at":"2024-07-19T06:17:53.428645035Z","response":"Error: イメージ \"test.my.hub/my/image-not-exist:1.1\" の取得に失敗しました。このイメージは存在しません。 Solution: 以下のようにステップバイステップで問題を解決します。\n\n1. 正しいイメージ名とURLを確認します。\n2. イメージ \"test.my.hub/my/image-not-exist\" の存在を確認してください。もし該当のイメージが存在しない場合、またはイメージの名前が間違いであれば修正します。\n3. Kubernetes デプロイメントやポッドの定義ファイルを更新して正しいイメージ名を使用します。\n4. コンテナの再デプロイまたはリブーストを行います。たとえば、「kubectl rollout restart deployment YOUR_DEPLOYMENT_NAME」でデプロイの再開始が可能です。","done":true,"done_reason":"stop","context":[151644,872,198,50,70206,279,2701,66374,1465,1943,85328,553,23725,87546,5326,304,10769,4128,26,12448,6841,12462,22266,2168,330,1944,948,287,436,14118,5071,3470,287,562,3760,23349,29169,10187,380,25,16,13,16,1,12448,13,39565,279,1429,3204,6291,304,264,3019,553,3019,1707,304,902,803,1091,220,17,23,15,5766,13,9645,279,2550,304,279,2701,3561,25,4600,25,314,840,20772,1465,1588,92,12478,25,314,8304,553,3019,6291,1588,11061,151645,198,151644,77091,198,1454,25,17587,97,38750,77667,330,1944,948,287,436,14118,5071,3470,287,562,3760,23349,29169,10187,380,25,16,13,16,1,96618,101094,19655,20726,115263,128516,1773,50230,133008,15322,47606,124097,71242,24791,1773,12478,25,220,87752,130264,125926,124871,126840,125926,124871,16161,104832,29412,109530,77334,3407,16,13,71928,96,124127,133008,13072,19182,3144,29412,114277,77334,8997,17,13,17587,97,38750,77667,330,1944,948,287,436,14118,5071,3470,287,562,3760,23349,29169,10187,380,1,96618,47606,29412,114277,134093,1773,130603,101366,39165,15767,133008,28195,47606,129306,125725,5373,135954,133008,15767,13072,24562,28195,138031,131738,108271,77334,8997,18,13,66374,20711,229,126574,24218,72490,70189,88054,130083,15767,22382,100942,142022,29412,50007,38826,142404,133008,13072,136187,77334,8997,19,13,66942,15698,56833,95352,15767,87256,67308,126574,24218,135954,36723,51784,60298,19674,127605,60589,1773,27613,19182,125384,35796,79971,71730,17024,23172,20922,2881,71824,5328,4708,10429,16161,67308,126574,24218,15767,87256,105161,28195,136478,1773],"total_duration":2604039968,"load_duration":16855752,"prompt_eval_count":97,"prompt_eval_duration":73669000,"eval_count":187,"eval_duration":2471996000}

使用中文提示词

这时我们尝试手工修改中文提示词,没有用,依旧是英文。

curl http://10.16.202.155:30870/api/generate -d '{
  "model": "qwen2-7b-q4",
  "prompt": "以下 Kubernetes 错误信息的答复请使用中文作答; --- Back-off pulling image \"test.my.hub/my/image-not-exist:1.1\" ---. Provide the most possible solution in a step by step style in no more than 280 characters. Write the output in the following format: Error: {Explain error here} Solution: {Step by step solution here}",
  "stream": false
}'
{"model":"qwen2-7b-q4","created_at":"2024-07-19T06:09:45.437743683Z","response":"Error: `image-not-exist` image doesn't exist on the provided registry.\n\nSolution:\n1. Confirm the correct image name and version: Ensure that \"test.my.hub/my/image-not-exist:1.1\" is accurately spelled and matches the image hosted on the registry.\n2. Check the registry access: Verify your credentials and permissions to pull images from `test.my.hub`. You might need to log in with a specific user or have necessary privileges.\n3. Confirm the registry status: Ensure that the registry server (`test.my.hub`) is up and running, accessible by Kubernetes nodes.\n4. Use an alternative image: If accessing the specified image fails, replace it with a different valid image suitable for your application's requirements.\n\nIf you continue to face issues after following these steps, consider reaching out to the registry service provider or checking their status page for any potential downtime notifications.","done":true,"done_reason":"stop","context":[151644,872,198,87752,66374,93178,247,29056,27369,9370,110463,14880,37029,104811,19403,99590,26,12448,6841,12462,22266,2168,330,1944,948,287,436,14118,5071,3470,287,562,3760,23349,29169,10187,380,25,16,13,16,1,12448,13,39565,279,1429,3204,6291,304,264,3019,553,3019,1707,304,902,803,1091,220,17,23,15,5766,13,9645,279,2550,304,279,2701,3561,25,4600,25,314,840,20772,1465,1588,92,12478,25,314,8304,553,3019,6291,1588,92,151645,198,151644,77091,198,1454,25,1565,1805,29169,10187,380,63,2168,3171,944,3000,389,279,3897,19424,382,36842,510,16,13,33563,279,4396,2168,829,323,2319,25,29279,429,330,1944,948,287,436,14118,5071,3470,287,562,3760,23349,29169,10187,380,25,16,13,16,1,374,29257,67818,323,9071,279,2168,21009,389,279,19424,624,17,13,4248,279,19424,2615,25,25429,697,16387,323,8541,311,6815,5335,504,1565,1944,948,287,436,14118,5071,28587,1446,2578,1184,311,1487,304,448,264,3151,1196,476,614,5871,35732,624,18,13,33563,279,19424,2639,25,29279,429,279,19424,3538,28654,1944,948,287,436,14118,5071,32881,374,705,323,4303,11,15614,553,66374,7798,624,19,13,5443,458,10555,2168,25,1416,31788,279,5189,2168,14525,11,8290,432,448,264,2155,2697,2168,14452,369,697,3766,594,8502,382,2679,498,3060,311,3579,4714,1283,2701,1493,7354,11,2908,18739,700,311,279,19424,2473,9109,476,13295,862,2639,2150,369,894,4650,74854,21969,13],"total_duration":2652872635,"load_duration":16288068,"prompt_eval_count":95,"prompt_eval_duration":73120000,"eval_count":191,"eval_duration":2521665000}%

我尝试在提示词结尾处增加限制,成功输出了中文

curl http://10.16.202.155:30870/api/generate -d '{
  "model": "qwen2-7b-q4",
  "prompt": "Simplify the following Kubernetes error message delimited by triple dashes written in Chinese language; --- Back-off pulling image \"test.my.hub/my/image-not-exist:1.1\" ---. Provide the most possible solution in a step by step style in no more than 280 characters. Write the output in the following format: Error: {Explain error here} Solution: {Step by step solution here}; 请使用中文回答",
  "stream": false
}'
{"model":"qwen2-7b-q4","created_at":"2024-07-19T06:10:44.399012295Z","response":"Error: 镜像\"test.my.hub/my/image-not-exist:1.1\"不存在。Solution: \n1. 检查镜像域名和标签是否正确。\n2. 确保该镜像存在于注册服务器中,如果未存在,则上传正确的镜像文件到相应的Docker registry。\n3. 使用正确的镜像名称和标签在Kubernetes配置或命令中进行引用。","done":true,"done_reason":"stop","context":[151644,872,198,50,70206,279,2701,66374,1465,1943,85328,553,23725,87546,5326,304,8453,4128,26,12448,6841,12462,22266,2168,330,1944,948,287,436,14118,5071,3470,287,562,3760,23349,29169,10187,380,25,16,13,16,1,12448,13,39565,279,1429,3204,6291,304,264,3019,553,3019,1707,304,902,803,1091,220,17,23,15,5766,13,9645,279,2550,304,279,2701,3561,25,4600,25,314,840,20772,1465,1588,92,12478,25,314,8304,553,3019,6291,1588,11061,220,14880,37029,104811,102104,151645,198,151644,77091,198,1454,25,18137,243,250,65101,1,1944,948,287,436,14118,5071,3470,287,562,3760,23349,29169,10187,380,25,16,13,16,1,69184,1773,36842,25,715,16,13,6567,96,222,32876,100811,65101,111584,33108,105151,64471,88991,8997,17,13,10236,94,106,32463,75882,100811,65101,114672,61689,89047,15946,3837,62244,38342,47606,95053,52526,105045,100811,65101,26898,26939,105004,35,13659,19424,8997,18,13,85658,105045,100811,65101,29991,33108,105151,18493,42,29827,85767,57191,106167,15946,71817,103346,1773],"total_duration":1367972501,"load_duration":16162805,"prompt_eval_count":102,"prompt_eval_duration":76594000,"eval_count":94,"eval_duration":1233430000}%

结语

AI 在回答问题的时候,是根据语意理解去作答的,同样要求其返回法语答复,它有时候能响应,有时候不能。如果想要准确的回复,那么需要将提示词限制的更为准确,就如上文在提示词最后强制要求其回答中文一样。 AI 有时候还是比较傻的。


行愚
13 声望2 粉丝