Update app.py
Browse files
app.py
CHANGED
|
@@ -4,83 +4,34 @@ from dotenv import load_dotenv
|
|
| 4 |
load_dotenv()
|
| 5 |
|
| 6 |
from langchain.agents.openai_assistant import OpenAIAssistantRunnable
|
|
|
|
|
|
|
| 7 |
from langchain.schema import HumanMessage, AIMessage
|
| 8 |
|
| 9 |
-
import gradio
|
| 10 |
|
| 11 |
-
# Load API key and assistant IDs
|
| 12 |
api_key = os.getenv('OPENAI_API_KEY')
|
| 13 |
-
|
| 14 |
-
"Solution Specifier A": os.getenv('ASSISTANT_ID_SOLUTION_SPECIFIER_A'),
|
| 15 |
-
"Solution Specifier A1": os.getenv('ASSISTANT_ID_SOLUTION_SPECIFIER_A1'),
|
| 16 |
-
}
|
| 17 |
|
| 18 |
-
|
| 19 |
-
def get_extractor_llm(agent_id):
|
| 20 |
-
return OpenAIAssistantRunnable(assistant_id=agent_id, api_key=api_key, as_agent=True)
|
| 21 |
|
| 22 |
-
# Utility function to remove citations
|
| 23 |
def remove_citation(text):
|
| 24 |
# Define the regex pattern to match the citation format 【number†text】
|
| 25 |
pattern = r"【\d+†\w+】"
|
| 26 |
# Replace the pattern with an empty string
|
| 27 |
return re.sub(pattern, "📚", text)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
def predict(message, history, selected_agent):
|
| 31 |
-
# Get the extractor LLM for the selected agent
|
| 32 |
-
agent_id = extractor_agents[selected_agent]
|
| 33 |
-
extractor_llm = get_extractor_llm(agent_id)
|
| 34 |
-
|
| 35 |
-
# Prepare the chat history
|
| 36 |
history_langchain_format = []
|
| 37 |
for human, ai in history:
|
| 38 |
history_langchain_format.append(HumanMessage(content=human))
|
| 39 |
history_langchain_format.append(AIMessage(content=ai))
|
| 40 |
history_langchain_format.append(HumanMessage(content=message))
|
| 41 |
-
|
| 42 |
-
# Get the response
|
| 43 |
gpt_response = extractor_llm.invoke({"content": message})
|
| 44 |
output = gpt_response.return_values["output"]
|
| 45 |
non_cited_output = remove_citation(output)
|
| 46 |
return non_cited_output
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
css="""
|
| 52 |
-
.container {
|
| 53 |
-
max-width: 100% !important;
|
| 54 |
-
padding: 0 !important;
|
| 55 |
-
}
|
| 56 |
-
.main-div {
|
| 57 |
-
height: 100vh;
|
| 58 |
-
display: flex;
|
| 59 |
-
flex-direction: column;
|
| 60 |
-
}
|
| 61 |
-
.chat-div {
|
| 62 |
-
flex-grow: 1;
|
| 63 |
-
min-height: 0;
|
| 64 |
-
}
|
| 65 |
-
"""
|
| 66 |
-
) as demo:
|
| 67 |
-
with gr.Column(elem_classes="main-div"):
|
| 68 |
-
dropdown = gr.Dropdown(
|
| 69 |
-
choices=list(extractor_agents.keys()),
|
| 70 |
-
value="Solution Specifier A",
|
| 71 |
-
label="Choose Extractor Agent",
|
| 72 |
-
interactive=True,
|
| 73 |
-
container=True
|
| 74 |
-
)
|
| 75 |
-
chat = gr.ChatInterface(
|
| 76 |
-
fn=predict,
|
| 77 |
-
additional_inputs=[dropdown],
|
| 78 |
-
title="Solution Specifier Chat",
|
| 79 |
-
description="Test with different solution specifiers",
|
| 80 |
-
elem_classes="chat-div"
|
| 81 |
-
)
|
| 82 |
-
return demo
|
| 83 |
-
|
| 84 |
-
# Launch the app
|
| 85 |
-
chat_interface = app_interface()
|
| 86 |
-
chat_interface.launch(share=True)
|
|
|
|
| 4 |
load_dotenv()
|
| 5 |
|
| 6 |
from langchain.agents.openai_assistant import OpenAIAssistantRunnable
|
| 7 |
+
from langchain.agents import AgentExecutor
|
| 8 |
+
|
| 9 |
from langchain.schema import HumanMessage, AIMessage
|
| 10 |
|
| 11 |
+
import gradio
|
| 12 |
|
|
|
|
| 13 |
api_key = os.getenv('OPENAI_API_KEY')
|
| 14 |
+
extractor_agent = os.getenv('ASSISTANT_ID_SOLUTION_SPECIFIER_A')
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
extractor_llm = OpenAIAssistantRunnable(assistant_id=extractor_agent, api_key=api_key, as_agent=True)
|
|
|
|
|
|
|
| 17 |
|
|
|
|
| 18 |
def remove_citation(text):
|
| 19 |
# Define the regex pattern to match the citation format 【number†text】
|
| 20 |
pattern = r"【\d+†\w+】"
|
| 21 |
# Replace the pattern with an empty string
|
| 22 |
return re.sub(pattern, "📚", text)
|
| 23 |
|
| 24 |
+
def predict(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
history_langchain_format = []
|
| 26 |
for human, ai in history:
|
| 27 |
history_langchain_format.append(HumanMessage(content=human))
|
| 28 |
history_langchain_format.append(AIMessage(content=ai))
|
| 29 |
history_langchain_format.append(HumanMessage(content=message))
|
|
|
|
|
|
|
| 30 |
gpt_response = extractor_llm.invoke({"content": message})
|
| 31 |
output = gpt_response.return_values["output"]
|
| 32 |
non_cited_output = remove_citation(output)
|
| 33 |
return non_cited_output
|
| 34 |
|
| 35 |
+
#gradio.Markdown("Click [here](https://www.google.com) to visit Google.")
|
| 36 |
+
chat = gradio.ChatInterface(predict, title="Solution Specifier A", description="testing for the time being")
|
| 37 |
+
chat.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|