Spaces:
Running
Running
Eurico149
commited on
Commit
·
0ede848
1
Parent(s):
ed7f758
feat: a functional RAG agent that retrive data from inmemory KB acording to the prompt context
Browse files- agents/BookRetriverAgent.py +18 -17
- agents/__init__.py +1 -1
- app.py +4 -2
- rag/BooksRag.py +7 -2
- requirements.txt +1 -0
- tools/BookRetriverRagAgent.py +12 -0
- tools/__init__.py +1 -0
- util/aws/S3Access.py +5 -4
agents/BookRetriverAgent.py
CHANGED
|
@@ -4,21 +4,22 @@ from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
)
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
tools = [retrieve_book_context]
|
| 16 |
-
prompt = (
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
)
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
|
| 7 |
+
def get_book_retriver_agent():
|
| 8 |
+
hf_model = HuggingFaceEndpoint(
|
| 9 |
+
repo_id="Qwen/Qwen3-30B-A3B-Instruct-2507",
|
| 10 |
+
task="text-generation",
|
| 11 |
+
provider="auto",
|
| 12 |
+
huggingfacehub_api_token=os.getenv("HF_TOKEN")
|
| 13 |
+
)
|
| 14 |
+
llm = ChatHuggingFace(llm=hf_model)
|
| 15 |
|
| 16 |
+
tools = [retrieve_book_context]
|
| 17 |
+
prompt = (
|
| 18 |
+
"You have access to a tool that retrieves context from books."
|
| 19 |
+
"Use the tool to help answer user queries."
|
| 20 |
+
)
|
| 21 |
+
return create_agent(
|
| 22 |
+
llm,
|
| 23 |
+
tools,
|
| 24 |
+
system_prompt=prompt
|
| 25 |
+
)
|
agents/__init__.py
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
from .BookRetriverAgent import
|
|
|
|
| 1 |
+
from .BookRetriverAgent import get_book_retriver_agent
|
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from langchain.agents import create_agent
|
|
| 4 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 5 |
from langgraph.checkpoint.memory import InMemorySaver
|
| 6 |
from dotenv import load_dotenv
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
class GradioAgent:
|
|
@@ -27,12 +28,13 @@ class GradioAgent:
|
|
| 27 |
repo_id="Qwen/Qwen3-30B-A3B-Instruct-2507",
|
| 28 |
task="text-generation",
|
| 29 |
provider="auto",
|
| 30 |
-
huggingfacehub_api_token=os.getenv("HF_TOKEN")
|
|
|
|
| 31 |
)
|
| 32 |
llm = ChatHuggingFace(llm=hf_model)
|
| 33 |
|
| 34 |
return create_agent(
|
| 35 |
-
tools=[],
|
| 36 |
model=llm,
|
| 37 |
checkpointer=InMemorySaver(),
|
| 38 |
system_prompt="You are a helpful and usefull assistant."
|
|
|
|
| 4 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 5 |
from langgraph.checkpoint.memory import InMemorySaver
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
+
from tools import retrieve_book_context_rag_agent
|
| 8 |
|
| 9 |
|
| 10 |
class GradioAgent:
|
|
|
|
| 28 |
repo_id="Qwen/Qwen3-30B-A3B-Instruct-2507",
|
| 29 |
task="text-generation",
|
| 30 |
provider="auto",
|
| 31 |
+
huggingfacehub_api_token=os.getenv("HF_TOKEN"),
|
| 32 |
+
do_sample=False
|
| 33 |
)
|
| 34 |
llm = ChatHuggingFace(llm=hf_model)
|
| 35 |
|
| 36 |
return create_agent(
|
| 37 |
+
tools=[retrieve_book_context_rag_agent],
|
| 38 |
model=llm,
|
| 39 |
checkpointer=InMemorySaver(),
|
| 40 |
system_prompt="You are a helpful and usefull assistant."
|
rag/BooksRag.py
CHANGED
|
@@ -21,7 +21,12 @@ def get_books_vector_store():
|
|
| 21 |
os.getenv("AWS_SECRET_ACCESS_KEY"),
|
| 22 |
"us-east-1"
|
| 23 |
)
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
return vector_store
|
|
|
|
| 21 |
os.getenv("AWS_SECRET_ACCESS_KEY"),
|
| 22 |
"us-east-1"
|
| 23 |
)
|
| 24 |
+
print("retrieved docs")
|
| 25 |
+
for doc in docs:
|
| 26 |
+
all_splits = text_splitter.split_documents(doc)
|
| 27 |
+
print("show")
|
| 28 |
+
vector_store.add_documents(documents=all_splits)
|
| 29 |
+
print("pri")
|
| 30 |
+
|
| 31 |
|
| 32 |
return vector_store
|
requirements.txt
CHANGED
|
@@ -7,6 +7,7 @@ annotated-types==0.7.0
|
|
| 7 |
antlr4-python3-runtime==4.9.3
|
| 8 |
anyio==4.11.0
|
| 9 |
attrs==25.4.0
|
|
|
|
| 10 |
Authlib==1.6.5
|
| 11 |
backoff==2.2.1
|
| 12 |
bcrypt==5.0.0
|
|
|
|
| 7 |
antlr4-python3-runtime==4.9.3
|
| 8 |
anyio==4.11.0
|
| 9 |
attrs==25.4.0
|
| 10 |
+
audioop-lts==0.2.2
|
| 11 |
Authlib==1.6.5
|
| 12 |
backoff==2.2.1
|
| 13 |
bcrypt==5.0.0
|
tools/BookRetriverRagAgent.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.tools import tool
|
| 2 |
+
from agents import get_book_retriver_agent
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
@tool
|
| 6 |
+
def retrieve_book_context_rag_agent(request: str):
|
| 7 |
+
"""Used to retrieve computer science related books context and answer the request."""
|
| 8 |
+
agent = get_book_retriver_agent()
|
| 9 |
+
result = agent.invoke({
|
| 10 |
+
"messages": [{"role": "user", "content": request}]
|
| 11 |
+
})
|
| 12 |
+
return result["messages"][-1].text
|
tools/__init__.py
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
from .RetriveBooksDataTool import retrieve_book_context
|
|
|
|
|
|
| 1 |
from .RetriveBooksDataTool import retrieve_book_context
|
| 2 |
+
from .BookRetriverRagAgent import retrieve_book_context_rag_agent
|
util/aws/S3Access.py
CHANGED
|
@@ -5,13 +5,13 @@ from langchain_community.document_loaders import S3FileLoader
|
|
| 5 |
def retrieve_s3_data_as_documents(
|
| 6 |
bucket_name: str,
|
| 7 |
prefix: str,
|
| 8 |
-
|
| 9 |
secret_key: str,
|
| 10 |
region: str
|
| 11 |
):
|
| 12 |
s3 = boto3.client(
|
| 13 |
's3',
|
| 14 |
-
aws_access_key_id=
|
| 15 |
aws_secret_access_key=secret_key,
|
| 16 |
region_name=region
|
| 17 |
)
|
|
@@ -26,8 +26,9 @@ def retrieve_s3_data_as_documents(
|
|
| 26 |
loader = S3FileLoader(
|
| 27 |
bucket=bucket_name,
|
| 28 |
key=key,
|
| 29 |
-
aws_access_key_id=
|
| 30 |
-
aws_secret_access_key=secret_key
|
|
|
|
| 31 |
)
|
| 32 |
doc = loader.load()
|
| 33 |
data.append(doc)
|
|
|
|
| 5 |
def retrieve_s3_data_as_documents(
|
| 6 |
bucket_name: str,
|
| 7 |
prefix: str,
|
| 8 |
+
access_id: str,
|
| 9 |
secret_key: str,
|
| 10 |
region: str
|
| 11 |
):
|
| 12 |
s3 = boto3.client(
|
| 13 |
's3',
|
| 14 |
+
aws_access_key_id=access_id,
|
| 15 |
aws_secret_access_key=secret_key,
|
| 16 |
region_name=region
|
| 17 |
)
|
|
|
|
| 26 |
loader = S3FileLoader(
|
| 27 |
bucket=bucket_name,
|
| 28 |
key=key,
|
| 29 |
+
aws_access_key_id=access_id,
|
| 30 |
+
aws_secret_access_key=secret_key,
|
| 31 |
+
strategy="fast"
|
| 32 |
)
|
| 33 |
doc = loader.load()
|
| 34 |
data.append(doc)
|