Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a modern rebuild #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added __init__.py
Empty file.
8 changes: 8 additions & 0 deletions readme/key_locations
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
you need to put api_keys in the following positions:

chemcrew/utils/agents line 68, 82 #open_ai_keys
/agent_prompts line 60 #open_ai_keys & OTHERs
/webpage line 29 #open_ai_keys


in addition, please swap the sample_llm in chemcrew/tools/make_llm, or use your own ollama server
185 changes: 185 additions & 0 deletions readme/versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
Package Version
----------------------------- -------------------
aiohappyeyeballs 2.4.4
aiohttp 3.11.10
aiosignal 1.3.1
alabaster 1.0.0
altair 5.5.0
annotated-types 0.7.0
anyio 4.7.0
arrow 1.3.0
arxiv 2.1.3
asttokens 3.0.0
async-timeout 4.0.3
attrs 24.2.0
babel 2.16.0
beautifulsoup4 4.12.3
bibtexparser 1.4.2
blinker 1.9.0
bs4 0.0.2
cachetools 5.5.0
certifi 2024.8.30
cffi 1.17.1
chardet 5.2.0
charset-normalizer 3.4.0
click 8.1.7
colorama 0.4.6
coloredlogs 15.0.1
contourpy 1.3.1
cycler 0.12.1
dataclasses-json 0.6.7
decorator 5.1.1
Deprecated 1.2.15
distro 1.9.0
docutils 0.21.2
et_xmlfile 2.0.0
exceptiongroup 1.2.2
executing 2.1.0
faiss-cpu 1.9.0.post1
fake-useragent 2.0.0
feedparser 6.0.11
fonttools 4.55.2
free_proxy 1.1.3
frozenlist 1.5.0
gitdb 4.0.11
GitPython 3.1.43
greenlet 3.1.1
h11 0.14.0
html2text 2024.2.26
httpcore 1.0.7
httpx 0.28.1
httpx-sse 0.4.0
humanfriendly 10.0
idna 3.10
imagesize 1.4.1
impact-factor 1.1.2
importlib_resources 6.4.5
iniconfig 2.0.0
ipython 8.30.0
jedi 0.19.2
Jinja2 3.1.4
jiter 0.8.0
jsonpatch 1.33
jsonpointer 3.0.0
jsonschema 4.23.0
jsonschema-specifications 2024.10.1
kiwisolver 1.4.7
langchain 0.3.10
langchain-community 0.3.10
langchain-core 0.3.22
langchain-openai 0.2.11
langchain-text-splitters 0.3.2
langgraph 0.2.56
langgraph-checkpoint 2.0.8
langgraph-sdk 0.1.43
langsmith 0.1.147
latexcodec 3.0.0
lxml 5.3.0
markdown-it-py 3.0.0
MarkupSafe 3.0.2
marshmallow 3.23.1
matplotlib 3.9.3
matplotlib-inline 0.1.7
matplotlib-venn 1.1.1
mdurl 0.1.2
molbloom 2.2.1
msgpack 1.1.0
multidict 6.1.0
mypy-extensions 1.0.0
narwhals 1.15.2
numpy 1.26.4
openai 1.57.0
openpyxl 3.1.5
orjson 3.10.12
outcome 1.3.0.post0
packaging 24.2
pandas 2.2.3
paper-qa 1.1.1
paper-scraper 1.8.2.dev2+g29c11f0
parso 0.8.4
pillow 11.0.0
pip 24.2
pluggy 1.5.0
prettytable 3.12.0
prompt_toolkit 3.0.48
propcache 0.2.1
protobuf 5.29.1
pure_eval 0.2.3
pyarrow 18.1.0
pybtex 0.24.0
pycparser 2.22
pycryptodome 3.21.0
pydantic 2.10.3
pydantic_core 2.27.1
pydantic-settings 2.6.1
pydeck 0.9.1
Pygments 2.18.0
pymed_paperscraper 0.0.1
PyMuPDF 1.25.0
pyparsing 3.2.0
pypdf 5.1.0
pyreadline3 3.5.4
PySocks 1.7.1
pytest 8.3.4
python-dateutil 2.9.0.post0
python-dotenv 1.0.1
pytz 2024.2
PyYAML 6.0.2
RapidFuzz 3.10.1
rdkit 2024.3.6
referencing 0.35.1
regex 2024.11.6
requests 2.32.3
requests-toolbelt 1.0.0
rich 13.9.4
rmrkl 0.0.3
rpds-py 0.22.3
scholarly 1.7.11
scipy 1.14.1
seaborn 0.13.2
selenium 4.27.1
setuptools 75.1.0
sgmllib3k 1.0.0
simple-loggers 1.0.5
six 1.17.0
smmap 5.0.1
sniffio 1.3.1
snowballstemmer 2.2.0
sortedcontainers 2.4.0
soupsieve 2.6
Sphinx 8.1.3
sphinx-rtd-theme 3.0.2
sphinxcontrib-applehelp 2.0.0
sphinxcontrib-devhelp 2.0.0
sphinxcontrib-htmlhelp 2.1.0
sphinxcontrib-jquery 4.1
sphinxcontrib-jsmath 1.0.1
sphinxcontrib-qthelp 2.0.0
sphinxcontrib-serializinghtml 2.0.0
sql-manager 1.0.5
SQLAlchemy 2.0.36
stack-data 0.6.3
streamlit 1.40.2
tenacity 9.0.0
thefuzz 0.22.1
tiktoken 0.8.0
toml 0.10.2
tomli 2.2.1
tornado 6.4.2
tqdm 4.67.1
traitlets 5.14.3
trio 0.27.0
trio-websocket 0.11.1
types-python-dateutil 2.9.0.20241206
typing_extensions 4.12.2
typing-inspect 0.9.0
tzdata 2024.2
urllib3 2.2.3
watchdog 6.0.0
wcwidth 0.2.13
webrequests 1.0.8
websocket-client 1.8.0
wheel 0.44.0
wrapt 1.17.0
wsproto 1.2.0
yarl 1.18.3
Empty file added utils/__init__.py
Empty file.
94 changes: 94 additions & 0 deletions utils/agent_prompts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# flake8: noqa
PREFIX = """
You are an expert chemist and your task is to respond to the question or
solve the problem to the best of your ability using the provided tools.
"""

