使用本地模型与Agentic RAG构建的客户服务Discord机器人#
您也可以在colab中查看这本手册这里
要运行此程序,请在免费的Tesla T4 Google Colab实例上点击"Runtime",然后点击"Run all"!
本笔记本演示了如何利用本地模型构建一个基于检索增强生成(RAG)技术的客户服务Discord机器人。它整合了以下技术:
CAMEL: 一个用于构建和部署大语言模型(LLM)应用的开源工具包。
Firecrawl: 一个用于网页抓取和创建本地知识库的工具。
Qdrant: 一个用于高效知识检索的向量数据库。
Ollama: 一个本地模型部署方案,用于运行LLM而无需外部依赖。
通过跟随本笔记本,您可以构建自己的定制客户服务机器人,该机器人使用本地模型和自定义知识库。

如需帮助请加入我们的Discord + ⭐ 在Github上给我们点星 ⭐
安装与设置#
首先,安装CAMEL包及其所有依赖项
[ ]:
!pip install "camel-ai[all]==0.2.16"
!pip install starlette
!pip install nest_asyncio
接下来,使用Firecrawl准备知识库。Firecrawl是一款多功能网络爬取工具,专为高效提取网站数据而设计,并已与CAMEL集成。如需了解更多信息,您可以查看我们的Firecrawl使用指南:https://colab.research.google.com/drive/1lOmM3VmgR1hLwDKdeLGFve_75RFW0R9I?usp=sharing#scrollTo=1Nj0Oqnoy6oJ
让我们来设置您的Firecrawl!如果您已经有知识文件,可以跳过这部分。
为了在本地运行所有内容,我们可以使用自托管的firecrawl。
更多详情,请查阅firecrawl文档:https://docs.firecrawl.dev/contributing/guide
[ ]:
from getpass import getpass
firecrawl_api_url = getpass('Enter your API url: ')
Enter your API url: ··········
本地设置#
请复制一份这个笔记本(重要),或者在本地运行这个笔记本。
如果您选择复制此笔记本并继续使用Google Colab,请按照以下步骤将复制的笔记本连接到本地运行时:
在终端中运行以下命令以本地安装notebook:
pip install notebook
jupyter notebook --NotebookApp.allow_origin='https://colab.research.google.com' \
--port=8888 \
--no-browser
您将在终端中看到类似这样的内容:
To access the server, open this file in a browser:
<some_path>
Or copy and paste one of these URLs:
<url1>
<url2>
复制任意一个url,点击Google Colab中的"连接到本地运行时"按钮,并将复制的url粘贴到Backend Url中。
点击‘connect’
基础代理与本地模型设置#
下载Ollama以获取本地模型,访问:https://ollama.com/download
设置好Ollama后,在终端中输入以下命令来拉取Llama3模型:
ollama pull qwq
3. cd into a desired directory
```bash
cd <target_drectory_path>
4. Create a `ModelFile` similar the one below in your project directory. (Optional)
```bash
FROM qwq
# Set parameters
PARAMETER temperature 0.8
PARAMETER stop Result
# Sets a custom system message to specify the behavior of the chat assistant
# Leaving it blank for now.
SYSTEM """ """
创建一个脚本来获取基础模型(llama3)并使用上面的
ModelFile创建自定义模型。将其保存为.sh文件:(可选)
#!/bin/zsh
# variables
model_name="qwq"
custom_model_name="camel-qwq"
#get the base model
ollama pull $model_name
#create the model file
ollama create $custom_model_name -f ./ModelFile
现在您已经部署了本地模型!
[2]:
from camel.models import ModelFactory
from camel.types import ModelPlatformType
ollama_model = ModelFactory.create(
model_platform=ModelPlatformType.OLLAMA,
model_type="qwq",
url="http://localhost:11434/v1", #optional
model_config_dict={"temperature": 0.4},
)
2024-12-29 11:15:47,983 - camel - INFO - Camel library logging has been configured.
[6]:
from camel.agents import ChatAgent
from camel.logger import disable_logging
disable_logging()
chat_agent = ChatAgent(
system_message="You're a helpful assistant",
message_window_size=10,
model=ollama_model,
token_limit=8192, #change base on your input size
)
知识爬取与存储#
使用Firecrawl爬取网站并将内容存储到markdown文件中:
[ ]:
import os
from camel.loaders import Firecrawl
from camel.messages import BaseMessage
os.makedirs('local_data', exist_ok=True)
firecrawl = Firecrawl(api_url=firecrawl_api_url, api_key="_")
crawl_response = firecrawl.crawl(
url="https://docs.camel-ai.org/"
)
with open('local_data/camel.md', 'w') as file:
file.write(crawl_response["data"][0]["markdown"])
将外部知识插入到Agent中
[8]:
with open('local_data/camel.md', 'r') as file:
knowledge = file.read()
knowledge_message = BaseMessage.make_user_message(
role_name="User", content=f"Based on the following knowledge: {knowledge}"
)
chat_agent.update_memory(knowledge_message, "user")
基础聊天机器人设置#
[ ]:
print("Start chatting! Type 'exit' to end the conversation.")
while True:
user_input = input("User: ")
if user_input.lower() == "exit":
print("Ending conversation.")
break
assistant_response = chat_agent.step(user_input)
print(f"Assistant: {assistant_response.msgs[0].content}")
Start chatting! Type 'exit' to end the conversation.
User: what is camel?
2024-12-28 14:57:51,584 - httpx - INFO - HTTP Request: POST http://localhost:11434/v1/chat/completions "HTTP/1.1 200 OK"
Assistant: CAMEL is a multi-agent framework that allows you to build and use large language model (LLM)-based agents for real-world task solving. It was introduced as one of the earliest LLM-based multi-agent frameworks in research, and it provides a generic platform for creating various types of agents, tasks, prompts, models, and simulated environments.
The primary goal of CAMEL is to facilitate large-scale studies on agent behaviors, capabilities, and potential risks by providing a comprehensive framework for building and interacting with LLM-based agents. It supports different modules such as models, messages, memory, tools, prompts, tasks, loaders, storages, societies, embeddings, retrievers, and workforce, each serving specific purposes in the agent ecosystem.
CAMEL offers a range of cookbooks and tutorials to help users get started with creating their first agents and agent societies, using tools, implementing memory and retrieval mechanisms, generating tasks, and more. Additionally, it provides API references and indices for developers looking to delve deeper into its functionalities.
If you're interested in contributing to CAMEL, whether through research, coding, or simply engaging with the community, there are various ways to get involved, including joining their Discord, WeChat group, or Slack channel.
User: exit
Ending conversation.
基础Discord机器人集成#
要构建一个discord机器人,需要一个discord机器人令牌。
如果您还没有机器人令牌,可以按照以下步骤获取:
前往 Discord 开发者门户:https://discord.com/developers/applications
使用您的Discord账号登录,如果没有账号请先创建一个
点击'New Application'创建一个新的机器人。
为您的应用程序命名并点击‘创建’。
在左侧边栏中导航至'Bot'选项卡,然后点击'添加Bot'。
机器人创建完成后,您会看到一个‘Token’部分。点击‘Reset Token’生成一个新令牌。
安全复制生成的令牌。
邀请机器人:
导航到‘OAuth2’选项卡,然后进入‘URL生成器’。
在‘Scopes’下,选择‘bot’。
在"Bot Permissions"下,选择你的机器人所需的权限(例如,对于我们的机器人使用需要"Send Messages"、"Read Messages")
复制生成的URL并将其粘贴到浏览器中,以邀请机器人加入您的服务器。
授予机器人权限:
导航至'Bot'选项卡
在‘特权网关意图’下,勾选‘服务器成员意图’和‘消息内容意图’。
如需了解更多详情,您也可以查看官方Discord机器人文档:https://discord.com/developers/docs/intro
[9]:
import os
from getpass import getpass
discord_bot_token = getpass('Enter your Discord bot token: ')
os.environ["DISCORD_BOT_TOKEN"] = discord_bot_token
Enter your Discord bot token: ··········
此代码单元使用来自camel.bots库的DiscordApp类设置了一个简单的Discord机器人。该机器人会监听它有权访问的任何频道中的消息,并根据输入消息提供响应。
[ ]:
from camel.bots import DiscordApp
import nest_asyncio
import discord
nest_asyncio.apply()
discord_bot = DiscordApp(token=discord_bot_token)
@discord_bot.client.event
async def on_message(message: discord.Message):
if message.author == discord_bot.client.user:
return
if message.type != discord.MessageType.default:
return
if message.author.bot:
return
user_input = message.content
chat_agent.reset()
chat_agent.update_memory(knowledge_message, "user")
assistant_response = chat_agent.step(user_input)
response_content = assistant_response.msgs[0].content
if len(response_content) > 2000: # discord message length limit
for chunk in [response_content[i:i+2000] for i in range(0, len(response_content), 2000)]:
await message.channel.send(chunk)
else:
await message.channel.send(response_content)
discord_bot.run()
集成Qdrant处理大文件以构建更强大的Discord机器人#
Qdrant是一个向量相似性搜索引擎和向量数据库。它专为在大型向量数据集上执行快速高效的相似性搜索而设计。这使得聊天机器人能够访问并利用外部信息来提供更全面准确的响应。通过将知识存储为向量,Qdrant实现了高效的语义搜索,使聊天机器人能够根据用户查询的含义找到相关信息。
为Qdrant设置嵌入模型和检索器:可以随意切换到CAMEL支持的其他嵌入模型。
为Qdrant设置嵌入模型和检索器:
[ ]:
from camel.embeddings import SentenceTransformerEncoder # CAMEL also support other embedding
sentence_encoder = SentenceTransformerEncoder(model_name='intfloat/e5-large-v2')
设置AutoRetriever以自动从存储系统中检索相关信息。
[18]:
from camel.retrievers import AutoRetriever
from camel.types import StorageType
assistant_sys_msg = """You are a helpful assistant to answer question,
I will give you the Original Query and Retrieved Context,
answer the Original Query based on the Retrieved Context,
if you can't answer the question just say I don't know.
Just give the answer to me directly, no more other words needed.
"""
auto_retriever = AutoRetriever(
vector_storage_local_path="local_data2/",
storage_type=StorageType.QDRANT,
embedding_model=sentence_encoder
)
chat_agent_with_rag = ChatAgent(
system_message=assistant_sys_msg,
model=ollama_model,
token_limit=8192, #change base on your input size
)
使用Auto RAG先检索信息,然后基于检索到的内容通过CAMEL ChatAgent来回答用户查询:
如果您将此cookbook连接到本地运行时,在contents中的本地路径添加文件可能会导致错误。
[ ]:
from camel.bots import DiscordApp
import nest_asyncio
import discord
nest_asyncio.apply()
discord_q_bot = DiscordApp(token=discord_bot_token)
@discord_q_bot.client.event # triggers when a message is sent in the channel
async def on_message(message: discord.Message):
if message.author == discord_q_bot.client.user:
return
if message.type != discord.MessageType.default:
return
if message.author.bot:
return
user_input = message.content
query_and_retrieved_info = auto_retriever.run_vector_retriever(
query=user_input,
contents=[ # don't add a local path if you are connecting to a local runtime
"https://docs.camel-ai.org/", #replace with your knowledge base
],
top_k=3,
return_detailed_info=False,
similarity_threshold=0.5
)
user_msg = str(query_and_retrieved_info)
assistant_response = chat_agent_with_rag.step(user_msg)
response_content = assistant_response.msgs[0].content
print(3)
if len(response_content) > 2000: # discord message length limit
for chunk in [response_content[i:i+2000] for i in range(0, len(response_content), 2000)]:
await message.channel.send(chunk)
else:
await message.channel.send(response_content)
discord_q_bot.run()
以上就是全部内容:对🐫 CAMEL-AI有疑问吗?欢迎加入我们的Discord社区!无论您是想分享反馈、探索多智能体系统的最新进展、获取支持,还是与其他开发者交流激动人心的项目,我们都期待您的加入!🤝
查看我们的其他一些作品:
🐫 创建您的第一个CAMEL代理 free Colab
Graph RAG 烹饪指南 免费 Colab
🧑⚖️ 使用Workforce创建一个黑客马拉松评审委员会 免费Colab
🔥 使用Firecrawl和CAMEL从网站获取数据的3种方法 免费Colab
🦥 使用CAMEL和Mistral模型进行代理式SFT数据生成,通过Unsloth进行微调 免费Colab
感谢来自🐫 CAMEL-AI团队的每一个人