errchh
commited on
Commit
·
5d7d186
1
Parent(s):
e7f4f55
v1
Browse files- .DS_Store +0 -0
- .gitignore +1 -0
- .python-version +1 -0
- agent.py +199 -0
- pyproject.toml +19 -0
- requirements.txt +10 -1
- system_prompt.txt +1 -0
- uv.lock +0 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
.python-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
3.12
|
agent.py
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import libraries for langgraph, huggingface
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from typing import TypedDict, List, Dict, Any, Optional, Annotated
|
| 5 |
+
|
| 6 |
+
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFaceEmbeddings
|
| 7 |
+
|
| 8 |
+
from langgraph.graph import StateGraph, MessagesState, START, END
|
| 9 |
+
from langgraph.graph.message import add_messages
|
| 10 |
+
from langchain_core.messages import SystemMessage, HumanMessage, AnyMessage, AIMessage
|
| 11 |
+
from langchain_core.messages.ai import subtract_usage
|
| 12 |
+
|
| 13 |
+
from langchain.tools import Tool
|
| 14 |
+
from langchain_core.tools import tool
|
| 15 |
+
from langchain_community.tools import WikipediaQueryRun
|
| 16 |
+
from langchain_community.utilities import WikipediaAPIWrapper
|
| 17 |
+
from langchain_community.utilities import SerpAPIWrapper
|
| 18 |
+
from langchain_community.utilities import ArxivAPIWrapper
|
| 19 |
+
from langchain_community.retrievers import BM25Retriever
|
| 20 |
+
|
| 21 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# load environment variables
|
| 25 |
+
load_dotenv()
|
| 26 |
+
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# maths tool
|
| 30 |
+
@tool
|
| 31 |
+
def add(a:int, b:int) -> int:
|
| 32 |
+
"""add two numbers.
|
| 33 |
+
args:
|
| 34 |
+
a: first int
|
| 35 |
+
b: second int
|
| 36 |
+
"""
|
| 37 |
+
return a + b
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
@tool
|
| 41 |
+
def subtract(a:int, b:int) -> int:
|
| 42 |
+
"""subtract two numbers.
|
| 43 |
+
args:
|
| 44 |
+
a: first int
|
| 45 |
+
b: second int
|
| 46 |
+
"""
|
| 47 |
+
return a - b
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
@tool
|
| 51 |
+
def multiply(a:int, b:int) -> int:
|
| 52 |
+
"""multiply two numbers.
|
| 53 |
+
args:
|
| 54 |
+
a: first int
|
| 55 |
+
b: second int
|
| 56 |
+
"""
|
| 57 |
+
return a * b
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
@tool
|
| 61 |
+
def divide(a:int, b:int) -> float:
|
| 62 |
+
"""divide two numbers.
|
| 63 |
+
args:
|
| 64 |
+
a: first int
|
| 65 |
+
b: second int
|
| 66 |
+
"""
|
| 67 |
+
try:
|
| 68 |
+
# Attempt the division
|
| 69 |
+
result = a / b
|
| 70 |
+
return result
|
| 71 |
+
except ZeroDivisionError:
|
| 72 |
+
# Handle the case where b is zero
|
| 73 |
+
raise ValueError("Cannot divide by zero.")
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
@tool
|
| 77 |
+
def modulus(a:int, b:int) -> int:
|
| 78 |
+
"""modulus remainder of two numbers.
|
| 79 |
+
args:
|
| 80 |
+
a: first int
|
| 81 |
+
b: second int
|
| 82 |
+
"""
|
| 83 |
+
return a % b
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# wikipedia search tool
|
| 87 |
+
@tool
|
| 88 |
+
def search_wiki(query: str) -> Dict[str, str]:
|
| 89 |
+
"""search wikipedia with a query
|
| 90 |
+
args:
|
| 91 |
+
query: a search query
|
| 92 |
+
"""
|
| 93 |
+
docs = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
|
| 94 |
+
docs.run(query)
|
| 95 |
+
formatted_result = f'<Document source="{docs.metadata["source"]}" page="{docs.metadata.get("page", "")}"/>\n{docs.page_content}\n</Document>'
|
| 96 |
+
return {"wiki_results": formatted_result}
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
# internet search tool
|
| 100 |
+
@tool
|
| 101 |
+
def search_web(query: str) -> Dict[str, str]:
|
| 102 |
+
"""search internet with a query
|
| 103 |
+
args:
|
| 104 |
+
query: a search query
|
| 105 |
+
"""
|
| 106 |
+
docs = SerpAPIWrapper()
|
| 107 |
+
docs.run(query)
|
| 108 |
+
formatted_result = f'<Document source="{docs.metadata["source"]}" page="{docs.metadata.get("page", "")}"/>\n{docs.page_content}\n</Document>'
|
| 109 |
+
return {"wiki_results": formatted_result}
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
# ArXiv search tool
|
| 113 |
+
@tool
|
| 114 |
+
def search_arxiv(query: str) -> Dict[str, str]:
|
| 115 |
+
"""search ArXiv for the paper with the given identifier
|
| 116 |
+
args:
|
| 117 |
+
query: a search identifier
|
| 118 |
+
"""
|
| 119 |
+
arxiv = ArxivAPIWrapper()
|
| 120 |
+
docs = arxiv.run(query)
|
| 121 |
+
formatted_result = f'<Document source="{docs.metadata["source"]}" page="{docs.metadata.get("page", "")}"/>\n{docs.page_content}\n</Document>'
|
| 122 |
+
return {"wiki_results": formatted_result}
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
# build retriever
|
| 126 |
+
bm25_retriever = BM25Retriever.from_documents(docs)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
# load system prompt from file
|
| 130 |
+
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
| 131 |
+
system_prompt = f.read()
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
# init system message
|
| 135 |
+
sys_msg = SystemMessage(content=system_prompt)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
# generate the chat interface and tools
|
| 139 |
+
llm = HuggingFaceEndpoint(
|
| 140 |
+
repo_id = "microsoft/Phi-4-reasoning-plus",
|
| 141 |
+
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
chat = ChatHuggingFace(llm=llm, verbose=False)
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
tools = [
|
| 149 |
+
add,
|
| 150 |
+
subtract,
|
| 151 |
+
multiply,
|
| 152 |
+
divide,
|
| 153 |
+
modulus,
|
| 154 |
+
search_wiki,
|
| 155 |
+
search_web,
|
| 156 |
+
search_arxiv
|
| 157 |
+
]
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
chat_with_tools = chat.bind_tools(tools)
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
# generate AgentState and Agent graph
|
| 164 |
+
class AgentState(TypedDict):
|
| 165 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
| 166 |
+
|
| 167 |
+
def assistant(state: AgentState):
|
| 168 |
+
return {
|
| 169 |
+
"messages": [chat_with_tools.invoke(state["messages"])],
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
# build graph
|
| 174 |
+
builder = StateGraph(AgentState)
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
# define nodes
|
| 178 |
+
builder.add_node("assistant", assistant)
|
| 179 |
+
builder.add_node("tools", ToolNode(tools))
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
# define edges
|
| 183 |
+
builder.add_edge(START, "assistant")
|
| 184 |
+
builder.add_conditional_edges(
|
| 185 |
+
"assistant",
|
| 186 |
+
# If the latest message requires a tool, route to tools
|
| 187 |
+
# Otherwise, provide a direct response
|
| 188 |
+
tools_condition,
|
| 189 |
+
)
|
| 190 |
+
builder.add_edge("tools", "assistant")
|
| 191 |
+
alfred = builder.compile()
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
if __name__ == "__main__":
|
| 195 |
+
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
| 196 |
+
messages = [HumanMessage(content=question)]
|
| 197 |
+
messages = graph.invoke({"messages": messages})
|
| 198 |
+
for m in messages["messages"]:
|
| 199 |
+
m.pretty_print()
|
pyproject.toml
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "hfagentscourse-finalassignment"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Huggingface agents course -- Final assignment"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.12"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"dotenv>=0.9.9",
|
| 9 |
+
"gradio>=5.29.0",
|
| 10 |
+
"langchain-community>=0.3.23",
|
| 11 |
+
"langchain-huggingface>=0.2.0",
|
| 12 |
+
"langchain-openai>=0.3.16",
|
| 13 |
+
"langchain-tools>=0.1.34",
|
| 14 |
+
"langgraph>=0.4.3",
|
| 15 |
+
"pandas>=2.2.3",
|
| 16 |
+
"rank-bm25>=0.2.2",
|
| 17 |
+
"requests>=2.32.3",
|
| 18 |
+
"wikipedia>=1.4.0",
|
| 19 |
+
]
|
requirements.txt
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
|
|
| 1 |
gradio
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
dotenv
|
| 2 |
gradio
|
| 3 |
+
langchain-community
|
| 4 |
+
langchain-huggingface
|
| 5 |
+
langchain-openai
|
| 6 |
+
langchain-tools
|
| 7 |
+
langgraph
|
| 8 |
+
pandas
|
| 9 |
+
rank-bm25
|
| 10 |
+
requests
|
| 11 |
+
wikipedia
|
system_prompt.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
You are a helpful assistant.
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|