初见AI-Agent——Autogen系列01

作者: 引线小白-本文永久链接:httpss://www.limoncc.com/post/a0fe24f2ca704c1e/
知识共享许可协议: 本博客采用署名-非商业-禁止演绎4.0国际许可证

一、前置条件

具体安装就不讲了,参考官方文档:Installation | AutoGen。llama2或者baichuan2模型的部署,网络上的文章已经很多,这里也不讲。这里就说一下gguf格式的baichuan2-7b模型去哪里下载:

baichuan2-7b-chat

不要自己用llama.ccp转换,这样会出现问题:停不下来和不断重复。如何想自己解决,参考这[issues/3270]。对于英文模型建议使用Mistral-7B-Instruct-v0.1。中文模型你可以关注Aquila2-34B这个是目前最强中文开源模型(2023-10-12)。还有一个也可以关注colossal-llama2-7b它超越了llama-13b,中文能力也非常不错。开启openAI的API服务就是使用llama_cpp.server。不过它不能同时启动多个大模型服务。如何要启动多个大模型服务可以直接魔改,或者你追求性能,你也可以使用rust-llama.cpp使用axum编写程序来启动服务。

如果你偏爱chatglm,你可以参考官方的这个文章Use AutoGen for Local LLMs | AutoGen

好,现在假设你解决了模型部署和API服务启动、开发环境安装问题。下面开始一个例子。

二、极简入门

我们开始一个例子,首先已经假设解决了模型部署和API服务启动、开发环境安装问题。这个时候你首先应该去了解一下openai的API,它现在是大模型API事实标准,其他模型的API服务都是实现了openai的。网址在这里

OpenAI接口文档中文版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from autogen import oai
from openai.openai_object import OpenAIObject

# create a text completion request
response: OpenAIObject = oai.Completion.create(
config_list=[
{
"model": "baichuan2-7b",
"api_base": "http://localhost:8000/v1",
"api_type": "open_ai",
"api_key": "NULL", # 仅仅是个占位符
}
],
prompt="你好呀!",
)
print(response)
text = response.get("choices")[0].get("text")
print(text)

输出为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"id": "cmpl-48e97360-7348-4c82-bbb4-f40684f5bc69",
"object": "text_completion",
"created": 1697115554,
"model": "baichuan2-7b",
"choices": [
{
"text": "\u4f5c\u4e3a\u4e00\u4e2a\u5927\u8bed\u8a00\u6a21\u578b\uff0c\u5f88\u9ad8\u5174\u4e3a\u60a8\u63d0\u4f9b\u5e2e\u52a9\u3002\u8bf7\u95ee\u60a8\u6709\u4ec0\u4e48\u95ee\u9898\u6216\u9700\u8981\u5e2e\u52a9\u7684\u5730\u65b9",
"index": 0,
"logprobs": null,
"finish_reason": "length"
}
],
"usage": {
"prompt_tokens": 5,
"completion_tokens": 16,
"total_tokens": 21
},
"cost": 0,
"config_id": 0,
"pass_filter": true
}

作为一个大语言模型,很高兴为您提供帮助。请问您有什么问题或需要帮助的地方
这个下面我们再来一个例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from autogen import oai
response = oai.ChatCompletion.create(
config_list=[
{
"model": "baichuan2-7b",
"api_base": "http://localhost:8000/v1",
"api_type": "open_ai",
"api_key": "NULL",
}
],
messages=[
{"role": "system", "content": "你是一名资深的大语言模型领域的专家,精通模型架构原理和落地应用实践"},
{"role": "user", "content": "你好呀!"}
],
temperature=0.2,
top_k=1,
top_p=0.96,
repeat_penalty=1.1,
stop=["</s>"],
max_tokens=1024,
stream=False
)
print(response)

content = response.get("choices")[0].get("message").get("content")

print(content)

这个是输出结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
"id": "chatcmpl-73791cbb-e82d-4f7b-978d-a6a298c7d9e6",
"object": "chat.completion",
"created": 1697116018,
"model": "baichuan2-7b",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\u5f88\u9ad8\u5174\u80fd\u4e0e\u60a8\u4ea4\u6d41\u5173\u4e8e\u5927\u8bed\u8a00\u6a21\u578b\u9886\u57df\u7684\u4e00\u4e9b\u77e5\u8bc6\u548c\u7ecf\u9a8c\u3002\u5982\u679c\u60a8\u6709\u4efb\u4f55\u95ee\u9898\u6216\u60f3\u8981\u4e86\u89e3\u67d0\u4e2a\u65b9\u9762\u7684\u4fe1\u606f\uff0c\u8bf7\u968f\u65f6\u63d0\u95ee\uff0c\u6211\u4f1a\u5c3d\u6211\u6240\u80fd\u4e3a\u60a8\u63d0\u4f9b\u5e2e\u52a9\u548c\u5efa\u8bae\u3002"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 44,
"completion_tokens": 35,
"total_tokens": 79
},
"cost": 0,
"config_id": 0,
"pass_filter": true
}
很高兴能与您交流关于大语言模型领域的一些知识和经验。如果您有任何问题或想要了解某个方面的信息,请随时提问,我会尽我所能为您提供帮助和建议。

Chat/Completion是非常重要的API,务必熟悉每一个参数的含义。这里重点介绍一下massage
messages=[
{“role”: “system”, “content”: “你是一名资深的大语言模型领域的专家,精通模型架构原理和落地应用实践”}, {“role”: “user”, “content”: “你好呀!”}
],
messages 数组类型,必填项。包含对话历史描述的消息列表。
role 字符串类型,必填项。该消息作者的角色。可选值为 system、user 或 assistant。
content 字符串类型,必填项。消息的内容。
name 字符串类型,可选。该消息的作者名称。可包含小写字母、大写字母、数字和下划线,最长长度为 64 个字符。
这是我们设定AI角色和提示词的地方。非常重要。弄完这些你基本就入门了。

