-
Notifications
You must be signed in to change notification settings - Fork 0
/
langchain-ade.py
62 lines (45 loc) · 1.42 KB
/
langchain-ade.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
53
54
55
56
57
58
59
60
# %% [markdown]
# #### IMPORT PACKAGES
# %%
import os
import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import TextLoader
from langchain.indexes import VectorstoreIndexCreator
# %% [markdown]
# #### SET OPENAI API KEY
# %%
# set API keys to authenticate requests to the API
API_KEY = '<API Key>'
os.environ["OPENAI_API_KEY"] = API_KEY
# %% [markdown]
# #### USE LANGCHAIN
# %%
# load text document
loaders = TextLoader('<path to text document>')
# create vector representation of the loaded document
index = VectorstoreIndexCreator().from_loaders([loaders])
# %% [markdown]
# #### SET UP STREAMLIT APP
# %%
# display page title and text box for the user to ask questions
# to run streamlit, input into terminal: streamlit run the .py version of this file
st.title('🦜 LangChain: Chat with Adverse Events Report')
prompt = st.text_input("Enter your question")
# %% [markdown]
# #### GENERATE RESPONSE
# %%
# get the resonse from LLM
if prompt:
response = index.query(
llm=ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.2),
question = prompt,
chain_type = 'stuff')
# write the results from the LLM to the UI
st.write("<b>" + prompt + "</b><br><i>" + response + "</i><hr>", unsafe_allow_html=True )
# %% [markdown]
# #### CONVERT TO .PY
# %%
# if all ok, convert to .py notebook
# bash
# ! python3 -m nbconvert --to script langchain-ade.ipynb