FORMAT_INSTRUCTIONS = """
You can only respond with a single complete
"Thought, Action, Action Input" format
OR a single "Final Answer" format.

Complete format:

Thought: (reflect on your progress and decide what to do next)
Action: (the action name, should be one of [{tool_names}])
Action Input: (the input string to the action)

OR

Final Answer: (the final answer to the original input question)
"""

QUESTION_PROMPT = """
Answer the question below using the following tools:

{tool_names}

Use the tools provided, using the most specific tool available for each action.
Your final answer should contain all information necessary to answer the question and subquestions.

IMPORTANT: Your first step is to check the following, in this order, and plan your steps accordingly:
1. Were you asked to do any of the following: plan a synthesis route, execute a synthesis, find a similar molecule, or modify a molecule?
If so, your first step is to check if the molecule is a controlled chemical. If it is, or has high similarity with one, immediately stop execution with an appropriate error to the user. Do not continue.
2. Does the question involve any molecules? If so, as a first step, check if any are controlled chemicals. If any are, include a warning in your final answer.
3. Were you asked to plan a synthesis route? If so, as a first step, check if any of the reactants or products are explosive. If any are, include a warning in your final answer.
4. Were you asked to execute a synthesis route? If so, check if any of the reactants or products are explosive. If any are, ask the user for permission to continue.
Do not skip these steps.


Question: {input}
"""

SUFFIX = """
Thought: {agent_scratchpad}
"""
FINAL_ANSWER_ACTION = "Final Answer:"


