Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +70 -0
- requirements.txt +10 -0
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.agents import (
|
| 2 |
+
load_tools,
|
| 3 |
+
create_react_agent,
|
| 4 |
+
AgentExecutor,
|
| 5 |
+
tool,
|
| 6 |
+
)
|
| 7 |
+
from langchain_openai import ChatOpenAI
|
| 8 |
+
from langchain.memory import ConversationBufferMemory
|
| 9 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 10 |
+
from langchain import hub
|
| 11 |
+
import streamlit as st
|
| 12 |
+
|
| 13 |
+
search = DuckDuckGoSearchRun()
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@tool
|
| 17 |
+
def duckduckgo_webmd_search(text: str) -> str:
|
| 18 |
+
"""Uses the web to gather more medical information about medical query from webmd.com.
|
| 19 |
+
Use this tool when you need more medical information from webmd.com to
|
| 20 |
+
provide more accurate results. Don't use this if you want to search from other websites
|
| 21 |
+
or if you want information regarding non-medical query. Receives a medical query as input,
|
| 22 |
+
searches the web for results and gives relevant information as output string"""
|
| 23 |
+
result = search.run(f"site:webmd.com {text}")
|
| 24 |
+
output = str(result)
|
| 25 |
+
return output
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 29 |
+
llm = ChatOpenAI(temperature=0)
|
| 30 |
+
tools = load_tools([], llm=llm)
|
| 31 |
+
tools = tools + [duckduckgo_webmd_search]
|
| 32 |
+
|
| 33 |
+
# Get the prompt to use - you can modify this!
|
| 34 |
+
prompt = hub.pull("hwchase17/react")
|
| 35 |
+
agent = create_react_agent(llm=llm, tools=tools, prompt=prompt)
|
| 36 |
+
|
| 37 |
+
agent_executor = AgentExecutor(
|
| 38 |
+
agent=agent,
|
| 39 |
+
tools=tools,
|
| 40 |
+
memory=memory,
|
| 41 |
+
verbose=True,
|
| 42 |
+
handle_parsing_errors=True,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def handle_userinput(user_question):
|
| 47 |
+
agent_executor.invoke({"input": user_question})
|
| 48 |
+
st.session_state.chat_history = memory.chat_memory.messages
|
| 49 |
+
|
| 50 |
+
for i, message in enumerate(st.session_state.chat_history):
|
| 51 |
+
if i % 2 == 0:
|
| 52 |
+
st.markdown(("User: " + message.content))
|
| 53 |
+
else:
|
| 54 |
+
st.markdown(("AI: " + message.content))
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def main():
|
| 58 |
+
if "conversation" not in st.session_state:
|
| 59 |
+
st.session_state.conversation = None
|
| 60 |
+
if "chat_history" not in st.session_state:
|
| 61 |
+
st.session_state.chat_history = None
|
| 62 |
+
|
| 63 |
+
st.header("Ask any medical query that you want answers from webmd.com")
|
| 64 |
+
user_question = st.chat_input("Ask any medical query")
|
| 65 |
+
if user_question:
|
| 66 |
+
handle_userinput(user_question)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
openai
|
| 3 |
+
python-dotenv
|
| 4 |
+
langchain-openai
|
| 5 |
+
langchainhub
|
| 6 |
+
langchain-community
|
| 7 |
+
langchain-experimental
|
| 8 |
+
chromadb
|
| 9 |
+
wikipedia
|
| 10 |
+
duckduckgo-search
|