File size: 2,896 Bytes
6b1678e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import datasets
from langchain.docstore.document import Document

# Load Dataset
guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train")

# Convert dataset entries to document
docs = [
    Document(
        page_content = "\n".join([
            f"Name: {guest['name']}",
            f"Relation: {guest['relation']}", 
            f"Description: {guest['description']}",
            f"Email: {guest['email']}"
        ]),
        metadata={"name": guest["name"]}
    )
    for guest in guest_dataset
]
# ---------------------------------------------------------------------------------------------
from langchain_community.retreivers import BM25Retreiver
from langchain.tools import Tool

bm25_retriever = BM25Retreiver.from_documents(docs)

def extract_text(query: str) -> str:
    """ Retrieves detailed information on the guests attending the Gala based on the name and relation."""
    results = bm25_retriever.invoke(query)
    if results:
        return "\n\n".join([doc.page_content for doc in results[:3]])
    else:
        return "No matching information of tthe guests found"


guest_info_tool = Tool(
    name = "guest_info_retriever",
    func = extract_text,
    description = "Retrieves detailed information on thr guests attending the Gala based on the name and the relation"
)
# ---------------------------------------------------------------------------------------------

from typing import TypeDict, Annotated
from langgraph.graph.message import add_messages
from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
from langgraph.prebuilt import ToolNode
from langgraph.graph import START, StateGraph
from langgraph.prebuilt import tools_condition
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace

llm = HuggingFaceEndpoint(
    repo_id = "Qwen/Qwen2.5-Coder-32B-Instruct",
    huggingfacehub_api_token = 
)

chat = ChatHuggingFace(llm=llm, verbose=True)
tools = [guest_info_tool]
chat_with_tools = chat.bind_tools(tools)

# Generate Agentstate & AgentGraph

class AgentState(TypeDict):
    messages: Annotated[list[AnyMessage], add_messages]

def assistant(state : AgentState):
    retutn {
        "messages" : [chat chat_with_tools.invoke(state["messages"])]
    }

builder = StateGraph(AgentState)

#  Define the nodes

builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))

# Define Edges

builder.add_edge(START, "assistant")
builder.add_conditional_edges("assistant",
    # If the latest message requires a tool, route to tools
    # Otherwise, provide a direct response
    tools_condition,
)
builder.add_edge("tools", "assistant")
alfred = builder.compile()

messages = [HumanMessage(content="Tell me about our guest named 'Lady Ada Lovelace'.")]
response = alfred.invoke({"messages": messages})

print("🎩 Alfred's Response:")
print(response['messages'][-1].content))