-
Notifications
You must be signed in to change notification settings - Fork 0
/
langchain-chain.py
52 lines (45 loc) · 2.28 KB
/
langchain-chain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
from dotenv import load_dotenv
from langchain import OpenAI, LLMChain, PromptTemplate
from langchain.chains import LLMChain, SimpleSequentialChain
# Set openAI key and create llm
load_dotenv('secrets.env')
llm = OpenAI(openai_api_key=os.getenv('OPENAI_KEY'), temperature=0.9)
# Add a text input box for the user's question
user_question = "Malaga is an area with a lot of rain"
# Chain 1: Give the question to the AI, the output of the chain is the first response (statement)
template = """{question}\n\n"""
prompt_template = PromptTemplate(input_variables=["question"], template=template)
question_chain = LLMChain(llm=llm, prompt=prompt_template)
# Chain 2: Generating assumptions made in the statement
template = """Here is a statement:
{statement}
Make a bullet point list of the assumptions you made when producing the above statement.\n\n"""
prompt_template = PromptTemplate(input_variables=["statement"], template=template)
assumptions_chain = LLMChain(llm=llm, prompt=prompt_template)
assumptions_chain_seq = SimpleSequentialChain(
chains=[question_chain, assumptions_chain], verbose=True
)
# Chain 3: Fact checking the assumptions
template = """Here is a bullet point list of assertions:
{assertions}
For each assertion, determine whether it is true or false. If it is false, explain why.\n\n"""
prompt_template = PromptTemplate(input_variables=["assertions"], template=template)
fact_checker_chain = LLMChain(llm=llm, prompt=prompt_template)
fact_checker_chain_seq = SimpleSequentialChain(
chains=[question_chain, assumptions_chain, fact_checker_chain], verbose=True
)
# Final Chain: Generating the final answer to the user's question based on the facts and assumptions
template = """In light of the above facts, how would you answer the question '{}'.
Consider the facts and the assertion to give a good answer""".format(
user_question
)
template = """{facts}\n""" + template
prompt_template = PromptTemplate(input_variables=["facts"], template=template)
answer_chain = LLMChain(llm=llm, prompt=prompt_template)
overall_chain = SimpleSequentialChain(
chains=[question_chain, assumptions_chain, fact_checker_chain, answer_chain],
verbose=True,
)
# Running all the chains on the user's question and displaying the final answer
print(overall_chain.run(user_question))