Skip to content

Commit

Permalink
Merge pull request #552 from Undertone0809/v1.15.0/opt-pne-chat
Browse files Browse the repository at this point in the history
feat: optimize pne chat and add AIChat
  • Loading branch information
Undertone0809 authored Mar 30, 2024
2 parents f078f92 + bf0c2ef commit d680bec
Show file tree
Hide file tree
Showing 29 changed files with 1,247 additions and 486 deletions.
204 changes: 132 additions & 72 deletions README.md

Large diffs are not rendered by default.

217 changes: 144 additions & 73 deletions README_zh.md

Large diffs are not rendered by default.

226 changes: 150 additions & 76 deletions docs/README.md

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions docs/get_started/quick_start.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 快速开始

通过该部分教学,你可以快速对 promptulate 有一个整体的认知,了解一些常用模块的基本使用方式,在阅读完该部分之后,你可以继续阅读 [User Cases](modules/usercases/intro.md#user-cases) 来了解 promptulate 的一些最佳实践,在遇到问题的时候,可以查看每个模块的具体使用方式,也欢迎你在 [issue](https://github.com/Undertone0809/promptulate/issues) 中为 promptulate 提供更好的建议。
通过该部分教学,你可以快速对 promptulate 有一个整体的认知,了解一些常用模块的基本使用方式,在阅读完该部分之后,你可以继续阅读 [User Cases](modules/usercases/intro.md#user-cases) [example](https://github.com/Undertone0809/promptulate/tree/main/example) 来了解 promptulate 的一些最佳实践,在遇到问题的时候,可以查看每个模块的具体使用方式,也欢迎你在 [issue](https://github.com/Undertone0809/promptulate/issues) 中为 promptulate 提供更好的建议。

## 安装最新版

Expand Down Expand Up @@ -234,7 +234,6 @@ pip install poetry
make install
```


本项目使用配备代码语法检查工具,如果你想提交 pr,则需要在 commit 之前运行 `make polish-codestyle` 进行代码规范格式化,并且运行 `make lint` 通过语法与单元测试的检查。

## 更多
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/agents/assistant_agent_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ However, it is really easy to create your own tools - see documentation [here](h
```python
from langchain_community.tools.tavily_search import TavilySearchResults

tools = [TavilySearchResults(max_results=3)]
tools = [TavilySearchResults(max_results=5)]
```

## Create the Assistant Agent
Expand All @@ -69,7 +69,7 @@ agent = AssistantAgent(tools=tools, llm=llm)
agent.run("what is the hometown of the 2024 Australia open winner?")
```

**output**
**Output:**

```text
[Agent] Assistant Agent start...
Expand Down
202 changes: 151 additions & 51 deletions docs/use_cases/chat_usage.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions example/agent/assistant_agent_usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"source": [
"from langchain_community.tools.tavily_search import TavilySearchResults\n",
"\n",
"tools = [TavilySearchResults(max_results=3)]"
"tools = [TavilySearchResults(max_results=5)]"
],
"metadata": {
"collapsed": true,
Expand Down Expand Up @@ -173,7 +173,7 @@
{
"cell_type": "markdown",
"source": [
"**output**\n",
"**Output:**\n",
"\n",
"```text\n",
"[Agent] Assistant Agent start...\n",
Expand Down
313 changes: 255 additions & 58 deletions example/chat_usage.ipynb

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions example/llm/factory.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
},
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## LLM-Factory\n",
"\n",
"This notebook show how to use LLMFactory."
]
},
Expand All @@ -20,9 +19,6 @@
"execution_count": 2,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"pycharm": {
"name": "#%%\n"
},
Expand Down Expand Up @@ -70,4 +66,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
6 changes: 3 additions & 3 deletions example/output_formatter/output_formatter_with_llm_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
from promptulate.pydantic_v1 import BaseModel, Field


class Response(BaseModel):
class LLMResponse(BaseModel):
provinces: List[str] = Field(description="List of provinces name")


