Skip to content

Commit

Permalink
Merge pull request #11 from dbpunk-labs/feat/add_selfhost
Browse files Browse the repository at this point in the history
fix: remove workspace from agent
  • Loading branch information
imotai authored Sep 12, 2023
2 parents f8aa594 + db8fc81 commit cca23b8
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 243 deletions.
18 changes: 2 additions & 16 deletions .github/workflows/release_docker_image.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
name: Docker Release CD
name: Release
on:
push:
tags:
- "[v]?[0-9]+.[0-9]+.[0-9]+"
jobs:

docker_image:
name: build_pr_image
runs-on: ubuntu-latest
Expand All @@ -15,20 +14,7 @@ jobs:
submodules: recursive
- name: Setup build env
run: |
sudo apt-get install protobuf-compiler
cargo install cargo-edit
ROOT_DIR=`pwd`
TAG=${GITHUB_REF/refs\/tags\//}
VERSION=${TAG#*v}
echo ${VERSION}
cargo set-version --workspace ${VERSION}
cd ${ROOT_DIR}/metadata && yarn install && npx hardhat compile
test -e ${ROOT_DIR}/metadata/artifacts/contracts/DB3MetaStore.sol/DB3MetaStore.json && cp -f ${ROOT_DIR}/metadata/artifacts/contracts/DB3MetaStore.sol/DB3MetaStore.json ${ROOT_DIR}/abi/
test -e ${ROOT_DIR}/metadata/artifacts/contracts/libraries/Events.sol/Events.json && cp -f ${ROOT_DIR}/metadata/artifacts/contracts/libraries/Events.sol/Events.json ${ROOT_DIR}/abi/
cd ${ROOT_DIR} && cargo build --release
cp ${ROOT_DIR}/target/release/db3 ${ROOT_DIR}/docker/
cd ${ROOT_DIR}/sdk && yarn && make && yarn build && yarn link
cd ${ROOT_DIR}/thirdparty/data-manager && yarn && yarn link db3.js && yarn build && mv -f dist ${ROOT_DIR}/docker/pages
- name: docker login
uses: docker/login-action@v1
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ __pycache__/
# Distribution / packaging
.Python
build/
sandbox/
develop-eggs/
dist/
downloads/
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ three core featues
python & bash: requirements.txt
typescripts: tslab , tslab install



## Thanks

* [Octopus icons created by Whitevector - Flaticon](https://www.flaticon.com/free-icons/octopus)
6 changes: 3 additions & 3 deletions agent/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

setup(
name="octopus_agent",
version="0.1.1",
version="0.1.2",
description="Open source code interpreter agent for LLM",
author="imotai",
author_email="[email protected]",
Expand All @@ -31,8 +31,8 @@
"octopus_agent": "src/octopus_agent",
},
install_requires=[
"octopus_proto>=0.1.1",
"octopus_kernel>=0.1.1",
"octopus_proto",
"octopus_kernel",
"langchain>=0.0.227",
"grpcio-tools>=1.57.0",
"grpc-google-iam-v1>=0.12.6",
Expand Down
12 changes: 4 additions & 8 deletions agent/src/octopus_agent/agent_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ def connect(self):
self.channel = grpc.insecure_channel(self.endpoint)
self.stub = AgentServerStub(self.channel)

def add_kernel(self, key, endpoint, workspace):
def add_kernel(self, key, endpoint):
"""
add kernel instance to the agent and only admin can call this method
"""
request = agent_server_pb2.AddKernelRequest(
endpoint=endpoint, workspace=workspace, key=key
)
request = agent_server_pb2.AddKernelRequest(endpoint=endpoint, key=key)
response = self.stub.add_kernel(request, metadata=self.metadata)
return response

Expand Down Expand Up @@ -109,13 +107,11 @@ def connect(self):
self.channel = channel
self.stub = AgentServerStub(channel)

async def add_kernel(self, key, endpoint, workspace):
async def add_kernel(self, key, endpoint):
"""
add kernel instance to the agent and only admin can call this method
"""
request = agent_server_pb2.AddKernelRequest(
endpoint=endpoint, workspace=workspace, key=key
)
request = agent_server_pb2.AddKernelRequest(endpoint=endpoint, key=key)
response = await self.stub.add_kernel(request, metadata=self.metadata)
return response

Expand Down
3 changes: 0 additions & 3 deletions agent/src/octopus_agent/agent_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,19 @@ async def add_kernel(
agent = build_openai_agent(
self.llm,
sdk,
request.workspace,
config.get("max_iterations", 5),
self.verbose,
)
# TODO a data dir per user
self.agents[request.key] = {
"sdk": sdk,
"workspace": request.workspace,
"agent": agent,
}
elif config["llm_key"] == "mock":
logger.info("create a mock agent for kernel")
agent = build_mock_agent(self.llm)
self.agents[request.key] = {
"sdk": sdk,
"workspace": request.workspace,
"agent": agent,
}
return agent_server_pb2.AddKernelResponse(code=0, msg="ok")
Expand Down
15 changes: 5 additions & 10 deletions agent/src/octopus_agent/agent_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,23 @@
from octopus_agent.agent_sdk import AgentSDK


async def add_kernel(endpoint, api_key, kernel_endpoint, kernel_api_key, workspace):
async def add_kernel(endpoint, api_key, kernel_endpoint, kernel_api_key):
sdk = AgentSDK(endpoint, api_key)
sdk.connect()
try:
await sdk.add_kernel(kernel_api_key, kernel_endpoint, workspace)
await sdk.add_kernel(kernel_api_key, kernel_endpoint)
print("add kernel %s done" % kernel_endpoint)
except Exception as ex:
print("add kernel %s failed" % kernel_endpoint)
print("add kernel %s failed %s" % (kernel_endpoint, ex))


@click.command()
@click.option("--kernel_endpoint", help="the endpoint of kernel")
@click.option("--kernel_api_key", help="the api key of kernel")
@click.option("--kernel_workspace", help="the workspace of kernel")
@click.option("--agent_endpoint", help="the endpoint of agent")
@click.option("--admin_key", help="the admin key of agent")
def setup(kernel_endpoint, kernel_api_key, kernel_workspace, agent_endpoint, admin_key):
def setup(kernel_endpoint, kernel_api_key, agent_endpoint, admin_key):
if not kernel_endpoint or not kernel_api_key or not admin_key or not agent_endpoint:
print("kernel_endpoint or kernel_api_key or admin key is empty")
return
asyncio.run(
add_kernel(
agent_endpoint, admin_key, kernel_endpoint, kernel_api_key, kernel_workspace
)
)
asyncio.run(add_kernel(agent_endpoint, admin_key, kernel_endpoint, kernel_api_key))
118 changes: 3 additions & 115 deletions agent/src/octopus_agent/gpt_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,64 +51,12 @@ async def _arun(
return "Yes"


class PrintFinalAnswerInput(BaseModel):
answer: str = Field(description="the final answer")


class PrintFinalAnswerTool(StructuredTool):
name = "print_final_answer"
description = """print the the final answer"""
args_schema: Type[BaseModel] = PrintFinalAnswerInput
return_direct = True

def _run(
self,
answer: str,
run_manager: Optional[CallbackManagerForToolRun] = None,
**kwargs: Any,
) -> Any:
return ""

async def _arun(
self, answer: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None
) -> str:
return ""


class PrintCodeInput(BaseModel):
code: str = Field(description="the code showed to the human")
language: str = Field(description="the programing language")
explanation: str = Field(description="the explanation of the code")


class PrintCodeTool(StructuredTool):
name = "print_code"
description = """print the code"""
args_schema: Type[BaseModel] = PrintCodeInput

def _run(
self,
code: str,
language: str,
explanation: str,
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> str:
return ""

async def _arun(
self,
code: str,
language: str,
explanation: str,
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
) -> str:
return ""


class ExecutePythonCodeInput(BaseModel):
code: str = Field(description="the python code to be executed")
explanation: str = Field(description="the explanation of the python code")
saved_filenames: Optional[List[str]] = Field(description="the saved filename list")
saved_filenames: Optional[List[str]] = Field(
description="the saved filename list", default=[]
)


class ExecutePythonCodeTool(StructuredTool):
Expand Down Expand Up @@ -140,63 +88,3 @@ async def _arun(
if not self.octopus_api:
self.octopus_api = OctopusAPIMarkdownOutput()
return await self.octopus_api.arun(code)


class ExecuteShellCodeInput(BaseModel):
code: str = Field(description="the shell code to be executed by bash")
explanation: str = Field(description="the explanation of the shell code")


class ExecuteShellCodeTool(StructuredTool):
name = "run_shell_code"
description = """Execute arbitrary shell code Returns a Markdown format string including return code, result, stdout, stderr, error"""
args_schema: Type[BaseModel] = ExecuteShellCodeInput
octopus_api: OctopusAPIMarkdownOutput = None

def _run(
self,
code: str,
explanation: str,
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> str:
based_code = "%%bash" + "\n" + code
result = self.octopus_api.run(based_code)
return result

async def _arun(
self,
code: str,
explanation: str,
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
) -> str:
based_code = "%%bash" + "\n" + code
return await self.octopus_api.arun(based_code)


class ExecuteTypescriptCodeInput(BaseModel):
code: str = Field(description="the typescript code to be executed")
explanation: str = Field(description="the explanation of the typescript code")


class ExecuteTypescriptCodeTool(StructuredTool):
name = "execute_ts_code"
description = """Execute arbitrary typescript code Returns a Markdown format string including return code, result, stdout, stderr, error"""
args_schema: Type[BaseModel] = ExecuteTypescriptCodeInput
octopus_api: OctopusAPIMarkdownOutput = None

def _run(
self,
code: str,
explanation: str,
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> str:
result = self.octopus_api.run(code, **{"kernel_name": "tslab"})
return result

async def _arun(
self,
code: str,
explanation: str,
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
) -> str:
return await self.octopus_api.arun(code, **{"kernel_name": "tslab"})
9 changes: 4 additions & 5 deletions agent/src/octopus_agent/langchain_agent_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@
from langchain.agents import initialize_agent
from langchain.schema.messages import SystemMessage
from .tools import OctopusAPIMarkdownOutput
from .gpt_tools import ExecutePythonCodeTool, ExecuteShellCodeTool, ExecuteTypescriptCodeTool, PrintCodeTool, PrintFinalAnswerTool
from .gpt_tools import ExecutePythonCodeTool
from .mock_tools import PrintFinalAnswerTool as MockPrintFinalAnswerTool
from .prompt import OCTOPUS_FUNCTION_SYSTEM
from .gpt_async_callback import AgentAsyncHandler
from langchain.agents import AgentType


def build_openai_agent(llm, sdk, workspace, max_iterations, verbose):
def build_openai_agent(llm, sdk, max_iterations, verbose):
"""build openai function call agent"""
# TODO a data dir per user
api = OctopusAPIMarkdownOutput(sdk, workspace)
api = OctopusAPIMarkdownOutput(sdk)
# init the agent
tools = [
ExecutePythonCodeTool(octopus_api=api),
PrintCodeTool(),
PrintFinalAnswerTool(),
]
prefix = (
"""%sBegin!
Expand All @@ -49,6 +47,7 @@ def build_openai_agent(llm, sdk, workspace, max_iterations, verbose):
agent_kwargs={"system_message": system_message},
verbose=verbose,
max_iterations=max_iterations,
handle_parsing_errors=True,
)
return agent

Expand Down
2 changes: 1 addition & 1 deletion agent/src/octopus_agent/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* You must try to correct your code when you get errors from the output
* You are only allowed to ask yes or no questions from the human
Thirdly, the programming environment used to execute code has the following capabilities
Thirdly, the programming environment used to execute code has the following capabilities
* Internet connection: This allows the programming environment to access online resources, such as documentation, libraries, and code repositories.
* IPython kernel: This allows the programming environment to execute Python code
* Filesystem: This allows the programming environment to open, write, and delete files in the workspace directory.
Expand Down
Loading

0 comments on commit cca23b8

Please sign in to comment.