Skip to content

Commit

Permalink
modify cookbook
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhong1 committed Dec 27, 2023
1 parent a3020e7 commit a160852
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@
"# 1. 导入第三方库\n",
"\n",
"主要是在导入一些必要的Python库和模块,以便实现functionalagentwithretrieval的功能。\n",
"+ erniebot: 这是导入erniebot模块的语句,erniebot可能是一个包含ERNIE Bot实现的主要代码库。\n",
"+ erniebot: 这是导入erniebot模块的语句,erniebot是一个包含ERNIE Bot实现的主要代码库。\n",
"+ ERNIEBot: 实现ERNIE Bot的主要类,包含了实现对话功能的主要逻辑。\n",
"+ BaizhongSearch:实现百中的检索功能。\n",
"+ calculator_tool: 实现计算器功能的tool。\n",
"+ WholeMemory: 用于存储和管理代理的记忆的类。"
"+ WholeMemory: 用于存储和管理代理的记忆的类。\n",
"\n",
"这里使用EB_AGENT_ACCESS_TOKEN, 申请地址请参考[accessToken](https://aistudio.baidu.com/index/accessToken)"
]
},
{
Expand All @@ -62,12 +64,15 @@
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from erniebot_agent.agents import FunctionalAgentWithRetrieval\n",
"from erniebot_agent.retrieval import BaizhongSearch\n",
"from erniebot_agent.memory.whole_memory import WholeMemory\n",
"from erniebot_agent.chat_models.erniebot import ERNIEBot\n",
"from erniebot_agent.tools.calculator_tool import CalculatorTool\n",
"import erniebot"
"import erniebot\n",
"\n",
"os.environ[\"EB_AGENT_ACCESS_TOKEN\"] = \"your access token\""
]
},
{
Expand All @@ -87,8 +92,9 @@
"metadata": {},
"outputs": [],
"source": [
"aistudio_access_token = os.environ.get(\"EB_AGENT_ACCESS_TOKEN\", \"\")\n",
"baizhong_search = BaizhongSearch(\n",
" access_token=\"your access token\",\n",
" access_token=aistudio_access_token,\n",
" knowledge_base_name=\"test\",\n",
" knowledge_base_id=\"495735236530245\",\n",
")"
Expand Down Expand Up @@ -248,9 +254,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2.2 配置 ERNIE BOT API\n",
"\n",
"这里使用aistudio的Access_token, 申请地址请参考[accessToken](https://aistudio.baidu.com/index/accessToken)"
"## 2.2 配置 ERNIE BOT API"
]
},
{
Expand All @@ -259,9 +263,7 @@
"metadata": {},
"outputs": [],
"source": [
"erniebot.api_type = \"aistudio\"\n",
"aistudio_access_token = \"your access token\"\n",
"erniebot.access_token = aistudio_access_token"
"erniebot.api_type = \"aistudio\""
]
},
{
Expand Down Expand Up @@ -299,9 +301,9 @@
"source": [
"# 创建一个ERNIEBot实例,使用\"ernie-4.0\"模型。\n",
"llm = ERNIEBot(model=\"ernie-4.0\")\n",
"# 创建一个WholeMemory实例。这可能是一个用于存储对话历史和上下文信息的类,有助于模型理解和持续对话。\n",
"# 创建一个WholeMemory实例。这是一个用于存储对话历史和上下文信息的类,有助于模型理解和持续对话。\n",
"memory = WholeMemory()\n",
"# 创建一个FunctionalAgent实例。这个代理将使用上面创建的ERNIEBot模型和WholeMemory,同时传入了一个名为tool的工具\n",
"# 创建一个FunctionalAgent实例。这个代理将使用上面创建的ERNIEBot模型和WholeMemory,同时传入了一个名为calculator_tool的工具\n",
"agent = FunctionalAgentWithRetrieval(\n",
" llm=llm, tools=[calculator_tool], memory=memory, knowledge_base=baizhong_search, threshold=0.2\n",
")"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@
"\n",
"主要是在导入一些必要的Python库和模块,以便实现FunctionAgentWithRetrival的功能。\n",
"+ os: Python的标准库,用于与操作系统进行交互,如读写文件、管理路径等。\n",
"+ faiss: 用于向量存储的模块,可能是用于存储和检索经过嵌入处理的文本或图像的向量表示\n",
"+ faiss: 用于向量存储的模块,是用于存储和检索经过嵌入处理的文本或图像的向量表示\n",
"+ SimpleDirectoryReader: 用于在文件夹中加载数据\n",
"+ ErnieEmbeddings: 用于文本嵌入的工具,将文本转换为可以在模型中使用的向量表示。\n",
"+ FunctionalAgent: 这个类实现function calling功能的Agent的类,如问答、对话等。\n",
"+ WholeMemory: 用于存储和管理代理的记忆的类。\n",
"+ ERNIEBot: 实现ERNIE Bot的主要类,包含了实现对话功能的主要逻辑。\n",
"+ erniebot: 这是导入erniebot模块的语句,erniebot可能是一个包含ERNIE Bot实现的主要代码库。\n",
"+ erniebot: 这是导入erniebot模块的语句,erniebot是一个包含ERNIE Bot实现的主要代码库。\n",
"+ load_index_from_storage: 用于从存储系统加载索引。\n",
"+ VectorStoreIndex: 提供了创建索引、添加数据到索引等功能。\n",
"+ StorageContext: 用于存储节点的实用容器。\n",
"+ FaissVectorStore: 提供了一种使用 FAISS 库来存储和检索向量的方法。\n",
"+ ServiceContext: 服务上下文容器是 LlamaIndex 的实用程序容器。\n",
"os.environ[\"OPENAI_API_KEY\"]赋值空字符"
"\n",
"这里只调用lamma_index的检索功能,os.environ[\"OPENAI_API_KEY\"]可以赋值空字符\n",
"\n",
"这里使用EB_AGENT_ACCESS_TOKEN, 申请地址请参考[accessToken](https://aistudio.baidu.com/index/accessToken)"
]
},
{
Expand All @@ -45,6 +48,7 @@
"import os\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \" \"\n",
"os.environ[\"EB_AGENT_ACCESS_TOKEN\"] = \"your access token\"\n",
"from erniebot_agent.agents.functional_agent_with_retrieval import FunctionalAgentWithRetrieval\n",
"from erniebot_agent.memory.whole_memory import WholeMemory\n",
"from erniebot_agent.chat_models.erniebot import ERNIEBot\n",
Expand Down Expand Up @@ -113,9 +117,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2.2 配置 ERNIE BOT API\n",
"\n",
"这里使用aistudio的Access_token, 申请地址请参考[accessToken](https://aistudio.baidu.com/index/accessToken)"
"## 2.2 配置 ERNIE BOT API"
]
},
{
Expand All @@ -124,9 +126,7 @@
"metadata": {},
"outputs": [],
"source": [
"erniebot.api_type = \"aistudio\"\n",
"aistudio_access_token = \"your access token\"\n",
"erniebot.access_token = aistudio_access_token"
"erniebot.api_type = \"aistudio\""
]
},
{
Expand All @@ -144,7 +144,7 @@
"metadata": {},
"outputs": [],
"source": [
"class FaissSearch_llama:\n",
"class FaissSearchLlamaIndex:\n",
" def __init__(\n",
" self,\n",
" faiss_index,\n",
Expand Down Expand Up @@ -209,6 +209,7 @@
"metadata": {},
"outputs": [],
"source": [
"aistudio_access_token = os.environ.get(\"EB_AGENT_ACCESS_TOKEN\", \"\")\n",
"embed_model = ErnieEmbeddings(aistudio_access_token=aistudio_access_token, chunk_size=16)"
]
},
Expand Down Expand Up @@ -293,7 +294,7 @@
}
],
"source": [
"faiss_search = FaissSearch_llama(embed_model=embed_model, documents=documents, faiss_index=faiss_index)\n",
"faiss_search = FaissSearchLlamaIndex(embed_model=embed_model, documents=documents, faiss_index=faiss_index)\n",
"query = \"城市管理执法主管部门的职责是什么?\"\n",
"res = faiss_search.search(query)\n",
"from pprint import pprint\n",
Expand All @@ -319,7 +320,7 @@
"source": [
"# 创建一个ERNIEBot实例,使用\"ernie-3.5\"模型。\n",
"llm = ERNIEBot(model=\"ernie-3.5\")\n",
"# 创建一个WholeMemory实例。这可能是一个用于存储对话历史和上下文信息的类,有助于模型理解和持续对话。\n",
"# 创建一个WholeMemory实例。这是一个用于存储对话历史和上下文信息的类,有助于模型理解和持续对话。\n",
"memory = WholeMemory()\n",
"# 创建一个CalculatorTool实例。这是一个计算器工具。\n",
"calculator_tool = CalculatorTool()\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
"主要是在导入一些必要的Python库和模块,以便实现FunctionAgentWithRetrival的功能。\n",
"+ os: Python的标准库,用于与操作系统进行交互,如读写文件、管理路径等。\n",
"+ SpacyTextSplitter: 一个文本分割工具,用于将文本分割为更小的部分,如句子或短语。\n",
"+ FAISS: 用于向量存储的模块,可能是用于存储和检索经过嵌入处理的文本或图像的向量表示\n",
"+ FAISS: 用于向量存储的模块,是用于存储和检索经过嵌入处理的文本或图像的向量表示\n",
"+ PyPDFDirectoryLoader: 用于从PDF文件中加载数据的工具。\n",
"+ ErnieEmbeddings: 用于文本嵌入的工具,将文本转换为可以在模型中使用的向量表示。\n",
"+ FunctionalAgent: 这个类实现function calling功能的Agent的类,如问答、对话等。\n",
"+ WholeMemory: 用于存储和管理代理的记忆的类。\n",
"+ ERNIEBot: 实现ERNIE Bot的主要类,包含了实现对话功能的主要逻辑。\n",
"+ erniebot: 导入erniebot模块的语句,erniebot可能是一个包含ERNIE Bot实现的主要代码库。\n",
"+ cosine_similarity: 用于计算余弦相似度的函数"
"+ erniebot: 导入erniebot模块的语句,erniebot是一个包含ERNIE Bot实现的主要代码库。\n",
"+ cosine_similarity: 用于计算余弦相似度的函数\n",
"这里使用EB_AGENT_ACCESS_TOKEN, 申请地址请参考[accessToken](https://aistudio.baidu.com/index/accessToken)"
]
},
{
Expand All @@ -48,7 +49,9 @@
"from langchain.vectorstores import FAISS\n",
"from langchain.document_loaders import PyPDFDirectoryLoader\n",
"from erniebot_agent.extensions.langchain.embeddings import ErnieEmbeddings\n",
"from sklearn.metrics.pairwise import cosine_similarity"
"from sklearn.metrics.pairwise import cosine_similarity\n",
"\n",
"os.environ[\"EB_AGENT_ACCESS_TOKEN\"] = \"your access token\""
]
},
{
Expand Down Expand Up @@ -106,20 +109,16 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2.2 配置 ERNIE BOT API\n",
"\n",
"这里使用aistudio的Access_token, 申请地址请参考[accessToken](https://aistudio.baidu.com/index/accessToken)"
"## 2.2 配置 ERNIE BOT API"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"erniebot.api_type = \"aistudio\"\n",
"aistudio_access_token = \"your access token\"\n",
"erniebot.access_token = aistudio_access_token"
"erniebot.api_type = \"aistudio\""
]
},
{
Expand Down Expand Up @@ -178,6 +177,7 @@
"metadata": {},
"outputs": [],
"source": [
"aistudio_access_token = os.environ.get(\"EB_AGENT_ACCESS_TOKEN\", \"\")\n",
"embeddings = ErnieEmbeddings(aistudio_access_token=aistudio_access_token, chunk_size=16)"
]
},
Expand Down Expand Up @@ -376,7 +376,7 @@
"source": [
"# 创建一个ERNIEBot实例,使用\"ernie-3.5\"模型。\n",
"llm = ERNIEBot(model=\"ernie-3.5\")\n",
"# 创建一个WholeMemory实例。这可能是一个用于存储对话历史和上下文信息的类,有助于模型理解和持续对话。\n",
"# 创建一个WholeMemory实例。这是一个用于存储对话历史和上下文信息的类,有助于模型理解和持续对话。\n",
"memory = WholeMemory()\n",
"# 创建一个CalculatorTool实例。这是一个计算器工具。\n",
"calculator_tool = CalculatorTool()\n",
Expand Down

0 comments on commit a160852

Please sign in to comment.