MetaGPT icon indicating copy to clipboard operation
MetaGPT copied to clipboard

feat: add tencent hunyuan api

Open david-zlj opened this issue 1 year ago • 13 comments

Features

  • 对接腾讯混元大模型API

Feature Docs

Influence

Result

测试内容:

import asyncio
from metagpt.context import Context
from metagpt.roles.product_manager import ProductManager
from metagpt.logs import logger


async def main():
    msg = "Write a PRD for a snake game"
    context = Context()
    role = ProductManager(context=context)
    while msg:
        msg = await role.run(msg)
        logger.info(str(msg))


if __name__ == "__main__":
    asyncio.run(main())

测试输出结果:

2024-06-20 10:10:07.905 | INFO     | metagpt.const:get_metagpt_package_root:29 - Package root set to e:\python_workspace\aiagent\metagpt_a\metagpt
2024-06-20 10:10:12.182 | INFO     | metagpt.roles.role:_act:402 - Alice(Product Manager): to do PrepareDocuments(PrepareDocuments)
2024-06-20 10:10:12.502 | INFO     | metagpt.utils.file_repository:save:57 - save to: E:\python_workspace\aiagent\metagpt_a\MetaGPT\workspace\20240620101012\docs\requirement.txt
2024-06-20 10:10:12.505 | INFO     | metagpt.roles.role:_act:402 - Alice(Product Manager): to do WritePRD(WritePRD)
2024-06-20 10:10:12.508 | INFO     | metagpt.actions.write_prd:run:86 - New requirement detected: Write a PRD for a snake game
[CONTENT]
{
    "Language": "en_us",
    "Programming Language": "Python",
    "Original Requirements": "Write a PRD for a snake game",
    "Project Name": "snake_game",
    "Product Goals": [
        "Create an engaging user experience",
        "Improve accessibility, be responsive",
        "More beautiful UI"
    ],
    "User Stories": [
        "As a player, I want to control the snake using arrow keys",
        "As a player, I want to eat food to grow longer",
        "As a player, I want to avoid hitting walls or myself",
        "As a player, I want to see my high score and current score",
        "As a player, I want to have multiple levels with increasing difficulty"
    ],
    "Competitive Analysis": [
        "Snake Classic: Basic gameplay, no frills",
        "Super Snake: Colorful graphics, power-ups, and levels",
        "Snake Adventure: Engaging storyline, different environments"
    ],
    "Competitive Quadrant Chart": "quadrantChart
    title \"Reach and engagement of campaigns\"
    x-axis \"Low Reach\" --> \"High Reach\"
    y-axis \"Low Engagement\" --> \"High Engagement\"
    quadrant-1 \"We should expand\"
    quadrant-2 \"Need to promote\"
    quadrant-3 \"Re-evaluate\"
    quadrant-4 \"May be improved\"
    \"Campaign A\": [0.3, 0.6]
    \"Campaign B\": [0.45, 0.23]
    \"Campaign C\": [0.57, 0.69]
    \"Campaign D\": [0.78, 0.34]
    \"Campaign E\": [0.40, 0.34]
    \"Campaign F\": [0.35, 0.78]
    \"Our Target Product\": [0.5, 0.6]",
    "Requirement Analysis": "",
    "Requirement Pool": [
        [
            "P0",
            "Implement the snake game logic"
        ],
        [
            "P1",
            "Design and implement the user interface"
        ],
        [
            "P2",
            "Add power-ups and level progression"
        ],
        [
            "P3",
            "Ensure responsive gameplay on various devices"
        ],
        [
            "P4",
            "Implement scoring and high score tracking"
        ]
    ],
    "UI Design draft": "A simple grid layout with a snake represented by a series of connected squares. The snake moves automatically and the player can control its direction using arrow keys. The game includes a scoreboard at the top and a play area in the center. Power-ups appear randomly and change the snake's behavior temporarily.",
    "Anything UNCLEAR": ""
}
2024-06-20 10:10:21.620 | WARNING  | metagpt.utils.cost_manager:update_cost:49 - Model hunyuan-standard not found in TOKEN_COSTS.
[/CONTENT]
2024-06-20 10:10:21.638 | INFO     | metagpt.utils.git_repository:rename_root:203 - Delete directory E:\python_workspace\aiagent\metagpt_a\MetaGPT\workspace\snake_game
2024-06-20 10:10:21.749 | WARNING  | metagpt.utils.git_repository:rename_root:214 - Move E:\python_workspace\aiagent\metagpt_a\MetaGPT\workspace\20240620101012 to E:\python_workspace\aiagent\metagpt_a\MetaGPT\workspace\snake_game error: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: 'E:\\python_workspace\\aiagent\\metagpt_a\\MetaGPT\\workspace\\20240620101012'
2024-06-20 10:10:21.752 | INFO     | metagpt.utils.git_repository:rename_root:219 - Rename directory E:\python_workspace\aiagent\metagpt_a\MetaGPT\workspace\20240620101012 to E:\python_workspace\aiagent\metagpt_a\MetaGPT\workspace\snake_game
2024-06-20 10:10:21.760 | INFO     | metagpt.utils.file_repository:save:57 - save to: E:\python_workspace\aiagent\metagpt_a\MetaGPT\workspace\snake_game\docs\prd\20240620101021.json
C:\Users\zlj\AppData\Roaming\npm\mmdc
C:\Users\zlj\AppData\Roaming\npm\mmdc.cmd
2024-06-20 10:10:21.944 | INFO     | metagpt.utils.mermaid:mermaid_to_file:44 - Generating E:\python_workspace\aiagent\metagpt_a\MetaGPT\workspace\snake_game\resources\competitive_analysis\20240620101021.pdf..
2024-06-20 10:10:24.224 | INFO     | metagpt.utils.mermaid:mermaid_to_file:68 - Generating single mermaid chart

