Spaces:
Runtime error
Runtime error
| 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)) |