三、简单的Agent

下面实现一个简单Agent,autogen远比langchain简单。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import autogen

# 1、建立必要配置
config_list = [
{
"model": "Mistral-7B",
"api_base": "http://localhost:8000/v1",
"api_type": "open_ai",
"api_key": "NULL",
}]


# 2、大模型请求配置
llm_config = {
"request_timeout": 600,
"seed": 45, # change the seed for different trials
"config_list": config_list,
"temperature": 0,
"max_tokens":16000,
}

# 3、新建一个助理智能体
assistant = autogen.AssistantAgent(
name="assistant",
llm_config=llm_config,
is_termination_msg=lambda x: True if "TERMINATE" in x.get("content") else False,
)

#创建名为 user_proxy 的用户代理实例,这里定义为进行干预
user_proxy = autogen.UserProxyAgent(
name="user_proxy",
human_input_mode="TERMINATE",
max_consecutive_auto_reply=1,
is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
code_execution_config={"work_dir": "web"},
llm_config=llm_config,
system_message="""Reply TERMINATE if the task has been solved at full satisfaction.
Otherwise, reply CONTINUE, or the reason why the task is not solved yet.""")


task1 = """今天是星期几?,还有几天周末?请告诉我答案。"""
user_proxy.initiate_chat(assistant,message=task1)

这是执行结果。首先AI给出了方法。然后开始执行代码。最后它作了个总结。所以虽然是思维链和规划能力很重要,但最后落地的是代码能力。所以才有代码能力是LLM的天花板的说法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
user_proxy (to assistant):

今天是星期几?,还有几天周末?请告诉我答案。

--------------------------------------------------------------------------------
assistant (to user_proxy):

1. To get the current date and day of the week, you can use Python's built-in `datetime` module. Here is a code block that will output the current date and day of the week:
python
from datetime import datetime

today = datetime.now()
day_of_week = today.strftime("%A")
print(f"Today is {day_of_week}.")

This code will output something like "Today is Monday." to indicate the current day of the week.

2. To determine how many days are left until the weekend, you can subtract the current date from the next Saturday or Sunday. Here is a code block that will output the number of days left until the weekend:
python
from datetime import datetime, timedelta

today = datetime.now()
next_saturday = today + timedelta(days=7)
next_sunday = next_saturday + timedelta(days=1)

days_left_to_weekend = (next_sunday - today).days
print(f"There are {days_left_to_weekend} days left until the weekend.")

This code will output something like "There are 4 days left until the weekend." to indicate how many days are left until the next weekend.

--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...

>>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...
execute_code was called with use_docker evaluating to True, but the python docker package is not available. Falling back to native code execution. Note: this fallback behavior is subject to change

>>>>>>>> EXECUTING CODE BLOCK 1 (inferred language is python)...
execute_code was called with use_docker evaluating to True, but the python docker package is not available. Falling back to native code execution. Note: this fallback behavior is subject to change
user_proxy (to assistant):

exitcode: 0 (execution succeeded)
Code output:
Today is Thursday.

There are 8 days left until the weekend.


--------------------------------------------------------------------------------
assistant (to user_proxy):

1. 首先To get the current date and day of the week, you can use Python's built-in `datetime` module. Here is a code block that will output the current date and day of the week:
python
from datetime import datetime

today = datetime.now()
day_of_week = today.strftime("%A")
print(f"Today is {day_of_week}.")

This code will output something like "Today is Monday." to indicate the current day of the week.

2. To determine how many days are left until the weekend, you can subtract the current date from the next Saturday or Sunday. Here is a code block that will output the number of days left until the weekend:
python
from datetime import datetime, timedelta

today = datetime.now()
next_saturday = today + timedelta(days=7)
next_sunday = next_saturday + timedelta(days=1)

days_left_to_weekend = (next_sunday - today).days
print(f"There are {days_left_to_weekend} days left until the weekend.")

This code will output something like "There are 4 days left until the weekend." to indicate how many days are left until the next weekend. exitcode: 0 (execution succeeded)
Code output:
Today is Thursday.

There are 8 days left until the weekend.

--------------------------------------------------------------------------------
Please give feedback to assistant. Press enter to skip and use auto-reply, or type 'exit' to stop the conversation: exit

四、评述

1、阅读到这里,相信你对什么事ai agent已经非常清楚了。autogen的是非常直观的。
2、但是这时你肯定会有很多想法,如果多个agent呢?如何我要自定义工具呢?等等。我们下回分解。
3、欢迎加入QQ群交流:593623958


版权声明
引线小白创作并维护的柠檬CC博客采用署名-非商业-禁止演绎4.0国际许可证。
本文首发于柠檬CC [ https://www.limoncc.com ] , 版权所有、侵权必究。
本文永久链接httpss://www.limoncc.com/post/a0fe24f2ca704c1e/
如果您需要引用本文,请参考:
引线小白. (Oct. 13, 2023). 《初见AI-Agent——Autogen系列01》[Blog post]. Retrieved from https://www.limoncc.com/post/a0fe24f2ca704c1e
@online{limoncc-a0fe24f2ca704c1e,
title={初见AI-Agent——Autogen系列01},
author={引线小白},
year={2023},
month={Oct},
date={13},
url={\url{https://www.limoncc.com/post/a0fe24f2ca704c1e}},
}

'