Converting Your Knowledge Graph CSV into a Large Language Model With Langchain and ChainLit.

Sulstice
2 min readFeb 5, 2024

--

So in our last blog post of converting your knowledge graph to a csv file:

perfluorohexanoic acid C(=O)(C(C(C(C(C(F)(F)F)(F)F)(F)F)(F)F)(F)F)O emerging_perfluoroalkyls environmental_chemistry global_chem.environment.emerging_perfluoroalkyls
perfluoroheptanoic acid C(=O)(C(C(C(C(C(C(F)(F)F)(F)F)(F)F)(F)F)(F)F)(F)F)O emerging_perfluoroalkyls environmental_chemistry global_chem.environment.emerging_perfluoroalkyls
perfluorononanoic acid C(=O)(C(C(C(C(C(C(C(C(F)(F)F)(F)F)(F)F)(F)F)(F)F)(F)F)(F)F)(F)F)O emerging_perfluoroalkyls environmental_chemistry global_chem.environment.emerging_perfluoroalkyls
perfluorodecanoic acid C(=O)(C(C(C(C(C(C(C(C(C(F)(F)F)(F)F)(F)F)(F)F)(F)F)(F)F)(F)F)(F)F)(F)F)O emerging_perfluoroalkyls environmental_chemistry global_chem.environment.emerging_perfluoroalkyls

The node path is labeled and the parent of the node with the chemical name. This gives the ability for a langchain model to make a correlation between the name, the molecule, and the elected category.

Let’s do some python imports first:

!pip install langchain
!pip install langchain_experimental
!pip install langchain_openai

Then our imports:

from langchain.agents.agent_types import AgentType
from langchain_experimental.agents.agent_toolkits import create_csv_agent
from langchain_openai import ChatOpenAI, OpenAI

Create the CSV agent, set your temperature to 0:

agent = create_csv_agent(
OpenAI(
temperature=0,
),
"./data/global_chem_data.tsv",
verbose=True,
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
openai_api_key='?',
pandas_kwargs={'sep': ' ', 'header': None}

)

agent.run("how many compounds are perfluoroalkyls?")

The chain then executes with thoughts and observations:

What does it need to do? Slowly it starts reasoning what my ask is.

Then wrap it in a chainlit app:

from langchain.agents.agent_types import AgentType
from langchain_experimental.agents.agent_toolkits import create_csv_agent
from langchain_openai import ChatOpenAI, OpenAI

import chainlit as cl

@cl.on_message
async def main(message: cl.Message):

agent = create_csv_agent(
OpenAI(
temperature=0,
openai_api_key=''
),
"./data/global_chem_data.tsv",
verbose=True,
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
pandas_kwargs={'sep': ' ', 'header': None}

)

answer = agent.run(cl.Message)

# Send the final answer.
await cl.Message(content=answer).send()

And here we go:

In the back-end it’s finding the compounds that I am looking for but giving back a weird response.

This needs some work but the start gets you there.

--

--