split code in several files
Browse files- .gitignore +1 -0
- app.py +3 -37
- retriever.py +19 -0
- tools.py +20 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.pyc
|
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
-
|
2 |
-
|
3 |
from typing import TypedDict, Annotated
|
4 |
from langgraph.graph.message import add_messages
|
5 |
from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
|
@@ -7,42 +7,8 @@ from langgraph.prebuilt import ToolNode
|
|
7 |
from langgraph.graph import START, StateGraph
|
8 |
from langgraph.prebuilt import tools_condition
|
9 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
10 |
-
from datasets import load_dataset
|
11 |
-
from langchain.docstore.document import Document
|
12 |
-
import os
|
13 |
-
|
14 |
-
# Load the dataset
|
15 |
-
guest_dataset = load_dataset("agents-course/unit3-invitees", split="train")
|
16 |
|
17 |
-
|
18 |
-
docs = [
|
19 |
-
Document(
|
20 |
-
page_content="\n".join([
|
21 |
-
f"Name: {guest['name']}",
|
22 |
-
f"Relation: {guest['relation']}",
|
23 |
-
f"Description: {guest['description']}",
|
24 |
-
f"Email: {guest['email']}"
|
25 |
-
]),
|
26 |
-
metadata={"name": guest["name"]}
|
27 |
-
)
|
28 |
-
for guest in guest_dataset
|
29 |
-
]
|
30 |
-
|
31 |
-
bm25_retriever = BM25Retriever.from_documents(docs)
|
32 |
-
|
33 |
-
def extract_text(query: str) -> str:
|
34 |
-
"""Retrieves detailed information about gala guests based on their name or relation."""
|
35 |
-
results = bm25_retriever.invoke(query)
|
36 |
-
if results:
|
37 |
-
return "\n\n".join([doc.page_content for doc in results[:3]])
|
38 |
-
else:
|
39 |
-
return "No matching guest information found."
|
40 |
-
|
41 |
-
guest_info_tool = Tool(
|
42 |
-
name="guest_info_retriever",
|
43 |
-
func=extract_text,
|
44 |
-
description="Retrieves detailed information about gala guests based on their name or relation."
|
45 |
-
)
|
46 |
|
47 |
# Generate the chat interface, including the tools
|
48 |
llm = HuggingFaceEndpoint(
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
from typing import TypedDict, Annotated
|
4 |
from langgraph.graph.message import add_messages
|
5 |
from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
|
|
|
7 |
from langgraph.graph import START, StateGraph
|
8 |
from langgraph.prebuilt import tools_condition
|
9 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
from tools import guest_info_tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Generate the chat interface, including the tools
|
14 |
llm = HuggingFaceEndpoint(
|
retriever.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
from langchain.docstore.document import Document
|
3 |
+
|
4 |
+
# Load the dataset
|
5 |
+
guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train")
|
6 |
+
|
7 |
+
# Convert dataset entries into Document objects
|
8 |
+
docs = [
|
9 |
+
Document(
|
10 |
+
page_content="\n".join([
|
11 |
+
f"Name: {guest['name']}",
|
12 |
+
f"Relation: {guest['relation']}",
|
13 |
+
f"Description: {guest['description']}",
|
14 |
+
f"Email: {guest['email']}"
|
15 |
+
]),
|
16 |
+
metadata={"name": guest["name"]}
|
17 |
+
)
|
18 |
+
for guest in guest_dataset
|
19 |
+
]
|
tools.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_community.retrievers import BM25Retriever
|
2 |
+
from langchain.tools import Tool
|
3 |
+
|
4 |
+
from retriever import docs
|
5 |
+
|
6 |
+
bm25_retriever = BM25Retriever.from_documents(docs)
|
7 |
+
|
8 |
+
def extract_text(query: str) -> str:
|
9 |
+
"""Retrieves detailed information about gala guests based on their name or relation."""
|
10 |
+
results = bm25_retriever.invoke(query)
|
11 |
+
if results:
|
12 |
+
return "\n\n".join([doc.page_content for doc in results[:3]])
|
13 |
+
else:
|
14 |
+
return "No matching guest information found."
|
15 |
+
|
16 |
+
guest_info_tool = Tool(
|
17 |
+
name="guest_info_retriever",
|
18 |
+
func=extract_text,
|
19 |
+
description="Retrieves detailed information about gala guests based on their name or relation."
|
20 |
+
)
|