def main():
llm = ChatOpenAI()
formatter = OutputFormatter(Response)
formatter = OutputFormatter(LLMResponse)

prompt = (
f"Please tell me the names of provinces in China.\n"
f"{formatter.get_formatted_instructions()}"
)
llm_output = llm(prompt)
response: Response = formatter.formatting_result(llm_output)
response: LLMResponse = formatter.formatting_result(llm_output)
print(response)


Expand Down
4 changes: 2 additions & 2 deletions promptulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from promptulate.agents.base import BaseAgent
from promptulate.agents.tool_agent.agent import ToolAgent
from promptulate.agents.web_agent.agent import WebAgent
from promptulate.chat import chat
from promptulate.chat import AIChat, chat
from promptulate.llms.base import BaseLLM
from promptulate.llms.factory import LLMFactory
from promptulate.llms.openai.openai import ChatOpenAI
Expand Down Expand Up @@ -52,7 +52,7 @@
"MessageSet",
]

_llm_fields = ["chat", "BaseLLM", "ChatOpenAI", "LLMFactory"]
_llm_fields = ["chat", "AIChat", "BaseLLM", "ChatOpenAI", "LLMFactory"]

_tool_fields = [
"Tool",
Expand Down
9 changes: 5 additions & 4 deletions promptulate/agents/tool_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
)
from promptulate.hook import Hook, HookTable
from promptulate.llms.base import BaseLLM
from promptulate.schema import TOOL_TYPES
from promptulate.llms.openai.openai import ChatOpenAI
from promptulate.schema import ToolTypes
from promptulate.tools.manager import ToolManager
from promptulate.utils.logger import logger
from promptulate.utils.string_template import StringTemplate
Expand Down Expand Up @@ -49,8 +50,8 @@ class ToolAgent(BaseAgent):
def __init__(
self,
*,
llm: BaseLLM,
tools: Optional[List[TOOL_TYPES]] = None,
llm: BaseLLM = None,
tools: Optional[List[ToolTypes]] = None,
prefix_prompt_template: StringTemplate = StringTemplate(PREFIX_TEMPLATE),
hooks: Optional[List[Callable]] = None,
enable_role: bool = False,
Expand All @@ -67,7 +68,7 @@ def __init__(
)

super().__init__(hooks=hooks, agent_type="Tool Agent", _from=_from)
self.llm: BaseLLM = llm
self.llm: BaseLLM = llm or ChatOpenAI(model="gpt-4-1106-preview")
"""llm provider"""
self.tool_manager: ToolManager = (
tool_manager if tool_manager is not None else ToolManager(tools or [])
Expand Down
15 changes: 10 additions & 5 deletions promptulate/beta/agents/assistant_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
from promptulate.agents.tool_agent import ToolAgent
from promptulate.agents.tool_agent.agent import ActionResponse
from promptulate.beta.agents.assistant_agent import operations
from promptulate.beta.agents.assistant_agent.schema import (
Plan,
)
from promptulate.beta.agents.assistant_agent.schema import Plan
from promptulate.hook import Hook, HookTable
from promptulate.llms.base import BaseLLM
from promptulate.schema import TOOL_TYPES
from promptulate.schema import ToolTypes
from promptulate.tools.manager import ToolManager
from promptulate.utils.logger import logger

Expand All @@ -38,7 +36,8 @@ def __init__(
self,
*,
llm: BaseLLM,
tools: Optional[List[TOOL_TYPES]] = None,
tools: Optional[List[ToolTypes]] = None,
max_iterations: Optional[int] = 20,
**kwargs,
):
super().__init__(agent_type="Assistant Agent", **kwargs)
Expand All @@ -52,6 +51,7 @@ def __init__(
self.task_handler, self.step_handler, self.result_handler
)
self.current_task_id: Optional[str] = None
self.max_iterations: int = max_iterations

logger.info("Assistant Agent initialized.")

Expand Down Expand Up @@ -226,6 +226,11 @@ def step_handler(self, step: uacp.Step) -> uacp.Step:
StepTypes.REVISE: self.revise,
}

if len(self.current_task.steps) > self.max_iterations:
final_output: str = self.current_task.steps[-1].output
step.output = f"Task has too many steps. Aborting. Recently step output: {final_output}" # noqa
return step

if step.name not in step_map:
raise ValueError(f"Step name {step.name} not found in step mapping.")

Expand Down
19 changes: 14 additions & 5 deletions promptulate/beta/agents/assistant_agent/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,29 @@
""" # noqa

REVISE_SYSTEM_PROMPT = """
For the given objective, come up with a simple step by step plan. \
This plan should involve individual tasks, that if executed correctly will yield the correct answer. Do not add any superfluous steps. \
The result of the final step should be the final answer. Make sure that each step has all the information needed - do not skip steps.
As a powerfule agent, your job is to help the user achieve their goal. This is user instruction: {{user_target}}
Your objective was this:
{{user_target}}
## Workflows
Currently, you are working on a job that has been planned. The plan execution details / job description is provided. Please review the plan and revise the plan according to the execution details to meet the goal. You should follow the following rules:
1. If you think any task is DONE already, mark the task status as DONE.
2. If the task execution result does not meet the target, mark the task status as ERROR.
3. If you think current plan is missing tasks to meet the goal, you can add new tasks.
4. Some of the task may be invalid, mark the task status as DISCARDED.
5. Pay attention to the status of each of the tasks.
6. If you think the plan is complete, next task id should be None.
Your original plan was this:
{{original_plan}}
You have currently done the follow steps:
{{past_steps}}
## Task
Update your plan accordingly. If no more steps are needed and you can return to the user, then respond with that. Otherwise, fill out the plan. Only add steps to the plan that still NEED to be done. Do not return previously done steps as part of the plan.
You need to output the updated plan and next task id.
## Constraints
- Next task id must be searchable in the current task list.
""" # noqa

PLAN_SYSTEM_PROMPT_TMP = StringTemplate(PLAN_SYSTEM_PROMPT, "jinja2")
Expand Down
28 changes: 25 additions & 3 deletions promptulate/beta/agents/assistant_agent/schema.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
from enum import Enum
from typing import List, Optional

from promptulate.pydantic_v1 import BaseModel, Field


class TaskStatus(str, Enum):
TODO = "todo"
DONE = "done"
ERROR = "error"
DISCARDED = "discarded"


class Task(BaseModel):
task_id: int = Field(..., description="The ID of the task. Start from 1.")
description: str = Field(..., description="The description of the task.")
status: TaskStatus = Field(TaskStatus.TODO, description="The status of the task.")


class AgentPlanResponse(BaseModel):
Expand All @@ -26,7 +35,7 @@ def get_next_task(self) -> Optional[Task]:
return next((t for t in self.tasks if t.task_id == self.next_task_id), None)


class AgentReviseResponse(Plan):
class AgentReviseResponse(BaseModel):
thought: str = Field(..., description="The thought of the reflect plan.")
goals: List[str] = Field(..., description="List of goals in the plan.")
tasks: List[Task] = Field(
Expand All @@ -41,8 +50,21 @@ class AgentReviseResponse(Plan):
AgentPlanResponse(
goals=["Goal 1"],
tasks=[
Task(task_id=1, description="Task 1"),
Task(task_id=2, description="Task 2"),
Task(task_id=1, description="Task 1", status=TaskStatus.TODO),
Task(task_id=2, description="Task 2", status=TaskStatus.TODO),
],
),
]


revise_plan_examples = [
AgentReviseResponse(
thought="thought what to do next",
goals=["Goal 1"],
tasks=[
Task(task_id=1, description="Task 1", status=TaskStatus.DONE),
Task(task_id=2, description="Task 2", status=TaskStatus.TODO),
],
next_task_id=2,
),
]
Loading

0 comments on commit d680bec

Please sign in to comment.