2024-06-20 10:10:24.226 | INFO     | metagpt.utils.mermaid:mermaid_to_file:44 - Generating E:\python_workspace\aiagent\metagpt_a\MetaGPT\workspace\snake_game\resources\competitive_analysis\20240620101021.svg..
2024-06-20 10:10:26.470 | INFO     | metagpt.utils.mermaid:mermaid_to_file:68 - Generating single mermaid chart

2024-06-20 10:10:26.472 | INFO     | metagpt.utils.mermaid:mermaid_to_file:44 - Generating E:\python_workspace\aiagent\metagpt_a\MetaGPT\workspace\snake_game\resources\competitive_analysis\20240620101021.png..
2024-06-20 10:10:28.837 | INFO     | metagpt.utils.mermaid:mermaid_to_file:68 - Generating single mermaid chart

2024-06-20 10:10:28.841 | INFO     | metagpt.utils.file_repository:save:57 - save to: E:\python_workspace\aiagent\metagpt_a\MetaGPT\workspace\snake_game\resources\prd\20240620101021.md
2024-06-20 10:10:28.843 | INFO     | __main__:main:16 - Alice(Product Manager): {'docs': {'20240620101021.json': {'root_path': 'docs\\prd', 'filename': '20240620101021.json', 'content': '{"Language":"en_us","Programming Language":"Python","Original Requirements":"Write a PRD for a snake game","Project Name":"snake_game","Product Goals":["Create an engaging user experience","Improve accessibility, be responsive","More beautiful UI"],"User Stories":["As a player, I want to control the snake using arrow keys","As a player, I want to eat food to grow longer","As a player, I want to avoid hitting walls or myself","As 
a player, I want to see my high score and current score","As a player, I want to have multiple levels with increasing difficulty"],"Competitive Analysis":["Snake Classic: Basic gameplay, no frills","Super Snake: Colorful graphics, power-ups, and levels","Snake Adventure: Engaging storyline, different environments"],"Competitive Quadrant Chart":"quadrantChart\\n    title \\"Reach and engagement of campaigns\\"\\n    x-axis \\"Low Reach\\" --> \\"High Reach\\"\\n    y-axis \\"Low Engagement\\" --> \\"High Engagement\\"\\n    quadrant-1 \\"We should expand\\"\\n    quadrant-2 \\"Need to promote\\"\\n    quadrant-3 \\"Re-evaluate\\"\\n    quadrant-4 \\"May be improved\\"\\n    \\"Campaign A\\": [0.3, 0.6]\\n    \\"Campaign B\\": [0.45, 0.23]\\n    \\"Campaign C\\": [0.57, 0.69]\\n    \\"Campaign D\\": [0.78, 0.34]\\n    \\"Campaign E\\": [0.40, 0.34]\\n    \\"Campaign F\\": [0.35, 0.78]\\n    \\"Our Target Product\\": [0.5, 0.6]","Requirement Analysis":"","Requirement Pool":[["P0","Implement the snake game logic"],["P1","Design and implement the user interface"],["P2","Add power-ups and level progression"],["P3","Ensure responsive gameplay on various devices"],["P4","Implement scoring and high score tracking"]],"UI Design draft":"A simple grid layout with a snake represented by a series of connected squares. The snake moves automatically and the player can control its direction using arrow keys. The game includes a scoreboard at the top and a play area in the center. Power-ups appear randomly and change the snake\'s behavior temporarily.","Anything UNCLEAR":""}'}}}
2024-06-20 10:10:28.846 | INFO     | __main__:main:16 - None

