Skip to content

Commit

Permalink
docs(agent): Add agents cookbook
Browse files Browse the repository at this point in the history
  • Loading branch information
fangyinc committed Apr 11, 2024
1 parent f01ad36 commit d115ec7
Show file tree
Hide file tree
Showing 9 changed files with 837 additions and 9 deletions.
12 changes: 11 additions & 1 deletion dbgpt/agent/expand/code_assistant_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@
"such as: True.\n"
" If it is determined to be a failure, return false and the reason, "
"such as: False. There are no numbers in the execution results that answer the "
"computational goals of the mission."
"computational goals of the mission.\n"
"You can refer to the following examples:\n"
"user: Please understand the following task objectives and results and give your "
"judgment:\nTask goal: Calculate the result of 1 + 2 using Python code.\n"
"Execution Result: 3\n"
"assistant: True\n\n"
"user: Please understand the following task objectives and results and give your "
"judgment:\nTask goal: Calculate the result of 100 * 10 using Python code.\n"
"Execution Result: 'you can get the result by multiplying 100 by 10'\n"
"assistant: False. There are no numbers in the execution results that answer the "
"computational goals of the mission.\n"
)


Expand Down
7 changes: 4 additions & 3 deletions dbgpt/agent/resource/resource_db_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Database resource client API."""
import logging
from contextlib import contextmanager
from typing import Iterator, List, Optional, Union
from typing import TYPE_CHECKING, Iterator, List, Optional, Union

from .resource_api import AgentResource, ResourceClient, ResourceType

Expand Down Expand Up @@ -48,7 +48,8 @@ async def run_sql(self, db: str, sql: str):
class SqliteLoadClient(ResourceDbClient):
"""SQLite resource client."""

from sqlalchemy.orm.session import Session
if TYPE_CHECKING:
from sqlalchemy.orm.session import Session

def __init__(self):
"""Create a SQLite resource client."""
Expand All @@ -59,7 +60,7 @@ def get_data_type(self, resource: AgentResource) -> str:
return "sqlite"

@contextmanager
def connect(self, db) -> Iterator[Session]:
def connect(self, db) -> Iterator["Session"]:
"""Connect to the database."""
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
Expand Down
3 changes: 3 additions & 0 deletions dbgpt/agent/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
"""Utils for the agent module."""
from .cmp import cmp_string_equal # noqa: F401

__ALL__ = ["cmp_string_equal"]
2 changes: 2 additions & 0 deletions dbgpt/vis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""GPT-Vis Module."""

from .base import Vis # noqa: F401
from .client import vis_client # noqa: F401
from .tags.vis_agent_message import VisAgentMessages # noqa: F401
from .tags.vis_agent_plans import VisAgentPlans # noqa: F401
Expand All @@ -9,6 +10,7 @@
from .tags.vis_plugin import VisPlugin # noqa: F401

__ALL__ = [
"Vis",
"vis_client",
"VisAgentMessages",
"VisAgentPlans",
Expand Down
91 changes: 91 additions & 0 deletions docs/docs/agents/cookbook/calculator_with_agents.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Calculator With Agents

In this example, we will show you how to use an agent as your calculator.

## Installations

Install the required packages by running the following command:

```bash
pip install "dbgpt[agent]>=0.5.4rc0" -U
pip install openai
```

## Code

Create a new Python file and add the following code:

```python
import asyncio

from dbgpt.agent import AgentContext, GptsMemory, LLMConfig, UserProxyAgent
from dbgpt.agent.expand.code_assistant_agent import CodeAssistantAgent
from dbgpt.model.proxy import OpenAILLMClient

async def main():
llm_client = OpenAILLMClient(model_alias="gpt-3.5-turbo")
context: AgentContext = AgentContext(conv_id="test123")
default_memory: GptsMemory = GptsMemory()

# Create a code assistant agent
coder = (
await CodeAssistantAgent()
.bind(context)
.bind(LLMConfig(llm_client=llm_client))
.bind(default_memory)
.build()
)

# Create a user proxy agent
user_proxy = await UserProxyAgent().bind(context).bind(default_memory).build()

# Initiate a chat with the user proxy agent
await user_proxy.initiate_chat(
recipient=coder,
reviewer=user_proxy,
message="calculate the result of 321 * 123"
)
# Obtain conversation history messages between agents
print(await default_memory.one_chat_completions("test123"))

if __name__ == "__main__":
asyncio.run(main())
```

You will see the following output:

````bash
--------------------------------------------------------------------------------
User (to Turing)-[]:

"calculate the result of 321 * 123"

--------------------------------------------------------------------------------
un_stream ai response: ```python
# Calculate the result of 321 * 123
result = 321 * 123
print(result)
```

>>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...
execute_code was called without specifying a value for use_docker. Since the python docker package is not available, code will be run natively. Note: this fallback behavior is subject to change
un_stream ai response: True

--------------------------------------------------------------------------------
Turing (to User)-[gpt-3.5-turbo]:

"```python\n# Calculate the result of 321 * 123\nresult = 321 * 123\nprint(result)\n```"
>>>>>>>>Turing Review info:
Pass(None)
>>>>>>>>Turing Action report:
execution succeeded,

39483


--------------------------------------------------------------------------------
```agent-plans
[{"name": "calculate the result of 321 * 123", "num": 1, "status": "complete", "agent": "Human", "markdown": "```agent-messages\n[{\"sender\": \"CodeEngineer\", \"receiver\": \"Human\", \"model\": \"gpt-3.5-turbo\", \"markdown\": \"```vis-code\\n{\\\"exit_success\\\": true, \\\"language\\\": \\\"python\\\", \\\"code\\\": [[\\\"python\\\", \\\"# Calculate the result of 321 * 123\\\\nresult = 321 * 123\\\\nprint(result)\\\"]], \\\"log\\\": \\\"\\\\n39483\\\\n\\\"}\\n```\"}]\n```"}]
```
(dbgpt-agents-py3.11) (base) ➜ dbgpt-agents
````
Loading

0 comments on commit d115ec7

Please sign in to comment.