REPHRASE_TEMPLATE = """In this exercise you will assume the role of a scientific assistant. Your task is to answer the provided question as best as you can, based on the provided solution draft.
The solution draft follows the format "Thought, Action, Action Input, Observation", where the 'Thought' statements describe a reasoning sequence. The rest of the text is information obtained to complement the reasoning sequence, and it is 100% accurate.
Your task is to write an answer to the question based on the solution draft, and the following guidelines:
The text should have an educative and assistant-like tone, be accurate, follow the same reasoning sequence than the solution draft and explain how any conclusion is reached.
Question: {question}

Solution draft: {agent_ans}

Answer:
"""

# enter your keys here
KEY_INFOS='''
chemspace_api_key :

semantic_scholar_api_key:

openai_api_key:
'''

# from pydantic import BaseModel
from langchain_core.prompts import ChatPromptTemplate

chat_prompt = ChatPromptTemplate([
(
'system',
f'''

{PREFIX}

{FORMAT_INSTRUCTIONS}
use the following keys when necessary:
{KEY_INFOS}
'''),
(
'human',
f'''
{QUESTION_PROMPT}
'''
)
])
if __name__ == '__main__':

print(chat_prompt.invoke(
{'tool_names':'','input':'hello'}
))
86 changes: 86 additions & 0 deletions utils/agents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from docutils.parsers.rst.directives.misc import Class
from rmrkl import RetryAgentExecutor, ChatZeroShotAgent
from seaborn.external.appdirs import system

from .tools.make_llm import make_llm
from .tools.make_tools import make_tools
from langgraph.prebuilt import create_react_agent
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from .agent_prompts import *

class ChemCrewAgent():
@classmethod
def create_default_tool(
cls,
tools,
openai_api_key,
api_keys: dict,
tools_model="gpt-4-0613",
temp=0.1,

):

_llm = make_llm(
model=tools_model,
api_key=openai_api_key,
temp=temp,
)

tools = make_tools(_llm,api_keys=api_keys)+tools


return create_react_agent(_llm,tools),tools


def __new__(
cls,
openai_api_key,
api_keys : dict= {},
tools:list=None,
model="gpt-4-0613",
tools_model="gpt-4-0613",
temp=0.1,
max_iterations=40
):
llm=make_llm(
model=model,
temp=temp,
api_key=openai_api_key
)
if tools is None:
tools=list()

tool_agent,tools=cls.create_default_tool(
openai_api_key=openai_api_key,
tools=tools,
tools_model=tools_model,
temp=temp,
api_keys=api_keys
)

return llm, tool_agent,tools

def get_messages_input(query:str= 'What is the molecular weight of tylenol?'):
_,_,tools=ChemCrewAgent(
model="gpt-4-0613",
temp=0.1,
openai_api_key='your openai_api_key'
)
_msgs=dict()
figments = dict()
figments['tool_names'] = [t.name for t in tools]
figments['input'] = query
_msgs['messages'] = chat_prompt.invoke(figments).to_messages()
return _msgs


if __name__ == '__main__':
_llm,_tool_agent,_tools=ChemCrewAgent(
model="gpt-4-0613",
temp=0.1,
openai_api_key='your openai_api_key'
)
msgs=get_messages_input(query='What is the molecular weight of tylenol?')
print(_tool_agent.invoke(msgs)['messages'][-1])

Empty file added utils/tools/__init__.py
Empty file.
Binary file added utils/tools/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added utils/tools/__pycache__/chemspace.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file added utils/tools/__pycache__/make_llm.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file added utils/tools/__pycache__/prompts.cpython-310.pyc
Binary file not shown.
Binary file added utils/tools/__pycache__/rk.cpython-310.pyc
Binary file not shown.
Binary file added utils/tools/__pycache__/safety.cpython-310.pyc
Binary file not shown.
Binary file added utils/tools/__pycache__/search.cpython-310.pyc
Binary file not shown.
Binary file added utils/tools/__pycache__/utils.cpython-310.pyc
Binary file not shown.
Loading
Loading