Skip to content

Commit

Permalink
🧼 pre-commit cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
shroominic committed Aug 2, 2023
1 parent 4bb4006 commit ae756d9
Show file tree
Hide file tree
Showing 31 changed files with 2,624 additions and 234 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ OPENAI_API_KEY=
# (optional, required for production)
# CODEBOX_API_KEY=
# (set True to enable logging)
VERBOSE=False
VERBOSE=False
27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.4.1
hooks:
- id: mypy
args: [--ignore-missing-imports, --follow-imports=skip]
additional_dependencies: [types-requests]
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.9.3
hooks:
- id: isort
args: [--profile=black]
- repo: https://github.com/PYCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
args: [--max-line-length=88]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ if __name__ == "__main__":

```

![Bitcoin YTD](https://github.com/shroominic/codeinterpreter-api/blob/main/examples/assets/bitcoin_chart.png?raw=true)
![Bitcoin YTD](https://github.com/shroominic/codeinterpreter-api/blob/main/examples/assets/bitcoin_chart.png?raw=true)
Bitcoin YTD Chart Output

## Dataset Analysis
Expand Down Expand Up @@ -94,7 +94,7 @@ if __name__ == "__main__":
asyncio.run(main())
```

![Iris Dataset Analysis](https://github.com/shroominic/codeinterpreter-api/blob/main/examples/assets/iris_analysis.png?raw=true)
![Iris Dataset Analysis](https://github.com/shroominic/codeinterpreter-api/blob/main/examples/assets/iris_analysis.png?raw=true)
Iris Dataset Analysis Output

## Production
Expand Down
2 changes: 1 addition & 1 deletion codeinterpreterapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from codeinterpreterapi.session import CodeInterpreterSession
from codeinterpreterapi.schema import File
from codeinterpreterapi.session import CodeInterpreterSession
2 changes: 1 addition & 1 deletion codeinterpreterapi/agents/custom_agent.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# TODO: override some methods of the ConversationalAgent class
# TODO: override some methods of the ConversationalAgent class
# to improve the agent's performance
9 changes: 4 additions & 5 deletions codeinterpreterapi/agents/functions_agent.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Module implements an agent that uses OpenAI's APIs function enabled API.
This file is a modified version of the original file
This file is a modified version of the original file
from langchain/agents/openai_functions_agent/base.py.
Credits go to the original authors :)
"""
Expand All @@ -12,14 +12,11 @@
from json import JSONDecodeError
from typing import Any, List, Optional, Sequence, Tuple, Union

from pydantic import root_validator

from langchain.agents import BaseSingleActionAgent
from langchain.base_language import BaseLanguageModel
from langchain.callbacks.base import BaseCallbackManager
from langchain.callbacks.manager import Callbacks
from langchain.chat_models.openai import ChatOpenAI
from langchain.schema import BasePromptTemplate
from langchain.prompts.chat import (
BaseMessagePromptTemplate,
ChatPromptTemplate,
Expand All @@ -31,13 +28,15 @@
AgentFinish,
AIMessage,
BaseMessage,
BasePromptTemplate,
FunctionMessage,
OutputParserException,
HumanMessage,
OutputParserException,
SystemMessage,
)
from langchain.tools import BaseTool
from langchain.tools.convert_to_openai import format_tool_to_openai_function
from pydantic import root_validator


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion codeinterpreterapi/chains/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .extract_code import extract_python_code
from .modifications_check import get_file_modifications
from .rm_dl_link import remove_download_link
from .extract_code import extract_python_code
18 changes: 10 additions & 8 deletions codeinterpreterapi/chains/extract_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from typing import List, Optional

from langchain.base_language import BaseLanguageModel
from langchain.chat_models.openai import ChatOpenAI
from langchain.chat_models.anthropic import ChatAnthropic
from langchain.chat_models.openai import ChatOpenAI
from langchain.schema import AIMessage, OutputParserException

# from codeinterpreterapi.prompts import extract_code_prompt
Expand All @@ -15,13 +15,12 @@ async def extract_python_code(
retry: int = 2,
) -> Optional[str]:
pass


async def test():
llm = ChatAnthropic(model="claude-1.3") # type: ignore

code = \
"""

code = """
import matplotlib.pyplot as plt
x = list(range(1, 11))
Expand All @@ -34,12 +33,15 @@ async def test():
plt.show()
"""

print(await extract_python_code(code, llm))


if __name__ == "__main__":
import asyncio, dotenv
import asyncio

import dotenv

dotenv.load_dotenv()

asyncio.run(test())
19 changes: 10 additions & 9 deletions codeinterpreterapi/chains/modifications_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from typing import List, Optional

from langchain.base_language import BaseLanguageModel
from langchain.chat_models.openai import ChatOpenAI
from langchain.chat_models.anthropic import ChatAnthropic
from langchain.chat_models.openai import ChatOpenAI
from langchain.schema import AIMessage, OutputParserException

from codeinterpreterapi.prompts import determine_modifications_prompt
Expand All @@ -21,21 +21,19 @@ async def get_file_modifications(

result = await llm.apredict(prompt, stop="```")


try:
result = json.loads(result)
except json.JSONDecodeError:
result = ""
if not result or not isinstance(result, dict) or "modifications" not in result:
return await get_file_modifications(code, llm, retry=retry - 1)
return result["modifications"]


async def test():
llm = ChatAnthropic(model="claude-1.3") # type: ignore

code = \
"""

code = """
import matplotlib.pyplot as plt
x = list(range(1, 11))
Expand All @@ -48,12 +46,15 @@ async def test():
plt.show()
"""

print(await get_file_modifications(code, llm))


if __name__ == "__main__":
import asyncio, dotenv
import asyncio

import dotenv

dotenv.load_dotenv()

asyncio.run(test())
6 changes: 5 additions & 1 deletion codeinterpreterapi/chains/rm_dl_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ async def remove_download_link(
input_response: str,
llm: BaseLanguageModel,
) -> str:
messages = remove_dl_link_prompt.format_prompt(input_response=input_response).to_messages()
messages = remove_dl_link_prompt.format_prompt(
input_response=input_response
).to_messages()
message = await llm.apredict_messages(messages)

if not isinstance(message, AIMessage):
Expand All @@ -28,7 +30,9 @@ async def test():

if __name__ == "__main__":
import asyncio

from dotenv import load_dotenv

load_dotenv()

asyncio.run(test())
5 changes: 3 additions & 2 deletions codeinterpreterapi/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from pydantic import BaseSettings
from dotenv import load_dotenv
from typing import Optional

from dotenv import load_dotenv
from pydantic import BaseSettings

# .env file
load_dotenv(dotenv_path="./.env")

Expand Down
4 changes: 2 additions & 2 deletions codeinterpreterapi/prompts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .system_message import system_message as code_interpreter_system_message
from .modifications_check import determine_modifications_prompt
from .remove_dl_link import remove_dl_link_prompt
from .remove_dl_link import remove_dl_link_prompt
from .system_message import system_message as code_interpreter_system_message
100 changes: 49 additions & 51 deletions codeinterpreterapi/prompts/modifications_check.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,54 @@
from langchain.prompts import PromptTemplate


determine_modifications_prompt = PromptTemplate(
input_variables=["code"],
template=
"The user will input some code and you need to determine if the code makes any changes to the file system. \n"
"With changes it means creating new files or modifying exsisting ones.\n"
"Format your answer as JSON inside a codeblock with a list of filenames that are modified by the code.\n"
"If the code does not make any changes to the file system, return an empty list.\n\n"
"Determine modifications:\n"
"```python\n"
"import matplotlib.pyplot as plt\n"
"import numpy as np\n\n"
"t = np.arange(0.0, 4.0*np.pi, 0.1)\n\n"
"s = np.sin(t)\n\n"
"fig, ax = plt.subplots()\n\n"
"ax.plot(t, s)\n\n"
"ax.set(xlabel=\"time (s)\", ylabel=\"sin(t)\",\n"
" title=\"Simple Sin Wave\")\n"
"ax.grid()\n\n"
"plt.savefig(\"sin_wave.png\")\n"
"```\n\n"
"Answer:\n"
"```json\n"
"{{\n"
" \"modifications\": [\"sin_wave.png\"]\n"
"}}\n"
"```\n\n"
"Determine modifications:\n"
"```python\n"
"import matplotlib.pyplot as plt\n"
"import numpy as np\n\n"
"x = np.linspace(0, 10, 100)\n"
"y = x**2\n\n"
"plt.figure(figsize=(8, 6))\n"
"plt.plot(x, y)\n"
"plt.title(\"Simple Quadratic Function\")\n"
"plt.xlabel(\"x\")\n"
"plt.ylabel(\"y = x^2\")\n"
"plt.grid(True)\n"
"plt.show()\n"
"```\n\n"
"Answer:\n"
"```json\n"
"{{\n"
" \"modifications\": []\n"
"}}\n"
"```\n\n"
"Determine modifications:\n"
"```python\n"
"{code}\n"
"```\n\n"
"Answer:\n"
"```json\n",
template="The user will input some code and you need to determine if the code makes any changes to the file system. \n"
"With changes it means creating new files or modifying exsisting ones.\n"
"Format your answer as JSON inside a codeblock with a list of filenames that are modified by the code.\n"
"If the code does not make any changes to the file system, return an empty list.\n\n"
"Determine modifications:\n"
"```python\n"
"import matplotlib.pyplot as plt\n"
"import numpy as np\n\n"
"t = np.arange(0.0, 4.0*np.pi, 0.1)\n\n"
"s = np.sin(t)\n\n"
"fig, ax = plt.subplots()\n\n"
"ax.plot(t, s)\n\n"
'ax.set(xlabel="time (s)", ylabel="sin(t)",\n'
' title="Simple Sin Wave")\n'
"ax.grid()\n\n"
'plt.savefig("sin_wave.png")\n'
"```\n\n"
"Answer:\n"
"```json\n"
"{{\n"
' "modifications": ["sin_wave.png"]\n'
"}}\n"
"```\n\n"
"Determine modifications:\n"
"```python\n"
"import matplotlib.pyplot as plt\n"
"import numpy as np\n\n"
"x = np.linspace(0, 10, 100)\n"
"y = x**2\n\n"
"plt.figure(figsize=(8, 6))\n"
"plt.plot(x, y)\n"
'plt.title("Simple Quadratic Function")\n'
'plt.xlabel("x")\n'
'plt.ylabel("y = x^2")\n'
"plt.grid(True)\n"
"plt.show()\n"
"```\n\n"
"Answer:\n"
"```json\n"
"{{\n"
' "modifications": []\n'
"}}\n"
"```\n\n"
"Determine modifications:\n"
"```python\n"
"{code}\n"
"```\n\n"
"Answer:\n"
"```json\n",
)
14 changes: 3 additions & 11 deletions codeinterpreterapi/prompts/remove_dl_link.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
from langchain.prompts.chat import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
)
from langchain.schema import (
AIMessage,
SystemMessage,
HumanMessage,
)

from langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate
from langchain.schema import AIMessage, HumanMessage, SystemMessage

remove_dl_link_prompt = ChatPromptTemplate(
input_variables=["input_response"],
Expand All @@ -23,4 +15,4 @@
AIMessage(content="The dataset has been successfully converted to CSV format."),
HumanMessagePromptTemplate.from_template("{input_response}"),
],
)
)
9 changes: 4 additions & 5 deletions codeinterpreterapi/prompts/system_message.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from langchain.schema import SystemMessage


system_message = SystemMessage(
content="""
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics.
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics.
As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
Assistant is constantly learning and improving, and its capabilities are constantly evolving.
It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives,
Assistant is constantly learning and improving, and its capabilities are constantly evolving.
It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives,
allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
This version of Assistant is called "Code Interpreter" and capable of using a python code interpreter (sandboxed jupyter kernel) to run code.
This version of Assistant is called "Code Interpreter" and capable of using a python code interpreter (sandboxed jupyter kernel) to run code.
The human also maybe thinks this code interpreter is for writing code but it is more for data science, data analysis, and data visualization, file manipulation, and other things that can be done using a jupyter kernel/ipython runtime.
Tell the human if they use the code interpreter incorrectly.
Already installed packages are: (numpy pandas matplotlib seaborn scikit-learn yfinance scipy statsmodels sympy bokeh plotly dash networkx).
Expand Down
2 changes: 1 addition & 1 deletion codeinterpreterapi/schema/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .file import File
from .response import CodeInterpreterResponse, UserRequest
from .input import CodeInput, FileInput
from .response import CodeInterpreterResponse, UserRequest
Loading

0 comments on commit ae756d9

Please sign in to comment.