Other

参考资料

  • 腾讯混元大模型产品概述:https://cloud.tencent.com/document/product/1729/104753
  • 腾讯混元API接口说明:https://cloud.tencent.com/document/api/1729/105701
  • 腾讯混元Python SDK源码:https://github.com/TencentCloud/tencentcloud-sdk-python/blob/master/tencentcloud/hunyuan/v20230901/models.py
  • 腾讯混元大模型价格说明:https://cloud.tencent.com/document/product/1729/97731

david-zlj avatar Jun 20 '24 02:06 david-zlj

对应文档:https://github.com/geekan/MetaGPT-docs/pull/153

david-zlj avatar Jun 20 '24 07:06 david-zlj

Amazing work! I have reviewed it and provided some potential improvement suggestions.

usamimeri avatar Jun 20 '24 09:06 usamimeri

:warning: Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 39.70588% with 41 lines in your changes missing coverage. Please review.

Project coverage is 62.25%. Comparing base (5446c7e) to head (27a0f09). Report is 44 commits behind head on main.

Files with missing lines Patch % Lines
metagpt/provider/hunyuan_api.py 35.93% 41 Missing :warning:

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1359      +/-   ##
==========================================
- Coverage   62.34%   62.25%   -0.09%     
==========================================
  Files         283      284       +1     
  Lines       17371    17439      +68     
==========================================
+ Hits        10830    10857      +27     
- Misses       6541     6582      +41     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov-commenter avatar Aug 01 '24 00:08 codecov-commenter

@usamimeri 请问Codecov这个要怎样处理?

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 39.70588% with 41 lines in your changes missing coverage. Please review.

Project coverage is 62.25%. Comparing base (5446c7e) to head (27a0f09).

Files Patch % Lines metagpt/provider/hunyuan_api.py 35.93% 41 Missing ⚠️ ❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ☔ View full report in Codecov by Sentry. 📢 Have feedback on the report? Share it here.

david-zlj avatar Aug 03 '24 03:08 david-zlj

Hello, could you please take another look at the code review?

geekan avatar Oct 20 '24 06:10 geekan

Hello, could you please take another look at the code review?

ok

david-zlj avatar Oct 21 '24 03:10 david-zlj

Hello, could you please take another look at the code review?

已处理

david-zlj avatar Oct 21 '24 07:10 david-zlj

Can you resolve the current conflict?

geekan avatar Oct 21 '24 08:10 geekan

@better629 Can you do another round of code review? I'm not sure what to do with new providers, especially if they provide OpenAI compatible interfaces? @david-zlj What are the benefits of incorporating this code? Does it bring any unique interface capabilities to Hunyuan?

geekan avatar Oct 21 '24 10:10 geekan

@better629 Can you do another round of code review? I'm not sure what to do with new providers, especially if they provide OpenAI compatible interfaces? @david-zlj What are the benefits of incorporating this code? Does it bring any unique interface capabilities to Hunyuan?

应该没有。既然混元 API 兼容了 OpenAI 的接口规范,预期效果会更简单、兼容性更强。建议放弃本方案,@better629 改用兼容OpenAI 接口的方案。

david-zlj avatar Oct 21 '24 12:10 david-zlj

@david-zlj I think you can register an LLMType to the OpenAI class and then add the document

geekan avatar Oct 21 '24 12:10 geekan

@david-zlj I think you can register an LLMType to the OpenAI class and then add the document

没看懂。能提供更多参考信息?

david-zlj avatar Oct 22 '24 12:10 david-zlj

https://github.com/geekan/MetaGPT/blob/8b209d4e17ad7dfc1ad7a80505eac42f71228734/metagpt/provider/openai_api.py#L43C1-L54C26

Just register a type on OpenAI provider. Documentation can be pushed to https://github.com/geekan/MetaGPT-docs/

geekan avatar Oct 23 '24 03:10 geekan