Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Undertone0809 committed Apr 21, 2024
2 parents 63f11c3 + 7b0a2b2 commit 6144a8c
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 137 deletions.
47 changes: 25 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ SHELL := /usr/bin/env bash
OS := $(shell python -c "import sys; print(sys.platform)")

# all test files define here
DEV_TEST_TOOL_FILES := ./tests/tools/test_human_feedback_tool.py ./tests/tools/test_calculator.py ./tests/tools/test_python_repl_tools.py ./tests/tools/test_sleep_tool.py ./tests/tools/test_arxiv_tools.py ./tests/tools/test_tool_manager.py
DEV_TEST_TOOL_FILES := ./tests/tools/test_human_feedback_tool.py ./tests/tools/test_calculator.py ./tests/tools/test_python_repl_tools.py ./tests/tools/test_sleep_tool.py ./tests/tools/test_arxiv_tools.py ./tests/tools/test_tool_manager.py ./tests/tools/test_file_tools.py
DEV_TEST_HOOK_FILES := ./tests/hook/test_llm.py ./tests/hook/test_tool_hook.py

DEV_TEST_LLM_FILES := ./tests/llms/test_openai.py ./tests/llms/test_factory.py
DEV_TEST_AGENT_FILES := ./tests/agents/test_tool_agent.py ./tests/agents/test_assistant_agent.py
DEV_TEST_FILES := $(DEV_TEST_TOOL_FILES) $(DEV_TEST_HOOK_FILES) $(DEV_TEST_LLM_FILES) $(DEV_TEST_AGENT_FILES) ./tests/test_chat.py ./tests/output_formatter ./tests/test_import.py ./tests/utils/test_string_template.py
Expand All @@ -20,87 +19,91 @@ else
TEST_PROD_COMMAND := PYTHONPATH=$(PYTHONPATH) poetry run pytest -c pyproject.toml --cov-report=html --cov=promptulate tests
endif

.PHONY: lock
lock:
poetry lock -n && poetry export --without-hashes > requirements.txt

.PHONY: install
install:
poetry install --with dev

.PHONY: install-integration
install-integration:
poetry install --with dev,test_integration

.PHONY: install-docs
install-docs:
npm i docsify-cli -g

.PHONY: pre-commit-install
pre-commit-install:
poetry run pre-commit install

.PHONY: polish-codestyle
polish-codestyle:
poetry run ruff format --config pyproject.toml promptulate tests example
poetry run ruff check --fix --config pyproject.toml promptulate tests example

.PHONY: formatting
formatting: polish-codestyle
format: polish-codestyle

.PHONY: test
test:
$(TEST_COMMAND)

.PHONY: test-prod
test-prod:
$(TEST_PROD_COMMAND)
poetry run coverage-badge -o docs/images/coverage.svg -f

.PHONY: check-codestyle
check-codestyle:
poetry run ruff format --check --config pyproject.toml promptulate tests example
poetry run ruff check --config pyproject.toml promptulate tests example

.PHONY: lint
lint: check-codestyle test

# https://github.com/Maxlinn/linn-jupyter-site-template/blob/main/.github/workflows/linn-jupyter-site-template-deploy.yml
# Any notebook will be converted here.
# If there are any notebook will be changed, then the notebook will be converted to markdown and pushed to the repo.
.PHONY: build-docs
build-docs:
jupyter nbconvert ./example/chat_usage.ipynb --to markdown --output-dir ./docs/use_cases/
jupyter nbconvert ./example/tools/custom_tool_usage.ipynb --to markdown --output-dir ./docs/modules/tools
jupyter nbconvert ./example/llm/custom_llm.ipynb --to markdown --output-dir ./docs/modules/llm
jupyter nbconvert ./example/tools/langchain_tool_usage.ipynb --to markdown --output-dir ./docs/modules/tools
jupyter nbconvert ./example/agent/assistant_agent_usage.ipynb --to markdown --output-dir ./docs/modules/agents


.PHONY: start-docs
start-docs:
docsify serve docs

#* Cleaning
.PHONY: pycache-remove
pycache-remove:
find . | grep -E "(__pycache__|\.pyc|\.pyo$$)" | xargs rm -rf

.PHONY: dsstore-remove
dsstore-remove:
find . | grep -E ".DS_Store" | xargs rm -rf

