AI Lang Chain in a Faith Based Setting — Python

Joseph Liu
8 min readMar 16, 2024
Memes are for Entertainment Purposes Only

As AI scales it becomes imperative to apply it every setting possible where it can empower people. One of my main struggles early in my life was that whenever I asked any difficult faith-based questions I had no way to get the answer. There was no-one for me to go to which could answer my questions.

Overtime I’ve lost that luster of curiosity for asking deep theological questions; although I seemed to maintain it in other disciplines. I wish for future generations to not lose that spark of curiosity that forges great theologians.

The traditional approach to this issue is to delve into Theology, but not everyone has the inclination or the time for such study. A modern alternative is to create an AI Language Chain with Retrieval Augmented Generation, which can analyze texts and store them for future reference, providing a convenient resource for users seeking insights.

Setting up the AI Lang Chain

At this point I’m assuming you already know coding so this delves into the main part of the Lang Chain. Let’s assume that we want to create an AI that can discuss about the existence of pain.

A theologian might refer to C.S Lewis “Problem of Pain” as a resource where someone is capable of understanding the intricacies behind why God would create something such as pain. Let’s say you don’t have time to read the whole book so like any programmer, you write a program in 3 hours for a task which could’ve taken 1 hour. Classic Programmer Move.

For this example I’ll assume that is the case. First install your packages.

pip install pypdf
pip install langchain

Note you can just Google “Problem of Pain CS Lewis pdf” to get the file.

Put it in the same folder as your program

it should look like this

We’ll render that in

from langchain_community.document_loaders import PyPDFLoader

loader = PyPDFLoader("Problem of Pain CS Lewis.pdf")
pages = loader.load_and_split()

Checking to see if it’s in there.

pages

which gives us

So like a senior engineer I look at it then go, “LGTM” Let’s gamble try merging. Ah my favorite saying, but we don’t merge here we have to make things digestible for the AI.

So now the book is inside, the next step we need to do is something called RAG. No not this Rag,

RAG to wash

I mean this RAG

credit: https://python.langchain.com/docs/use_cases/question_answering/
credit: https://python.langchain.com/docs/use_cases/question_answering/

It’s a shortened version of Retrieval Augmented Generation. It means that it takes documents and other shenanigans you put inside of it, split it like Gordon Ramsey cutting up some onions, then it can retrieve certain bits of information.

Me

The reason we do this is because it makes it easier on the AI.

from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma

text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(pages)
vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings())

Note: langchain_openai is depreciated and pip install langchain will not work anymore. You need to do “pip install langchain-openai” to get it to work.

We can inspect the outputs

splits

Furthermore we can inspect to see if it actually created more splits by inspecting the length of pages and split

len(splits)
len(pages)
360
108

Next we need to firstly set our openai API-Key. OpenAI made a better tutorial on how to get it.

https://help.openai.com/en/articles/4936850-where-do-i-find-my-openai-api-key

Set up an .env file inside of the same folder as everything else

it should look like this

Inside it should look like this

Then in your code do

import getpass
import dotenv
import os

dotenv.load_dotenv()

os.environ["Open_AI_API_Key"] = getpass.getpass()

Now you can use OpenAI in your code


from langchain import hub
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI, OpenAIEmbeddings

# Retrieve and generate using the relevant snippets of the blog.
retriever = vectorstore.as_retriever()
prompt = hub.pull("rlm/rag-prompt")
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)

Note: you need to do “pip install langchainhub” due to depreciation reasons. Yeah idk man it do be like that sometimes.

Next you would want to set up the function which joins together the splits and then the rag_chain which sets up the LLM.

def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)


rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)

Then now you can use it!

Here are a few examples:

rag_chain.invoke("How can we learn to love God and hate pain at the same time?")

“We can learn to love God and hate pain at the same time by understanding that God’s goodness and the existence of suffering can coexist without contradiction. God may allow pain in our lives to make us less attached to false sources of happiness and to ultimately make us lovable in His eyes. Our ultimate happiness is not God’s primary focus, but rather becoming lovable in His sight is what will bring true happiness.”

rag_chain.invoke("How can an infintely good God create pain?")

“The possibility of pain is inherent in a world where souls can meet, and when souls become wicked, they will use this possibility to hurt one another. Much suffering is caused by humans themselves, through actions like producing weapons and creating poverty. God’s love causes all goodness in objects, loving them into existence and loveability.”

