first agent iteration
Browse files- app.py +75 -51
- requirements.txt +5 -1
app.py
CHANGED
@@ -1,64 +1,88 @@
|
|
1 |
-
|
2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
41 |
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
)
|
|
|
|
|
61 |
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
|
|
1 |
+
from langchain_community.retrievers import BM25Retriever
|
2 |
+
from langchain.tools import Tool
|
3 |
+
from typing import TypedDict, Annotated
|
4 |
+
from langgraph.graph.message import add_messages
|
5 |
+
from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
|
6 |
+
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 |
+
# Convert dataset entries into Document objects
|
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(
|
49 |
+
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
50 |
+
huggingfacehub_api_token=os.environ["HUGGINGFACEHUB_API_TOKEN"],
|
51 |
+
)
|
52 |
+
|
53 |
+
chat = ChatHuggingFace(llm=llm, verbose=True)
|
54 |
+
tools = [guest_info_tool]
|
55 |
+
chat_with_tools = chat.bind_tools(tools)
|
56 |
|
57 |
+
# Generate the AgentState and Agent graph
|
58 |
+
class AgentState(TypedDict):
|
59 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
60 |
|
61 |
+
def assistant(state: AgentState):
|
62 |
+
return {
|
63 |
+
"messages": [chat_with_tools.invoke(state["messages"])],
|
64 |
+
}
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
## The graph
|
67 |
+
builder = StateGraph(AgentState)
|
68 |
|
69 |
+
# Define nodes: these do the work
|
70 |
+
builder.add_node("assistant", assistant)
|
71 |
+
builder.add_node("tools", ToolNode(tools))
|
72 |
|
73 |
+
# Define edges: these determine how the control flow moves
|
74 |
+
builder.add_edge(START, "assistant")
|
75 |
+
builder.add_conditional_edges(
|
76 |
+
"assistant",
|
77 |
+
# If the latest message requires a tool, route to tools
|
78 |
+
# Otherwise, provide a direct response
|
79 |
+
tools_condition,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
)
|
81 |
+
builder.add_edge("tools", "assistant")
|
82 |
+
alfred = builder.compile()
|
83 |
|
84 |
+
messages = [HumanMessage(content="Tell me about our guest named 'Lady Ada Lovelace'.")]
|
85 |
+
response = alfred.invoke({"messages": messages})
|
86 |
|
87 |
+
print("π© Alfred's Response:")
|
88 |
+
print(response['messages'][-1].content)
|
requirements.txt
CHANGED
@@ -1 +1,5 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
datasets
|
2 |
+
langgraph
|
3 |
+
langchain_huggingface
|
4 |
+
langchain_community
|
5 |
+
rank_bm25
|