.PHONY: ipynbcheckpoints-remove
ipynbcheckpoints-remove:
find . | grep -E ".ipynb_checkpoints" | xargs rm -rf

.PHONY: pytestcache-remove
pytestcache-remove:
find . | grep -E ".pytest_cache" | xargs rm -rf

.PHONY: build-remove
build-remove:
rm -rf build/

.PHONY: cleanup
cleanup: pycache-remove dsstore-remove ipynbcheckpoints-remove pytestcache-remove

help:
@echo "lock: Lock the dependencies and export to requirements.txt"
@echo "install: Install the dependencies"
@echo "install-integration: Install the dependencies for integration testing"
@echo "install-docs: Install the dependencies for building docs"
@echo "pre-commit-install: Install the pre-commit hooks"
@echo "polish-codestyle: Format the code"
@echo "formatting: Format the code"
@echo "test: Run the tests"
@echo "test-prod: Run the tests for production"
@echo "check-codestyle: Check the code style"
@echo "lint: Run the tests and check the code style"
@echo "build-docs: Build the docs"
@echo "start-docs: Start the docs server"
@echo "pycache-remove: Remove the pycache"
@echo "dsstore-remove: Remove the .DS_Store files"
@echo "ipynbcheckpoints-remove: Remove the ipynb checkpoints"
@echo "pytestcache-remove: Remove the pytest cache"
@echo "build-remove: Remove the build directory"
@echo "cleanup: Remove all the cache files"

.PHONY: lock install install-integration install-docs pre-commit-install polish-codestyle formatting format test test-prod check-codestyle lint build-docs start-docs pycache-remove dsstore-remove ipynbcheckpoints-remove pytestcache-remove build-remove cleanup help
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</head>
<body>
<nav>
<a href="#/">文档 v1.11.2</a>
<a href="#/">文档</a>
<a href="#/">示例</a>
</nav>
<div id="app"></div>
Expand Down
154 changes: 77 additions & 77 deletions promptulate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,77 @@
# Copyright (c) 2023 promptulate
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Copyright Owner: Zeeland
# GitHub Link: https://github.com/Undertone0809/
# Project Link: https://github.com/Undertone0809/promptulate
# Contact Email: [email protected]

import warnings

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 AIChat, chat
from promptulate.llms.base import BaseLLM
from promptulate.llms.factory import LLMFactory
from promptulate.llms.openai.openai import ChatOpenAI
from promptulate.output_formatter import OutputFormatter
from promptulate.schema import (
AssistantMessage,
BaseMessage,
MessageSet,
SystemMessage,
UserMessage,
)
from promptulate.tools.base import BaseTool, Tool, define_tool
from promptulate.utils.logger import enable_log
from promptulate.utils.string_template import StringTemplate

_util_fields = [
"enable_log",
"OutputFormatter",
"StringTemplate",
]

_schema_fields = [
"AssistantMessage",
"SystemMessage",
"UserMessage",
"BaseMessage",
"MessageSet",
]

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

_tool_fields = [
"Tool",
"define_tool",
"BaseTool",
]

_agent_fields = [
"BaseAgent",
"WebAgent",
"ToolAgent",
]

__all__ = [
*_util_fields,
*_schema_fields,
*_llm_fields,
*_tool_fields,
*_agent_fields,
]

warnings.filterwarnings("always", category=DeprecationWarning)
# Copyright (c) 2023 promptulate
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Copyright Owner: Zeeland
# GitHub Link: https://github.com/Undertone0809/
# Project Link: https://github.com/Undertone0809/promptulate
# Contact Email: [email protected]

import warnings

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 AIChat, chat
from promptulate.llms.base import BaseLLM
from promptulate.llms.factory import LLMFactory
from promptulate.llms.openai.openai import ChatOpenAI
from promptulate.output_formatter import OutputFormatter
from promptulate.schema import (
AssistantMessage,
BaseMessage,
MessageSet,
SystemMessage,
UserMessage,
)
from promptulate.tools.base import BaseTool, Tool, define_tool
from promptulate.utils.logger import enable_log
from promptulate.utils.string_template import StringTemplate

_util_fields = [
"enable_log",
"OutputFormatter",
"StringTemplate",
]

