#1 AI调用工具和编程
发表于 : 2025年 2月 20日 09:23
目前对行内的人来说一个既不幸又幸运的事情是,一眼看得到底的工程性的东西基本上不是被解决了,就是已经有很专业团队在做了。工具整合就是这么一个东西。Composio是一家做工具整合做得质量非常好的公司https://composio.dev/。 常用的功能比如gmail, github, serpapi这些都整合了,非常方便。Composio提供了接口可以直接连OpenAI。我的平台因为工作在completion level,并不假设底层工具调用的接口。但是也非常方便地整合了。下面是我整合composio的代码+
#!/usr/bin/env python3
import os
import asyncio
import json
import postline
from composio import ComposioToolSet, Action
API_KEY = os.getenv('COMPOSIO_API_KEY')
toolset = ComposioToolSet(api_key=API_KEY)
def make_reply_message (self_address, message, content):
re = postline.Message()
re['From'] = self_address
re['To'] = message['From']
re['Content-Type'] = 'text/plain'
re.set_content(content)
return re
app = postline.App("composio")
self_address = "composio@realm.localdomain"
@app.receive(self_address) # 监听邮件地址
async def handle_message (app, message):
action = message['Subject'] # 邮件标题是action
content = message.get_content()
params = json.loads(content) # 邮件内容是参数
resp = toolset.execute_action( # 调用工具就是这一行
action=getattr(Action, action),
params=params,
entity_id="default",
)
msg = make_reply_message(self_address, message, json.dumps(resp))
await app.send(msg)
asyncio.run(app.run())
然后对话的时候教AI,告诉它想要用composio的功能就是给composio@realm.localdomain发邮件,标题是action,比如SERPAPI_SEARCH, GMAIL_SEND_EMAIL, 参数则以json形式作为邮件内容。比如像下面这样:
From: user@localdomain
To: a100@agents.localdomain
Let's try to test the composio tool. The way to use it is to send an email to composio@realm.localdomain . The subject should be the action name, and the email should be of the MIME type application/json . The email body is a JSON dict of params.
For the first test, try to use the action (email subject) GMAIL_GET_PROFILE . The body has only one parameter: "user_id": "me" .
From: a100@agents.localdomain
To: composio@realm.localdomain
Subject: GMAIL_GET_PROFILE
Content-Type: application/json
{
"user_id": "me"
}
From: composio@realm.localdomain
To: a100@agents.localdomain
{"data": {"response_data": {"emailAddress": "wdong@wdong.org", "messagesTotal
": 93456, "threadsTotal": 66445, "historyId": "15502828"}}, "error": null, "s
uccessfull": true, "successful": true}
可以看到agent收到我的邮件后就给composio发了个邮件。这个邮件格式是正确的,然后上面贴的那个composio接口也回复了。发送邮件,google search我试了都没问题。我非要自己搞一套,是因为我发现AI和传统软件不同,非常versatile,基本上你想要什么姿势它都能用什么姿势配合你。OpenAI的工具调用接口根本就没有需要做。有时候我这边程序没写好,它也能变通一下配合我让程序跑起来。
眼前的问题是,google search结果很大,搜几次如果积累在journal里,一次inference的开销马上就涨到了一毛多。需要利用agent clone功能开发出一个调用子agent的方法,让子agent对搜索结果摘要之后再返回。
Composio里面有大量的和软件开发相关的工具。然后他们在这之上和crewAI结合搞了个swe-kit, 是一个做软件工程的agent。我看了眼目前他们的逻辑比较简单,高度依赖底层AI的自主性。我感觉用agent做软件工程这个方向有大量的事情可以做。Benchmark已经有了https://www.swebench.com/.
#!/usr/bin/env python3
import os
import asyncio
import json
import postline
from composio import ComposioToolSet, Action
API_KEY = os.getenv('COMPOSIO_API_KEY')
toolset = ComposioToolSet(api_key=API_KEY)
def make_reply_message (self_address, message, content):
re = postline.Message()
re['From'] = self_address
re['To'] = message['From']
re['Content-Type'] = 'text/plain'
re.set_content(content)
return re
app = postline.App("composio")
self_address = "composio@realm.localdomain"
@app.receive(self_address) # 监听邮件地址
async def handle_message (app, message):
action = message['Subject'] # 邮件标题是action
content = message.get_content()
params = json.loads(content) # 邮件内容是参数
resp = toolset.execute_action( # 调用工具就是这一行
action=getattr(Action, action),
params=params,
entity_id="default",
)
msg = make_reply_message(self_address, message, json.dumps(resp))
await app.send(msg)
asyncio.run(app.run())
然后对话的时候教AI,告诉它想要用composio的功能就是给composio@realm.localdomain发邮件,标题是action,比如SERPAPI_SEARCH, GMAIL_SEND_EMAIL, 参数则以json形式作为邮件内容。比如像下面这样:
From: user@localdomain
To: a100@agents.localdomain
Let's try to test the composio tool. The way to use it is to send an email to composio@realm.localdomain . The subject should be the action name, and the email should be of the MIME type application/json . The email body is a JSON dict of params.
For the first test, try to use the action (email subject) GMAIL_GET_PROFILE . The body has only one parameter: "user_id": "me" .
From: a100@agents.localdomain
To: composio@realm.localdomain
Subject: GMAIL_GET_PROFILE
Content-Type: application/json
{
"user_id": "me"
}
From: composio@realm.localdomain
To: a100@agents.localdomain
{"data": {"response_data": {"emailAddress": "wdong@wdong.org", "messagesTotal
": 93456, "threadsTotal": 66445, "historyId": "15502828"}}, "error": null, "s
uccessfull": true, "successful": true}
可以看到agent收到我的邮件后就给composio发了个邮件。这个邮件格式是正确的,然后上面贴的那个composio接口也回复了。发送邮件,google search我试了都没问题。我非要自己搞一套,是因为我发现AI和传统软件不同,非常versatile,基本上你想要什么姿势它都能用什么姿势配合你。OpenAI的工具调用接口根本就没有需要做。有时候我这边程序没写好,它也能变通一下配合我让程序跑起来。
眼前的问题是,google search结果很大,搜几次如果积累在journal里,一次inference的开销马上就涨到了一毛多。需要利用agent clone功能开发出一个调用子agent的方法,让子agent对搜索结果摘要之后再返回。
Composio里面有大量的和软件开发相关的工具。然后他们在这之上和crewAI结合搞了个swe-kit, 是一个做软件工程的agent。我看了眼目前他们的逻辑比较简单,高度依赖底层AI的自主性。我感觉用agent做软件工程这个方向有大量的事情可以做。Benchmark已经有了https://www.swebench.com/.