-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
补充通过sdk模块记录会话的用户示例 #441
Merged
Merged
补充通过sdk模块记录会话的用户示例 #441
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c93f16c
update
6c32098
update
da6b75a
update
4b36207
update
c703d6b
update
06debae
update
a49839b
update
3d46222
update
9bedf11
update
774998c
update
e02a1fe
update
2516a21
update
89fa02c
update
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
# `AgentRuntime`类 | ||
|
||
## 简介 | ||
|
||
AgentRuntime 是对组件调用的服务化封装,开发者不是必须要用 AgentRuntime 才能运行自己的组件服务。但 AgentRuntime 可以快速帮助开发者服务化组件服务,并且提供API、对话框等部署方式。此外,结合 Component 和 Message 自带的运行和调试接口,可以方便开发者快速获得一个调试 Agent 的服务。 | ||
|
||
|
||
## Python基本用法 | ||
|
||
### 1、实例化`AgentRuntime() -> AgentRuntime` | ||
|
||
#### 方法参数 | ||
|
||
| 参数名称 | 参数类型 | 描述 | 示例值 | | ||
|--------|--------|------------|-----------| | ||
| component | Component | 可运行的 Component,需要实现 run(message, stream, **args) 方法 | "正确的component组件或client" | | ||
| user_session_config | sqlalchemy.engine.URL、str、None | Session 输出存储配置字符串。默认使用 sqlite:///user_session.db | "正确的存储配置字符串" | | ||
|
||
#### 方法功能 | ||
|
||
返回一个调试 Agent 的服务 | ||
|
||
#### 示例代码 | ||
|
||
```python | ||
import os | ||
import appbuilder | ||
os.environ["APPBUILDER_TOKEN"] = '...' | ||
component = appbuilder.Playground( | ||
prompt_template="{query}", | ||
model="eb-4" | ||
) | ||
agent = appbuilder.AgentRuntime(component=component) | ||
``` | ||
|
||
### 2、运行Agent服务`AgentRuntime.chat(message: Message, stream: bool = False, **args) -> Message` | ||
|
||
#### 方法参数 | ||
|
||
| 参数名称 | 参数类型 | 描述 | 示例值 | | ||
|--------|--------|------------|-----------| | ||
| message | Message | 该次对话用户输入的 Message | "正确的Message" | | ||
| stream | bool | 是否使用流式请求。默认为 False | False | | ||
|
||
#### 方法功能 | ||
|
||
运行一个 Agent 服务,执行一次对话 | ||
|
||
#### 示例代码 | ||
|
||
```python | ||
import os | ||
import appbuilder | ||
os.environ["APPBUILDER_TOKEN"] = '...' | ||
component = appbuilder.Playground( | ||
prompt_template="{query}", | ||
model="eb-4" | ||
) | ||
agent = appbuilder.AgentRuntime(component=component) | ||
message = appbuilder.Message({"query": "你好"}) | ||
print(agent.chat(message, stream=False)) | ||
``` | ||
|
||
### 3、提供 Flask http API 接口`AgentRuntime.serve(self, host='0.0.0.0', debug=True, port=8092, url_rule="/chat"` | ||
|
||
#### 方法参数 | ||
|
||
| 参数名称 | 参数类型 | 描述 | 示例值 | | ||
|--------|--------|------------|-----------| | ||
| host | String | 服务主机地址,默认为 '0.0.0.0' | '0.0.0.0' | | ||
| debug | bool | 是否是调试模式,默认为 True | False | | ||
| port | int | 服务端口号,默认为 8092 | 8092 | | ||
| url_rule | String | Flask 路由规则,默认为 '/chat' | '/chat' | | ||
|
||
#### 方法功能 | ||
|
||
将 component 服务化,提供 Flask http API 接口 | ||
|
||
#### 示例代码 | ||
|
||
```python | ||
import os | ||
import appbuilder | ||
os.environ["APPBUILDER_TOKEN"] = '...' | ||
component = appbuilder.Playground( | ||
prompt_template="{query}", | ||
model="eb-4" | ||
) | ||
user_session_config = "sqlite:///foo.db" | ||
agent = appbuilder.AgentRuntime( | ||
component=component, user_session_config=user_session_config) | ||
agent.serve(debug=False, port=8091) | ||
``` | ||
|
||
|
||
### 4、提供 chainlit demo 页面`AgentRuntime.chainlit_demo(host='0.0.0.0', port=8091)` | ||
|
||
|
||
#### 方法参数 | ||
|
||
| 参数名称 | 参数类型 | 描述 | 示例值 | | ||
|--------|--------|------------|-----------| | ||
| host | string | 服务主机地址,默认为 '0.0.0.0' | "0.0.0.0" | | ||
| port | int | 服务端口号,默认为 8092 | 8091 | | ||
|
||
#### 方法功能 | ||
|
||
将 component 服务化,提供 chainlit demo 页面 | ||
|
||
#### 示例代码 | ||
|
||
```python | ||
import os | ||
import logging | ||
from appbuilder.core.component import Component | ||
from appbuilder import ( | ||
AgentRuntime, UserSession, Message, QueryRewrite, Playground, | ||
) | ||
os.environ["APPBUILDER_TOKEN"] = 'YOUR_APPBUILDER_TOKEN' | ||
class PlaygroundWithHistory(Component): | ||
def __init__(self): | ||
super().__init__() | ||
self.query_rewrite = QueryRewrite(model="ERNIE Speed-AppBuilder") | ||
self.play = Playground( | ||
prompt_template="{query}", | ||
model="eb-4" | ||
) | ||
def run(self, message: Message, stream: bool=False): | ||
user_session = UserSession() | ||
# 获取 Session 历史数据 | ||
history_queries = user_session.get_history("query", limit=1) | ||
history_answers = user_session.get_history("answer", limit=1) | ||
if history_queries and history_answers: | ||
history = [] | ||
for query, answer in zip(history_queries, history_answers): | ||
history.extend([query.content, answer.content]) | ||
logging.info(f"history: {history}") | ||
message = self.query_rewrite( | ||
Message(history + [message.content]), rewrite_type="带机器人回复") | ||
logging.info(f"message: {message}") | ||
answer = self.play.run(message, stream) | ||
# 保存本轮数据 | ||
user_session.append({ | ||
"query": message, | ||
"answer": answer, | ||
}) | ||
return answer | ||
|
||
agent = AgentRuntime(component=PlaygroundWithHistory()) | ||
agent.chainlit_demo(port=8091) | ||
``` | ||
|
||
### 5、将 appbuilder client 服务化,提供 chainlit demo 页面`AgentRuntime.chainlit_agent(host='0.0.0.0', port=8091)` | ||
|
||
|
||
#### 方法参数 | ||
|
||
| 参数名称 | 参数类型 | 描述 | 示例值 | | ||
|--------|--------|------------|-----------| | ||
| host | string | 服务主机地址,默认为 '0.0.0.0' | "0.0.0.0" | | ||
| port | int | 服务端口号,默认为 8092 | 8091 | | ||
|
||
#### 方法返回值 | ||
|
||
将 appbuilder client 服务化,提供 chainlit demo 页面 | ||
|
||
#### 示例代码 | ||
|
||
```python | ||
import appbuilder | ||
import os | ||
|
||
os.environ["APPBUILDER_TOKEN"] = '...' | ||
app_id = '...' # 已发布AppBuilder应用ID,可在console端查看 | ||
builder = appbuilder.AppBuilderClient(app_id) | ||
conversation_id = builder.create_conversation() | ||
agent = appbuilder.AgentRuntime(component=builder) | ||
message = appbuilder.Message({"query": "北京今天天气怎么样"}) | ||
print(agent.chat(message, stream=False)) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# `UserSession`类 | ||
|
||
## 简介 | ||
|
||
会话数据管理工具,实例化后将是一个全局变量。提供保存对话数据与获取历史数据的方法。 | ||
|
||
## 应用场景 | ||
|
||
**必须**在 AgentRuntime 启动的服务中使用。 | ||
|
||
## Python基本用法 | ||
|
||
### 1、实例化`UserSession().__init__ -> UserSession` | ||
|
||
#### 方法参数 | ||
|
||
| 参数名称 | 参数类型 | 描述 | 示例值 | | ||
|--------|--------|------------|-----------| | ||
| user_session_config | sqlalchemy.engine.URL、str、None | Session 输出存储配置字符串。默认使用 sqlite:///user_session.db | "正确的存储配置字符串" | | ||
|
||
#### 方法功能 | ||
|
||
初始化 UserSession | ||
|
||
#### 示例代码 | ||
|
||
```python | ||
user_session = UserSession() | ||
``` | ||
|
||
### 2、获取同个 session 中名为 key 的历史变量`UserSession().get_history(self, key: str, limit: int=10) -> List[Message]:` | ||
|
||
#### 方法参数 | ||
|
||
| 参数名称 | 参数类型 | 描述 | 示例值 | | ||
|--------|--------|------------|-----------| | ||
| key | String | 历史变量 | "answer" | | ||
| limit | int | 最近 limit 条 Message 数据 | 10 | | ||
|
||
#### 方法功能 | ||
|
||
获取同个 session 中名为 key 的历史变量。在非服务化版本中从内存获取。在服务化版本中,将从数据库获取。 | ||
|
||
#### 方法返回值 | ||
|
||
`List[Message]` | ||
|
||
衍生类`Message`定义如下: | ||
|
||
```python | ||
class Message(BaseModel, Generic[_T], extra=Extra.allow): | ||
content: Optional[_T] = {} | ||
name: Optional[str] = "msg" | ||
mtype: Optional[str] = "dict" | ||
id: Optional[str] = str(uuid.uuid4()) | ||
``` | ||
|
||
#### 示例代码 | ||
|
||
```python | ||
history_queries = user_session.get_history("query", limit=1) | ||
``` | ||
|
||
### 3、将 message_dict 中的变量保存到 session 中`UserSession().append(self, message_dict: Dict[str, Message]) -> None` | ||
|
||
#### 方法参数 | ||
|
||
| 参数名称 | 参数类型 | 描述 | 示例值 | | ||
|--------|--------|------------|-----------| | ||
| message_dict | Dict | 包含 Message 的字典,其中键为字符串类型,值为 Message 类型 | {"query": message} | | ||
|
||
#### 方法功能 | ||
|
||
将 message_dict 中的变量保存到 session 中。在非服务化版本中使用内存存储。在服务化版本中,将使用数据库进行存储。 | ||
|
||
#### 示例代码 | ||
|
||
```python | ||
user_session.append({ | ||
"query": message, | ||
"answer": answer, | ||
}) | ||
``` | ||
|
||
### 4、UserSession结合AgentRuntime使用以及user_session.db文件读取 | ||
|
||
- [UserSession结合AgentRuntime使用以及user_session.db文件读取](https://github.com/baidubce/app-builder/blob/master/cookbooks/components/agent_runtime.ipynb) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
将文档目录链接放在 进阶实践 下面,同时更新下docs/ 目录下的README