_schema_fields = [
"AssistantMessage",
"SystemMessage",
"UserMessage",
"BaseMessage",
"MessageSet",
]

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

_tool_fields = [
"Tool",
"define_tool",
"BaseTool",
]

_agent_fields = [
"BaseAgent",
"WebAgent",
"ToolAgent",
]

__all__ = [
*_util_fields,
*_schema_fields,
*_llm_fields,
*_tool_fields,
*_agent_fields,
]

warnings.filterwarnings("always", category=DeprecationWarning)
10 changes: 5 additions & 5 deletions promptulate/agents/tool_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
from promptulate.hook import Hook, HookTable
from promptulate.llms.base import BaseLLM
from promptulate.llms.openai.openai import ChatOpenAI
from promptulate.schema import ToolTypes
from promptulate.tools.base import ToolTypes
from promptulate.tools.manager import ToolManager
from promptulate.utils.logger import logger
from promptulate.utils.string_template import StringTemplate


class ActionResponse(TypedDict):
thought: str
analysis: str
action_name: str
action_parameters: Union[dict, str]

Expand All @@ -29,7 +29,6 @@ class ToolAgent(BaseAgent):
Attributes:
llm (BaseLLM): The language model driver. Default is ChatOpenAI with model
"gpt-3.5-turbo-16k".
stop_sequences (List[str]): The sequences that, when met, will stop the output
of the llm.
system_prompt_template (StringTemplate): The preset system prompt template.
prefix_prompt_template (StringTemplate): The prefix system prompt template.
Expand All @@ -45,6 +44,7 @@ class ToolAgent(BaseAgent):
agent_goal (str): The goal of the agent. Default is "provides better assistance
and services for humans.".
agent_constraints (str): The constraints of the agent. Default is "none".
_from (Optional[str]): The initialization source. Default is None.
"""

def __init__(
Expand Down Expand Up @@ -161,7 +161,7 @@ def _run(
Hook.call_hook(
HookTable.ON_AGENT_ACTION,
self,
thought=action_resp["thought"],
thought=action_resp["analysis"],
action=action_resp["action_name"],
action_input=action_resp["action_parameters"],
)
Expand Down Expand Up @@ -211,7 +211,7 @@ def _parse_llm_response(self, llm_resp: str) -> ActionResponse:
data: dict = json.loads(llm_resp)

return ActionResponse(
thought=data["thought"],
analysis=data["analysis"],
action_name=data["action"]["name"],
action_parameters=data["action"]["args"],
)
11 changes: 6 additions & 5 deletions promptulate/agents/tool_agent/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
```json
{
"thought": "The thought of what to do and why.",
"self_criticism":"Constructive self-criticism of the thought",
"analysis": "The thought of what to do and why.",
"action": # the action to take, must be one of provided tools
{
"name": "tool name",
Expand All @@ -61,21 +60,23 @@
to answer the question without using any more tools. At that point, you MUST respond
in the one of the following two formats:
- If you can answer the question:
```json
{
"thought": "The thought of what to do and why.",
"self_criticism":"Constructive self-criticism of the thought",
"analysis": "The thought of what to do and why.",
"action": {
"name": "finish",
"args": {"content": "You answer here."}
}
}
```
- If you cannot answer the question in the current context:
```json
{
"thought": "The thought of what to do and why.",
"self_criticism":"Constructive self-criticism of the thought",
"action": {
"name": "finish",
"args": {"content": "Sorry, I cannot answer your query, because (Summary all the upper steps, and explain)"}
Expand Down
3 changes: 3 additions & 0 deletions promptulate/beta/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from promptulate.beta import agents, rag

__all__ = ["agents", "rag"]
2 changes: 1 addition & 1 deletion promptulate/beta/agents/assistant_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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 ToolTypes
from promptulate.tools.base import ToolTypes
from promptulate.tools.manager import ToolManager
from promptulate.utils.logger import logger

Expand Down
3 changes: 1 addition & 2 deletions promptulate/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
BaseMessage,
MessageSet,
StreamIterator,
ToolTypes,
)
from promptulate.tools.base import BaseTool
from promptulate.tools.base import BaseTool, ToolTypes
from promptulate.utils.logger import logger

T = TypeVar("T", bound=BaseModel)
Expand Down
Loading

0 comments on commit 6144a8c

Please sign in to comment.