1、部分效果展示
- 访问阿里云资源
- 生成代码
2、前期准备
2.1、获取RAM密钥和大模型密钥
AccessKey管理-->继续使用AccessKey-->创建AccessKey,传送门
模块服务灵积-->管理-->API-KEY管理,传送门
RAM访问控制-->权限管路-->授权,传送门
2.2、基本环境配置
开始之前,先安装依赖,使用的python环境,所以使用pip命令安装就行:
pip install dashscope==1.13.6
pip install langchain==0.1.0
pip install langchain_core==0.1.8
pip install langchainhub==0.1.14
pip install langchain-experimental==0.0.49
pip install beautifulsoup4==4.12.2
pip install html2text==2020.1.16
pip install alibabacloud_cloudcontrol20220830==1.1.0
Windows 环境下:可以在终端使用 set 命令设置环境变量
set DASHSCOPE_API_KEY=sk-****
set ALIBABA_CLOUD_ACCESS_KEY_ID=your access key id
set ALIBABA_CLOUD_ACCESS_KEY_SECRET=your access key secret
由于Windows的各种权限安全问题,有可能在IDEA里面会识别不到,建议直接将这三个变量写入系统变量里。
3、编写代码
- 预览代码
- 完整代码
# -*- coding: utf-8 -*-
import json
import os
import sys
from typing import Any
from langchain_core.tools import BaseTool
from alibabacloud_cloudcontrol20220830.client import Client as cloudcontrol20220830Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_cloudcontrol20220830 import models as cloudcontrol_20220830_models
from alibabacloud_tea_util import models as util_models
from langchain import hub
from langchain.agents import AgentExecutor
from langchain.agents import create_react_agent
from langchain_community.llms import Tongyi
# 阿里云资源查询工具
class AliyunResourceTool(BaseTool):
"""查询阿里云资源的工具"""
name: str = "阿里云的云资源查询工具"
description: str = (
"用于查询账号下的阿里云的源信息,需要指通过 json 指定产品名 product、资源类型 resource、地域 region。"
"ECS 的默认资源为 Instance,VPC 的默认资源为 VPC。"
"region 只能是某一个指定的地域,参考格式:cn-hangzhou、cn-beijing、us-east-1"
)
def _run(self, params) -> Any:
params_dict = json.loads(params)
request_path = f'/api/v1/providers/Aliyun/products/{params_dict["product"]}/resources/{params_dict["resource"]}'
get_resources_request = cloudcontrol_20220830_models.GetResourcesRequest(
region_id=params_dict.get("region") or 'cn-hangzhou',
max_results=2
)
runtime = util_models.RuntimeOptions()
headers = {}
try:
config = open_api_models.Config(
access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
)
# Endpoint 请参考 https://api.aliyun.com/product/cloudcontrol
config.endpoint = f'cloudcontrol.aliyuncs.com'
client = cloudcontrol20220830Client(config)
# 复制代码运行请自行打印 API 的返回值
result = client.get_resources_with_options(request_path, get_resources_request, headers, runtime)
return result.body
except Exception as error:
# 错误 message
print(error)
return '查询失败'
# 定义通义千问大模型AGENT
class MyAgent():
def __init__(self):
self.agent_executor=self.assemble_agent_executor()
def assemble_agent_executor(self):
model = Tongyi()
model.model_name = 'qwen-max'
model.model_kwargs = {'temperature': 0.5}
tools = [
AliyunResourceTool(),
]
prompt = hub.pull("hwchase17/react")
agent = create_react_agent(model, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
return agent_executor
def ask(self, question):
result = self.agent_executor.invoke({'input': question})
return result['output']
if __name__ == '__main__':
if len(sys.argv) < 2:
print('请输入问题')
sys.exit(1)
question = sys.argv[1]
print('问题:' + question)
llm = MyAgent()
result = llm.ask(question)
print(result)
4、高阶操作
由于比较懒,建议去官方文档看看,很详细。传送门
115 comments
你的文章充满了欢乐,让人忍不住一笑。 https://www.yonboz.com/video/91790.html
你的文章内容非常用心,让人感动。 https://www.4006400989.com/qyvideo/38491.html
你的文章让我感受到了不一样的风景,谢谢分享。 http://www.55baobei.com/huSdXqcIND.html
《孤独的野兽》国产剧高清在线免费观看:https://www.jgz518.com/xingkong/30153.html
《留给女儿的日记》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/83663.html
《海洋天堂》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/24199.html
你的文章让我感受到了生活的美好,谢谢! http://www.55baobei.com/ZJEaocX9RA.html
《快乐之后》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/15336.html
你的才华让人瞩目,期待你的更多文章。 https://www.yonboz.com/video/53305.html
你的文章让我感受到了无尽的欢乐,谢谢分享。 http://www.55baobei.com/js3siswnr8.html
你的文章内容非常专业,让人佩服。 http://www.55baobei.com/HUezyloHuS.html
《赤赤炼恋》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/10237.html
你的文章让我感受到了无尽的欢乐,谢谢分享。 https://www.yonboz.com/video/35621.html
你的文章让我心情愉悦,真是太棒了! https://www.4006400989.com/qyvideo/78221.html
你的才华让人瞩目,期待你的更多文章。 http://www.55baobei.com/FlYZqDJISO.html
你的文章内容非常卖力,让人点赞。 https://www.4006400989.com/qyvideo/48866.html
你的文章充满了智慧,让人敬佩。 https://www.yonboz.com/video/38941.html
《极寒之地》科幻片高清在线免费观看:https://www.jgz518.com/xingkong/102190.html
《枪声国语》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/95354.html
《青春的脚步》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/85691.html