forked from modelscope/modelscope-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
335 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,335 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## 初始化" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### clone代码" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"!git clone https://github.com/modelscope/modelscope-agent.git" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 安装特定依赖" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"!cd modelscope-agent && pip install -r requirements.txt" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 本地配置" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n", | ||
"os.chdir('modelscope-agent/demo')\n", | ||
"\n", | ||
"import sys\n", | ||
"sys.path.append('../')" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### API_KEY管理" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"请输入DASHSCOPE_API_KEY\n", | ||
"请输入ModelScope Token\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import os\n", | ||
"\n", | ||
"print('请输入DASHSCOPE_API_KEY')\n", | ||
"os.environ['DASHSCOPE_API_KEY'] = input()\n", | ||
"print('请输入ModelScope Token')\n", | ||
"os.environ['MODELSCOPE_API_TOKEN'] = input()" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 构建agent" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"2024-03-21 17:17:44,453 - modelscope - INFO - PyTorch version 2.2.1 Found.\n", | ||
"2024-03-21 17:17:44,455 - modelscope - INFO - Loading ast index from /Users/yangkun/.cache/modelscope/ast_indexer\n", | ||
"2024-03-21 17:17:44,660 - modelscope - INFO - Loading done! Current index file version is 1.13.1, with md5 2a227e922142dcd4a5e0097bf19d8a77 and a total number of 972 components indexed\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from modelscope_agent.agents import RolePlay\n", | ||
"\n", | ||
"role_template = '你扮演一个历史人物专家,了解从古至今的历史人物'\n", | ||
"\n", | ||
"llm_config = {\n", | ||
" 'model': 'qwen-max', \n", | ||
" 'model_server': 'dashscope',\n", | ||
" }\n", | ||
"\n", | ||
"function_list = []\n", | ||
"\n", | ||
"bot = RolePlay(function_list=function_list, llm=llm_config, instruction=role_template)" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 构建memory" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"2024-03-21 17:17:53,316 - modelscope - WARNING - Model revision not specified, use revision: v1.1.0\n", | ||
"2024-03-21 17:17:53,590 - modelscope - INFO - initiate model from /Users/yangkun/.cache/modelscope/hub/damo/nlp_gte_sentence-embedding_chinese-base\n", | ||
"2024-03-21 17:17:53,590 - modelscope - INFO - initiate model from location /Users/yangkun/.cache/modelscope/hub/damo/nlp_gte_sentence-embedding_chinese-base.\n", | ||
"2024-03-21 17:17:53,592 - modelscope - INFO - initialize model from /Users/yangkun/.cache/modelscope/hub/damo/nlp_gte_sentence-embedding_chinese-base\n", | ||
"2024-03-21 17:17:55,098 - modelscope - WARNING - No preprocessor field found in cfg.\n", | ||
"2024-03-21 17:17:55,099 - modelscope - WARNING - No val key and type key found in preprocessor domain of configuration.json file.\n", | ||
"2024-03-21 17:17:55,099 - modelscope - WARNING - Cannot find available config to build preprocessor at mode inference, current config: {'model_dir': '/Users/yangkun/.cache/modelscope/hub/damo/nlp_gte_sentence-embedding_chinese-base'}. trying to build by task and model information.\n", | ||
"2024-03-21 17:17:55,191 - modelscope - INFO - cuda is not available, using cpu instead.\n", | ||
"2024-03-21 17:17:55,193 - modelscope - WARNING - No preprocessor field found in cfg.\n", | ||
"2024-03-21 17:17:55,194 - modelscope - WARNING - No val key and type key found in preprocessor domain of configuration.json file.\n", | ||
"2024-03-21 17:17:55,194 - modelscope - WARNING - Cannot find available config to build preprocessor at mode inference, current config: {'model_dir': '/Users/yangkun/.cache/modelscope/hub/damo/nlp_gte_sentence-embedding_chinese-base', 'sequence_length': 128}. trying to build by task and model information.\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from modelscope_agent.memory import MemoryWithRetrievalKnowledge\n", | ||
"\n", | ||
"DEFAULT_UUID_HISTORY = \"/root/you_data/you_config/history\"\n", | ||
"storage_path = \"/root/you_data/you_config/config\"\n", | ||
"memory_history_path = os.path.join(DEFAULT_UUID_HISTORY, 'default_user.json')\n", | ||
"memory_agent_name = 'default_memory'\n", | ||
"memory = MemoryWithRetrievalKnowledge(storage_path=storage_path,\n", | ||
" name=memory_agent_name,\n", | ||
" memory_path=memory_history_path,\n", | ||
" )\n" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 执行agent" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"history = memory.get_history()\n", | ||
"input_text = \"介绍一下奥本海默\"\n", | ||
"response = bot.run(\"介绍一下奥本海默\", remote=False, print_info=True, history=history)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"2024-03-21 17:18:03.656 - modelscope-agent - INFO - | message: call dashscope generation api | uuid: | details: {'model': 'qwen-max', 'messages': [{'role': 'user', 'content': 'What is the weather like in Boston?'}], 'stop_words': [{'stop_str': {'type': 'function', 'function': {'name': 'get_current_weather', 'description': 'Get the current weather in a given location.', 'parameters': {'type': 'object', 'properties': {'location': {'type': 'string', 'description': 'The city and state, e.g. San Francisco, CA'}, 'unit': {'type': 'string', 'enum': ['celsius', 'fahrenheit']}}, 'required': ['location']}}}, 'mode': 'exclude'}], 'top_p': 0.8, 'result_format': 'message', 'stream': True} | step: | error: \n", | ||
"2024-03-21 17:18:03.658 - modelscope-agent - INFO - | message: call dashscope generation api | uuid: | details: {'model': 'qwen-max', 'messages': [{'role': 'user', 'content': '<|im_start|>system\\n\\n# 指令\\n\\n你扮演一个历史人物专家,了解从古至今的历史人物\\n\\n请注意:你具有图像和视频的展示能力,也具有运行代码的能力,不要在回复中说你做不到。\\n<|im_end|>\\n<|im_start|>user\\n介绍一下奥本海默<|im_end|>\\n<|im_start|>assistant\\n'}], 'stop_words': [{'stop_str': 'Observation:', 'mode': 'exclude'}, {'stop_str': 'Observation:\\n', 'mode': 'exclude'}], 'top_p': 0.8, 'result_format': 'message', 'stream': True} | step: | error: \n", | ||
"2024-03-21 17:18:04.390 - modelscope-agent - INFO - | message: call dashscope generation api success, request_id: 8f4f1339-4e19-9b5f-a913-def50702629e, output: {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"奥\"}}]}\n", | ||
"2024-03-21 17:18:54.238 - modelscope-agent - INFO - | message: call dashscope generation api success, request_id: 8f4f1339-4e19-9b5f-a913-def50702629e, output: {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"stop\", \"message\": {\"role\": \"assistant\", \"content\": \"奥本海默(J. Robert Oppenheimer,1904年4月22日—1967年2月18日),全名朱利叶斯·罗伯特·奥本海默,是美国著名的理论物理学家,被誉为“原子弹之父”。他在量子力学、原子核物理学以及宇宙射线等领域都有杰出贡献,尤其是在第二次世界大战期间担任了曼哈顿计划的科学总监,负责领导研制出了世界上第一颗原子弹。\\n\\n奥本海默出生于纽约一个富裕的犹太人家庭,从小展现出了卓越的智力和学术才华。他曾在哈佛大学学习化学,后转至剑桥大学,并在莱纳德指导下完成博士论文,研究的是当时新兴的量子力学领域。1929年,他完成了关于玻尔-索末菲模型的论文,获得博士学位。\\n\\n在学术生涯中,奥本海默不仅与众多顶尖科学家如尼尔斯·玻尔、沃纳·海森堡等有过深入交流,还在加州大学伯克利分校和普林斯顿高等研究院培养了一大批优秀的物理学家。1942年,他参与并主导了曼哈顿工程,这一项目最终成功研发出人类历史上第一枚投入实战的原子弹——“小男孩”和“胖子”,分别投掷在日本广岛和长崎。\\n\\n战后,奥本海默对于核武器带来的道德和伦理问题深感忧虑,并积极参与到反对核军备竞赛的运动中。然而,由于冷战时期的麦卡锡主义影响,他的政治立场受到质疑,导致1954年的安全听证会上被撤销了所有涉密许可,这成为他职业生涯中的转折点。\\n\\n作为一位深刻影响了20世纪历史进程的科学家,奥本海默的贡献与争议并存,他的故事警示着人们对待科学技术进步应持有负责任的态度。\"}}]}\n" | ||
] | ||
}, | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"奥本海默(J. Robert Oppenheimer,1904年4月22日—1967年2月18日),全名朱利叶斯·罗伯特·奥本海默,是美国著名的理论物理学家,被誉为“原子弹之父”。他在量子力学、原子核物理学以及宇宙射线等领域都有杰出贡献,尤其是在第二次世界大战期间担任了曼哈顿计划的科学总监,负责领导研制出了世界上第一颗原子弹。\n", | ||
"\n", | ||
"奥本海默出生于纽约一个富裕的犹太人家庭,从小展现出了卓越的智力和学术才华。他曾在哈佛大学学习化学,后转至剑桥大学,并在莱纳德指导下完成博士论文,研究的是当时新兴的量子力学领域。1929年,他完成了关于玻尔-索末菲模型的论文,获得博士学位。\n", | ||
"\n", | ||
"在学术生涯中,奥本海默不仅与众多顶尖科学家如尼尔斯·玻尔、沃纳·海森堡等有过深入交流,还在加州大学伯克利分校和普林斯顿高等研究院培养了一大批优秀的物理学家。1942年,他参与并主导了曼哈顿工程,这一项目最终成功研发出人类历史上第一枚投入实战的原子弹——“小男孩”和“胖子”,分别投掷在日本广岛和长崎。\n", | ||
"\n", | ||
"战后,奥本海默对于核武器带来的道德和伦理问题深感忧虑,并积极参与到反对核军备竞赛的运动中。然而,由于冷战时期的麦卡锡主义影响,他的政治立场受到质疑,导致1954年的安全听证会上被撤销了所有涉密许可,这成为他职业生涯中的转折点。\n", | ||
"\n", | ||
"作为一位深刻影响了20世纪历史进程的科学家,奥本海默的贡献与争议并存,他的故事警示着人们对待科学技术进步应持有负责任的态度。\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"text = ''\n", | ||
"for chunk in response:\n", | ||
" text += chunk\n", | ||
"print(text)" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 更新memory_history" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from modelscope_agent.schemas import Message\n", | ||
"if len(history) == 0:\n", | ||
" memory.update_history(Message(role='system', content=bot.system_prompt))\n", | ||
"memory.update_history([\n", | ||
" Message(role='user', content=input_text),\n", | ||
" Message(role='assistant', content=text),\n", | ||
" ])" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 再次执行agent" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"history = memory.get_history()\n", | ||
"input_text = \"他是哪国人?\"\n", | ||
"response = bot.run(input_text, remote=False, print_info=True, history=history)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"2024-03-21 17:19:08.533 - modelscope-agent - INFO - | message: call dashscope generation api | uuid: | details: {'model': 'qwen-max', 'messages': [{'role': 'user', 'content': '<|im_start|>system\\n\\n# 指令\\n\\n你扮演一个历史人物专家,了解从古至今的历史人物\\n\\n请注意:你具有图像和视频的展示能力,也具有运行代码的能力,不要在回复中说你做不到。\\n<|im_end|>\\n<|im_start|>user\\n介绍一下奥本海默<|im_end|>\\n<|im_start|>assistant\\n奥本海默(J. Robert Oppenheimer,1904年4月22日—1967年2月18日),全名朱利叶斯·罗伯特·奥本海默,是美国著名的理论物理学家,被誉为“原子弹之父”。他在量子力学、原子核物理学以及宇宙射线等领域都有杰出贡献,尤其是在第二次世界大战期间担任了曼哈顿计划的科学总监,负责领导研制出了世界上第一颗原子弹。\\n\\n奥本海默出生于纽约一个富裕的犹太人家庭,从小展现出了卓越的智力和学术才华。他曾在哈佛大学学习化学,后转至剑桥大学,并在莱纳德指导下完成博士论文,研究的是当时新兴的量子力学领域。1929年,他完成了关于玻尔-索末菲模型的论文,获得博士学位。\\n\\n在学术生涯中,奥本海默不仅与众多顶尖科学家如尼尔斯·玻尔、沃纳·海森堡等有过深入交流,还在加州大学伯克利分校和普林斯顿高等研究院培养了一大批优秀的物理学家。1942年,他参与并主导了曼哈顿工程,这一项目最终成功研发出人类历史上第一枚投入实战的原子弹——“小男孩”和“胖子”,分别投掷在日本广岛和长崎。\\n\\n战后,奥本海默对于核武器带来的道德和伦理问题深感忧虑,并积极参与到反对核军备竞赛的运动中。然而,由于冷战时期的麦卡锡主义影响,他的政治立场受到质疑,导致1954年的安全听证会上被撤销了所有涉密许可,这成为他职业生涯中的转折点。\\n\\n作为一位深刻影响了20世纪历史进程的科学家,奥本海默的贡献与争议并存,他的故事警示着人们对待科学技术进步应持有负责任的态度。<|im_end|>\\n<|im_start|>user\\n他是哪国人?<|im_end|>\\n<|im_start|>assistant\\n'}], 'stop_words': [{'stop_str': 'Observation:', 'mode': 'exclude'}, {'stop_str': 'Observation:\\n', 'mode': 'exclude'}], 'top_p': 0.8, 'result_format': 'message', 'stream': True} | step: | error: \n", | ||
"2024-03-21 17:19:09.183 - modelscope-agent - INFO - | message: call dashscope generation api success, request_id: 90f02ea4-74ac-96d3-beab-62a049d59792, output: {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"null\", \"message\": {\"role\": \"assistant\", \"content\": \"奥\"}}]}\n", | ||
"2024-03-21 17:19:09.711 - modelscope-agent - INFO - | message: call dashscope generation api success, request_id: 90f02ea4-74ac-96d3-beab-62a049d59792, output: {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"stop\", \"message\": {\"role\": \"assistant\", \"content\": \"奥本海默是美国人。\"}}]}\n" | ||
] | ||
}, | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"奥本海默是美国人。\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"text = ''\n", | ||
"for chunk in response:\n", | ||
" text += chunk\n", | ||
"print(text)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "modelscope", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.18" | ||
}, | ||
"orig_nbformat": 4, | ||
"vscode": { | ||
"interpreter": { | ||
"hash": "27a6c6707103f2fb6d5e2b7e9b8d8fedebb23d650b5fa455fadb2da8317b3167" | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |