├── constants.py ├── README.md ├── requirements.txt ├── main.py └── example1.py /constants.py: -------------------------------------------------------------------------------- 1 | openai_key="" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Langchain-Tutorials -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | openai 2 | langchain 3 | streamlit -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | ## Integrate our code OpenAI API 2 | import os 3 | from constants import openai_key 4 | from langchain.llms import OpenAI 5 | 6 | import streamlit as st 7 | 8 | os.environ["OPENAI_API_KEY"]=openai_key 9 | 10 | # streamlit framework 11 | 12 | st.title('Langchain Demo With OPENAI API') 13 | input_text=st.text_input("Search the topic u want") 14 | 15 | ## OPENAI LLMS 16 | llm=OpenAI(temperature=0.8) 17 | 18 | 19 | 20 | if input_text: 21 | st.write(llm(input_text)) 22 | -------------------------------------------------------------------------------- /example1.py: -------------------------------------------------------------------------------- 1 | ## Integrate our code OpenAI API 2 | import os 3 | from constants import openai_key 4 | from langchain.llms import OpenAI 5 | from langchain import PromptTemplate 6 | from langchain.chains import LLMChain 7 | 8 | from langchain.memory import ConversationBufferMemory 9 | 10 | from langchain.chains import SequentialChain 11 | 12 | import streamlit as st 13 | 14 | os.environ["OPENAI_API_KEY"]=openai_key 15 | 16 | # streamlit framework 17 | 18 | st.title('Celebrity Search Results') 19 | input_text=st.text_input("Search the topic u want") 20 | 21 | # Prompt Templates 22 | 23 | first_input_prompt=PromptTemplate( 24 | input_variables=['name'], 25 | template="Tell me about celebrity {name}" 26 | ) 27 | 28 | # Memory 29 | 30 | person_memory = ConversationBufferMemory(input_key='name', memory_key='chat_history') 31 | dob_memory = ConversationBufferMemory(input_key='person', memory_key='chat_history') 32 | descr_memory = ConversationBufferMemory(input_key='dob', memory_key='description_history') 33 | 34 | ## OPENAI LLMS 35 | llm=OpenAI(temperature=0.8) 36 | chain=LLMChain( 37 | llm=llm,prompt=first_input_prompt,verbose=True,output_key='person',memory=person_memory) 38 | 39 | # Prompt Templates 40 | 41 | second_input_prompt=PromptTemplate( 42 | input_variables=['person'], 43 | template="when was {person} born" 44 | ) 45 | 46 | chain2=LLMChain( 47 | llm=llm,prompt=second_input_prompt,verbose=True,output_key='dob',memory=dob_memory) 48 | # Prompt Templates 49 | 50 | third_input_prompt=PromptTemplate( 51 | input_variables=['dob'], 52 | template="Mention 5 major events happened around {dob} in the world" 53 | ) 54 | chain3=LLMChain(llm=llm,prompt=third_input_prompt,verbose=True,output_key='description',memory=descr_memory) 55 | parent_chain=SequentialChain( 56 | chains=[chain,chain2,chain3],input_variables=['name'],output_variables=['person','dob','description'],verbose=True) 57 | 58 | 59 | 60 | if input_text: 61 | st.write(parent_chain({'name':input_text})) 62 | 63 | with st.expander('Person Name'): 64 | st.info(person_memory.buffer) 65 | 66 | with st.expander('Major Events'): 67 | st.info(descr_memory.buffer) 68 | --------------------------------------------------------------------------------