Now is this the full answer? Eh kinda? We shouldn’t blindly trust the writer without examining the source. Let’s pull back behind the curtains a bit to see how this AI works. It pulls relevant pieces of information from the splits and presents them. We can examine how the following text is generated

Example 1:

Answer: “The possibility of pain is inherent in a world where souls can meet, and when souls become wicked, they will use this possibility to hurt one another.”

In the Book:

Note: There is slight nuance of word for word, as the AI specifies, “…is inherent in a world…” while the book says, “…is inherent in the very existence of a world”

Example 2:

Answer: “God’s goodness and the existence of suffering can coexist without contradiction”

Note: You can see how the AI rewords the pieces of evidence which is fed to it.

Appendix

If you want to see which splits is used to answer the question you can do the following to see which splits it used

retriever.invoke("What is the difference between pleasure and happiness?")

[Document(page_content=’to like it? “ did not then arise, since doing things for God’s sake was what he chiefly “happened to like”. His God-ward will rode his happiness like a well-managed horse, whereas our will, when we are happy, is carried away in the happiness as in a ship racing down a swift stream. Pleasure was then an acceptable offering to God because offering was a pleasure. but we inherit a whole sys-tem of desires which do not necessarily contradict God’s will but which, after centuries of usurped autonomy, steadfastly ignore it. if the thing we like doing is, in fact, the thing God wants us to do, yet that is not our reason for doing it; it remains a mere happy coincidence. We cannot therefore know that we are acting at all, or primarily, for God’s sake, unless the material of the action is contrary to our inclinations, or (in other words) painful, and what we cannot know that we are choosing, we cannot choose. the full’, metadata={‘page’: 67, ‘source’: ‘Problem of Pain CS Lewis.pdf’}),

Document(page_content=’4. the Christian doctrine of suffering explains, i believe, a very \ncurious fact about the world we live in. the settled happiness and security which we all desire, God withholds from us by the very nature of the world: but joy, pleasure, and merriment, He has scattered broadcast. We are never safe, but we have plenty of fun, and some ecstasy. it is not hard to see why. the security we crave would teach us to rest our hearts in this world and oppose an obstacle to our return to God: a few moments of happy love, a landscape, a symphony, a merry meeting with our friends, a bathe or a football match, have no such tendency. Our Father refreshes us on the journey with some pleasant inns, but will not encourage us to mistake them for home.\n5. We must never make the problem of pain worse than it is’, metadata={‘page’: 78, ‘source’: ‘Problem of Pain CS Lewis.pdf’}),

Document(page_content=’tHe PrO bLem OF Pain\n94ested because he wants to run and leap and walk. Love, by defini-\ntion; seeks to enjoy its object.\nyou may think that there is another reason for our silence about \nheaven — namely, that we do not really desire it. but that may \nbe an illusion. What i am now going to say is merely an opinion of my own without the slightest authority, which i submit to the judgement of better Christians and better scholars than myself. there have been times when i think we do not desire heaven; but more often i find myself wondering whether, in our heart of hearts, we have ever desired anything else. y ou may have no-\nticed that the books you really love are bound together by a se-cret thread. you know very well what is the common quality that’, metadata={‘page’: 100, ‘source’: ‘Problem of Pain CS Lewis.pdf’}),

Document(page_content=’ing of heaven, is leaving out almost the whole of one side of the account. Scripture and tradition habitually put the joys of heaven into the scale against the sufferings of earth, and no solution of the problem of pain which does not do so can be called a Christian one. We are very shy nowadays of even mentioning heaven: We are afraid of the jeer about “pie in the sky”, and of being told that we are trying to “escape” from the duty of making a happy world here and now into dreams of a happy world elsewhere. but either there is “pie in the sky” or there is not. if there is not, then Chris-tianity is false, for this doctrine is woven into its whole fabric. if there is, then this truth, like any other, must be faced, whether it is useful at political meetings or no. again, we are afraid that heaven is a bribe, and that if we make it our goal we shall no longer be disinterested. it is not so. Heaven offers nothing that a mercenary soul can desire. it is safe to tell the pure in heart’, metadata={‘page’: 99, ‘source’: ‘Problem of Pain CS Lewis.pdf’})]

--

--

Joseph Liu
0 Followers

Dream Catcher, Dream Catcher, Tell me What You See