Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,37 @@
|
|
| 1 |
from langchain.agents import create_agent
|
| 2 |
from tool import vectorize_documents, get_weather, get_content_from_site
|
| 3 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 4 |
-
from langchain_ollama import ChatOllama
|
| 5 |
import gradio as gr
|
| 6 |
from typing import Any, cast
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from langchain_groq import ChatGroq
|
| 9 |
import os
|
| 10 |
|
| 11 |
-
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
placeholder_html = """
|
| 15 |
<div style='text-align: center; padding: 25px;'>
|
| 16 |
<h1 style='font-size: 2.5rem; font-weight: 800; margin-bottom: 10px;'>🚀 Ask Me Anything</h1>
|
| 17 |
-
<p style='font-size: 1.5rem; color: #666;'>Your
|
| 18 |
</div>
|
| 19 |
"""
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
llm =
|
| 23 |
-
temperature=0)
|
| 24 |
|
| 25 |
normal_prompt = ChatPromptTemplate.from_messages([
|
| 26 |
-
("system","""You are a helpful AI assistant.
|
| 27 |
-
|
| 28 |
Follow these rules:
|
| 29 |
-
|
| 30 |
1. Do not mention tools to the user never only you know about tools.
|
| 31 |
2. Use available tools if needed so."""),
|
| 32 |
MessagesPlaceholder("history"),
|
| 33 |
-
("human","{question}")
|
| 34 |
])
|
| 35 |
|
| 36 |
file_prompt = ChatPromptTemplate.from_messages([
|
| 37 |
("system", """You are a helpful AI assistant.
|
| 38 |
-
|
| 39 |
Follow these rules:
|
| 40 |
-
|
| 41 |
-
1. Never disclose, list, describe, or reveal internal tools, tool names,
|
| 42 |
-
tool definitions, function schemas, or implementation details.
|
| 43 |
2. For unrelated questions, answer using your general knowledge or available tools.
|
| 44 |
3. When the user asks about the uploaded file, use the provided context to answer.
|
| 45 |
4. If the answer to a file-related question is not available in the provided context, clearly say you don't know.
|
|
@@ -61,9 +53,7 @@ def star_buck(name):
|
|
| 61 |
|
| 62 |
tools = [get_weather, star_buck, get_content_from_site]
|
| 63 |
|
| 64 |
-
|
| 65 |
-
agent = create_agent(tools=tools,
|
| 66 |
-
model=llm)
|
| 67 |
|
| 68 |
def chat(message, history, retriver):
|
| 69 |
history = history or []
|
|
@@ -75,7 +65,7 @@ def chat(message, history, retriver):
|
|
| 75 |
|
| 76 |
if question == "":
|
| 77 |
question = "the user just send an empty message"
|
| 78 |
-
elif question =='cls':
|
| 79 |
history = []
|
| 80 |
|
| 81 |
if retriver is not None:
|
|
@@ -83,42 +73,35 @@ def chat(message, history, retriver):
|
|
| 83 |
context = "\n\n".join(doc.page_content for doc in docs)
|
| 84 |
|
| 85 |
f_prompt = file_prompt.invoke({
|
| 86 |
-
"context":context,
|
| 87 |
-
"tools":tools,
|
| 88 |
-
"question":question
|
| 89 |
}).to_messages()
|
| 90 |
|
| 91 |
-
result = agent.invoke(cast(Any,{
|
| 92 |
"messages": f_prompt
|
| 93 |
}))
|
| 94 |
-
|
| 95 |
-
|
| 96 |
else:
|
| 97 |
-
# agent = create_agent(model=llm, tools=tools)
|
| 98 |
-
|
| 99 |
n_prompt = normal_prompt.invoke({
|
| 100 |
"history": history,
|
| 101 |
-
"question":question
|
| 102 |
-
|
| 103 |
}).to_messages()
|
| 104 |
|
| 105 |
result = agent.invoke(cast(Any, {
|
| 106 |
"messages": n_prompt
|
| 107 |
}))
|
| 108 |
-
|
| 109 |
response = result["messages"][-1].content
|
| 110 |
|
| 111 |
if question == "the user just send an empty message":
|
| 112 |
-
history = history + [
|
| 113 |
-
{"role": "assistant", "content": response }]
|
| 114 |
-
|
| 115 |
else:
|
| 116 |
history = history + [
|
| 117 |
-
|
| 118 |
-
|
|
|
|
| 119 |
|
| 120 |
-
return
|
| 121 |
-
|
| 122 |
|
| 123 |
with gr.Blocks() as Demo:
|
| 124 |
state = gr.State(None)
|
|
@@ -134,7 +117,7 @@ with gr.Blocks() as Demo:
|
|
| 134 |
text = gr.MultimodalTextbox(
|
| 135 |
placeholder="What is your question.....",
|
| 136 |
file_count="single",
|
| 137 |
-
file_types=[".pdf",".txt"],
|
| 138 |
sources=["upload"],
|
| 139 |
show_label=False
|
| 140 |
)
|
|
@@ -145,4 +128,5 @@ with gr.Blocks() as Demo:
|
|
| 145 |
outputs=[chatbot, state, text]
|
| 146 |
)
|
| 147 |
|
|
|
|
| 148 |
Demo.launch()
|
|
|
|
| 1 |
from langchain.agents import create_agent
|
| 2 |
from tool import vectorize_documents, get_weather, get_content_from_site
|
| 3 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from typing import Any, cast
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
import os
|
| 9 |
|
|
|
|
| 10 |
load_dotenv()
|
| 11 |
|
| 12 |
placeholder_html = """
|
| 13 |
<div style='text-align: center; padding: 25px;'>
|
| 14 |
<h1 style='font-size: 2.5rem; font-weight: 800; margin-bottom: 10px;'>🚀 Ask Me Anything</h1>
|
| 15 |
+
<p style='font-size: 1.5rem; color: #666;'>Your AI-assistant</p>
|
| 16 |
</div>
|
| 17 |
"""
|
| 18 |
|
| 19 |
+
# Use a cloud-hosted LLM provider like Groq for Hugging Face Spaces compatibility
|
| 20 |
+
llm = ChatGroq(model="llama-3.3-70b-versatile", temperature=0)
|
|
|
|
| 21 |
|
| 22 |
normal_prompt = ChatPromptTemplate.from_messages([
|
| 23 |
+
("system", """You are a helpful AI assistant.
|
|
|
|
| 24 |
Follow these rules:
|
|
|
|
| 25 |
1. Do not mention tools to the user never only you know about tools.
|
| 26 |
2. Use available tools if needed so."""),
|
| 27 |
MessagesPlaceholder("history"),
|
| 28 |
+
("human", "{question}")
|
| 29 |
])
|
| 30 |
|
| 31 |
file_prompt = ChatPromptTemplate.from_messages([
|
| 32 |
("system", """You are a helpful AI assistant.
|
|
|
|
| 33 |
Follow these rules:
|
| 34 |
+
1. Never disclose, list, describe, or reveal internal tools, tool names, tool definitions, function schemas, or implementation details.
|
|
|
|
|
|
|
| 35 |
2. For unrelated questions, answer using your general knowledge or available tools.
|
| 36 |
3. When the user asks about the uploaded file, use the provided context to answer.
|
| 37 |
4. If the answer to a file-related question is not available in the provided context, clearly say you don't know.
|
|
|
|
| 53 |
|
| 54 |
tools = [get_weather, star_buck, get_content_from_site]
|
| 55 |
|
| 56 |
+
agent = create_agent(tools=tools, model=llm)
|
|
|
|
|
|
|
| 57 |
|
| 58 |
def chat(message, history, retriver):
|
| 59 |
history = history or []
|
|
|
|
| 65 |
|
| 66 |
if question == "":
|
| 67 |
question = "the user just send an empty message"
|
| 68 |
+
elif question == 'cls':
|
| 69 |
history = []
|
| 70 |
|
| 71 |
if retriver is not None:
|
|
|
|
| 73 |
context = "\n\n".join(doc.page_content for doc in docs)
|
| 74 |
|
| 75 |
f_prompt = file_prompt.invoke({
|
| 76 |
+
"context": context,
|
| 77 |
+
"tools": tools,
|
| 78 |
+
"question": question
|
| 79 |
}).to_messages()
|
| 80 |
|
| 81 |
+
result = agent.invoke(cast(Any, {
|
| 82 |
"messages": f_prompt
|
| 83 |
}))
|
| 84 |
+
response = result["messages"][-1].content
|
|
|
|
| 85 |
else:
|
|
|
|
|
|
|
| 86 |
n_prompt = normal_prompt.invoke({
|
| 87 |
"history": history,
|
| 88 |
+
"question": question
|
|
|
|
| 89 |
}).to_messages()
|
| 90 |
|
| 91 |
result = agent.invoke(cast(Any, {
|
| 92 |
"messages": n_prompt
|
| 93 |
}))
|
|
|
|
| 94 |
response = result["messages"][-1].content
|
| 95 |
|
| 96 |
if question == "the user just send an empty message":
|
| 97 |
+
history = history + [{"role": "assistant", "content": response}]
|
|
|
|
|
|
|
| 98 |
else:
|
| 99 |
history = history + [
|
| 100 |
+
{"role": "user", "content": question},
|
| 101 |
+
{"role": "assistant", "content": response}
|
| 102 |
+
]
|
| 103 |
|
| 104 |
+
return history, retriver, {"text": "", "files": []}
|
|
|
|
| 105 |
|
| 106 |
with gr.Blocks() as Demo:
|
| 107 |
state = gr.State(None)
|
|
|
|
| 117 |
text = gr.MultimodalTextbox(
|
| 118 |
placeholder="What is your question.....",
|
| 119 |
file_count="single",
|
| 120 |
+
file_types=[".pdf", ".txt"],
|
| 121 |
sources=["upload"],
|
| 122 |
show_label=False
|
| 123 |
)
|
|
|
|
| 128 |
outputs=[chatbot, state, text]
|
| 129 |
)
|
| 130 |
|
| 131 |
+
# Hugging Face runs this automatically on port 7860
|
| 132 |
Demo.launch()
|