Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tencent hunyuan api #1359

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

david-zlj
Copy link
Contributor

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

参考资料

@david-zlj
Copy link
Contributor Author

对应文档:geekan/MetaGPT-docs#153

@usamimeri
Copy link
Contributor

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

@@ -59,6 +60,10 @@ class LLMConfig(YamlModel):
secret_key: Optional[str] = None
endpoint: Optional[str] = None # for self-deployed model on the cloud

# For Tencent Hunyuan
secret_id: Optional[str] = None
secret_key: Optional[str] = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

secret key duplicates with line 60

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已处理

Comment on lines 86 to 90
"hunyuan-pro": {"prompt": 0.03, "completion": 0.1},
"hunyuan-standard": {"prompt": 0.0045, "completion": 0.005},
"hunyuan-standard-256k": {"prompt": 0.015, "completion": 0.06},
"hunyuan-lite": {"prompt": 0.0, "completion": 0.0},
"hunyuan-embedding": {"prompt": 0.0007, "completion": 0.0007},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be USD instead of CNY.
try:

{
 "hunyuan-pro": {"prompt": "0.00420", "completion": "0.01400"},
 "hunyuan-standard": {"prompt": "0.00063", "completion": "0.00070"},
 "hunyuan-standard-256k": {"prompt": "0.00210", "completion": "0.00840"},
 "hunyuan-lite": {"prompt": "0.0", "completion": "0.0"}
 }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已处理

@codecov-commenter
Copy link

codecov-commenter commented Aug 1, 2024

⚠️ 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 ⚠️

❗ 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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@david-zlj
Copy link
Contributor Author

@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.

@geekan geekan requested a review from better629 October 11, 2024 09:36
self.secret_key = self.config.secret_key
self.endpoint = self.config.endpoint
self.model = self.config.model
self.region = ""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we config this? or use self.config.region_name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.region的值为空字符串时,表示自动路由选路。建议保持现状,日后有需要再更改为可配置项。

@register_provider(LLMType.HUNYUAN)
class HunYuanLLM(BaseLLM):
"""参考资料
腾讯混元大模型产品概述:https://cloud.tencent.com/document/product/1729/104753
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems hunyuan provide a compatible interface https://cloud.tencent.com/document/product/1729/111007, should we to re-create a provider ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

近期混元 API 兼容了 OpenAI 的接口规范,初步来看是更好的方案。

def _make_request(
self,
messages: list[dict],
timeout=USE_CONFIG_TIMEOUT,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeout not used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已删除

req.from_json_string(json.dumps(params))
return req

async def _achat_completion(self, messages: list[dict], timeout=USE_CONFIG_TIMEOUT) -> ChatCompletionsResponse:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing get_choice_text implement

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无需重写get_choice_text()方法

腾讯混元大模型价格说明:https://cloud.tencent.com/document/product/1729/97731
"""
HUNYUAN_MODEL_TOKEN_COSTS = {
"hunyuan-pro": {"prompt": 0.0042, "completion": 0.014},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing hunyuan-turbo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已添加

@geekan
Copy link
Owner

geekan commented Oct 20, 2024

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

@david-zlj
Copy link
Contributor Author

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

ok

…ed timeout in hunyuan_api.py; fix price in token_counter.py
@david-zlj
Copy link
Contributor Author

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

已处理

@geekan
Copy link
Owner

geekan commented Oct 21, 2024

Can you resolve the current conflict?

@geekan
Copy link
Owner

geekan commented Oct 21, 2024

@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?

@david-zlj
Copy link
Contributor Author

@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 接口的方案。

@geekan
Copy link
Owner

geekan commented Oct 21, 2024

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

@david-zlj
Copy link
Contributor Author

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

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

@geekan
Copy link
Owner

geekan commented Oct 23, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants