Skip to content

Commit

Permalink
refactoring of coding examples
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkulaga committed Oct 23, 2024
1 parent dccd5b4 commit 0258349
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 159 deletions.
Empty file added examples/__init__.py
Empty file.
27 changes: 0 additions & 27 deletions examples/bioinformatic_prompt.txt

This file was deleted.

64 changes: 64 additions & 0 deletions examples/code_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from pathlib import Path
from dotenv import load_dotenv
from just_agents.interfaces.IAgent import build_agent, IAgent
from just_agents.llm_session import LLMSession
from llm_sandbox.micromamba import MicromambaSession
from llm_sandbox.docker import SandboxDockerSession
from docker.types import Mount
import os

load_dotenv(override=True)

"""
This example shows how to use a Chain Of Thought code agent to run python code and bash commands, it uses volumes and is based on Chain Of Thought Agent class.
"""

def make_mounts():
examples_dir = Path(__file__).parent.absolute()
assert examples_dir.exists(), f"Examples directory {str(examples_dir)} does not exist, check the current working directory"
input_dir = examples_dir / "input"
output_dir = examples_dir / "output"
return [
Mount(target="/input", source=str(input_dir), type="bind"),
Mount(target="/output", source=str(output_dir), type="bind")
]

def run_bash_command(command: str):
"""
command: str # command to run in bash, for example install software inside micromamba environment
"""
mounts = make_mounts()

with MicromambaSession(image="ghcr.io/longevity-genie/just-agents/biosandbox:main",
lang="python",
keep_template=True,
verbose=True,
mounts=mounts
) as session:
result = session.execute_command(command=command)
return result


def run_python_code(code: str):
"""
code: str # python code to run in micromamba environment
"""
mounts = make_mounts()

with MicromambaSession(image="ghcr.io/longevity-genie/just-agents/biosandbox:main",
lang="python",
keep_template=True,
verbose=True,
mounts=mounts
) as session:
result = session.run(code)
return result

if __name__ == "__main__":
examples_dir = Path(__file__).parent.absolute()
assert examples_dir.exists(), f"Examples directory {str(examples_dir)} does not exist, check the current working directory"

assistant: LLMSession= build_agent(examples_dir / "code_agent.yaml")
result, thoughts = assistant.query("Get FGF2 human protein sequence with biopython from uniprot and save it as FGF2.fasta")
print("Thoughts: ", thoughts)
print("Result: ", result)
13 changes: 8 additions & 5 deletions examples/code_agent.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
class: "ChainOfThoughtAgent" #TODO: still work in progress
class: "ChainOfThoughtAgent"
system_prompt: "You are a bioinformatician AI assistant.
Your role is to help with bioinformatics tasks and generate plans or code as needed.
Please adhere to the following guidelines strictly:
1. Always maintain your role as a bioinformatician.
2. You are working on an Ubuntu 24.04 system with base micromamba installed, no other software is installed by default.
2. You are working on an Ubuntu 24.04 system with base micromamba environment installed which includes biopython, requests, polars, genomepy, pyensembl, plotly and GEOparse libraries installed.
However no other software is installed by default.
3. You use run_bash_command tool to install new dependencies. You do not need to activate base micromamba environment, it is already preactivated when you run commands.
4. Use run_python_code tool to run python code. The code will be run in the base micromamba environment in which the dependencies are installed with run_bash_command.
5. Use information provided in the input to write detailed plans or bash code to accomplish the given goal or task.
6. Do not include loading data as a separate step in your plans.
6. Do not include loading data as a separate step in your plans. If input data is provided it will be in the /input directory.
7. When writing code:
- Use full absolute paths for all files.
- Install dependencies and software using micromamba, pip with the -y flag.
- Use default values for unspecified parameters.
- Only use software directly installed with micromamba or pip.
- Do not repeat steps already completed in the history.
- If you create files and folders with results save them inside /output directory unless other is specified explicitly.
8. Pay attention to the number of input files and do not miss any.
9. Do not create or activate the micromamba environment 'base', it is already activated by default.
10. Be aware of file name changes or outputs from previous steps when provided with history.
11. If execution errors occur, fix the code based on the error information provided.
12. When you are ready to give the final answer, explain the results obtained and files and folders created in the /output (if any).
System constraints:
- You are working on an Ubuntu 24.04 system.
- You have a micromamba environment named 'base'.
Expand Down Expand Up @@ -56,9 +59,9 @@ thought_max_tokes: 500
max_steps: 25
final_max_tokens: 1500
tools:
- package: "just_agents_coding.tools"
- package: "examples.code_agent"
function: "run_bash_command"
- package: "just_agents_coding.tools"
- package: "examples.code_agent"
function: "run_python_code"
options:
model: "gpt-4o-mini"
Expand Down
3 changes: 2 additions & 1 deletion examples/input/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
!.gitignore
!.gitignore
*
2 changes: 1 addition & 1 deletion examples/output/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
!.gitignore
./*
*
48 changes: 0 additions & 48 deletions examples/run_code_with_output.py

This file was deleted.

9 changes: 0 additions & 9 deletions examples/run_simple_code.py

This file was deleted.

18 changes: 18 additions & 0 deletions examples/simple_code_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pathlib import Path
from dotenv import load_dotenv

from just_agents.interfaces.IAgent import build_agent, IAgent
load_dotenv(override=True)

"""
This example shows how to use a simple code agent to run python code and bash commands, it does not use volumes and is based on basic LLMSession class.
"""


if __name__ == "__main__":

examples_dir = Path(__file__).parent.absolute()
assert examples_dir.exists(), f"Examples directory {str(examples_dir)} does not exist, check the current working directory"

assistant: IAgent = build_agent( examples_dir / "simple_code_agent.yaml")
assistant.query("Get FGF2 human protein sequence with biopython from uniprot")
3 changes: 2 additions & 1 deletion examples/simple_code_agent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ system_prompt: "You are a bioinformatician AI assistant.
Your role is to help with bioinformatics tasks and generate plans or code as needed.
Please adhere to the following guidelines strictly:
1. Always maintain your role as a bioinformatician.
2. You are working on an Ubuntu 24.04 system with base micromamba installed, no other software is installed by default.
2. You are working on an Ubuntu 24.04 system with base micromamba environment which includes biopython, requests, polars, genomepy, pyensembl, plotly and GEOparse libraries installed.
However, no other software is installed by default.
3. You use run_bash_command tool to install new dependencies. You do not need to activate base micromamba environment, it is already preactivated when you run commands.
4. Use run_python_code tool to run python code. The code will be run in the base micromamba environment in which the dependencies are installed with run_bash_command.
5. Use information provided in the input to write detailed plans or bash code to accomplish the given goal or task.
Expand Down
5 changes: 5 additions & 0 deletions just_agents_coding/containers/biosandbox/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ RUN apt update && \
apt upgrade -y && \
apt install -y tar gzip libz-dev software-properties-common python3-software-properties automake nano cmake zip wget gcc git build-essential curl gosu libbz2-dev zlib1g-dev gawk libxml2-dev

RUN mkdir /input && \
chown -R $MAMBA_USER:$MAMBA_USER /input
RUN mkdir /output && \
chown -R $MAMBA_USER:$MAMBA_USER /output

USER $MAMBA_USER
COPY --chown=$MAMBA_USER:$MAMBA_USER env.yaml /tmp/env.yaml
RUN micromamba install -y -n base -f /tmp/env.yaml && \
Expand Down
5 changes: 5 additions & 0 deletions just_agents_coding/containers/sandbox/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ RUN apt update && \
apt upgrade -y && \
apt install -y tar gzip libz-dev software-properties-common python3-software-properties automake nano cmake zip wget gcc git build-essential curl gosu libbz2-dev zlib1g-dev gawk libxml2-dev

RUN mkdir /input && \
chown -R $MAMBA_USER:$MAMBA_USER /input
RUN mkdir /output && \
chown -R $MAMBA_USER:$MAMBA_USER /output

USER $MAMBA_USER
COPY --chown=$MAMBA_USER:$MAMBA_USER env.yaml /tmp/env.yaml
RUN micromamba install -y -n base -f /tmp/env.yaml && \
Expand Down
65 changes: 0 additions & 65 deletions just_agents_coding/micromamba_session.py

This file was deleted.

4 changes: 2 additions & 2 deletions just_agents_coding/tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from just_agents_coding.micromamba_session import MicromambaSession
from llm_sandbox.micromamba import MicromambaSession
from llm_sandbox.docker import ConsoleOutput

def run_bash_command(command: str):
Expand All @@ -18,7 +18,7 @@ def run_python_code(code: str):
result: ConsoleOutput = session.run(code)
return result

def copy_files_from_runtime(src: str, dest: str):
def copy_from_container(src: str, dest: str):
"""
src: str # path to file in runtime
dest: str # path to file in host
Expand Down

0 comments on commit 0258349

Please sign in to comment.