Spaces:
Sleeping
Sleeping
find a better way to include safeguard
Browse files- agent/agent.py +77 -0
agent/agent.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# from ragatouille import RAGPretrainedModel
|
| 8 |
+
from langchain_core.vectorstores import VectorStore
|
| 9 |
+
from langchain_core.language_models.llms import LLM
|
| 10 |
+
from typing import Tuple
|
| 11 |
+
from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
|
| 12 |
+
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
|
| 13 |
+
from langchain_core.documents import Document as LangchainDocument
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
from langchain.chat_models import init_chat_model
|
| 18 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 19 |
+
import faiss
|
| 20 |
+
from langchain_community.docstore.in_memory import InMemoryDocstore
|
| 21 |
+
from langchain_community.vectorstores import FAISS
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def predict(message, history, retriever, llm):
|
| 27 |
+
# Build conversation history
|
| 28 |
+
history_langchain_format = []
|
| 29 |
+
for msg in history:
|
| 30 |
+
if msg['role'] == "user":
|
| 31 |
+
history_langchain_format.append(HumanMessage(content=msg['content']))
|
| 32 |
+
elif msg['role'] == "assistant":
|
| 33 |
+
history_langchain_format.append(AIMessage(content=msg['content']))
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# Retrieve relevant documents for the current message
|
| 37 |
+
relevant_docs = retriever.similarity_search(message,k=2) # Your retriever
|
| 38 |
+
|
| 39 |
+
# Build context from retrieved documents
|
| 40 |
+
context = "\nExtracted documents:\n" + "\n".join([
|
| 41 |
+
f"Document {i}: Content: {doc.page_content}\n\n context_source_url: {doc.metadata.get('source_url')}\n context_date: {doc.metadata.get('date')}\n---"
|
| 42 |
+
for i, doc in enumerate(relevant_docs)
|
| 43 |
+
])
|
| 44 |
+
|
| 45 |
+
RAG_PROMPT_TEMPLATE="""Using the information contained in the context,
|
| 46 |
+
give a comprehensive answer to the question.
|
| 47 |
+
Respond only to the question asked, response should be concise and relevant to the question.
|
| 48 |
+
Provide the context source url and context date of the source document when relevant.
|
| 49 |
+
If the answer cannot be deduced from the context, do not give an answer.
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# Create the prompt with system message, context, and conversation history
|
| 54 |
+
messages = [
|
| 55 |
+
SystemMessage(content=RAG_PROMPT_TEMPLATE),
|
| 56 |
+
history_langchain_format
|
| 57 |
+
]
|
| 58 |
+
combined_message = f"Context: {context}\n\nQuestion: {message}"
|
| 59 |
+
messages.append(HumanMessage(content=combined_message))
|
| 60 |
+
|
| 61 |
+
# Get response with tracking metadata
|
| 62 |
+
gpt_response = llm.invoke(
|
| 63 |
+
messages,
|
| 64 |
+
config={
|
| 65 |
+
"tags": ["Testing", 'RAG-Bot', 'V1'],
|
| 66 |
+
"metadata": {
|
| 67 |
+
"rag_llm": "gpt-5-nano",
|
| 68 |
+
"num_retrieved_docs": len(relevant_docs),
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
source_context = "\nSources:\n" + "\n".join([
|
| 74 |
+
f"{doc.metadata.get('source_url')} ({doc.metadata.get('date')})\n---"
|
| 75 |
+
for i, doc in enumerate(relevant_docs)])
|
| 76 |
+
|
| 77 |
+
return gpt_response.content + source_context
|