Skip to content

Commit

Permalink
Merge branch 'develop' into agent/feat/restorable_agent_run
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobholamovic committed Jan 10, 2024
2 parents 2d0e1e6 + b55b450 commit e796cab
Show file tree
Hide file tree
Showing 41 changed files with 1,206 additions and 631 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<div align="center">

<h1>ERNIE Bot SDK</h1>
<h1>ERNIE SDK</h1>

[开发文档](http://ernie-bot-agent.readthedocs.io/) | [智能体应用体验](https://aistudio.baidu.com/application/center?tag=agent)

[![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](LICENSE)
[![Version](https://img.shields.io/github/release/PaddlePaddle/ERNIE-Bot-SDK.svg)](https://github.com/PaddlePaddle/ERNIE-Bot-SDK/releases)
[![Version](https://img.shields.io/github/release/PaddlePaddle/ERNIE-SDK.svg)](https://github.com/PaddlePaddle/ERNIE-SDK/releases)
![Supported Python versions](https://img.shields.io/badge/python-3.8+-orange.svg)
![Supported OSs](https://img.shields.io/badge/os-linux%2C%20win%2C%20mac-yellow.svg)
[![Downloads](https://pepy.tech/badge/erniebot-agent)](https://pepy.tech/project/erniebot-agent)
[![codecov](https://codecov.io/gh/PaddlePaddle/ERNIE-Bot-SDK/branch/develop/graph/badge.svg)](https://codecov.io/gh/PaddlePaddle/ERNIE-Bot-SDK)
[![codecov](https://codecov.io/gh/PaddlePaddle/ERNIE-SDK/branch/develop/graph/badge.svg)](https://codecov.io/gh/PaddlePaddle/ERNIE-SDK)

</div>

ERNIE Bot SDK 仓库包含两个项目:ERNIE Bot Agent 和 ERNIE Bot。ERNIE Bot Agent 是百度飞桨推出的基于文心大模型编排能力的大模型智能体开发框架,结合了飞桨星河社区的丰富预置平台功能。ERNIE Bot 则为开发者提供便捷接口,轻松调用文心大模型的文本创作、通用对话、语义向量及AI作图等基础功能。

![eb_sdk_agent_structure](https://github.com/PaddlePaddle/ERNIE-Bot-SDK/assets/11987277/1fbcfbca-7695-4cca-9b4f-35a49d1d7c52)
![eb_sdk_agent_structure](https://github.com/PaddlePaddle/ERNIE-SDK/assets/11987277/1fbcfbca-7695-4cca-9b4f-35a49d1d7c52)

## ERNIE Bot Agent

Expand All @@ -41,8 +41,8 @@ ERNIE Bot SDK 仓库包含两个项目:ERNIE Bot Agent 和 ERNIE Bot。ERNIE B
执行如下命令,使用源码安装 ERNIE Bot Agent(要求Python >= 3.8)。

```shell
git clone https://github.com/PaddlePaddle/ERNIE-Bot-SDK.git
cd ERNIE-Bot-SDK
git clone https://github.com/PaddlePaddle/ERNIE-SDK.git
cd ERNIE-SDK

# 首先安装Ernie Bot
pip install ./erniebot
Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ hide:

</div>

ERNIE Bot SDK 仓库包含两个项目:ERNIE Bot Agent 和 ERNIE Bot。ERNIE Bot Agent 是百度飞桨推出的基于文心大模型编排能力的大模型智能体开发框架,结合了飞桨星河社区的丰富预置平台功能。ERNIE Bot 则为开发者提供便捷接口,轻松调用文心大模型的文本创作、通用对话、语义向量及AI作图等基础功能。
ERNIE SDK 仓库包含两个项目:ERNIE Bot Agent 和 ERNIE Bot。ERNIE Bot Agent 是百度飞桨推出的基于文心大模型编排能力的大模型智能体开发框架,结合了飞桨星河社区的丰富预置平台功能。ERNIE Bot 则为开发者提供便捷接口,轻松调用文心大模型的文本创作、通用对话、语义向量及AI作图等基础功能。

![eb_sdk_agent_structure](https://github.com/PaddlePaddle/ERNIE-Bot-SDK/assets/11987277/1fbcfbca-7695-4cca-9b4f-35a49d1d7c52)
![eb_sdk_agent_structure](https://github.com/PaddlePaddle/ERNIE-SDK/assets/11987277/1fbcfbca-7695-4cca-9b4f-35a49d1d7c52)


## ERNIE Bot Agent
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/chat_models.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
| 属性 | 类型 | 描述 |
| ---------- | -------------- | ------------------------------------------------------- |
| model | str | 文心一言模型的名称,支持"ernie-3.5", "ernie-turbo", "ernie-4.0", "ernie-longtext" |
| default_chat_kwargs | Dict[str, Any] | 设置调用文心一言模型的默认参数,支持`temperature`,`top_p`等,具体参考[文档](https://github.com/PaddlePaddle/ERNIE-Bot-SDK/blob/develop/docs/sdk/api_reference/chat_completion.md)|
| default_chat_kwargs | Dict[str, Any] | 设置调用文心一言模型的默认参数,支持`temperature`,`top_p`等,具体参考[文档](https://github.com/PaddlePaddle/ERNIE-SDK/blob/develop/docs/sdk/api_reference/chat_completion.md)|

| 方法 | 描述 |
| ----------------- | -------------------------------------------------------------------- |
Expand All @@ -41,7 +41,7 @@
| api_type | str | 文心一言模型的后端,支持"aistudio"和"qianfan",默认是"aistudio"。 |
| access_token | Optional[str] | 文心一言模型的鉴权access token,不同后端需要使用对应的access token |
| enable_multi_step_tool_call | bool | 设置是否开启多工具规划连续调用的功能,默认为False |
| default_chat_kwargs | Dict[str, Any] | 设置调用文心一言模型的默认参数,支持`temperature`,`top_p`等等,具体参考[文档](https://github.com/PaddlePaddle/ERNIE-Bot-SDK/blob/develop/docs/sdk/api_reference/chat_completion.md) |
| default_chat_kwargs | Dict[str, Any] | 设置调用文心一言模型的默认参数,支持`temperature`,`top_p`等等,具体参考[文档](https://github.com/PaddlePaddle/ERNIE-SDK/blob/develop/docs/sdk/api_reference/chat_completion.md) |

| 方法 | 描述 |
| ----------------- | -------------------------------------------------------------------- |
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async def demo_function():
local_file = await file_manager.create_file_from_path(file_path='your_path', file_type='local')
```
3. 通过 `GlobalFileManagerHandler`搜索以及保存 File

```python
async def demo_function():
# 需要在事件循环最开始配置,打开远程文件开关,注意需配置access token
await GlobalFileManagerHandler().configure(enable_remote_file=True)
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* add_messages(self, messages): 批量地增加message。
* add_message(self, message): 增加一条message。
* get_messages(self): 获取Memory中的所有messages。
* get_system_message(self): 获取Memory中的系统消息,Memory中有且仅有一条系统信息,可以传入LLM的* * system接口,用于建立LLM的特性。
* set_system_message(self): 设置Memory中的系统消息,Memory中有且仅有一条系统信息,通过Agent中system的接口进行同步,用于建立LLM的特性。
* clear_chat_history(self): 清除memory中所有message的历史。
* 关系:Memory的基类,关联到MessageManager类。

Expand Down
4 changes: 2 additions & 2 deletions docs/quickstart/preparation.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
执行如下命令,使用源码安装ERNIE Bot Agent(要求Python >= 3.8)。

```shell
git clone https://github.com/PaddlePaddle/ERNIE-Bot-SDK.git
cd ERNIE-Bot-SDK
git clone https://github.com/PaddlePaddle/ERNIE-SDK.git
cd ERNIE-SDK

# 切换到特定分支,默认是develop分支
# git checkout -b xxx origin/xxx
Expand Down
6 changes: 3 additions & 3 deletions docs/quickstart/use-tool.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

`erniebot-agent` 支持 LocalTool 和 RemoteTool的开发,使用方法如下所示:
`ERNIE Bot Agent` 支持 LocalTool 和 RemoteTool的开发,使用方法如下所示:


## 使用 LocalTool

顾名思义,LocalTool 是一个运行在本地的工具,提供了与 EB 进行交互的能力,接下来展示如何使用 `CurrentTimeTool` 来获取当前的准确时间:、。
顾名思义,LocalTool 是一个运行在本地的工具,提供了与 EB 进行交互的能力,接下来展示如何使用 `CurrentTimeTool` 来获取当前的准确时间:

!!! tips

Expand All @@ -25,7 +25,7 @@ async def main():
asyncio.run(main())
```

以上示例展示了如何使用内置的 `CurrentTimeTool` 获取当前时间,开发者也可以自定义 LocalTool,具体请参考 [自定义工具](../modules/tools/create-tool.md) 文档。
以上示例展示了如何使用内置的 `CurrentTimeTool` 获取当前时间,开发者也可以自定义 LocalTool,具体请参考 [自定义工具](../modules/tools.md) 文档。


## 使用 RemoteTool
Expand Down
4 changes: 2 additions & 2 deletions docs/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ response = erniebot.Image.create(
print(response.get_result())
```

<img width="512" src="https://github.com/PaddlePaddle/ERNIE-Bot-SDK/assets/1371212/73911c97-ef42-4803-8dc6-d385486c128c" />
<img width="512" src="https://github.com/PaddlePaddle/ERNIE-SDK/assets/1371212/73911c97-ef42-4803-8dc6-d385486c128c" />

我们推荐两个撰写文生图提示词的文档,大家可以组合使用,创作出更加精美的图片。

Expand Down Expand Up @@ -219,7 +219,7 @@ print(response.get_result())

## Gradio Demos

为了让用户更全面、更直观地了解ERNIE Bot的各项功能,我们基于Gradio开发了一系列带有web用户界面的演示应用。请参阅[说明文档](https://github.com/PaddlePaddle/ERNIE-Bot-SDK/tree/develop/examples/README.md),尝试对话补全、语义向量、文生图、函数调用等可交互例子。
为了让用户更全面、更直观地了解ERNIE Bot的各项功能,我们基于Gradio开发了一系列带有web用户界面的演示应用。请参阅[说明文档](https://github.com/PaddlePaddle/ERNIE-SDK/tree/develop/examples/README.md),尝试对话补全、语义向量、文生图、函数调用等可交互例子。

<img width="1296" src="https://user-images.githubusercontent.com/19339784/264367116-600c34b9-0103-4fb7-bbe5-6d71ddc6af09.gif" />

Expand Down
4 changes: 2 additions & 2 deletions docs/sdk/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pip install --upgrade erniebot
如果希望自源码安装,可以执行如下命令:

```{.sh .copy}
git clone https://github.com/PaddlePaddle/ERNIE-Bot-SDK
cd ERNIE-Bot-SDK
git clone https://github.com/PaddlePaddle/ERNIE-SDK
cd ERNIE-SDK
pip install --upgrade setuptools
pip install .
```
9 changes: 5 additions & 4 deletions erniebot-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
**ERNIE Bot Agent** 是由百度飞桨全新推出的大模型智能体(agent)开发框架。基于文心大模型强大的编排能力,并结合飞桨星河社区提供的丰富预置平台化功能,**ERNIE Bot Agent** 旨在成为功能全面且高度可定制的一站式大模型智能体和应用开发框架。

[![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](LICENSE)
[![Version](https://img.shields.io/github/release/PaddlePaddle/ERNIE-Bot-SDK.svg)](https://github.com/PaddlePaddle/ERNIE-Bot-SDK/releases)
[![Version](https://img.shields.io/github/release/PaddlePaddle/ERNIE-SDK.svg)](https://github.com/PaddlePaddle/ERNIE-SDK/releases)
![Supported Python versions](https://img.shields.io/badge/python-3.8+-orange.svg)
![Supported OSs](https://img.shields.io/badge/os-linux%2C%20win%2C%20mac-yellow.svg)
[![Downloads](https://pepy.tech/badge/erniebot-agent)](https://pepy.tech/project/erniebot-agent)
[![codecov](https://codecov.io/gh/PaddlePaddle/ERNIE-Bot-SDK/branch/develop/graph/badge.svg)](https://codecov.io/gh/PaddlePaddle/ERNIE-Bot-SDK)
[![codecov](https://codecov.io/gh/PaddlePaddle/ERNIE-SDK/branch/develop/graph/badge.svg)](https://codecov.io/gh/PaddlePaddle/ERNIE-SDK)

</div>

Expand All @@ -32,8 +32,8 @@
执行如下命令,使用源码安装 ERNIE Bot Agent(要求Python >= 3.8)。

```shell
git clone https://github.com/PaddlePaddle/ERNIE-Bot-SDK.git
cd ERNIE-Bot-SDK
git clone https://github.com/PaddlePaddle/ERNIE-SDK.git
cd ERNIE-SDK

# 首先安装Ernie Bot
pip install ./erniebot
Expand All @@ -54,6 +54,7 @@ pip install --upgrade erniebot-agent
# 安装所有模块
pip install --upgrade erniebot-agent[all]
```

## 快速体验

Expand Down
43 changes: 43 additions & 0 deletions erniebot-agent/applications/eb-agent-qa-bot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# ERNIE Bot Agent QA Bot

ERNIE Bot Agent QA Bot是一个ERNIE Bot Agent使用教学机器人,基于 `FunctionAgentWithRetrieval`,该工具旨在辅助用户解决与EB-Agent相关的问题,帮助用户更快的使用 `erniebot_agent`库,搭建属于自己的Agent。

## 架构

此应用基于 `FunctionAgentWithRetrieval`(后续 `RetrievalAgent`上线后将同步更换),将此仓库中相关模块的markdown文件以及ipynb的示例代码文件向量化并通过自定义检索工具检索,实现EB-Agent教学机器人。

### 自定义检索工具

此应用中的检索工具基于 `langchain``faiss`本地向量库,同时基于此应用特性,用户可能需要了解具体的代码实现。因此在实现时同时检索召回说明文档的内容(存储于db)以及相关的代码内容(存储于module_code_db)。

```python
class FaissSearch:
def __init__(self, db, embeddings, module_code_db):
self.db = db
self.module_code_db = module_code_db
self.embeddings = embeddings
```

## 如何开始

**注意:** 建库的过程比较缓慢,请耐心等待。

> 第一步:下载项目源代码,请确保您已经安装了erniebot_agent以及erniebot
```bash
git clone https://github.com/PaddlePaddle/ERNIE-SDK.git
cd ERNIE-SDK
pip install ernie_agent
```

> 第二步:如果是第一次运行,请先初始化向量库(应用中同时上传了向量库也可以)
```bash
python question_bot.py --init=True --access-token <aistudio-access-token>
```

> 如果已经初始化过向量库,直接运行即可
```bash
python question_bot.py --access-token <aistudio-access-token>
```
119 changes: 119 additions & 0 deletions erniebot-agent/applications/eb-agent-qa-bot/init_vector_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
from typing import List, Union

import erniebot
import nbformat
from langchain.text_splitter import (
MarkdownHeaderTextSplitter,
RecursiveCharacterTextSplitter,
)
from langchain.vectorstores import FAISS
from langchain_core.documents import Document
from tqdm import tqdm

headers_to_split_on = [
("#", "Header 1"),
("##", "Header 2"),
# ("###", "Header 3"),
# ("####", "Header 4"),
]


def get_summary(content: str) -> Union[str, None]:
"""Get summary of md files, you can also change another llm model."""
chat_message = {"role": "user", "content": f"请帮我给以下markdown文件生成摘要用于用户问文档内容时的检索匹配,不要超过400个字:\n{content}"}
summary = erniebot.ChatCompletion.create(
model="ernie-longtext",
messages=[chat_message],
).get_result()
return summary


def open_and_concatenate_ipynb(ipynb_path: str, encoding: str) -> str:
"""Get content of ipynb file."""
with open(ipynb_path, "r", encoding=encoding) as f:
notebook_content = nbformat.read(f, as_version=4)

# 按顺序拼接code单元
concatenated_content = ""
for cell in notebook_content["cells"]:
if cell["cell_type"] == "code":
concatenated_content += "```python\n" + cell["source"] + "```\n\n"

return concatenated_content


def read_md_file(file_path: str) -> Union[str, None]:
try:
with open(file_path, "r", encoding="utf-8") as file:
md_content = file.read()
return md_content
except FileNotFoundError:
print(f"文件 '{file_path}' 不存在。")
return None
except Exception as e:
print(f"读取文件时出现错误: {e}")
return None


def load_md_files_to_doc(
file_paths: List[str],
chunk_size: int = 1000,
chunk_overlap: int = 30,
) -> List[Document]:
markdown_splitter = MarkdownHeaderTextSplitter(headers_to_split_on=headers_to_split_on)
text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
output_document = []
for file in tqdm(file_paths):
content = read_md_file(file)
if content is None:
continue
md_header_splits = markdown_splitter.split_text(content)
splits = text_splitter.split_documents(md_header_splits)
for i in range(len(splits)):
# 生成summary用于检索
splits[i].metadata["raw_text"] = splits[i].page_content
splits[i].page_content = get_summary(splits[i].page_content)
output_document.extend(splits)
return output_document


def init_db(faiss_name, faiss_name_module, embeddings):
md_file_path = [
"./docs/modules/file.md",
"./docs/modules/agents.md",
"./docs/modules/memory.md",
"./docs/modules/message.md",
"./docs/modules/chat_models.md",
"./docs/modules/tools.md",
"./docs/quickstart/agent.md",
"./docs/quickstart/use-tool.md",
]
chunk_size = 1000
chunk_overlap = 30
content_doc = load_md_files_to_doc(md_file_path, chunk_size, chunk_overlap)

db = FAISS.from_documents(content_doc, embeddings)
db.save_local(faiss_name)

ipynb_path = [
"./docs/cookbooks/agent/function_agent.ipynb",
"./docs/cookbooks/agent/chat_models.ipynb",
"./docs/cookbooks/agent/memory.ipynb",
"./docs/cookbooks/agent/message.ipynb",
"./docs/cookbooks/agent/local_tool.ipynb",
"./docs/cookbooks/agent/tools_intro.ipynb",
"./docs/cookbooks/agent/remote-tool/remote_tool.ipynb",
]
modules = [item[item.rfind("/") + 1 : item.rfind(".ipynb")] for item in ipynb_path]
module_doc = []

for i in range(len(modules)):
module_doc.append(
Document(
page_content=modules[i],
metadata={"ipynb": open_and_concatenate_ipynb(ipynb_path[i], "utf-8")},
)
)

module_code_db = FAISS.from_documents(module_doc, embeddings)
module_code_db.save_local(faiss_name_module)
Loading

0 comments on commit e796cab

Please sign in to comment.