在第一章节中,主要介绍 Claude 调用的两种方法;在第二章节中,会对基于slack bot的方法调用 Claude做具体打开,包含如安在 slack 页面运用,以及装备 slack 的 API;在第三章节里,会基于slack API 来进行基于python代码的完成。
1 | Claude 调用的几种姿态
很多大模型现已开放了 API 调用,免费的,收费的,商用的等等。但是,部分大模型的 API 调用还是需求请求,进 waiting list,然后遥遥无期的等候。Claude 的调用方法有两种:
请求 API
经过Slack
第一种方法,在现在来说约等于无,由于 Claude 现在只针对其要害的合作伙伴(如 Notion,Quora等)供给 API,对大众的 API的请求,现在还处于一个小心翼翼的逐渐放开阶段
After working for the past few months with key partners like Notion, Quora, and DuckDuckGo in a closed alpha, we’ve been able to carefully test out our systems in the wild, and areready to offer Claude more broadlyso it can power crucial, cutting-edge use cases at scale.
假如呈现 Authorization hasn’t been set up for this app, so you won’t be able to install it., 点击右上角的 Manage(图1)-> App Management Settings(图2) -> Require APP Approval(图2) -> Approve(图3) -> Learn More(图3) -> Add to Slack(图4) -> Allow(图5) -> Enjoy(图6)!
图1
图2
图3
图4
图5
图6
2.2 | slack token 信息获取
进入 slack api 并创立 app, 挑选 From scratch
装备权限
获取 token (下面会用到)
获取member id (下面会用到)
3 | python 代码实例
python 核心代码
import time
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from ClaudeDto import ClaudeChatReqDto
from ReturnBase import ReturnBase
defsend_message(client, channel, text):
try:
return client.chat_postMessage(channel=channel, text=text)
except SlackApiError as e:
print(f"Error sending message: {e}")
deffetch_messages(client, channel, last_message_timestamp, bot_user_id):
response = client.conversations_history(channel=channel, oldest=last_message_timestamp)
return [msg['text'] for msg in response['messages'] if msg['user'] == bot_user_id]
defget_new_messages(client, channel, last_message_timestamp, bot_user_id):
whileTrue:
messages = fetch_messages(client, channel, last_message_timestamp, bot_user_id)
if messages andnot messages[-1].endswith('Typing…_'):
return messages[-1]
time.sleep(1) # 这儿的时刻设置需求 balance 一下,越小越可能被限流,越大返回时刻越长deffind_direct_message_channel(client, user_id):
try:
response = client.conversations_open(users=user_id)
return response['channel']['id']
except SlackApiError as e:
print(f"Error opening DM channel: {e}")
defclaude_chat_by_slack_svc(req_dto: ClaudeChatReqDto):
slack_user_token = {上面取得的token}
bot_user_id = {上面取得的member id}
client = WebClient(token=slack_user_token)
dm_channel_id = find_direct_message_channel(client, bot_user_id)
ifnot dm_channel_id:
print("Could not find DM channel with the bot.")
return
last_message_timestamp = None
prompt = req_dto.prompt
response = send_message(client, dm_channel_id, prompt)
if response:
last_message_timestamp = response['ts']
new_message = get_new_messages(client, dm_channel_id, last_message_timestamp, bot_user_id)
return ReturnBase(
data=new_message
)
if __name__ == "__main__":
pass
ClaudeDto.py
from pydantic import BaseModel
classClaudeChatReqDto(BaseModel):
prompt: str
ReturnBase.py
from pydantic import BaseModel
from typing importOptionalclassReturnBase(BaseModel):
msg: Optional[str] = "ok"
msgCode: Optional[str] = "10000"
data: Optional[object] = None